zkvm_opcodes.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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::crypto::{
  19. pedersen::pedersen_commitment_u64, util::mod_r_p, MerkleNode, MerkleTree, PublicKey, SecretKey,
  20. };
  21. use halo2_gadgets::poseidon::{
  22. primitives as poseidon,
  23. primitives::{ConstantLength, P128Pow5T3},
  24. };
  25. use halo2_proofs::{
  26. arithmetic::{CurveAffine, Field},
  27. circuit::Value,
  28. dev::MockProver,
  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 zkvm_opcodes() -> Result<()> {
  44. let bincode = include_bytes!("../proof/opcodes.zk.bin");
  45. let zkbin = ZkBinary::decode(bincode)?;
  46. // Values for the proof
  47. let value = 666_u64;
  48. let value_blind = pallas::Scalar::random(&mut OsRng);
  49. let blind = pallas::Base::random(&mut OsRng);
  50. let secret = pallas::Base::random(&mut OsRng);
  51. let a = pallas::Base::from(42);
  52. let b = pallas::Base::from(69);
  53. let mut tree = MerkleTree::new(100);
  54. let c0 = pallas::Base::random(&mut OsRng);
  55. let c1 = pallas::Base::random(&mut OsRng);
  56. let c3 = pallas::Base::random(&mut OsRng);
  57. let c2 = {
  58. let messages = [pallas::Base::one(), pallas::Base::from(2), blind];
  59. poseidon::Hash::<_, P128Pow5T3, ConstantLength<3>, 3, 2>::init().hash(messages)
  60. };
  61. tree.append(MerkleNode::from(c0));
  62. tree.mark();
  63. tree.append(MerkleNode::from(c1));
  64. tree.append(MerkleNode::from(c2));
  65. let leaf_pos = tree.mark().unwrap();
  66. tree.append(MerkleNode::from(c3));
  67. tree.mark();
  68. let root = tree.root(0).unwrap();
  69. let merkle_path = tree.witness(leaf_pos, 0).unwrap();
  70. let leaf_pos: u64 = leaf_pos.into();
  71. let ephem_secret = SecretKey::random(&mut OsRng);
  72. let pubkey = PublicKey::from_secret(ephem_secret).inner();
  73. let (ephem_x, ephem_y) = PublicKey::from(pubkey * mod_r_p(ephem_secret.inner())).xy();
  74. let prover_witnesses = vec![
  75. Witness::Base(Value::known(pallas::Base::from(value))),
  76. Witness::Scalar(Value::known(value_blind)),
  77. Witness::Base(Value::known(blind)),
  78. Witness::Base(Value::known(a)),
  79. Witness::Base(Value::known(b)),
  80. Witness::Base(Value::known(secret)),
  81. Witness::EcNiPoint(Value::known(pubkey)),
  82. Witness::Base(Value::known(ephem_secret.inner())),
  83. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  84. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  85. Witness::Base(Value::known(pallas::Base::ONE)),
  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. a,
  104. pallas::Base::ZERO,
  105. ];
  106. let circuit = ZkCircuit::new(prover_witnesses, &zkbin);
  107. let mockprover = MockProver::run(zkbin.k, &circuit, vec![public_inputs.clone()])?;
  108. mockprover.assert_satisfied();
  109. let proving_key = ProvingKey::build(zkbin.k, &circuit);
  110. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  111. let verifier_witnesses = empty_witnesses(&zkbin)?;
  112. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  113. let verifying_key = VerifyingKey::build(zkbin.k, &circuit);
  114. proof.verify(&verifying_key, &public_inputs)?;
  115. Ok(())
  116. }