commitment.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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. zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
  20. zkas::ZkBinary,
  21. Result,
  22. };
  23. use darkfi_sdk::crypto::Keypair;
  24. use rand::rngs::OsRng;
  25. use wasm_hello_world::HelloParams;
  26. pub struct ContractCallDebris {
  27. pub params: HelloParams,
  28. pub proofs: Vec<Proof>,
  29. }
  30. /// Struct holding necessary information to build a wasm-hello-world contract call.
  31. pub struct ContractCallBuilder {
  32. /// Member keypair this call is for
  33. pub member: Keypair,
  34. /// `SecretCommitment` zkas circuit ZkBinary
  35. pub commitment_zkbin: ZkBinary,
  36. /// Proving key for the `SecretCommitment` zk circuit,
  37. pub commitment_pk: ProvingKey,
  38. }
  39. impl ContractCallBuilder {
  40. pub fn build(&self) -> Result<ContractCallDebris> {
  41. // Build the commitment proof
  42. let prover_witnesses = vec![Witness::Base(Value::known(self.member.secret.inner()))];
  43. let (public_x, public_y) = self.member.public.xy();
  44. let public_inputs = vec![public_x, public_y];
  45. let circuit = ZkCircuit::new(prover_witnesses, &self.commitment_zkbin);
  46. let proof = Proof::create(&self.commitment_pk, &[circuit], &public_inputs, &mut OsRng)?;
  47. // Generate the params and call debris
  48. let params = HelloParams { x: public_x, y: public_y };
  49. let debris = ContractCallDebris { params, proofs: vec![proof] };
  50. Ok(debris)
  51. }
  52. }