smt.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::{
  19. constants::SPARSE_MERKLE_DEPTH,
  20. smt::{Poseidon, SparseMerkleTree},
  21. };
  22. use halo2_proofs::{arithmetic::Field, circuit::Value, dev::MockProver, pasta::Fp};
  23. use rand::rngs::OsRng;
  24. use darkfi::{
  25. zk::{
  26. proof::{ProvingKey, VerifyingKey},
  27. vm::ZkCircuit,
  28. vm_heap::{empty_witnesses, Witness},
  29. Proof,
  30. },
  31. zkas::ZkBinary,
  32. Result,
  33. };
  34. #[test]
  35. fn zkvm_smt() -> Result<()> {
  36. let bincode = include_bytes!("../proof/smt.zk.bin");
  37. let zkbin = ZkBinary::decode(bincode)?;
  38. let poseidon = Poseidon::<Fp, 2>::new();
  39. let empty_leaf = [0u8; 64];
  40. let leaves = [Fp::random(&mut OsRng), Fp::random(&mut OsRng), Fp::random(&mut OsRng)];
  41. let smt = SparseMerkleTree::<Fp, Poseidon<Fp, 2>, SPARSE_MERKLE_DEPTH>::new_sequential(
  42. &leaves,
  43. &poseidon.clone(),
  44. &empty_leaf,
  45. )
  46. .unwrap();
  47. let path = smt.generate_membership_proof(0);
  48. let root = path.calculate_root(&leaves[0], &poseidon).unwrap();
  49. let mut witnessed_path = [(Value::unknown(), Value::unknown()); SPARSE_MERKLE_DEPTH];
  50. for (i, (left, right)) in path.path.into_iter().enumerate() {
  51. witnessed_path[i] = (Value::known(left), Value::known(right));
  52. }
  53. let path = witnessed_path;
  54. // Values for the proof
  55. let prover_witnesses = vec![
  56. Witness::Base(Value::known(root)),
  57. Witness::SparseMerklePath(path),
  58. Witness::Base(Value::known(leaves[0])),
  59. ];
  60. let public_inputs = vec![root];
  61. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  62. let mockprover = MockProver::run(zkbin.k, &circuit, vec![public_inputs.clone()])?;
  63. mockprover.assert_satisfied();
  64. let proving_key = ProvingKey::build(zkbin.k, &circuit);
  65. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  66. let verifier_witnesses = empty_witnesses(&zkbin)?;
  67. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  68. let verifying_key = VerifyingKey::build(zkbin.k, &circuit);
  69. proof.verify(&verifying_key, &public_inputs)?;
  70. Ok(())
  71. }