halo2_vk_ser.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.clone());
  49. let vk1 = VerifyingKey::build(13, &circuit);
  50. println!("Building vk2");
  51. let circuit = ZkCircuit::new(verifier_witnesses.clone(), zkbin.clone());
  52. let vk2 = VerifyingKey::build(13, &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. let vk3 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf1_c)?;
  64. println!("Reading vk4");
  65. let mut buf2_c = Cursor::new(buf2);
  66. let vk4 = VerifyingKey::read::<Cursor<Vec<u8>>, ZkCircuit>(&mut buf2_c)?;
  67. // Now let's see if we can verify a proof with all four keys.
  68. println!("Creating pk");
  69. let circuit = ZkCircuit::new(verifier_witnesses.clone(), zkbin.clone());
  70. let pk = ProvingKey::build(13, &circuit);
  71. let value = 666_u64;
  72. let value_blind = pallas::Scalar::random(&mut OsRng);
  73. let blind = pallas::Base::random(&mut OsRng);
  74. let secret = pallas::Base::random(&mut OsRng);
  75. let a = pallas::Base::from(42);
  76. let b = pallas::Base::from(69);
  77. let mut tree = MerkleTree::new(100);
  78. let c0 = pallas::Base::random(&mut OsRng);
  79. let c1 = pallas::Base::random(&mut OsRng);
  80. let c3 = pallas::Base::random(&mut OsRng);
  81. let c2 = {
  82. let messages = [pallas::Base::one(), pallas::Base::from(2), blind];
  83. poseidon::Hash::<_, P128Pow5T3, ConstantLength<3>, 3, 2>::init().hash(messages)
  84. };
  85. tree.append(MerkleNode::from(c0));
  86. tree.mark();
  87. tree.append(MerkleNode::from(c1));
  88. tree.append(MerkleNode::from(c2));
  89. let leaf_pos = tree.mark().unwrap();
  90. tree.append(MerkleNode::from(c3));
  91. tree.mark();
  92. let root = tree.root(0).unwrap();
  93. let merkle_path = tree.witness(leaf_pos, 0).unwrap();
  94. let leaf_pos: u64 = leaf_pos.into();
  95. let ephem_secret = SecretKey::random(&mut OsRng);
  96. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  97. let (ephem_x, ephem_y) = PublicKey::from(pubkey * mod_r_p(ephem_secret.inner())).xy();
  98. let prover_witnesses = vec![
  99. Witness::Base(Value::known(pallas::Base::from(value))),
  100. Witness::Scalar(Value::known(value_blind)),
  101. Witness::Base(Value::known(blind)),
  102. Witness::Base(Value::known(a)),
  103. Witness::Base(Value::known(b)),
  104. Witness::Base(Value::known(secret)),
  105. Witness::EcNiPoint(Value::known(pubkey)),
  106. Witness::Base(Value::known(ephem_secret.inner())),
  107. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  108. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  109. Witness::Base(Value::known(pallas::Base::ONE)),
  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. a,
  128. ];
  129. println!("Creating proof");
  130. let circuit = ZkCircuit::new(prover_witnesses, zkbin);
  131. let proof = Proof::create(&pk, &[circuit], &public_inputs, &mut OsRng)?;
  132. println!("Verifying with vk1");
  133. proof.verify(&vk1, &public_inputs)?;
  134. println!("Verifying with vk2");
  135. proof.verify(&vk2, &public_inputs)?;
  136. println!("Verifying with vk3");
  137. proof.verify(&vk3, &public_inputs)?;
  138. println!("Verifying with vk4");
  139. proof.verify(&vk4, &public_inputs)?;
  140. Ok(())
  141. }