main.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 std::{
  19. env,
  20. fs::File,
  21. io::{Cursor, Read},
  22. path::Path,
  23. };
  24. use darkfi::{
  25. zk::{empty_witnesses, Proof, VerifyingKey, ZkCircuit},
  26. zkas::ZkBinary,
  27. Result,
  28. };
  29. use darkfi_sdk::pasta::pallas::Base;
  30. use darkfi_serial::deserialize;
  31. fn main() -> Result<()> {
  32. let args: Vec<String> = env::args().collect();
  33. if args.len() != 2 {
  34. println!("Usage: ./verifier [ZK_BIN_FILE_PATH]");
  35. return Ok(())
  36. }
  37. let zk_bin_path = Path::new(&args[1]);
  38. if !zk_bin_path.is_file() || !zk_bin_path.exists() || !&args[1].ends_with(".zk.bin") {
  39. println!("Zk bin file path does not exist or is invalid");
  40. return Ok(())
  41. }
  42. //Load zkbin, proof, verifying key, public inputs from file
  43. let mut file = File::open(zk_bin_path)?;
  44. let mut bincode = vec![];
  45. file.read_to_end(&mut bincode)?;
  46. let proof_path = zk_bin_path.to_str().unwrap().replace(".zk.bin", ".proof.bin");
  47. let mut file = File::open(proof_path)?;
  48. let mut proof_bin = vec![];
  49. file.read_to_end(&mut proof_bin)?;
  50. let vks_path = zk_bin_path.to_str().unwrap().replace(".zk.bin", ".vks.bin");
  51. let mut file = File::open(vks_path)?;
  52. let mut vkbin = vec![];
  53. file.read_to_end(&mut vkbin)?;
  54. let pi_path = zk_bin_path.to_str().unwrap().replace(".zk.bin", ".pi.bin");
  55. let mut file = File::open(pi_path)?;
  56. let mut public_inputs_bin = vec![];
  57. file.read_to_end(&mut public_inputs_bin)?;
  58. // Deserialize and Verify
  59. let zkbin = ZkBinary::decode(&bincode, false)?;
  60. let verifier_witnesses = empty_witnesses(&zkbin)?;
  61. // Create the circuit
  62. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  63. let proof: Proof = deserialize(&proof_bin)?;
  64. let mut vk_buf = Cursor::new(vkbin);
  65. let vk = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut vk_buf, circuit)?;
  66. //let vk = VerifyingKey::build(zkbin.k, &circuit);
  67. let public_inputs: Vec<Base> = deserialize(&public_inputs_bin)?;
  68. Ok(proof.verify(&vk, &public_inputs)?)
  69. }