fee_v1.zk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. k = 11;
  2. field = "pallas";
  3. constant "Fee_V1" {
  4. EcFixedPointShort VALUE_COMMIT_VALUE,
  5. EcFixedPoint VALUE_COMMIT_RANDOM,
  6. EcFixedPointBase NULLIFIER_K,
  7. }
  8. witness "Fee_V1" {
  9. # Secret key used to derive input's nullifier
  10. Base input_secret,
  11. # Input coin's leaf position in the Merkle tree of coins
  12. Uint32 input_leaf_pos,
  13. # Merkle path to the coin
  14. MerklePath input_path,
  15. # Secret key used to derive public key for the tx signature
  16. Base signature_secret,
  17. # Value of the input coin
  18. Base input_value,
  19. # Random blinding factor for the input value commitment
  20. Scalar input_value_blind,
  21. # Input coin's spend hook
  22. Base input_spend_hook,
  23. # Data passed from the input to the invoked contract
  24. Base input_user_data,
  25. # Unique coin blind corresponding to the input coin
  26. Base input_coin_blind,
  27. # Blinding factor for the encrypted user_data
  28. Base input_user_data_blind,
  29. # Value of the output coin
  30. Base output_value,
  31. # Output coin's spend hook
  32. Base output_spend_hook,
  33. # Data passed from the output coin to the invoked contract
  34. Base output_user_data,
  35. # Random blinding factor for the output value commitment
  36. Scalar output_value_blind,
  37. # Unique coin blind corresponding to the output coin
  38. Base output_coin_blind,
  39. # Token ID
  40. Base token,
  41. # Random blinding factor for the token ID
  42. Base token_blind,
  43. }
  44. circuit "Fee_V1" {
  45. # Derive the input coin
  46. pub = ec_mul_base(input_secret, NULLIFIER_K);
  47. pub_x = ec_get_x(pub);
  48. pub_y = ec_get_y(pub);
  49. input_coin = poseidon_hash(
  50. pub_x,
  51. pub_y,
  52. input_value,
  53. token,
  54. input_spend_hook,
  55. input_user_data,
  56. input_coin_blind,
  57. );
  58. nullifier = poseidon_hash(input_secret, input_coin);
  59. constrain_instance(nullifier);
  60. # Pedersen commitment for the input coin value
  61. input_vcv = ec_mul_short(input_value, VALUE_COMMIT_VALUE);
  62. input_vcr = ec_mul(input_value_blind, VALUE_COMMIT_RANDOM);
  63. input_value_commit = ec_add(input_vcv, input_vcr);
  64. constrain_instance(ec_get_x(input_value_commit));
  65. constrain_instance(ec_get_y(input_value_commit));
  66. # Commitment for the token ID
  67. token_commit = poseidon_hash(token, token_blind);
  68. constrain_instance(token_commit);
  69. # Merkle root
  70. root = merkle_root(input_leaf_pos, input_path, input_coin);
  71. constrain_instance(root);
  72. # Export user_data
  73. user_data_enc = poseidon_hash(input_user_data, input_user_data_blind);
  74. constrain_instance(user_data_enc);
  75. # Reveal spend_hook
  76. ZERO = witness_base(0);
  77. constrain_equal_base(input_spend_hook, ZERO);
  78. # Derive a public key for the signature and
  79. # constrain its coordinates
  80. signature_public = ec_mul_base(signature_secret, NULLIFIER_K);
  81. constrain_instance(ec_get_x(signature_public));
  82. constrain_instance(ec_get_y(signature_public));
  83. # Derive output coin
  84. output_coin = poseidon_hash(
  85. pub_x,
  86. pub_y,
  87. output_value,
  88. token,
  89. output_spend_hook,
  90. output_user_data,
  91. output_coin_blind,
  92. );
  93. constrain_instance(output_coin);
  94. # Pedersen commitment for the output coin value
  95. output_vcv = ec_mul_short(output_value, VALUE_COMMIT_VALUE);
  96. output_vcr = ec_mul(output_value_blind, VALUE_COMMIT_RANDOM);
  97. output_value_commit = ec_add(output_vcv, output_vcr);
  98. constrain_instance(ec_get_x(output_value_commit));
  99. constrain_instance(ec_get_y(output_value_commit));
  100. }