mint_proof.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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::{
  20. pedersen::{pedersen_commitment_base, pedersen_commitment_u64},
  21. PublicKey, SecretKey,
  22. },
  23. pasta::{
  24. arithmetic::CurveAffine,
  25. group::{ff::Field, Curve},
  26. pallas,
  27. },
  28. };
  29. use halo2_gadgets::poseidon::primitives as poseidon;
  30. use halo2_proofs::circuit::Value;
  31. use rand::rngs::OsRng;
  32. use darkfi::{
  33. zk::{
  34. proof::{ProvingKey, VerifyingKey},
  35. vm::{Witness, ZkCircuit},
  36. vm_stack::empty_witnesses,
  37. Proof,
  38. },
  39. zkas::decoder::ZkBinary,
  40. Result,
  41. };
  42. #[test]
  43. fn mint_proof() -> Result<()> {
  44. /* ANCHOR: main */
  45. let bincode = include_bytes!("../proof/mint.zk.bin");
  46. let zkbin = ZkBinary::decode(bincode)?;
  47. // ======
  48. // Prover
  49. // ======
  50. // Witness values
  51. let value = 42;
  52. let token_id = pallas::Base::random(&mut OsRng);
  53. let value_blind = pallas::Scalar::random(&mut OsRng);
  54. let token_blind = pallas::Scalar::random(&mut OsRng);
  55. let serial = pallas::Base::random(&mut OsRng);
  56. let coin_blind = pallas::Base::random(&mut OsRng);
  57. let public_key = PublicKey::from_secret(SecretKey::random(&mut OsRng));
  58. let (pub_x, pub_y) = public_key.xy();
  59. let prover_witnesses = vec![
  60. Witness::Base(Value::known(pub_x)),
  61. Witness::Base(Value::known(pub_y)),
  62. Witness::Base(Value::known(pallas::Base::from(value))),
  63. Witness::Base(Value::known(token_id)),
  64. Witness::Base(Value::known(serial)),
  65. Witness::Base(Value::known(coin_blind)),
  66. Witness::Scalar(Value::known(value_blind)),
  67. Witness::Scalar(Value::known(token_blind)),
  68. ];
  69. // Create the public inputs
  70. let msgs = [pub_x, pub_y, pallas::Base::from(value), token_id, serial, coin_blind];
  71. let coin = poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<6>, 3, 2>::init()
  72. .hash(msgs);
  73. let value_commit = pedersen_commitment_u64(value, value_blind);
  74. let value_coords = value_commit.to_affine().coordinates().unwrap();
  75. let token_commit = pedersen_commitment_base(token_id, token_blind);
  76. let token_coords = token_commit.to_affine().coordinates().unwrap();
  77. let public_inputs =
  78. vec![coin, *value_coords.x(), *value_coords.y(), *token_coords.x(), *token_coords.y()];
  79. // Create the circuit
  80. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  81. let proving_key = ProvingKey::build(13, &circuit);
  82. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  83. // ========
  84. // Verifier
  85. // ========
  86. // Construct empty witnesses
  87. let verifier_witnesses = empty_witnesses(&zkbin);
  88. // Create the circuit
  89. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  90. let verifying_key = VerifyingKey::build(13, &circuit);
  91. proof.verify(&verifying_key, &public_inputs)?;
  92. /* ANCHOR_END: main */
  93. Ok(())
  94. }