zkvm_opcodes.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. crypto::{pedersen::pedersen_commitment_u64, util::mod_r_p, MerkleNode, PublicKey, SecretKey},
  20. incrementalmerkletree::{bridgetree::BridgeTree, Tree},
  21. };
  22. use halo2_gadgets::poseidon::{
  23. primitives as poseidon,
  24. primitives::{ConstantLength, P128Pow5T3},
  25. };
  26. use halo2_proofs::{
  27. arithmetic::{CurveAffine, Field},
  28. circuit::Value,
  29. pasta::{group::Curve, pallas},
  30. };
  31. use rand::rngs::OsRng;
  32. use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
  33. use darkfi::{
  34. zk::{
  35. proof::{ProvingKey, VerifyingKey},
  36. vm::ZkCircuit,
  37. vm_stack::{empty_witnesses, Witness},
  38. Proof,
  39. },
  40. zkas::ZkBinary,
  41. Result,
  42. };
  43. #[test]
  44. fn zkvm_opcodes() -> Result<()> {
  45. TermLogger::init(LevelFilter::Info, Config::default(), TerminalMode::Mixed, ColorChoice::Auto)
  46. .unwrap();
  47. let bincode = include_bytes!("../proof/opcodes.zk.bin");
  48. let zkbin = ZkBinary::decode(bincode)?;
  49. // Values for the proof
  50. let value = 666_u64;
  51. let value_blind = pallas::Scalar::random(&mut OsRng);
  52. let blind = pallas::Base::random(&mut OsRng);
  53. let secret = pallas::Base::random(&mut OsRng);
  54. let a = pallas::Base::from(42);
  55. let b = pallas::Base::from(69);
  56. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  57. let c0 = pallas::Base::random(&mut OsRng);
  58. let c1 = pallas::Base::random(&mut OsRng);
  59. let c3 = pallas::Base::random(&mut OsRng);
  60. let c2 = {
  61. let messages = [pallas::Base::one(), blind];
  62. poseidon::Hash::<_, P128Pow5T3, ConstantLength<2>, 3, 2>::init().hash(messages)
  63. };
  64. tree.append(&MerkleNode::from(c0));
  65. tree.witness();
  66. tree.append(&MerkleNode::from(c1));
  67. tree.append(&MerkleNode::from(c2));
  68. let leaf_pos = tree.witness().unwrap();
  69. tree.append(&MerkleNode::from(c3));
  70. tree.witness();
  71. let root = tree.root(0).unwrap();
  72. let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap();
  73. let leaf_pos: u64 = leaf_pos.into();
  74. let ephem_secret = SecretKey::random(&mut OsRng);
  75. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  76. let (ephem_x, ephem_y) = PublicKey::from(pubkey * mod_r_p(ephem_secret.inner())).xy();
  77. let prover_witnesses = vec![
  78. Witness::Base(Value::known(pallas::Base::from(value))),
  79. Witness::Scalar(Value::known(value_blind)),
  80. Witness::Base(Value::known(blind)),
  81. Witness::Base(Value::known(a)),
  82. Witness::Base(Value::known(b)),
  83. Witness::Base(Value::known(secret)),
  84. Witness::EcNiPoint(Value::known(pubkey)),
  85. Witness::Base(Value::known(ephem_secret.inner())),
  86. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  87. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  88. ];
  89. let value_commit = pedersen_commitment_u64(value, value_blind);
  90. let value_coords = value_commit.to_affine().coordinates().unwrap();
  91. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  92. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  93. let public = PublicKey::from_secret(SecretKey::from(secret));
  94. let (pub_x, pub_y) = public.xy();
  95. let public_inputs = vec![
  96. *value_coords.x(),
  97. *value_coords.y(),
  98. c2,
  99. d,
  100. root.inner(),
  101. pub_x,
  102. pub_y,
  103. ephem_x,
  104. ephem_y,
  105. ];
  106. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  107. let proving_key = ProvingKey::build(13, &circuit);
  108. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  109. let verifier_witnesses = empty_witnesses(&zkbin);
  110. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  111. let verifying_key = VerifyingKey::build(13, &circuit);
  112. proof.verify(&verifying_key, &public_inputs)?;
  113. Ok(())
  114. }