zk.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_stack::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::{
  33. ff::Field,
  34. Curve,
  35. },
  36. pallas,
  37. },
  38. };
  39. use halo2_proofs::circuit::Value;
  40. use rand::rngs::OsRng;
  41. fn main() -> Result<()> {
  42. let bincode = include_bytes!("simple.zk.bin");
  43. let zkbin = ZkBinary::decode(bincode)?;
  44. // ======
  45. // Prover
  46. // ======
  47. // Bigger k = more rows, but slower circuit
  48. // Number of rows is 2^k
  49. let k = 13;
  50. // Witness values
  51. let value = 42;
  52. let value_blind = pallas::Scalar::random(&mut OsRng);
  53. let prover_witnesses = vec![
  54. Witness::Base(Value::known(pallas::Base::from(value))),
  55. Witness::Scalar(Value::known(value_blind)),
  56. ];
  57. // Create the public inputs
  58. let value_commit = pedersen_commitment_u64(value, value_blind);
  59. let value_coords = value_commit.to_affine().coordinates().unwrap();
  60. let public_inputs = vec![*value_coords.x(), *value_coords.y()];
  61. // Create the circuit
  62. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  63. let now = std::time::Instant::now();
  64. let proving_key = ProvingKey::build(k, &circuit);
  65. println!("ProvingKey built [{} s]", now.elapsed().as_secs_f64());
  66. let now = std::time::Instant::now();
  67. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  68. println!("Proof created [{} s]", now.elapsed().as_secs_f64());
  69. // ========
  70. // Verifier
  71. // ========
  72. // Construct empty witnesses
  73. let verifier_witnesses = empty_witnesses(&zkbin);
  74. // Create the circuit
  75. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  76. let now = std::time::Instant::now();
  77. let verifying_key = VerifyingKey::build(k, &circuit);
  78. println!("VerifyingKey built [{} s]", now.elapsed().as_secs_f64());
  79. let now = std::time::Instant::now();
  80. proof.verify(&verifying_key, &public_inputs)?;
  81. println!("proof verify [{} s]", now.elapsed().as_secs_f64());
  82. Ok(())
  83. }