sapling3.prf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # You will need this repo:
  2. # https://github.com/zcash/librustzcash/
  3. # Then compare this code to the file:
  4. # librustzcash/zcash_proofs/src/circuit/sapling.rs
  5. # What is the LC stuff?
  6. # Difference between AllocatedNum and Num
  7. # Why BlsScalar vs JJScalar?
  8. const:
  9. G_VCV: Point
  10. G_VCR: Point
  11. G_SPEND: Point
  12. G_PROOF: Point
  13. G_NOTE_COMMIT_R: Point
  14. G_NULL: Point
  15. CRH_IVK: Blake2sPersonalization
  16. NOTE_COMMIT: PedersenPersonalization
  17. MERKLE: list<PedersenPersonalization>
  18. PRF_NF: Blake2sPersonalization
  19. def value_commit(value: u64, randomness: Scalar) -> (Point):
  20. let value_bits: list<bool> = value as list<bool>
  21. let value: Point = value * G_VCV
  22. let rcv: list<bool> = randomness as list<bool>
  23. let rcv: Point = rcv * G_VCR
  24. let cv: Point = value + rcv
  25. emit cv
  26. return value_bits
  27. # The parameters to this function are the same as in:
  28. # struct Spend
  29. contract input_burn(
  30. value: u64, # ValueCommitment.value
  31. randomness: Scalar, # ValueCommitment.randomness
  32. ak: Point, # from ProofGenerationKey
  33. ar: Scalar,
  34. nsk: Scalar, # from ProofGenerationKey
  35. g_d: Point, # Computed from payment_address
  36. commitment_randomness: Scalar,
  37. auth_path: list<(Scalar, bool)>,
  38. anchor: Scalar
  39. ) -> (Point, Point, Point, list<bool>):
  40. let ak = witness(ak)
  41. ak.assert_not_small_order()
  42. let ar: list<bool> = ar as list<bool>
  43. let ar: Point = ar * G_SPEND
  44. let rk: Point = ak + ar
  45. emit rk
  46. let nsk: list<bool> = nsk as list<bool>
  47. let nk: Point = nsk * G_PROOF
  48. let mut ivk_preimage: list<bool> = []
  49. # Must be list<bool> as well
  50. ivk_preimage.extend(ak.repr())
  51. let mut nf_preimage: list<bool> = []
  52. let nk_repr: list<bool> = nk.repr()
  53. ivk_preimage.extend(nk_repr)
  54. nf_preimage.extend(nk_repr)
  55. assert len(ivk_preimage) == 512
  56. assert len(nf_preimage) == 256
  57. let mut ivk: list<bool> = blake2s(ivk_preimage, CRH_IVK)
  58. ivk.truncate(Scalar::CAPACITY)
  59. let g_d: Point = witness g_d
  60. g_d.assert_not_small_order()
  61. let pk_d: Point = ivk * g_d
  62. let mut note_contents: list<bool> = []
  63. let (cv: Point, value_bits: list<bool>) = value_commit(value, randomness)
  64. let mut value_num: Num = Num.zero()
  65. let mut coeff: Scalar = Scalar.one()
  66. for bit in value_bits:
  67. value_num = value_num.add_bool_with_coeff(bit, coeff)
  68. coeff = coeff.double()
  69. # Is this equivalent?
  70. let value_num = value_bits as Num
  71. note_contents.extend(value_bits)
  72. note_contents.extend(g_d)
  73. note_contents.extend(pk_d)
  74. assert len(note_contents) == 64 + 256 + 256
  75. let mut cm: Point = pedersen_hash(NOTE_COMMIT, note_contents)
  76. let rcm: list<bool> = commitment_randomness as list<bool>
  77. let rcm: Point = rcm * G_NOTE_COMMIT_R
  78. cm += rcm
  79. let mut position_bits: list<bool> = []
  80. let mut cur: Scalar = cm.u
  81. for i, (node, is_right) in enumerate(auth_path):
  82. position_bits.push(is_right)
  83. let node: EncryptedNum = EncryptedNum.from(node)
  84. print(node)
  85. let (left: list<bool>, right: list<bool>) = Num.swap_if(is_right, cur, node)
  86. let mut preimage: list<bool> = []
  87. preimage.extend(left)
  88. preimage.extend(right)
  89. cur = pedersen_hash(MERKLE_TREE[i], preimage).u
  90. let rt: Point = EncryptedNum.from(anchor)
  91. enforce (cur - rt) * value_num == 0
  92. emit rt
  93. let position: Point = position_bits * G_NULL
  94. let rho: Point = cm + position
  95. nf_preimage.extend(rho)
  96. assert len(nf_preimage) == 512
  97. let nf: list<bool> = blake2s(nf_preimage, PRF_NF)
  98. emit nf
  99. def output_mint(
  100. value: u64,
  101. randomness: Scalar,
  102. g_d: Point,
  103. esk: Scalar,
  104. pk_d: Point,
  105. commitment_randomness: Scalar
  106. ) -> (Point, Point, Scalar):
  107. let (cv: Point, value_bits: list<bool>) = value_commit(value, randomness)
  108. let mut note_contents: list<bool> = []
  109. note_contents.extend(value_bits)
  110. let g_d: Point = witness g_d
  111. assert is_not_small_order(g_d)
  112. let esk: list<bool> = esk as list<bool>
  113. let epk: Point = esk * g_d
  114. let v_contents: list<bool> = pk_d.v as list<bool>
  115. let sign_bit: bool = pk_d.u.is_odd() as bool
  116. note_contents.extend(v_contents)
  117. note_contents.push(sign_bit)
  118. assert len(note_contents) == 64 + 256 + 256
  119. let mut cm: Point = pedersen_hash(NOTE_COMMIT, note_contents)
  120. let rcm: list<bool> = commitment_randomness as list<bool>
  121. let rcm: Point = rcm * G_NOTE_COMMIT_R
  122. cm += rcm
  123. let cmu: Scalar = cm.u
  124. return (cv, epk, cmu)