Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signer bug fix #347

Merged
merged 28 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed

### Fixed
-[\#347](https://github.com/Manta-Network/manta-rs/pull/347) Signer bug fix

### Security

Expand Down
17 changes: 12 additions & 5 deletions manta-accounting/src/transfer/utxo/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,9 +2405,7 @@ where
Eq(bound = "UtxoCommitmentRandomness<C>: Eq"),
Hash(bound = "UtxoCommitmentRandomness<C>: Hash"),
Ord(bound = "UtxoCommitmentRandomness<C>: Ord"),
PartialEq(
bound = "UtxoCommitmentRandomness<C>: core::cmp::PartialEq<UtxoCommitmentRandomness<C>>"
),
PartialEq(bound = "UtxoCommitmentRandomness<C>: cmp::PartialEq<UtxoCommitmentRandomness<C>>"),
PartialOrd(bound = "UtxoCommitmentRandomness<C>: PartialOrd")
)]
pub struct Identifier<C>
Expand Down Expand Up @@ -2734,8 +2732,7 @@ where
Debug(bound = "Nullifier<C>: Debug, OutgoingNote<C>: Debug"),
Default(bound = "Nullifier<C>: Default, OutgoingNote<C>: Default"),
Eq(bound = "Nullifier<C>: cmp::Eq, OutgoingNote<C>: cmp::Eq"),
Hash(bound = "Nullifier<C>: Hash, OutgoingNote<C>: Hash"),
PartialEq(bound = "Nullifier<C>: cmp::PartialEq, OutgoingNote<C>: cmp::PartialEq")
Hash(bound = "Nullifier<C>: Hash, OutgoingNote<C>: Hash")
)]
pub struct FullNullifier<C>
where
Expand Down Expand Up @@ -2782,6 +2779,16 @@ where
}
}

impl<C> cmp::PartialEq for FullNullifier<C>
where
C: Configuration<Bool = bool>,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
!self.is_independent(other)
}
}

