stx.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_sdk::{
  19. crypto::MerkleNode,
  20. pasta::{arithmetic::CurveAffine, group::Curve, pallas},
  21. };
  22. use crate::{
  23. zk::{proof::VerifyingKey, Proof},
  24. Error, Result,
  25. };
  26. use darkfi_serial::{SerialDecodable, SerialEncodable};
  27. #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
  28. pub struct TransferStx {
  29. /// sender's coin, or coin1_commitment in zk
  30. pub coin_commitment: pallas::Point,
  31. /// sender's coin pk
  32. pub coin_pk: pallas::Base,
  33. /// sender's coin sk's root
  34. pub coin_root_sk: MerkleNode,
  35. /// coin3_commitment in zk
  36. pub change_coin_commitment: pallas::Point,
  37. /// coin4_commitment in zk
  38. pub transfered_coin_commitment: pallas::Point,
  39. /// nullifiers coin1_nullifier
  40. pub nullifier: pallas::Base,
  41. /// sk coin creation slot
  42. pub slot: pallas::Base,
  43. /// root to coin's commitments
  44. pub root: MerkleNode,
  45. /// transfer proof
  46. pub proof: Proof,
  47. }
  48. impl TransferStx {
  49. /// verify the transfer proof.
  50. pub fn verify(&self, vk: VerifyingKey) -> Result<()> {
  51. if self.proof.verify(&vk, &self.public_inputs()).is_err() {
  52. return Err(Error::TransferTxVerification)
  53. }
  54. Ok(())
  55. }
  56. /// arrange public inputs from Stxfer
  57. pub fn public_inputs(&self) -> Vec<pallas::Base> {
  58. let cm1 = self.coin_commitment.to_affine().coordinates().unwrap();
  59. let cm3 = self.change_coin_commitment.to_affine().coordinates().unwrap();
  60. let cm4 = self.transfered_coin_commitment.to_affine().coordinates().unwrap();
  61. vec![
  62. self.coin_pk,
  63. *cm1.x(),
  64. *cm1.y(),
  65. *cm3.x(),
  66. *cm3.y(),
  67. *cm4.x(),
  68. *cm4.y(),
  69. self.root.inner(),
  70. self.coin_root_sk.inner(),
  71. self.nullifier,
  72. ]
  73. }
  74. }