opcodes.zk 1.6 KB

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