halo2_vk_ser.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 std::io::Cursor;
  19. use darkfi_sdk::{
  20. crypto::{pedersen::pedersen_commitment_u64, util::mod_r_p, MerkleNode, PublicKey, SecretKey},
  21. incrementalmerkletree::{bridgetree::BridgeTree, Tree},
  22. };
  23. use halo2_gadgets::poseidon::{
  24. primitives as poseidon,
  25. primitives::{ConstantLength, P128Pow5T3},
  26. };
  27. use halo2_proofs::{
  28. arithmetic::{CurveAffine, Field},
  29. circuit::Value,
  30. pasta::{group::Curve, pallas},
  31. };
  32. use rand::rngs::OsRng;
  33. use darkfi::{
  34. zk::{
  35. proof::{ProvingKey, VerifyingKey},
  36. vm::ZkCircuit,
  37. vm_heap::{empty_witnesses, Witness},
  38. Proof,
  39. },
  40. zkas::ZkBinary,
  41. Result,
  42. };
  43. #[test]
  44. fn zkvm_opcodes() -> Result<()> {
  45. let bincode = include_bytes!("../proof/opcodes.zk.bin");
  46. let zkbin = ZkBinary::decode(bincode)?;
  47. let verifier_witnesses = empty_witnesses(&zkbin);
  48. println!("Building vk1");
  49. let circuit = ZkCircuit::new(verifier_witnesses.clone(), zkbin.clone());
  50. let vk1 = VerifyingKey::build(13, &circuit);
  51. println!("Building vk2");
  52. let circuit = ZkCircuit::new(verifier_witnesses.clone(), zkbin.clone());
  53. let vk2 = VerifyingKey::build(13, &circuit);
  54. let mut buf1 = vec![];
  55. let mut buf2 = vec![];
  56. println!("Writing vk1");
  57. vk1.write(&mut buf1)?;
  58. println!("Writing vk2");
  59. vk2.write(&mut buf2)?;
  60. println!("{} kB", buf1.len() / 1024);
  61. assert_eq!(buf1, buf2);
  62. println!("Reading vk3");
  63. let mut buf1_c = Cursor::new(buf1);
  64. let vk3 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf1_c)?;
  65. println!("Reading vk4");
  66. let mut buf2_c = Cursor::new(buf2);
  67. let vk4 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf2_c)?;
  68. // Now let's see if we can verify a proof with all four keys.
  69. println!("Creating pk");
  70. let circuit = ZkCircuit::new(verifier_witnesses.clone(), zkbin.clone());
  71. let pk = ProvingKey::build(13, &circuit);
  72. let value = 666_u64;
  73. let value_blind = pallas::Scalar::random(&mut OsRng);
  74. let blind = pallas::Base::random(&mut OsRng);
  75. let secret = pallas::Base::random(&mut OsRng);
  76. let a = pallas::Base::from(42);
  77. let b = pallas::Base::from(69);
  78. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  79. let c0 = pallas::Base::random(&mut OsRng);
  80. let c1 = pallas::Base::random(&mut OsRng);
  81. let c3 = pallas::Base::random(&mut OsRng);
  82. let c2 = {
  83. let messages = [pallas::Base::one(), blind];
  84. poseidon::Hash::<_, P128Pow5T3, ConstantLength<2>, 3, 2>::init().hash(messages)
  85. };
  86. tree.append(&MerkleNode::from(c0));
  87. tree.witness();
  88. tree.append(&MerkleNode::from(c1));
  89. tree.append(&MerkleNode::from(c2));
  90. let leaf_pos = tree.witness().unwrap();
  91. tree.append(&MerkleNode::from(c3));
  92. tree.witness();
  93. let root = tree.root(0).unwrap();
  94. let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap();
  95. let leaf_pos: u64 = leaf_pos.into();
  96. let ephem_secret = SecretKey::random(&mut OsRng);
  97. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  98. let (ephem_x, ephem_y) = PublicKey::from(pubkey * mod_r_p(ephem_secret.inner())).xy();
  99. let prover_witnesses = vec![
  100. Witness::Base(Value::known(pallas::Base::from(value))),
  101. Witness::Scalar(Value::known(value_blind)),
  102. Witness::Base(Value::known(blind)),
  103. Witness::Base(Value::known(a)),
  104. Witness::Base(Value::known(b)),
  105. Witness::Base(Value::known(secret)),
  106. Witness::EcNiPoint(Value::known(pubkey)),
  107. Witness::Base(Value::known(ephem_secret.inner())),
  108. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  109. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  110. ];
  111. let value_commit = pedersen_commitment_u64(value, value_blind);
  112. let value_coords = value_commit.to_affine().coordinates().unwrap();
  113. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  114. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  115. let public = PublicKey::from_secret(SecretKey::from(secret));
  116. let (pub_x, pub_y) = public.xy();
  117. let public_inputs = vec![
  118. *value_coords.x(),
  119. *value_coords.y(),
  120. c2,
  121. d,
  122. root.inner(),
  123. pub_x,
  124. pub_y,
  125. ephem_x,
  126. ephem_y,
  127. ];
  128. println!("Creating proof");
  129. let circuit = ZkCircuit::new(prover_witnesses, zkbin);
  130. let proof = Proof::create(&pk, &[circuit], &public_inputs, &mut OsRng)?;
  131. println!("Verifying with vk1");
  132. proof.verify(&vk1, &public_inputs)?;
  133. println!("Verifying with vk2");
  134. proof.verify(&vk2, &public_inputs)?;
  135. println!("Verifying with vk3");
  136. proof.verify(&vk3, &public_inputs)?;
  137. println!("Verifying with vk4");
  138. proof.verify(&vk4, &public_inputs)?;
  139. Ok(())
  140. }