From 6bddb66e0c44d33a57b22922b9f8b4c9651889bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Tue, 10 Sep 2024 00:16:43 +0200 Subject: [PATCH] transfer-contract: check for repeated nullifiers in the same TX --- contracts/transfer/src/state.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/contracts/transfer/src/state.rs b/contracts/transfer/src/state.rs index a680aa8ffd..461b69bd77 100644 --- a/contracts/transfer/src/state.rs +++ b/contracts/transfer/src/state.rs @@ -498,14 +498,14 @@ impl TransferState { panic!("Root not found in the state!"); } - // panic if any of the given nullifiers already exist - if self.any_nullifier_exists(phoenix_tx.nullifiers()) { - panic!("A provided nullifier already exists!"); + // append the nullifiers to the set, and panic if an equal one has + // already been inserted + for nullifier in phoenix_tx.nullifiers() { + if !self.nullifiers.insert(*nullifier) { + panic!("A provided nullifier has already been spent"); + } } - // append the nullifiers to the nullifiers set - self.nullifiers.extend(phoenix_tx.nullifiers()); - // verify the phoenix-circuit if !verify_tx_proof(phoenix_tx) { panic!("Invalid transaction proof!"); @@ -816,16 +816,6 @@ impl TransferState { } } - fn any_nullifier_exists(&self, nullifiers: &[BlsScalar]) -> bool { - for nullifier in nullifiers { - if self.nullifiers.contains(nullifier) { - return true; - } - } - - false - } - fn root_exists(&self, root: &BlsScalar) -> bool { self.roots.contains(root) }