zk.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. // ../zkas simple.zk
  19. use darkfi::{
  20. zk::{
  21. proof::{Proof, ProvingKey, VerifyingKey},
  22. vm::{Witness, ZkCircuit},
  23. vm_heap::empty_witnesses,
  24. },
  25. zkas::decoder::ZkBinary,
  26. Result,
  27. };
  28. use darkfi_sdk::{
  29. crypto::pedersen::pedersen_commitment_u64,
  30. pasta::{
  31. arithmetic::CurveAffine,
  32. group::{ff::Field, Curve},
  33. pallas,
  34. },
  35. };
  36. use halo2_proofs::circuit::Value;
  37. use rand::rngs::OsRng;
  38. fn main() -> Result<()> {
  39. let bincode = include_bytes!("simple.zk.bin");
  40. let zkbin = ZkBinary::decode(bincode)?;
  41. // ======
  42. // Prover
  43. // ======
  44. // Bigger k = more rows, but slower circuit
  45. // Number of rows is 2^k
  46. let k = zkbin.k;
  47. // Witness values
  48. let value = 42;
  49. let value_blind = pallas::Scalar::random(&mut OsRng);
  50. let prover_witnesses = vec![
  51. Witness::Base(Value::known(pallas::Base::from(value))),
  52. Witness::Scalar(Value::known(value_blind)),
  53. ];
  54. // Create the public inputs
  55. let value_commit = pedersen_commitment_u64(value, value_blind);
  56. let value_coords = value_commit.to_affine().coordinates().unwrap();
  57. let public_inputs = vec![*value_coords.x(), *value_coords.y()];
  58. // Create the circuit
  59. let circuit = ZkCircuit::new(prover_witnesses, &zkbin.clone());
  60. let now = std::time::Instant::now();
  61. let proving_key = ProvingKey::build(k, &circuit);
  62. println!("ProvingKey built [{} s]", now.elapsed().as_secs_f64());
  63. let now = std::time::Instant::now();
  64. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  65. println!("Proof created [{} s]", now.elapsed().as_secs_f64());
  66. // ========
  67. // Verifier
  68. // ========
  69. // Construct empty witnesses
  70. let verifier_witnesses = empty_witnesses(&zkbin);
  71. // Create the circuit
  72. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  73. let now = std::time::Instant::now();
  74. let verifying_key = VerifyingKey::build(k, &circuit);
  75. println!("VerifyingKey built [{} s]", now.elapsed().as_secs_f64());
  76. let now = std::time::Instant::now();
  77. proof.verify(&verifying_key, &public_inputs)?;
  78. println!("proof verify [{} s]", now.elapsed().as_secs_f64());
  79. Ok(())
  80. }