arithmetic_proof.rs 2.6 KB

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