mint_proof.rs 3.6 KB

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