lead.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. use pasta_curves::group::Curve;
  28. use pasta_curves::arithmetic::CurveAffine;
  29. //use halo2_proofs::arithmetic::CurveAffine;
  30. #[derive(Debug,Default,Clone,Copy)]
  31. pub struct Coin
  32. {
  33. value : Option<pallas::Base>, //stake
  34. cm : Option<pallas::Point>,
  35. cm2 : Option<pallas::Point>,
  36. cm_blind : Option<pallas::Base>,
  37. sl : Option<pallas::Base>, //slot id
  38. tau : Option<pallas::Base>,
  39. nonce : Option<pallas::Base>,
  40. sn : Option<pallas::Point>, // coin's serial number
  41. //sk : Option<SecretKey>,
  42. pk : Option<pallas::Point>,
  43. root_cm : Option<pallas::Scalar>,
  44. root_sk : Option<pallas::Scalar>,
  45. path: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>,
  46. path_sk: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>,
  47. opening1 : Option<pallas::Base>,
  48. opening2 : Option<pallas::Base>,
  49. }
  50. fn main()
  51. {
  52. let k = 13;
  53. //
  54. //TODO calculate commitment here
  55. //this is the commitment of the first coin
  56. //TODO construct a tree of multiple coins
  57. const LEN : usize = 10;
  58. let mut rng = thread_rng();
  59. let mut sks : Vec<u64> = vec![];
  60. let mut root_sks : Vec<MerkleNode> = vec![];
  61. let mut path_sks : Vec<[MerkleNode;MERKLE_DEPTH_ORCHARD]> = vec![];
  62. let mut tree = BridgeTree::<MerkleNode, 32>::new(LEN);
  63. for i in 0..LEN {
  64. let tmp : u64 = rng.gen();
  65. let mut sk : u64 = tmp;
  66. sks.push(sk.clone());
  67. let node = MerkleNode(pallas::Base::from(sk));
  68. tree.append(&node.clone());
  69. let (leaf_pos, path) = tree.authentication_path(&node).unwrap();
  70. root_sks.push(tree.root().clone());
  71. path_sks.push(path.as_slice().try_into().unwrap());
  72. }
  73. let mut seeds : Vec<u64> = vec![];
  74. for i in 0..LEN {
  75. let rho : u64 = rng.gen();
  76. seeds.push(rho.clone());
  77. }
  78. //
  79. let yu64 : u64 = rng.gen();
  80. let rhou64 : u64 = rng.gen();
  81. let mau_y : pallas::Scalar = pallas::Scalar::from(yu64);
  82. let mau_rho : pallas::Scalar = pallas::Scalar::from(rhou64);
  83. //
  84. let mut coins : Vec<Coin> = vec![];
  85. //
  86. let mut tree_cm = BridgeTree::<MerkleNode, 32>::new(LEN);
  87. let zerou64 : u64 = 0;
  88. for i in 0..LEN {
  89. let c_v = pallas::Base::from(u64::try_from(i*2).unwrap());
  90. //random sampling of the same size of prf,
  91. //pseudo random sampling that is the size of pederson commitment
  92. let c_sk : u64 = sks[i];
  93. let iu64 : u64 = u64::try_from(i).unwrap();
  94. let c_sl = pallas::Base::from(iu64);
  95. let c_tau = pallas::Base::from(u64::try_from(i).unwrap()); // let's assume it's sl for simplicity
  96. let c_root_sk : MerkleNode = root_sks[i];
  97. // =========================
  98. //TODO 512 secret-key/public-key to cop with pallas curves
  99. // =========================
  100. //note! sk is used in MerkleNode takes pallas::Base as input
  101. //while the pallas::base is 512, the SecretKey is of size 256, a larger keyring is needed
  102. //TODO what is the endianess of this keyring
  103. //let sk_bits = vec![];
  104. //sk_bits.append(&mut c_sk.to_le_bytes().to_vec());
  105. //sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  106. //sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  107. //sk_bits.append(&mut zerou64.to_le_bytes().to_vec());
  108. //let c_pk = PublicKey::from_secret(SecretKey::from_bytes(sk_bits.as_slice().try_into().unwrap()).unwrap());
  109. let c_pk = pedersen_commitment_scalar(mod_r_p(c_tau), mod_r_p(c_root_sk.inner()));
  110. //
  111. // TODO (fix) no use random value for the secret key as random pallas base
  112. // =======================
  113. let c_seed = pallas::Base::from(seeds[i]);
  114. let c_sn = pedersen_commitment_scalar(mod_r_p(c_seed), mod_r_p(c_root_sk.inner()));
  115. let c_pk_pt = c_pk.to_affine().coordinates().unwrap();
  116. let c_cm_message = [*c_pk_pt.x(), *c_pk_pt.y(), c_v.clone(), c_seed.clone()];
  117. let c_cm_v = poseidon::Hash::<_,P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(c_cm_message);
  118. let c_cm1_blind = pallas::Base::from(0); //tmp val
  119. let c_cm2_blind = pallas::Base::from(0); //tmp val
  120. let c_cm : pallas::Point = pedersen_commitment_scalar(mod_r_p(c_cm_v), mod_r_p(c_cm1_blind));
  121. //TODO (fix) which affine coefficient point to be used a/b ?
  122. let c_cm_node = MerkleNode(*c_cm.to_affine().coordinates().unwrap().x()); //CurveAffine::a()
  123. tree_cm.append(&c_cm_node.clone());
  124. let (leaf_pos, c_cm_path) = tree_cm.authentication_path(&c_cm_node).unwrap();
  125. let c_root_cm = tree_cm.root();
  126. // lead coin commitment
  127. //TODO this c_v can be
  128. let c_seed2 = pedersen_commitment_scalar(mod_r_p(c_seed), mod_r_p(c_root_sk.inner()));
  129. let c_seed2_pt = c_seed2.to_affine().coordinates().unwrap();
  130. let lead_coin_msg = [*c_pk_pt.x(), *c_pk_pt.y(), c_v, *c_seed2_pt.x(), *c_seed2_pt.y()];
  131. let lead_coin_msg_hash = poseidon::Hash::<_,P128Pow5T3, ConstantLength<5>, 3, 2>::init().hash(lead_coin_msg);
  132. let c_cm2 = pedersen_commitment_scalar(mod_r_p(lead_coin_msg_hash), mod_r_p(c_cm2_blind));
  133. let c_root_sk = root_sks[i];
  134. let c_path_sk = path_sks[i];
  135. let coin = Coin {
  136. value: Some(c_v),
  137. cm: Some(c_cm),
  138. cm2: Some(c_cm2),
  139. cm_blind: Some(c_cm1_blind),
  140. sl: Some(c_sl),
  141. tau: Some(c_tau),
  142. nonce: Some(c_seed),
  143. sn: Some(c_sn),
  144. //sk: Some(c_sk),
  145. pk: Some(c_pk),
  146. root_cm: Some(mod_r_p(c_root_cm.inner())),
  147. root_sk: Some(mod_r_p(c_root_sk.inner())),
  148. path: Some(c_cm_path.as_slice().try_into().unwrap()),
  149. path_sk: Some(c_path_sk),
  150. opening1: Some(c_cm1_blind),
  151. opening2: Some(c_cm2_blind),
  152. };
  153. coins.push(coin);
  154. }
  155. // ================
  156. // public inputs
  157. // ================
  158. let coin_idx = 0;
  159. let coin = coins[coin_idx];
  160. let c0 = pedersen_commitment_scalar(mod_r_p(coin.nonce.unwrap()), coin.root_cm.unwrap())
  161. .to_affine()
  162. .coordinates()
  163. .unwrap();
  164. let c1 = pedersen_commitment_scalar(mod_r_p(coin.tau.unwrap()), coin.root_cm.unwrap())
  165. .to_affine()
  166. .coordinates()
  167. .unwrap();
  168. //TODO root_cm need to be converted to Fp
  169. let c2 = pedersen_commitment_scalar(mod_r_p(coin.nonce.unwrap()), coin.root_cm.unwrap())
  170. .to_affine()
  171. .coordinates()
  172. .unwrap();
  173. //
  174. let c3 = coin.cm.unwrap().to_affine().coordinates().unwrap();
  175. let c4 = coin.cm2.unwrap().to_affine().coordinates().unwrap();
  176. //TODO (fix) public key is a commitemnet of the root of secret key, and the timestamp
  177. let c7 = coin.pk.unwrap().to_affine().coordinates().unwrap();
  178. let c8 = coin.sn.unwrap().to_affine().coordinates().unwrap();
  179. //TODO (fix) this need to be replaced by computed final path as pallas::Base
  180. let c5 = coin.path.unwrap();
  181. let c6 = pallas::Base::from(0);
  182. // ===============
  183. let path_sk = path_sks[coin_idx];
  184. let contract = LeadContract {
  185. path: coin.path,
  186. root_sk: coin.root_sk,
  187. path_sk: Some(path_sk),
  188. coin_timestamp: coin.tau, //
  189. coin_nonce: coin.nonce,
  190. coin_opening_1: Some(mod_r_p(coin.opening1.unwrap())),
  191. value: coin.value,
  192. coin_opening_2: Some(mod_r_p(coin.opening2.unwrap())),
  193. cm_c1_x: Some(*c3.x()),
  194. cm_c1_y: Some(*c3.y()),
  195. cm_c2_x: Some(*c4.x()),
  196. cm_c2_y: Some(*c4.y()),
  197. cm_pos : Some(u32::try_from(coin_idx).unwrap()),
  198. //sn_c1: Some(coin.sn.unwrap()),
  199. slot: Some(coin.sl.unwrap()),
  200. mau_rho: Some(mau_rho.clone()),
  201. mau_y: Some(mau_y.clone()),
  202. root_cm: Some(coin.root_cm.unwrap()),
  203. };
  204. let mut public_inputs = vec![*c0.x(), *c0.y(),
  205. *c1.x(), *c1.y(),
  206. *c2.x(), *c2.y(),
  207. *c7.x(), *c7.y(),
  208. *c8.x(), *c8.y(),
  209. *c3.x(), *c3.y(),
  210. *c4.x(), *c4.y(),
  211. c5[31].inner(), //TODO (res) how the path is structured assumed root is last node in the path.
  212. c6,
  213. ];
  214. let mut vec_inputs = vec![public_inputs];
  215. //TODO
  216. let prover = MockProver::run(k, &contract, vec_inputs).unwrap();
  217. assert_eq!(prover.verify(), Ok(()));
  218. }