halo2_vk_ser.rs 5.8 KB

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