zk.rs 2.9 KB

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