zkvm_opcodes.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 darkfi_sdk::{
  19. crypto::{pedersen::pedersen_commitment_u64, util::mod_r_p, MerkleNode, PublicKey, SecretKey},
  20. incrementalmerkletree::{bridgetree::BridgeTree, Tree},
  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. dev::MockProver,
  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. // Values for the proof
  48. let value = 666_u64;
  49. let value_blind = pallas::Scalar::random(&mut OsRng);
  50. let blind = pallas::Base::random(&mut OsRng);
  51. let secret = pallas::Base::random(&mut OsRng);
  52. let a = pallas::Base::from(42);
  53. let b = pallas::Base::from(69);
  54. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  55. let c0 = pallas::Base::random(&mut OsRng);
  56. let c1 = pallas::Base::random(&mut OsRng);
  57. let c3 = pallas::Base::random(&mut OsRng);
  58. let c2 = {
  59. let messages = [pallas::Base::one(), blind];
  60. poseidon::Hash::<_, P128Pow5T3, ConstantLength<2>, 3, 2>::init().hash(messages)
  61. };
  62. tree.append(&MerkleNode::from(c0));
  63. tree.witness();
  64. tree.append(&MerkleNode::from(c1));
  65. tree.append(&MerkleNode::from(c2));
  66. let leaf_pos = tree.witness().unwrap();
  67. tree.append(&MerkleNode::from(c3));
  68. tree.witness();
  69. let root = tree.root(0).unwrap();
  70. let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap();
  71. let leaf_pos: u64 = leaf_pos.into();
  72. let ephem_secret = SecretKey::random(&mut OsRng);
  73. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  74. let (ephem_x, ephem_y) = PublicKey::from(pubkey * mod_r_p(ephem_secret.inner())).xy();
  75. let prover_witnesses = vec![
  76. Witness::Base(Value::known(pallas::Base::from(value))),
  77. Witness::Scalar(Value::known(value_blind)),
  78. Witness::Base(Value::known(blind)),
  79. Witness::Base(Value::known(a)),
  80. Witness::Base(Value::known(b)),
  81. Witness::Base(Value::known(secret)),
  82. Witness::EcNiPoint(Value::known(pubkey)),
  83. Witness::Base(Value::known(ephem_secret.inner())),
  84. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  85. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  86. ];
  87. let value_commit = pedersen_commitment_u64(value, value_blind);
  88. let value_coords = value_commit.to_affine().coordinates().unwrap();
  89. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  90. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  91. let public = PublicKey::from_secret(SecretKey::from(secret));
  92. let (pub_x, pub_y) = public.xy();
  93. let public_inputs = vec![
  94. *value_coords.x(),
  95. *value_coords.y(),
  96. c2,
  97. d,
  98. root.inner(),
  99. pub_x,
  100. pub_y,
  101. ephem_x,
  102. ephem_y,
  103. ];
  104. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  105. let mockprover = MockProver::run(13, &circuit, vec![public_inputs.clone()])?;
  106. mockprover.assert_satisfied();
  107. let proving_key = ProvingKey::build(13, &circuit);
  108. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  109. let verifier_witnesses = empty_witnesses(&zkbin);
  110. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  111. let verifying_key = VerifyingKey::build(13, &circuit);
  112. proof.verify(&verifying_key, &public_inputs)?;
  113. Ok(())
  114. }