zkvm_opcodes.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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::crypto::MerkleNode;
  19. use halo2_gadgets::poseidon::{
  20. primitives as poseidon,
  21. primitives::{ConstantLength, P128Pow5T3},
  22. };
  23. use halo2_proofs::{
  24. arithmetic::{CurveAffine, Field},
  25. circuit::Value,
  26. pasta::{group::Curve, pallas},
  27. };
  28. use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
  29. use rand::rngs::OsRng;
  30. use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
  31. use darkfi::{
  32. crypto::{
  33. keypair::{PublicKey, SecretKey},
  34. proof::{ProvingKey, VerifyingKey},
  35. util::pedersen_commitment_u64,
  36. Proof,
  37. },
  38. zk::{
  39. vm::ZkCircuit,
  40. vm_stack::{empty_witnesses, Witness},
  41. },
  42. zkas::ZkBinary,
  43. Result,
  44. };
  45. #[test]
  46. fn zkvm_opcodes() -> Result<()> {
  47. TermLogger::init(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto)
  48. .unwrap();
  49. let bincode = include_bytes!("../proof/opcodes.zk.bin");
  50. let zkbin = ZkBinary::decode(bincode)?;
  51. // Values for the proof
  52. let value = 666_u64;
  53. let value_blind = pallas::Scalar::random(&mut OsRng);
  54. let blind = pallas::Base::random(&mut OsRng);
  55. let secret = pallas::Base::random(&mut OsRng);
  56. let a = pallas::Base::from(42);
  57. let b = pallas::Base::from(69);
  58. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  59. let c0 = pallas::Base::random(&mut OsRng);
  60. let c1 = pallas::Base::random(&mut OsRng);
  61. let c3 = pallas::Base::random(&mut OsRng);
  62. let c2 = {
  63. let messages = [pallas::Base::one(), blind];
  64. poseidon::Hash::<_, P128Pow5T3, ConstantLength<2>, 3, 2>::init().hash(messages)
  65. };
  66. tree.append(&MerkleNode::from(c0));
  67. tree.witness();
  68. tree.append(&MerkleNode::from(c1));
  69. tree.append(&MerkleNode::from(c2));
  70. let leaf_pos = tree.witness().unwrap();
  71. tree.append(&MerkleNode::from(c3));
  72. tree.witness();
  73. let root = tree.root(0).unwrap();
  74. let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap();
  75. let leaf_pos: u64 = leaf_pos.into();
  76. let prover_witnesses = vec![
  77. Witness::Base(Value::known(pallas::Base::from(value))),
  78. Witness::Scalar(Value::known(value_blind)),
  79. Witness::Base(Value::known(blind)),
  80. Witness::Base(Value::known(a)),
  81. Witness::Base(Value::known(b)),
  82. Witness::Base(Value::known(secret)),
  83. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  84. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  85. ];
  86. let value_commit = pedersen_commitment_u64(value, value_blind);
  87. let value_coords = value_commit.to_affine().coordinates().unwrap();
  88. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  89. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  90. let public = PublicKey::from_secret(SecretKey::from(secret));
  91. let public_coords = public.0.to_affine().coordinates().unwrap();
  92. let public_inputs = vec![
  93. *value_coords.x(),
  94. *value_coords.y(),
  95. c2,
  96. d,
  97. root.inner(),
  98. *public_coords.x(),
  99. *public_coords.y(),
  100. ];
  101. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  102. let proving_key = ProvingKey::build(13, &circuit);
  103. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  104. let verifier_witnesses = empty_witnesses(&zkbin);
  105. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  106. let verifying_key = VerifyingKey::build(13, &circuit);
  107. proof.verify(&verifying_key, &public_inputs)?;
  108. Ok(())
  109. }