impl<C> PartialEq<Self> for FullNullifier<C>
where
C: Configuration<Bool = bool>,
Expand Down
43 changes: 19 additions & 24 deletions manta-accounting/src/wallet/signer/functions.rs
SupremoUGH marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ use crate::{
UtxoAccumulatorItem, UtxoAccumulatorModel, UtxoAccumulatorWitness,
},
wallet::signer::{
AccountTable, BalanceUpdate, Checkpoint, Configuration, InitialSyncRequest, SignError,
SignResponse, SignWithTransactionDataResponse, SignWithTransactionDataResult,
SignerParameters, SyncData, SyncError, SyncRequest, SyncResponse,
nullifier_map::NullifierMap, AccountTable, BalanceUpdate, Checkpoint, Configuration,
InitialSyncRequest, SignError, SignResponse, SignWithTransactionDataResponse,
SignWithTransactionDataResult, SignerParameters, SyncData, SyncError, SyncRequest,
SyncResponse,
},
};
use alloc::{vec, vec::Vec};
Expand All @@ -51,9 +52,7 @@ use manta_crypto::{
rand::Rand,
};
use manta_util::{
array_map,
cmp::Independence,
into_array_unchecked,
array_map, into_array_unchecked,
iter::IteratorExt,
num::{CheckedAdd, CheckedSub},
persistence::Rollback,
Expand Down Expand Up @@ -150,7 +149,7 @@ fn insert_next_item<C>(
assets: &mut C::AssetMap,
parameters: &Parameters<C>,
identified_asset: IdentifiedAsset<C>,
nullifiers: &mut Vec<Nullifier<C>>,
nullifiers: &mut C::NullifierMap,
SupremoUGH marked this conversation as resolved.
Show resolved Hide resolved
deposit: &mut Vec<Asset<C>>,
rng: &mut C::Rng,
) where
Expand All @@ -163,20 +162,15 @@ fn insert_next_item<C>(
asset.clone(),
rng,
);
if let Some(index) = nullifiers
.iter()
.position(move |n| n.is_related(&nullifier))
{
nullifiers.remove(index);
if nullifiers.contains_item(&nullifier) {
utxo_accumulator.insert_nonprovable(&item_hash::<C>(parameters, &utxo));
} else {
utxo_accumulator.insert(&item_hash::<C>(parameters, &utxo));
if !asset.is_zero() {
deposit.push(asset.clone());
}
assets.insert(identifier, asset);
return;
}
utxo_accumulator.insert_nonprovable(&item_hash::<C>(parameters, &utxo));
}

/// Checks if `asset` matches with `nullifier`, removing it from the `utxo_accumulator` and
Expand All @@ -189,7 +183,7 @@ fn is_asset_unspent<C>(
parameters: &Parameters<C>,
identifier: Identifier<C>,
asset: Asset<C>,
nullifiers: &mut Vec<Nullifier<C>>,
nullifiers: &mut C::NullifierMap,
withdraw: &mut Vec<Asset<C>>,
rng: &mut C::Rng,
) -> bool
Expand All @@ -198,11 +192,7 @@ where
{
let (_, utxo, nullifier) =
parameters.derive_spend(authorization_context, identifier, asset.clone(), rng);
if let Some(index) = nullifiers
.iter()
.position(move |n| n.is_related(&nullifier))
{
nullifiers.remove(index);
if nullifiers.contains_item(&nullifier) {
SupremoUGH marked this conversation as resolved.
Show resolved Hide resolved
utxo_accumulator.remove_proof(&item_hash::<C>(parameters, &utxo));
if !asset.is_zero() {
withdraw.push(asset);
Expand All @@ -219,11 +209,12 @@ where
fn sync_with<C, I>(
authorization_context: &mut AuthorizationContext<C>,
assets: &mut C::AssetMap,
nullifiers: &mut C::NullifierMap,
checkpoint: &mut C::Checkpoint,
utxo_accumulator: &mut C::UtxoAccumulator,
parameters: &Parameters<C>,
inserts: I,
mut nullifiers: Vec<Nullifier<C>>,
nullifier_data: Vec<Nullifier<C>>,
is_partial: bool,
rng: &mut C::Rng,
) -> Result<SyncResponse<C, C::Checkpoint>, SyncError<C::Checkpoint>>
Expand All @@ -232,7 +223,8 @@ where
I: Iterator<Item = (Utxo<C>, Note<C>)>,
C::AssetValue: CheckedAdd<Output = C::AssetValue> + CheckedSub<Output = C::AssetValue>,
{
let nullifier_count = nullifiers.len();
let nullifier_count = nullifier_data.len();
nullifiers.extend(nullifier_data);
let mut deposit = Vec::new();
let mut withdraw = Vec::new();
let decryption_key = parameters.derive_decryption_key(authorization_context);
Expand All @@ -250,7 +242,7 @@ where
assets,
parameters,
transfer::utxo::IdentifiedAsset::new(identifier, asset),
&mut nullifiers,
nullifiers,
&mut deposit,
rng,
);
Expand All @@ -269,7 +261,7 @@ where
parameters,
identifier.clone(),
asset.clone(),
&mut nullifiers,
nullifiers,
&mut withdraw,
rng,
)
Expand Down Expand Up @@ -852,11 +844,13 @@ where
}

/// Updates `assets`, `checkpoint` and `utxo_accumulator`, returning the new asset distribution.
#[allow(clippy::too_many_arguments)] // This function must take 8 arguments
#[inline]
pub fn sync<C>(
parameters: &SignerParameters<C>,
authorization_context: &mut AuthorizationContext<C>,
assets: &mut C::AssetMap,
nullifiers: &mut C::NullifierMap,
checkpoint: &mut C::Checkpoint,
utxo_accumulator: &mut C::UtxoAccumulator,
request: SyncRequest<C, C::Checkpoint>,
Expand All @@ -876,6 +870,7 @@ where
let response = sync_with::<C, _>(
authorization_context,
assets,
nullifiers,
checkpoint,
utxo_accumulator,
&parameters.parameters,
Expand Down
Loading