arithmetic_proof.rs 1.8 KB

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