smt.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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::smt::{MemoryStorageFp, PoseidonFp, SmtMemoryFp, EMPTY_NODES_FP};
  19. use halo2_proofs::{arithmetic::Field, circuit::Value, dev::MockProver, pasta::Fp};
  20. use rand::rngs::OsRng;
  21. use darkfi::{
  22. zk::{
  23. proof::{ProvingKey, VerifyingKey},
  24. vm::ZkCircuit,
  25. vm_heap::{empty_witnesses, Witness},
  26. Proof,
  27. },
  28. zkas::ZkBinary,
  29. Result,
  30. };
  31. #[test]
  32. fn zkvm_smt() -> Result<()> {
  33. let bincode = include_bytes!("../proof/smt.zk.bin");
  34. let zkbin = ZkBinary::decode(bincode)?;
  35. let hasher = PoseidonFp::new();
  36. let store = MemoryStorageFp::new();
  37. let mut smt = SmtMemoryFp::new(store, hasher.clone(), &EMPTY_NODES_FP);
  38. let leaves = vec![Fp::random(&mut OsRng), Fp::random(&mut OsRng), Fp::random(&mut OsRng)];
  39. // Use the leaf value as its position in the SMT
  40. // Therefore we need an additional constraint that leaf == pos
  41. let leaves: Vec<_> = leaves.into_iter().map(|l| (l, l)).collect();
  42. smt.insert_batch(leaves.clone()).unwrap();
  43. let (pos, leaf) = leaves[2];
  44. assert_eq!(pos, leaf);
  45. assert_eq!(smt.get_leaf(&pos), leaf);
  46. let root = smt.root();
  47. let path = smt.prove_membership(&pos);
  48. assert!(path.verify(&root, &leaf, &pos));
  49. // Values for the proof
  50. let prover_witnesses =
  51. vec![Witness::SparseMerklePath(Value::known(path.path)), Witness::Base(Value::known(leaf))];
  52. let public_inputs = vec![root];
  53. //darkfi::zk::export_witness_json("proof/witness/smt.json", &prover_witnesses, &public_inputs);
  54. //let (prover_witnesses, public_inputs) = darkfi::zk::import_witness_json("witness.json");
  55. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  56. let mockprover = MockProver::run(zkbin.k, &circuit, vec![public_inputs.clone()])?;
  57. mockprover.assert_satisfied();
  58. let proving_key = ProvingKey::build(zkbin.k, &circuit);
  59. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  60. let verifier_witnesses = empty_witnesses(&zkbin)?;
  61. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  62. let verifying_key = VerifyingKey::build(zkbin.k, &circuit);
  63. proof.verify(&verifying_key, &public_inputs)?;
  64. Ok(())
  65. }