halo2_vk_ser.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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::fp_mod_fv, Blind, MerkleNode, MerkleTree, PublicKey,
  21. SecretKey,
  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 halo2_vk_ser() -> Result<()> {
  45. let bincode = include_bytes!("../proof/opcodes.zk.bin");
  46. let zkbin = ZkBinary::decode(bincode, false)?;
  47. let verifier_witnesses = empty_witnesses(&zkbin)?;
  48. println!("Building vk1");
  49. let circuit = ZkCircuit::new(verifier_witnesses.clone(), &zkbin);
  50. let vk1 = VerifyingKey::build(zkbin.k, &circuit);
  51. println!("Building vk2");
  52. let circuit = ZkCircuit::new(verifier_witnesses.clone(), &zkbin);
  53. let vk2 = VerifyingKey::build(zkbin.k, &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. // Construct the circuit to be able to read the VerifyingKey
  65. let circuit = ZkCircuit::new(empty_witnesses(&zkbin)?, &zkbin);
  66. let vk3 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf1_c, circuit)?;
  67. println!("Reading vk4");
  68. let mut buf2_c = Cursor::new(buf2);
  69. // Construct the circuit to be able to read the VerifyingKey
  70. let circuit = ZkCircuit::new(empty_witnesses(&zkbin)?, &zkbin);
  71. let vk4 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf2_c, circuit)?;
  72. // Now let's see if we can verify a proof with all four keys.
  73. println!("Creating pk");
  74. let circuit = ZkCircuit::new(verifier_witnesses.clone(), &zkbin);
  75. let pk = ProvingKey::build(zkbin.k, &circuit);
  76. let value = 666_u64;
  77. let value_blind = Blind::random(&mut OsRng);
  78. let blind = pallas::Base::random(&mut OsRng);
  79. let secret = pallas::Base::random(&mut OsRng);
  80. let a = pallas::Base::from(42);
  81. let b = pallas::Base::from(69);
  82. let mut tree = MerkleTree::new(1);
  83. let c0 = pallas::Base::random(&mut OsRng);
  84. let c1 = pallas::Base::random(&mut OsRng);
  85. let c3 = pallas::Base::random(&mut OsRng);
  86. let c2 = {
  87. let messages = [pallas::Base::one(), pallas::Base::from(2), blind];
  88. poseidon::Hash::<_, P128Pow5T3, ConstantLength<3>, 3, 2>::init().hash(messages)
  89. };
  90. tree.append(MerkleNode::from(c0));
  91. tree.mark();
  92. tree.append(MerkleNode::from(c1));
  93. tree.append(MerkleNode::from(c2));
  94. let leaf_pos = tree.mark().unwrap();
  95. tree.append(MerkleNode::from(c3));
  96. tree.mark();
  97. let root = tree.root(0).unwrap();
  98. let merkle_path = tree.witness(leaf_pos, 0).unwrap();
  99. let leaf_pos: u64 = leaf_pos.into();
  100. let ephem_secret = SecretKey::random(&mut OsRng);
  101. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  102. let (ephem_x, ephem_y) =
  103. PublicKey::try_from(pubkey * fp_mod_fv(ephem_secret.inner())).unwrap().xy();
  104. let prover_witnesses = vec![
  105. Witness::Base(Value::known(pallas::Base::from(value))),
  106. Witness::Scalar(Value::known(value_blind.inner())),
  107. Witness::Base(Value::known(blind)),
  108. Witness::Base(Value::known(a)),
  109. Witness::Base(Value::known(b)),
  110. Witness::Base(Value::known(secret)),
  111. Witness::EcNiPoint(Value::known(pubkey)),
  112. Witness::Base(Value::known(ephem_secret.inner())),
  113. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  114. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  115. Witness::Base(Value::known(pallas::Base::ONE)),
  116. ];
  117. let value_commit = pedersen_commitment_u64(value, value_blind);
  118. let value_coords = value_commit.to_affine().coordinates().unwrap();
  119. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  120. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  121. let public = PublicKey::from_secret(SecretKey::from(secret));
  122. let (pub_x, pub_y) = public.xy();
  123. let public_inputs = vec![
  124. *value_coords.x(),
  125. *value_coords.y(),
  126. c2,
  127. d,
  128. root.inner(),
  129. pub_x,
  130. pub_y,
  131. ephem_x,
  132. ephem_y,
  133. a,
  134. pallas::Base::ZERO,
  135. ];
  136. println!("Creating proof");
  137. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  138. let proof = Proof::create(&pk, &[circuit], &public_inputs, &mut OsRng)?;
  139. println!("Verifying with vk1");
  140. proof.verify(&vk1, &public_inputs)?;
  141. println!("Verifying with vk2");
  142. proof.verify(&vk2, &public_inputs)?;
  143. println!("Verifying with vk3");
  144. proof.verify(&vk3, &public_inputs)?;
  145. println!("Verifying with vk4");
  146. proof.verify(&vk4, &public_inputs)?;
  147. Ok(())
  148. }