set_v1.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  16. * License along with this program.
  17. * If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. use darkfi::{
  20. zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
  21. zkas::ZkBinary,
  22. Result,
  23. };
  24. use darkfi_sdk::{
  25. crypto::{poseidon_hash, SecretKey},
  26. pasta::pallas,
  27. };
  28. use log::debug;
  29. use rand::rngs::OsRng;
  30. use crate::model::SetParamsV1;
  31. pub struct SetCallBuilder {
  32. pub secret: SecretKey,
  33. pub lock: pallas::Base,
  34. pub car: pallas::Base,
  35. pub key: pallas::Base,
  36. pub value: pallas::Base,
  37. pub zkbin: ZkBinary,
  38. pub prove_key: ProvingKey,
  39. }
  40. pub struct SetCallDebris {
  41. pub params: SetParamsV1,
  42. pub proofs: Vec<Proof>,
  43. pub signature_secrets: Vec<SecretKey>,
  44. }
  45. impl SetCallBuilder {
  46. pub fn build(&self) -> Result<SetCallDebris> {
  47. debug!("Building Map::SetV1 contract call");
  48. let params = SetParamsV1 {
  49. // !!!!private computation done in rust!!!!
  50. account: poseidon_hash([self.secret.inner()]),
  51. lock: self.lock,
  52. car: self.car,
  53. key: self.key,
  54. value: self.value,
  55. };
  56. Ok(SetCallDebris {
  57. params: params.clone(),
  58. proofs: vec![self.create_set_proof(params.clone())?],
  59. signature_secrets: vec![self.secret],
  60. })
  61. }
  62. pub fn create_set_proof(&self, public_inputs: SetParamsV1) -> Result<Proof> {
  63. debug!("Creating map set proof");
  64. let witness = vec![
  65. Witness::Base(Value::known(self.secret.inner())),
  66. Witness::Base(Value::known(self.car)),
  67. Witness::Base(Value::known(self.lock)),
  68. Witness::Base(Value::known(self.key)),
  69. Witness::Base(Value::known(self.value)),
  70. ];
  71. let circuit = ZkCircuit::new(witness, self.zkbin.clone());
  72. let proof = Proof::create(
  73. &self.prove_key,
  74. &[circuit],
  75. &public_inputs.to_vec(),
  76. &mut OsRng,
  77. )?;
  78. Ok(proof)
  79. }
  80. }