lead.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. use incrementalmerkletree::{bridgetree::BridgeTree, Frontier, Tree};
  2. use halo2_gadgets::primitives::{
  3. poseidon,
  4. poseidon::{ConstantLength, P128Pow5T3},
  5. };
  6. use halo2_proofs::{
  7. dev::MockProver,
  8. };
  9. use rand::{thread_rng, Rng};
  10. use pasta_curves::{pallas, Fp};
  11. use darkfi::{
  12. zk:: {
  13. circuit::lead_contract::{LeadContract},
  14. },
  15. crypto::{
  16. merkle_node::MerkleNode,
  17. keypair::{Keypair, PublicKey, SecretKey},
  18. types::*,
  19. constants::{
  20. NullifierK, OrchardFixedBases, OrchardFixedBasesFull, ValueCommitV, MERKLE_DEPTH_ORCHARD,
  21. },
  22. nullifier::Nullifier,
  23. proof::{Proof, ProvingKey, VerifyingKey},
  24. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  25. },
  26. };
  27. #[derive(Debug,Default,Clone)]
  28. pub struct Coin
  29. {
  30. value : Option<pallas::Base>, //stake
  31. cm : Option<pallas::Point>,
  32. cm2 : Option<pallas::Point>,
  33. cm_blind : Option<pallas::Base>,
  34. sl : Option<pallas::Base>, //slot id
  35. tau : Option<pallas::Base>,
  36. nonce : Option<pallas::Base>,
  37. sn : Option<pallas::Base>, // coin's serial number
  38. sk : Option<SecretKey>,
  39. pk : Option<PublicKey>,
  40. root_cm : Option<pallas::Scalar>,
  41. root_sk : Option<pallas::Scalar>,
  42. path: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>,
  43. path_sk: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>,
  44. opening1 : Option<pallas::Base>,
  45. opening2 : Option<pallas::Base>,
  46. }
  47. fn main()
  48. {
  49. let k = 13;
  50. //
  51. //TODO calculate commitment here
  52. //this is the commitment of the first coin
  53. //TODO construct a tree of multiple coins
  54. const LEN : usize = 10;
  55. let mut rng = thread_rng();
  56. let sks : Vec<u64> = vec![];
  57. let root_sks : Vec<MerkleNode> = vec![];
  58. let path_sks : Vec<[MerkleNode;MERKLE_DEPTH_ORCHARD]> = vec![];
  59. let tree = BridgeTree::<MerkleNode, 32>::new(LEN);
  60. for i in 0..LEN {
  61. let tmp : u64 = rng.gen();
  62. let sk : u64 = tmp;
  63. sks.push(sk);
  64. let node = MerkleNode(pallas::Base::from(sk));
  65. tree.append(&node);
  66. let (leaf_pos, path) = tree.authentication_path(&node).unwrap();
  67. root_sks.push(tree.root());
  68. path_sks.push(path);
  69. }
  70. let seeds : Vec<u64> = vec![];
  71. for i in 0..LEN {
  72. let rho : u64 = rng.gen();
  73. seeds.push(rho);
  74. }
  75. //
  76. let mau_y : pallas::Scalar = pallas::Scalar::from(rng.gen());
  77. let mau_rho : pallas::Scalar = pallas::Scalar::from(rng.gen());
  78. //
  79. let coins : Vec<Coin> = vec![];
  80. //
  81. let tree_cm = BridgeTree::<MerkleNode, 32>::new(LEN);
  82. let zerou64 : u64 = 0;
  83. for i in 0..LEN {
  84. let c_v = pallas::Base::from(u64::try_from(i*2).unwrap());
  85. //random sampling of the same size of prf,
  86. //pseudo random sampling that is the size of pederson commitment
  87. let c_sk : u64 = sks[i];
  88. let iu64 : u64 = u64::try_from(i).unwrap();
  89. let c_sl = pallas::Base::from(iu64);
  90. //TODO 512 secret-key/public-key to cop with pallas curves
  91. //note! sk is used in MerkleNode takes pallas::Base as input
  92. //while the pallas::base is 512, the SecretKey is of size 256, a larger keyring is needed
  93. //TODO what is the endianess of this keyring
  94. let sk_bits = vec![];
  95. sk_bits.append(&mut c_sk.to_le_bytes().to_vec());
  96. sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  97. sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  98. sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  99. let c_pk = PublicKey::from_secret(SecretKey::from_bytes(sk_bits.as_slice().try_into().unwrap()).unwrap());
  100. let c_tau = pallas::Base::from(u64::try_from(i).unwrap()); // let's assume it's sl for simplicity
  101. let c_root_sk : MerkleNode = root_sks[i];
  102. let c_seed = pallas::Base::from(seeds[i]);
  103. let c_sn = pedersen_commitment_base(c_seed, c_root_sk);
  104. let c_cm_message = [c_pk.clone(), c_v.clone(), c_seed.clone()];
  105. let c_cm_v = poseidon::Hash::<_,P128Pow5T3, ConstantLength<6>, 3, 2>::init().hash(c_cm_message);
  106. let c_cm1_blind = pallas::Base::from(0); //tmp val
  107. let c_cm2_blind = pallas::Base::from(0); //tmp val
  108. let c_cm : pallas::Point = pedersen_commitment_scalar(c_cm_v, c_cm1_blind);
  109. let c_cm_node = MerkleNode(c_cm);
  110. tree_cm.append(&c_cm_node);
  111. let (leaf_pos, c_cm_path) = tree_cm.authentication_path(&c_cm_node).unwrap();
  112. let c_root_cm = tree_cm.root();
  113. // lead coin commitment
  114. //TODO this c_v can be
  115. let c_seed2 = pedersen_commitment_u64(c_seed, c_root_sk);
  116. let lead_coin_msg = [c_pk, c_v, c_seed2];
  117. poseidon::Hash::<_,P128Pow5T3, ConstantLength<6>, 3, 2>::init().hash(lead_coin_msg);
  118. let c_cm2 = pedersen_commitment_u64(lead_coin_msg, c_seed2);
  119. let c_root_sk = root_sks[i];
  120. let c_path_sk = path_sks[i];
  121. let coin = Coin {
  122. value: Some(c_v),
  123. cm: Some(c_cm),
  124. cm2: Some(c_cm2),
  125. cm_blind: Some(c_cm1_blind),
  126. sl: Some(c_sl),
  127. tau: Some(c_tau),
  128. nonce: Some(c_seed),
  129. sn: Some(c_sn),
  130. sk: Some(c_sk),
  131. pk: Some(c_pk),
  132. root_cm: Some(c_root_cm),
  133. root_sk: Some(c_root_sk),
  134. path: Some(c_cm_path),
  135. path_sk: Some(c_path_sk),
  136. opening1: Some(c_cm1_blind),
  137. opening2: Some(c_cm2_blind),
  138. };
  139. coins.push(coin);
  140. }
  141. let coin_idx = 0;
  142. let coin = coins[coin_idx];
  143. let path_sk = path_sks[coin_idx];
  144. let contract = LeadContract {
  145. path: Some(coin.path),
  146. root_sk: Some(coin.root_sk), //TODO where doesn' this come from?
  147. path_sk: Some(path_sk),
  148. coin_timestamp: Some(pallas::Base::from(coin.tau)), //
  149. coin_nonce: Some(pallas::Base::from(coin.nonce)),
  150. coin_opening_1: Some(coin.opening1),
  151. value: Some(coin.value),
  152. coin_opening_2: Some(coin.opening2),
  153. cm_c1_x: Some(coin.cm.0.x),
  154. cm_c1_y: Some(coin.cm.0.y),
  155. cm_c2_x: Some(coin.cm2.0.x),
  156. cm_c2_y: Some(coin.cm2.0.y),
  157. cm_pos : Some(coin_idx.unwrap()),
  158. sn_c1: Some(coin.sn.unwrap()),
  159. slot: Some(coin.sl.unwrap()),
  160. mau_rho: Some(mau_rho.clone()),
  161. mau_y: Some(mau_y.clone()),
  162. root_cm: Some(coin.root_cm.unwrap()),
  163. };
  164. //public inputs
  165. let c0 = pedersen_commitment_scalar(mod_r_p(coin.nonce.unwrap()), coin.root_cm.unwrap());
  166. let c1 = pedersen_commitment_scalar(mod_r_p(coin.tau.unwrap()), coin.root_cm.unwrap());
  167. //TODO root_cm need to be converted to Fp
  168. let c2 = pedersen_commitment_scalar(mod_r_p(coin.nonce.unwrap()), coin.root_cm.unwrap());
  169. let c3 = coin.cm.unwrap();
  170. let c4 = coin.cm2.unwrap();
  171. let c5 = coin.path.unwrap();
  172. let c6 = pallas::Base::from(0);
  173. let mut public_inputs = vec![c0.x, c0.y,
  174. c1.x, c1.y,
  175. c2.x, c2.y,
  176. c3.x, c3.y,
  177. c4.x(), c4.y(),
  178. c5,
  179. c6,
  180. ];
  181. //TODO
  182. let prover = MockProver::run(k, &contract, vec![public_inputs]).unwrap();
  183. assert_eq!(prover.verify(), Ok(()));
  184. }