arithmetic_proof.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #[test]
  16. fn arithmetic_proof() -> Result<()> {
  17. /* ANCHOR: main */
  18. let bincode = include_bytes!("../proof/arithmetic.zk.bin");
  19. let zkbin = ZkBinary::decode(bincode)?;
  20. // ======
  21. // Prover
  22. // ======
  23. // Witness values
  24. let a = pallas::Base::from(42);
  25. let b = pallas::Base::from(69);
  26. let y_0 = pallas::Base::from(0); // Here we will compare a > b, which is false (0)
  27. let y_1 = pallas::Base::from(1); // Here we will compare b > a, which is true (1)
  28. let prover_witnesses = vec![Witness::Base(Some(a)), Witness::Base(Some(b))];
  29. // Create the public inputs
  30. let sum = a + b;
  31. let product = a * b;
  32. let difference = a - b;
  33. let public_inputs = vec![sum, product, difference, y_0, y_1];
  34. // Create the circuit
  35. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  36. let proving_key = ProvingKey::build(13, &circuit);
  37. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  38. // ========
  39. // Verifier
  40. // ========
  41. // Construct empty witnesses
  42. let verifier_witnesses = empty_witnesses(&zkbin);
  43. // Create the circuit
  44. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  45. let verifying_key = VerifyingKey::build(13, &circuit);
  46. proof.verify(&verifying_key, &public_inputs)?;
  47. /* ANCHOR_END: main */
  48. Ok(())
  49. }