arithmetic_proof.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use darkfi::{
  2. crypto::{
  3. proof::{ProvingKey, VerifyingKey},
  4. Proof,
  5. },
  6. zk::{
  7. vm::{Witness, ZkCircuit},
  8. vm_stack::empty_witnesses,
  9. },
  10. zkas::decoder::ZkBinary,
  11. Result,
  12. };
  13. use pasta_curves::pallas;
  14. use rand::rngs::OsRng;
  15. use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
  16. #[test]
  17. fn arithmetic_proof() -> Result<()> {
  18. TermLogger::init(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto)
  19. .unwrap();
  20. /* ANCHOR: main */
  21. let bincode = include_bytes!("../proof/arithmetic.zk.bin");
  22. let zkbin = ZkBinary::decode(bincode)?;
  23. // ======
  24. // Prover
  25. // ======
  26. // Witness values
  27. let a = pallas::Base::from(42);
  28. let b = pallas::Base::from(69);
  29. let y_0 = pallas::Base::from(0); // Here we will compare a > b, which is false (0)
  30. let y_1 = pallas::Base::from(1); // Here we will compare b > a, which is true (1)
  31. let prover_witnesses = vec![Witness::Base(Some(a)), Witness::Base(Some(b))];
  32. // Create the public inputs
  33. let sum = a + b;
  34. let product = a * b;
  35. let difference = a - b;
  36. let public_inputs = vec![sum, product, difference, y_0, y_1];
  37. // Create the circuit
  38. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  39. let proving_key = ProvingKey::build(13, &circuit);
  40. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  41. // ========
  42. // Verifier
  43. // ========
  44. // Construct empty witnesses
  45. let verifier_witnesses = empty_witnesses(&zkbin);
  46. // Create the circuit
  47. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  48. let verifying_key = VerifyingKey::build(13, &circuit);
  49. proof.verify(&verifying_key, &public_inputs)?;
  50. /* ANCHOR_END: main */
  51. Ok(())
  52. }