opcodes.zk 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. zero = witness_base(0);
  32. one = witness_base(1);
  33. two = witness_base(2);
  34. c = poseidon_hash(one, two, blind);
  35. constrain_instance(c);
  36. d = poseidon_hash(one, blind, ec_get_x(value_commit), ec_get_y(value_commit));
  37. constrain_instance(d);
  38. d2 = poseidon_hash(one, blind, ec_get_x(value_commit2), ec_get_y(value_commit2));
  39. constrain_equal_base(d, d2);
  40. range_check(64, a);
  41. range_check(253, b);
  42. less_than_strict(a, b);
  43. less_than_loose(a, b);
  44. root = merkle_root(leaf_pos, path, c);
  45. constrain_instance(root);
  46. public = ec_mul_base(secret, NULLIFIER_K);
  47. constrain_instance(ec_get_x(public));
  48. constrain_instance(ec_get_y(public));
  49. bool_check(one);
  50. ephem_public = ec_mul_var_base(ephem_secret, pubkey);
  51. constrain_instance(ec_get_x(ephem_public));
  52. constrain_instance(ec_get_y(ephem_public));
  53. out = cond_select(cond, a, b);
  54. constrain_instance(out);
  55. zz = zero_cond(zero, c);
  56. constrain_instance(zz);
  57. }