mint.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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::{
  19. zk::{halo2, Proof, ProvingKey, Witness, ZkCircuit},
  20. zkas::ZkBinary,
  21. Result,
  22. };
  23. use darkfi_sdk::{
  24. crypto::{poseidon_hash, PublicKey, SecretKey, TokenId},
  25. pasta::pallas,
  26. };
  27. use log::debug;
  28. use rand::rngs::OsRng;
  29. use crate::model::DaoMintParams;
  30. #[derive(Clone)]
  31. pub struct DaoInfo {
  32. pub proposer_limit: u64,
  33. pub quorum: u64,
  34. pub approval_ratio_quot: u64,
  35. pub approval_ratio_base: u64,
  36. pub gov_token_id: TokenId,
  37. pub public_key: PublicKey,
  38. pub bulla_blind: pallas::Base,
  39. }
  40. pub fn make_mint_call(
  41. dao: &DaoInfo,
  42. dao_secret_key: &SecretKey,
  43. dao_mint_zkbin: &ZkBinary,
  44. dao_mint_pk: &ProvingKey,
  45. ) -> Result<(DaoMintParams, Vec<Proof>)> {
  46. debug!(target: "dao", "Building DAO contract mint transaction");
  47. let dao_proposer_limit = pallas::Base::from(dao.proposer_limit);
  48. let dao_quorum = pallas::Base::from(dao.quorum);
  49. let dao_approval_ratio_quot = pallas::Base::from(dao.approval_ratio_quot);
  50. let dao_approval_ratio_base = pallas::Base::from(dao.approval_ratio_base);
  51. let (pub_x, pub_y) = dao.public_key.xy();
  52. let dao_bulla = poseidon_hash([
  53. dao_proposer_limit,
  54. dao_quorum,
  55. dao_approval_ratio_quot,
  56. dao_approval_ratio_base,
  57. dao.gov_token_id.inner(),
  58. pub_x,
  59. pub_y,
  60. dao.bulla_blind,
  61. ]);
  62. // NOTE: It's important to keep these in the same order as the zkas code.
  63. let prover_witnesses = vec![
  64. Witness::Base(halo2::Value::known(dao_proposer_limit)),
  65. Witness::Base(halo2::Value::known(dao_quorum)),
  66. Witness::Base(halo2::Value::known(dao_approval_ratio_quot)),
  67. Witness::Base(halo2::Value::known(dao_approval_ratio_base)),
  68. Witness::Base(halo2::Value::known(dao.gov_token_id.inner())),
  69. Witness::Base(halo2::Value::known(dao_secret_key.inner())),
  70. Witness::Base(halo2::Value::known(dao.bulla_blind)),
  71. ];
  72. let public = vec![pub_x, pub_y, dao_bulla];
  73. let circuit = ZkCircuit::new(prover_witnesses, &dao_mint_zkbin);
  74. let proof = Proof::create(dao_mint_pk, &[circuit], &public, &mut OsRng)?;
  75. let dao_mint_params = DaoMintParams { dao_bulla: dao_bulla.into(), dao_pubkey: dao.public_key };
  76. Ok((dao_mint_params, vec![proof]))
  77. }