dao.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use bitvec::prelude::*;
  2. use halo2_gadgets::primitives::{
  3. poseidon,
  4. poseidon::{ConstantLength, P128Pow5T3},
  5. };
  6. use incrementalmerkletree::{bridgetree::BridgeTree, Frontier, Tree};
  7. use pasta_curves::{
  8. arithmetic::{CurveAffine, Field, FieldExt},
  9. group::{Curve, Group},
  10. pallas,
  11. };
  12. use rand::rngs::OsRng;
  13. use simplelog::{ColorChoice::Auto, Config, LevelFilter, TermLogger, TerminalMode::Mixed};
  14. use darkfi::{
  15. crypto::{
  16. keypair::Keypair,
  17. merkle_node::MerkleNode,
  18. schnorr::SchnorrSecret,
  19. util::{mod_r_p, pedersen_commitment_scalar},
  20. },
  21. Result,
  22. };
  23. fn main() -> Result<()> {
  24. let loglevel = match option_env!("RUST_LOG") {
  25. Some("debug") => LevelFilter::Debug,
  26. Some("trace") => LevelFilter::Trace,
  27. Some(_) | None => LevelFilter::Info,
  28. };
  29. TermLogger::init(loglevel, Config::default(), Mixed, Auto)?;
  30. /*
  31. let bincode = include_bytes!("../proof/dao.zk.bin");
  32. let zkbin = ZkBinary::decode(bincode)?;
  33. */
  34. // Contract address
  35. let a = pallas::Base::random(&mut OsRng);
  36. // Money in treasury
  37. let t = pallas::Base::from(666);
  38. // Serial number
  39. let s = pallas::Base::random(&mut OsRng);
  40. // Bulla blind
  41. let b_b = pallas::Base::random(&mut OsRng);
  42. let message = [a, t, s, b_b];
  43. let hasher = poseidon::Hash::init(P128Pow5T3, ConstantLength::<4>);
  44. let bulla = hasher.hash(message);
  45. // Merkle tree of DAOs
  46. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  47. let dao0 = pallas::Base::random(&mut OsRng);
  48. let dao2 = pallas::Base::random(&mut OsRng);
  49. tree.append(&MerkleNode(dao0));
  50. tree.witness();
  51. tree.append(&MerkleNode(bulla));
  52. tree.witness();
  53. tree.append(&MerkleNode(dao2));
  54. tree.witness();
  55. let (leaf_pos, merkle_path) = tree.authentication_path(&MerkleNode(bulla)).unwrap();
  56. let leaf_pos: u64 = leaf_pos.into();
  57. let leaf_pos = leaf_pos as u32;
  58. // Output 0:
  59. let output0_val = 42_u64;
  60. let output0_dest = pallas::Point::random(&mut OsRng);
  61. let output0_coords = output0_dest.to_affine().coordinates().unwrap();
  62. let output0_blind = pallas::Base::random(&mut OsRng);
  63. let message =
  64. [pallas::Base::from(output0_val), *output0_coords.x(), *output0_coords.y(), output0_blind];
  65. let hasher = poseidon::Hash::init(P128Pow5T3, ConstantLength::<4>);
  66. let output0 = hasher.hash(message);
  67. let authority = Keypair::random(&mut OsRng);
  68. let signature = authority.secret.sign(&output0.to_bytes());
  69. let vote_1 = pallas::Base::from(44);
  70. let vote_2 = pallas::Base::from(13);
  71. // This is a NO vote
  72. let vote_3 = -pallas::Base::from(49);
  73. let vote_1_blind = pallas::Scalar::random(&mut OsRng);
  74. let vote_1_commit = pedersen_commitment_scalar(mod_r_p(vote_1), vote_1_blind);
  75. let vote_2_blind = pallas::Scalar::random(&mut OsRng);
  76. let vote_2_commit = pedersen_commitment_scalar(mod_r_p(vote_2), vote_2_blind);
  77. let vote_3_blind = pallas::Scalar::random(&mut OsRng);
  78. let vote_3_commit = pedersen_commitment_scalar(mod_r_p(vote_3), vote_3_blind);
  79. let vote_commit = vote_1_commit + vote_2_commit + vote_3_commit;
  80. let vote_blinds = vote_1_blind + vote_2_blind + vote_3_blind;
  81. let vote_commit_coords = vote_commit.to_affine().coordinates().unwrap();
  82. /*
  83. let number = pallas::Base::from(u64::MAX).to_bytes();
  84. let bits = number.view_bits::<Lsb0>();
  85. println!("Positive: {:?}", bits);
  86. //let number = (-pallas::Base::from(u64::MAX)).to_bytes();
  87. let number = pallas::Base::from(0).to_bytes();
  88. let bits = number.view_bits::<Lsb0>();
  89. println!("Negative: {:?}", bits);
  90. */
  91. Ok(())
  92. }