Skip to content

Commit

Permalink
Nullifier map optimization (#349)
Browse files Browse the repository at this point in the history
* nullifier map optimization

* changelog
  • Loading branch information
SupremoUGH authored May 24, 2023
1 parent 82606a8 commit 7a0a4be
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- [\#349](https://github.com/Manta-Network/manta-rs/pull/349) Nullifier map optimization.
- [\#345](https://github.com/Manta-Network/manta-rs/pull/345) Precompute ledger and minor bug fix.

### Changed
Expand Down
3 changes: 2 additions & 1 deletion manta-accounting/src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ pub trait Configuration {
Secret = Self::SpendSecret,
Nullifier = Self::Nullifier,
Identifier = Self::Identifier,
> + utxo::UtxoReconstruct;
> + utxo::NullifierOpen
+ utxo::UtxoReconstruct;

/// Authorization Context Variable Type
type AuthorizationContextVar: Variable<
Expand Down
20 changes: 20 additions & 0 deletions manta-accounting/src/transfer/utxo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ impl IndependenceContext for NullifierIndependence {
const DEFAULT: bool = false;
}

/// Nullifier Open
pub trait NullifierOpen: AssetType + DeriveDecryptionKey + NullifierType {
/// Opens the outgoing note in `nullifier` with `decryption_key`.
fn open(
&self,
nullifier: &Self::Nullifier,
decryption_key: &Self::DecryptionKey,
) -> Option<Self::Asset>;

/// Returns `true` if `nullifier` can be opened with `decryption_key`.
#[inline]
fn can_be_opened(
&self,
nullifier: &Self::Nullifier,
decryption_key: &Self::DecryptionKey,
) -> bool {
self.open(nullifier, decryption_key).is_some()
}
}

/// Identifier
pub trait IdentifierType {
/// Identifier Type
Expand Down
25 changes: 25 additions & 0 deletions manta-accounting/src/transfer/utxo/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,31 @@ where
}
}

impl<C> utxo::NullifierOpen for Parameters<C>
where
C: Configuration<Bool = bool>,
C::OutgoingBaseEncryptionScheme:
Decrypt<DecryptionKey = C::Group, DecryptedPlaintext = Option<Asset<C>>>,
{
#[inline]
fn open(
&self,
nullifier: &Self::Nullifier,
decryption_key: &Self::DecryptionKey,
) -> Option<Self::Asset> {
Hybrid::new(
StandardDiffieHellman::new(self.base.group_generator.generator().clone()),
self.base.outgoing_base_encryption_scheme.clone(),
)
.decrypt(
decryption_key,
&C::OutgoingHeader::default(),
&nullifier.outgoing_note.ciphertext,
&mut (),
)
}
}

impl<C> utxo::NoteOpen for Parameters<C>
where
C: Configuration<Bool = bool>,
Expand Down
14 changes: 9 additions & 5 deletions manta-accounting/src/wallet/signer/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
receiver::ReceiverPost,
requires_authorization,
utxo::{
auth::DeriveContext, DeriveAddress as _, DeriveDecryptionKey, DeriveSpend, Spend,
UtxoReconstruct,
auth::DeriveContext, DeriveAddress as _, DeriveDecryptionKey, DeriveSpend,
NullifierOpen, Spend, UtxoReconstruct,
},
Address, Asset, AssociatedData, Authorization, AuthorizationContext, FullParametersRef,
IdentifiedAsset, Identifier, IdentityProof, Note, Nullifier, Parameters, PreSender,
Expand Down Expand Up @@ -162,7 +162,7 @@ fn insert_next_item<C>(
asset.clone(),
rng,
);
if nullifiers.contains_item(&nullifier) {
if nullifiers.remove(&nullifier) {
utxo_accumulator.insert_nonprovable(&item_hash::<C>(parameters, &utxo));
} else {
utxo_accumulator.insert(&item_hash::<C>(parameters, &utxo));
Expand Down Expand Up @@ -192,7 +192,7 @@ where
{
let (_, utxo, nullifier) =
parameters.derive_spend(authorization_context, identifier, asset.clone(), rng);
if nullifiers.contains_item(&nullifier) {
if nullifiers.remove(&nullifier) {
utxo_accumulator.remove_proof(&item_hash::<C>(parameters, &utxo));
if !asset.is_zero() {
withdraw.push(asset);
Expand Down Expand Up @@ -224,10 +224,14 @@ where
C::AssetValue: CheckedAdd<Output = C::AssetValue> + CheckedSub<Output = C::AssetValue>,
{
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);
nullifiers.extend(
nullifier_data
.into_iter()
.filter(|nullifier| parameters.can_be_opened(nullifier, &decryption_key)),
);
let mut nonprovable_inserts = Vec::new();
for (utxo, note) in inserts {
if let Some((identifier, asset)) = parameters.open_with_check(&decryption_key, &utxo, note)
Expand Down
23 changes: 23 additions & 0 deletions manta-accounting/src/wallet/signer/nullifier_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub trait NullifierMap<T>: Default {
where
I: IntoIterator<Item = T>;

/// Removes `item` from `self`.
fn remove(&mut self, item: &T) -> bool;

/// Checks if `self` contains `item`.
fn contains_item(&self, item: &T) -> bool;
}
Expand Down Expand Up @@ -75,6 +78,16 @@ where
Extend::extend(self, items)
}

#[inline]
fn remove(&mut self, item: &T) -> bool {
if let Some(index) = self.iter().position(|x| x == item) {
self.remove(index);
true
} else {
false
}
}

#[inline]
fn contains_item(&self, item: &T) -> bool {
self.contains(item)
Expand Down Expand Up @@ -108,6 +121,11 @@ where
Extend::extend(self, items)
}

#[inline]
fn remove(&mut self, item: &T) -> bool {
self.remove(item)
}

#[inline]
fn contains_item(&self, item: &T) -> bool {
self.contains(item)
Expand Down Expand Up @@ -142,6 +160,11 @@ where
Extend::extend(self, items)
}

#[inline]
fn remove(&mut self, item: &T) -> bool {
self.remove(item)
}

#[inline]
fn contains_item(&self, item: &T) -> bool {
self.contains(item)
Expand Down
4 changes: 2 additions & 2 deletions manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub mod config;
#[cfg_attr(doc_cfg, doc(cfg(feature = "key")))]
pub mod key;

#[cfg(all(feature = "parameters"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "parameters"))))]
#[cfg(feature = "parameters")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parameters")))]
pub mod parameters;

#[cfg(feature = "groth16")]
Expand Down

0 comments on commit 7a0a4be

Please sign in to comment.