tx.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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::{crypto::Proof, Result, VerifyFailed::ProofVerifyFailed};
  19. use darkfi_sdk::{
  20. crypto::{
  21. schnorr::{SchnorrPublic, Signature},
  22. PublicKey,
  23. },
  24. pasta::pallas,
  25. tx::ContractCall,
  26. };
  27. use darkfi_serial::Encodable;
  28. use log::debug;
  29. use crate::{
  30. contract::{dao, example, money},
  31. note::EncryptedNote2,
  32. schema::WalletCache,
  33. util::{sign, StateRegistry, ZkContractInfo, ZkContractTable},
  34. };
  35. macro_rules! zip {
  36. ($x: expr) => ($x);
  37. ($x: expr, $($y: expr), +) => (
  38. $x.iter().zip(
  39. zip!($($y), +))
  40. )
  41. }
  42. pub struct Transaction {
  43. pub calls: Vec<ContractCall>,
  44. pub proofs: Vec<Vec<Proof>>,
  45. pub signatures: Vec<Vec<Signature>>,
  46. }
  47. impl Transaction {
  48. /// Verify ZK contracts for the entire tx
  49. /// In real code, we could parallelize this for loop
  50. /// TODO: fix use of unwrap with Result type stuff
  51. pub fn zk_verify(
  52. &self,
  53. zk_bins: &ZkContractTable,
  54. zkpub_table: &Vec<Vec<(String, Vec<pallas::Base>)>>,
  55. ) -> Result<()> {
  56. assert_eq!(
  57. self.calls.len(),
  58. self.proofs.len(),
  59. "calls.len()={} and proofs.len()={} do not match",
  60. self.calls.len(),
  61. self.proofs.len()
  62. );
  63. assert_eq!(
  64. self.calls.len(),
  65. zkpub_table.len(),
  66. "calls.len()={} and zkpub_table.len()={} do not match",
  67. self.calls.len(),
  68. zkpub_table.len()
  69. );
  70. for (call, (proofs, pubvals)) in zip!(self.calls, self.proofs, zkpub_table) {
  71. assert_eq!(
  72. proofs.len(),
  73. pubvals.len(),
  74. "proofs.len()={} and pubvals.len()={} do not match",
  75. proofs.len(),
  76. pubvals.len()
  77. );
  78. for (i, (proof, (key, public_vals))) in proofs.iter().zip(pubvals.iter()).enumerate() {
  79. match zk_bins.lookup(key).unwrap() {
  80. ZkContractInfo::Binary(info) => {
  81. let verifying_key = &info.verifying_key;
  82. let verify_result = proof.verify(verifying_key, public_vals);
  83. if verify_result.is_err() {
  84. return Err(ProofVerifyFailed(key.to_string()).into())
  85. }
  86. //assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
  87. }
  88. ZkContractInfo::Native(info) => {
  89. let verifying_key = &info.verifying_key;
  90. let verify_result = proof.verify(verifying_key, public_vals);
  91. if verify_result.is_err() {
  92. return Err(ProofVerifyFailed(key.to_string()).into())
  93. }
  94. //assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
  95. }
  96. };
  97. debug!(target: "demo", "zk_verify({}) passed [i={}]", key, i);
  98. }
  99. }
  100. Ok(())
  101. }
  102. pub fn verify_sigs(&self, sigpub_table: &Vec<Vec<pallas::Point>>) -> Result<()> {
  103. let mut tx_data = Vec::new();
  104. self.calls.encode(&mut tx_data)?;
  105. self.proofs.encode(&mut tx_data)?;
  106. // TODO: Hash it and use the hash as the signing data
  107. // let sighash = ...
  108. for (i, (signatures, signature_public_keys)) in
  109. self.signatures.iter().zip(sigpub_table.iter()).enumerate()
  110. {
  111. for (signature_pub_key, signature) in signature_public_keys.iter().zip(signatures) {
  112. let signature_pub_key = PublicKey::from(*signature_pub_key);
  113. let verify_result = signature_pub_key.verify(&tx_data[..], &signature);
  114. assert!(verify_result, "verify sigs[{}] failed", i);
  115. }
  116. debug!(target: "demo", "verify_sigs({}) passed", i);
  117. }
  118. Ok(())
  119. }
  120. }