opcodes.zk 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. k = 13;
  2. constant "Opcodes" {
  3. EcFixedPointShort VALUE_COMMIT_VALUE,
  4. EcFixedPoint VALUE_COMMIT_RANDOM,
  5. EcFixedPointBase NULLIFIER_K,
  6. }
  7. witness "Opcodes" {
  8. Base value,
  9. Scalar value_blind,
  10. Base blind,
  11. Base a,
  12. Base b,
  13. Base secret,
  14. EcNiPoint pubkey,
  15. Base ephem_secret,
  16. Uint32 leaf_pos,
  17. MerklePath path,
  18. Base cond,
  19. }
  20. circuit "Opcodes" {
  21. vcv = ec_mul_short(value, VALUE_COMMIT_VALUE);
  22. vcr = ec_mul(value_blind, VALUE_COMMIT_RANDOM);
  23. value_commit = ec_add(vcv, vcr);
  24. value_commit_x = ec_get_x(value_commit);
  25. value_commit_y = ec_get_y(value_commit);
  26. constrain_instance(ec_get_x(value_commit));
  27. constrain_instance(ec_get_y(value_commit));
  28. vcv2 = ec_mul_short(value, VALUE_COMMIT_VALUE);
  29. vcr2 = ec_mul(value_blind, VALUE_COMMIT_RANDOM);
  30. value_commit2 = ec_add(vcv2, vcr2);
  31. constrain_equal_point(value_commit, value_commit2);
  32. zero = witness_base(0);
  33. one = witness_base(1);
  34. two = witness_base(2);
  35. c = poseidon_hash(one, two, blind);
  36. constrain_instance(c);
  37. d = poseidon_hash(one, blind, ec_get_x(value_commit), ec_get_y(value_commit));
  38. constrain_instance(d);
  39. d2 = poseidon_hash(one, blind, ec_get_x(value_commit2), ec_get_y(value_commit2));
  40. constrain_equal_base(d, d2);
  41. range_check(64, a);
  42. range_check(253, b);
  43. less_than_strict(a, b);
  44. less_than_loose(a, b);
  45. root = merkle_root(leaf_pos, path, c);
  46. constrain_instance(root);
  47. public = ec_mul_base(secret, NULLIFIER_K);
  48. constrain_instance(ec_get_x(public));
  49. constrain_instance(ec_get_y(public));
  50. bool_check(one);
  51. ephem_public = ec_mul_var_base(ephem_secret, pubkey);
  52. constrain_instance(ec_get_x(ephem_public));
  53. constrain_instance(ec_get_y(ephem_public));
  54. out = cond_select(cond, a, b);
  55. constrain_instance(out);
  56. zz = zero_cond(zero, c);
  57. constrain_instance(zz);
  58. }