opcodes.zk 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 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. }