sapling.prf 4.5 KB

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