zk.rs 3.1 KB

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