lead.zk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. constant "Lead" {
  2. EcFixedPointShort VALUE_COMMIT_VALUE,
  3. EcFixedPoint VALUE_COMMIT_RANDOM,
  4. EcFixedPointBase NULLIFIER_K,
  5. }
  6. contract "Lead" {
  7. MerklePath c1_cm_path,
  8. Uint32 c1_cm_pos,
  9. Uint32 c1_sk_pos,
  10. Base c1_sk,
  11. Base c1_sk_root,
  12. MerklePath c1_sk_path,
  13. Base c1_tau,
  14. Base c1_rho,
  15. Scalar c1_opening,
  16. Base value,
  17. Scalar c2_opening,
  18. Scalar rho_opening,
  19. Scalar y_opening,
  20. Base sigma1,
  21. Base sigma2,
  22. }
  23. circuit "Lead" {
  24. ZERO = witness_base(0);
  25. ONE = witness_base(1);
  26. PREFIX_EVL = witness_base(2);
  27. PREFIX_SEED = witness_base(3);
  28. PREFIX_CM = witness_base(4);
  29. PREFIX_PK = witness_base(5);
  30. PREFIX_SN = witness_base(6);
  31. # coin (1) pk
  32. pk = poseidon_hash(PREFIX_PK, c1_sk_root, c1_tau, ZERO);
  33. constrain_instance(pk);
  34. # coin (2) rho/nonce
  35. c2_rho = poseidon_hash(PREFIX_EVL, c1_sk_root, c1_rho, ZERO);
  36. constrain_instance(c2_rho);
  37. # coin (1) cm/commitment
  38. c1_cm_msg = poseidon_hash(PREFIX_CM, pk, value, c1_rho);
  39. c1_cm_v = ec_mul_base(c1_cm_msg, NULLIFIER_K);
  40. c1_cm_r = ec_mul(c1_opening, VALUE_COMMIT_RANDOM);
  41. c1_cm = ec_add(c1_cm_v, c1_cm_r);
  42. c1_cm_x = ec_get_x(c1_cm);
  43. c1_cm_y = ec_get_y(c1_cm);
  44. c1_cm_hash = poseidon_hash(c1_cm_x, c1_cm_y);
  45. constrain_instance(c1_cm_x);
  46. constrain_instance(c1_cm_y);
  47. # coin (2) cm/commitment
  48. c2_cm_msg = poseidon_hash(PREFIX_CM, pk, value, c2_rho);
  49. c2_cm_v = ec_mul_base(c2_cm_msg, NULLIFIER_K);
  50. c2_cm_r = ec_mul(c2_opening, VALUE_COMMIT_RANDOM);
  51. c2_cm = ec_add(c2_cm_v, c2_cm_r);
  52. c2_cm_x = ec_get_x(c2_cm);
  53. c2_cm_y = ec_get_y(c2_cm);
  54. constrain_instance(c2_cm_x);
  55. constrain_instance(c2_cm_y);
  56. # root of path to burnt coin commitment at given pos
  57. root = merkle_root(c1_cm_pos, c1_cm_path, c1_cm_hash);
  58. constrain_instance(root);
  59. # root of path at c1_sk_pos
  60. root_sk = merkle_root(c1_sk_pos, c1_sk_path, c1_sk);
  61. constrain_instance(root_sk);
  62. # coin (1) sn/nullifier
  63. sn = poseidon_hash(PREFIX_SN, c1_sk_root, c1_rho, ZERO);
  64. constrain_instance(sn);
  65. # lottery seed
  66. seed = poseidon_hash(PREFIX_SEED, c1_sk_root, c1_rho, ZERO);
  67. # y
  68. y_v = ec_mul_base(seed, NULLIFIER_K);
  69. y_r = ec_mul(y_opening, VALUE_COMMIT_RANDOM);
  70. y = ec_add(y_v, y_r);
  71. y_x = ec_get_x(y);
  72. y_y = ec_get_y(y);
  73. y_hash = poseidon_hash(y_x, y_y);
  74. constrain_instance(y_x);
  75. constrain_instance(y_y);
  76. # rho
  77. rho_v = ec_mul_base(seed, NULLIFIER_K);
  78. rho_r = ec_mul(rho_opening, VALUE_COMMIT_RANDOM);
  79. rho = ec_add(rho_v, rho_r);
  80. rho_x = ec_get_x(rho);
  81. rho_y = ec_get_y(rho);
  82. constrain_instance(rho_x);
  83. constrain_instance(rho_y);
  84. # target
  85. term1 = base_mul(sigma1, value);
  86. term2_1 = base_mul(sigma2, value);
  87. term2 = base_mul(term2_1, value);
  88. target = base_add(term1, term2);
  89. #lottery
  90. less_than(y_hash, target);
  91. }