zkvm_opcodes.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 darkfi_sdk::crypto::{
  19. pedersen::pedersen_commitment_u64, util::fp_mod_fv, Blind, MerkleNode, MerkleTree, PublicKey,
  20. 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. 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, false)?;
  47. // Values for the proof
  48. let value = 666_u64;
  49. let value_blind = Blind::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 = MerkleTree::new(1);
  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(), pallas::Base::from(2), blind];
  60. poseidon::Hash::<_, P128Pow5T3, ConstantLength<3>, 3, 2>::init().hash(messages)
  61. };
  62. tree.append(MerkleNode::from(c0));
  63. tree.mark();
  64. tree.append(MerkleNode::from(c1));
  65. tree.append(MerkleNode::from(c2));
  66. let leaf_pos = tree.mark().unwrap();
  67. tree.append(MerkleNode::from(c3));
  68. tree.mark();
  69. let root = tree.root(0).unwrap();
  70. let merkle_path = tree.witness(leaf_pos, 0).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) =
  75. PublicKey::try_from(pubkey * fp_mod_fv(ephem_secret.inner())).unwrap().xy();
  76. let prover_witnesses = vec![
  77. Witness::Base(Value::known(pallas::Base::from(value))),
  78. Witness::Scalar(Value::known(value_blind.inner())),
  79. Witness::Base(Value::known(blind)),
  80. Witness::Base(Value::known(a)),
  81. Witness::Base(Value::known(b)),
  82. Witness::Base(Value::known(secret)),
  83. Witness::EcNiPoint(Value::known(pubkey)),
  84. Witness::Base(Value::known(ephem_secret.inner())),
  85. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  86. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  87. Witness::Base(Value::known(pallas::Base::ONE)),
  88. ];
  89. let value_commit = pedersen_commitment_u64(value, value_blind);
  90. let value_coords = value_commit.to_affine().coordinates().unwrap();
  91. let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
  92. let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
  93. let public = PublicKey::from_secret(SecretKey::from(secret));
  94. let (pub_x, pub_y) = public.xy();
  95. let public_inputs = vec![
  96. *value_coords.x(),
  97. *value_coords.y(),
  98. c2,
  99. d,
  100. root.inner(),
  101. pub_x,
  102. pub_y,
  103. ephem_x,
  104. ephem_y,
  105. a,
  106. pallas::Base::ZERO,
  107. ];
  108. //darkfi::zk::export_witness_json("proof/witness/opcodes.json", &prover_witnesses, &public_inputs);
  109. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  110. let mockprover = MockProver::run(zkbin.k, &circuit, vec![public_inputs.clone()])?;
  111. mockprover.assert_satisfied();
  112. let proving_key = ProvingKey::build(zkbin.k, &circuit);
  113. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  114. let verifier_witnesses = empty_witnesses(&zkbin)?;
  115. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  116. let verifying_key = VerifyingKey::build(zkbin.k, &circuit);
  117. proof.verify(&verifying_key, &public_inputs)?;
  118. Ok(())
  119. }