arithmetic_proof.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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. use darkfi::{
  19. crypto::{
  20. proof::{ProvingKey, VerifyingKey},
  21. Proof,
  22. },
  23. zk::{
  24. vm::{Witness, ZkCircuit},
  25. vm_stack::empty_witnesses,
  26. },
  27. zkas::decoder::ZkBinary,
  28. Result,
  29. };
  30. use halo2_proofs::circuit::Value;
  31. use pasta_curves::pallas;
  32. use rand::rngs::OsRng;
  33. use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
  34. #[test]
  35. fn arithmetic_proof() -> Result<()> {
  36. TermLogger::init(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto)
  37. .unwrap();
  38. /* ANCHOR: main */
  39. let bincode = include_bytes!("../proof/arithmetic.zk.bin");
  40. let zkbin = ZkBinary::decode(bincode)?;
  41. // ======
  42. // Prover
  43. // ======
  44. // Witness values
  45. let a = pallas::Base::from(42);
  46. let b = pallas::Base::from(69);
  47. let y_0 = pallas::Base::from(0); // Here we will compare a > b, which is false (0)
  48. let y_1 = pallas::Base::from(1); // Here we will compare b > a, which is true (1)
  49. let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))];
  50. // Create the public inputs
  51. let sum = a + b;
  52. let product = a * b;
  53. let difference = a - b;
  54. let public_inputs = vec![sum, product, difference, y_0, y_1];
  55. // Create the circuit
  56. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  57. let proving_key = ProvingKey::build(13, &circuit);
  58. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  59. // ========
  60. // Verifier
  61. // ========
  62. // Construct empty witnesses
  63. let verifier_witnesses = empty_witnesses(&zkbin);
  64. // Create the circuit
  65. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  66. let verifying_key = VerifyingKey::build(13, &circuit);
  67. proof.verify(&verifying_key, &public_inputs)?;
  68. /* ANCHOR_END: main */
  69. Ok(())
  70. }