zk.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // ../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. //darkfi::zk::export_witness_json("example/simple.witness.json", &prover_witnesses, &public_inputs);
  60. let mut circuit = ZkCircuit::new(prover_witnesses, &zkbin.clone());
  61. circuit.enable_trace();
  62. let now = std::time::Instant::now();
  63. let proving_key = ProvingKey::build(k, &circuit);
  64. println!("ProvingKey built [{} s]", now.elapsed().as_secs_f64());
  65. let now = std::time::Instant::now();
  66. let circuits = [circuit];
  67. let proof = Proof::create(&proving_key, &circuits, &public_inputs, &mut OsRng)?;
  68. println!("Proof created [{} s]", now.elapsed().as_secs_f64());
  69. println!("Debug trace:");
  70. let opvalue_binding = circuits[0].tracer.opvalues.borrow();
  71. let opvalues = opvalue_binding.as_ref().unwrap();
  72. for (i, (opcode, opvalue)) in zkbin.opcodes.iter().zip(opvalues.iter()).enumerate() {
  73. let opcode = opcode.0;
  74. println!(" {}: {:?} {:?}", i, opcode, opvalue);
  75. }
  76. // ========
  77. // Verifier
  78. // ========
  79. // Construct empty witnesses
  80. let verifier_witnesses = empty_witnesses(&zkbin)?;
  81. // Create the circuit
  82. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  83. let now = std::time::Instant::now();
  84. let verifying_key = VerifyingKey::build(k, &circuit);
  85. println!("VerifyingKey built [{} s]", now.elapsed().as_secs_f64());
  86. let now = std::time::Instant::now();
  87. proof.verify(&verifying_key, &public_inputs)?;
  88. println!("proof verify [{} s]", now.elapsed().as_secs_f64());
  89. Ok(())
  90. }