sapling.prf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, list<bool>):
  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. return cv, value_bits
  26. # The parameters to this function are the same as in:
  27. # struct Spend
  28. def input_burn(
  29. value: u64, # ValueCommitment.value
  30. randomness: Scalar, # ValueCommitment.randomness
  31. ak: Point, # from ProofGenerationKey
  32. ar: Scalar,
  33. nsk: Scalar, # from ProofGenerationKey
  34. g_d: Point, # Computed from payment_address
  35. commitment_randomness: Scalar,
  36. auth_path: list<(Scalar, bool)>,
  37. anchor: Scalar
  38. ) -> (Point, Point, Point, list<bool>):
  39. let ak = witness(ak)
  40. ak.assert_not_small_order()
  41. let ar: list<bool> = ar as list<bool>
  42. let ar: Point = ar * G_SPEND
  43. let rk: Point = ak + ar
  44. let nsk: list<bool> = nsk as list<bool>
  45. let nk: Point = nsk * G_PROOF
  46. let mut ivk_preimage: list<bool> = []
  47. # Must be list<bool> as well
  48. ivk_preimage.extend(ak.repr())
  49. let mut nf_preimage: list<bool> = []
  50. let nk_repr: list<bool> = nk.repr()
  51. ivk_preimage.extend(nk_repr)
  52. nf_preimage.extend(nk_repr)
  53. assert len(ivk_preimage) == 512
  54. assert len(nf_preimage) == 256
  55. let mut ivk: list<bool> = blake2s(ivk_preimage, CRH_IVK)
  56. ivk.truncate(Scalar::CAPACITY)
  57. let g_d: Point = witness g_d
  58. g_d.assert_not_small_order()
  59. let pk_d: Point = ivk * g_d
  60. let mut note_contents: list<bool> = []
  61. let (cv: Point, value_bits: list<bool>) = value_commit(value, randomness)
  62. let mut value_num: Num = Num.zero()
  63. let mut coeff: Scalar = Scalar.one()
  64. for bit in value_bits:
  65. value_num = value_num.add_bool_with_coeff(bit, coeff)
  66. coeff = coeff.double()
  67. # Is this equivalent?
  68. let value_num = value_bits as Num
  69. note_contents.extend(value_bits)
  70. note_contents.extend(g_d)
  71. note_contents.extend(pk_d)
  72. assert len(note_contents) == 64 + 256 + 256
  73. let mut cm: Point = pedersen_hash(NOTE_COMMIT, note_contents)
  74. let rcm: list<bool> = commitment_randomness as list<bool>
  75. let rcm: Point = rcm * G_NOTE_COMMIT_R
  76. cm += rcm
  77. let mut position_bits: list<bool> = []
  78. let mut cur: Scalar = cm.u
  79. for i, (node, is_right) in enumerate(auth_path):
  80. position_bits.push(is_right)
  81. let node: EncryptedNum = EncryptedNum.from(node)
  82. print(node)
  83. let (left: list<bool>, right: list<bool>) = Num.swap_if(is_right, cur, node)
  84. let mut preimage: list<bool> = []
  85. preimage.extend(left)
  86. preimage.extend(right)
  87. cur = pedersen_hash(MERKLE_TREE[i], preimage).u
  88. let rt: Point = EncryptedNum.from(anchor)
  89. enforce (cur - rt) * value_num == 0
  90. let position: Point = position_bits * G_NULL
  91. let rho: Point = cm + position
  92. nf_preimage.extend(rho)
  93. assert len(nf_preimage) == 512
  94. let nf: list<bool> = blake2s(nf_preimage, PRF_NF)
  95. return (rk, cv, rt, nf)
  96. def output_mint(
  97. value: u64,
  98. randomness: Scalar,
  99. g_d: Point,
  100. esk: Scalar,
  101. pk_d: Point,
  102. commitment_randomness: Scalar
  103. ) -> (Point, Point, Scalar):
  104. let (cv: Point, value_bits: list<bool>) = value_commit(value, randomness)
  105. let mut note_contents: list<bool> = []
  106. note_contents.extend(value_bits)
  107. let g_d: Point = witness g_d
  108. assert is_not_small_order(g_d)
  109. let esk: list<bool> = esk as list<bool>
  110. let epk: Point = esk * g_d
  111. let v_contents: list<bool> = pk_d.v as list<bool>
  112. let sign_bit: bool = pk_d.u.is_odd() as bool
  113. note_contents.extend(v_contents)
  114. note_contents.push(sign_bit)
  115. assert len(note_contents) == 64 + 256 + 256
  116. let mut cm: Point = pedersen_hash(NOTE_COMMIT, note_contents)
  117. let rcm: list<bool> = commitment_randomness as list<bool>
  118. let rcm: Point = rcm * G_NOTE_COMMIT_R
  119. cm += rcm
  120. let cmu: Scalar = cm.u
  121. return (cv, epk, cmu)