zk.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // ../zkas simple.zk
  2. use darkfi::{
  3. crypto::{
  4. proof::{ProvingKey, VerifyingKey},
  5. util::pedersen_commitment_u64,
  6. Proof,
  7. },
  8. zk::{
  9. vm::{Witness, ZkCircuit},
  10. vm_stack::empty_witnesses,
  11. },
  12. zkas::decoder::ZkBinary,
  13. Result,
  14. };
  15. use halo2_proofs::circuit::Value;
  16. use pasta_curves::{
  17. arithmetic::CurveAffine,
  18. group::{ff::Field, Curve},
  19. pallas,
  20. };
  21. use rand::rngs::OsRng;
  22. fn main() -> Result<()> {
  23. let bincode = include_bytes!("simple.zk.bin");
  24. let zkbin = ZkBinary::decode(bincode)?;
  25. // ======
  26. // Prover
  27. // ======
  28. // Bigger k = more rows, but slower circuit
  29. // Number of rows is 2^k
  30. let k = 13;
  31. // Witness values
  32. let value = 42;
  33. let value_blind = pallas::Scalar::random(&mut OsRng);
  34. let prover_witnesses = vec![
  35. Witness::Base(Value::known(pallas::Base::from(value))),
  36. Witness::Scalar(Value::known(value_blind)),
  37. ];
  38. // Create the public inputs
  39. let value_commit = pedersen_commitment_u64(value, value_blind);
  40. let value_coords = value_commit.to_affine().coordinates().unwrap();
  41. let public_inputs = vec![*value_coords.x(), *value_coords.y()];
  42. // Create the circuit
  43. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  44. let now = std::time::Instant::now();
  45. let proving_key = ProvingKey::build(k, &circuit);
  46. println!("ProvingKey built [{} s]", now.elapsed().as_secs_f64());
  47. let now = std::time::Instant::now();
  48. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  49. println!("Proof created [{} s]", now.elapsed().as_secs_f64());
  50. // ========
  51. // Verifier
  52. // ========
  53. // Construct empty witnesses
  54. let verifier_witnesses = empty_witnesses(&zkbin);
  55. // Create the circuit
  56. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  57. let now = std::time::Instant::now();
  58. let verifying_key = VerifyingKey::build(k, &circuit);
  59. println!("VerifyingKey built [{} s]", now.elapsed().as_secs_f64());
  60. let now = std::time::Instant::now();
  61. proof.verify(&verifying_key, &public_inputs)?;
  62. println!("proof verify [{} s]", now.elapsed().as_secs_f64());
  63. Ok(())
  64. }