smt.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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::smt::{MemoryStorageFp, PoseidonFp, SmtMemoryFp};
  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 empty_leaf = Fp::from(0);
  37. let store = MemoryStorageFp::new();
  38. let mut smt = SmtMemoryFp::new(store, hasher.clone(), empty_leaf.clone());
  39. let leaves = vec![Fp::random(&mut OsRng), Fp::random(&mut OsRng), Fp::random(&mut OsRng)];
  40. // Use the leaf value as its position in the SMT
  41. // Therefore we need an additional constraint that leaf == pos
  42. let leaves: Vec<_> = leaves.into_iter().map(|l| (l, l)).collect();
  43. smt.insert_batch(leaves.clone());
  44. let (pos, leaf) = leaves[2];
  45. assert_eq!(pos, leaf);
  46. assert_eq!(smt.get_leaf(&pos), leaf);
  47. let root = smt.root();
  48. let path = smt.prove_membership(&pos);
  49. assert!(path.verify(&root, &leaf, &pos));
  50. // Values for the proof
  51. let prover_witnesses = vec![
  52. Witness::Base(Value::known(root)),
  53. Witness::SparseMerklePath(Value::known(path.path)),
  54. Witness::Base(Value::known(leaf)),
  55. ];
  56. let public_inputs = vec![root];
  57. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  58. let mockprover = MockProver::run(zkbin.k, &circuit, vec![public_inputs.clone()])?;
  59. mockprover.assert_satisfied();
  60. let proving_key = ProvingKey::build(zkbin.k, &circuit);
  61. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  62. let verifier_witnesses = empty_witnesses(&zkbin)?;
  63. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  64. let verifying_key = VerifyingKey::build(zkbin.k, &circuit);
  65. proof.verify(&verifying_key, &public_inputs)?;
  66. Ok(())
  67. }