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

Precompute Ledger and overflow bug fix #345

Merged
merged 22 commits into from
May 18, 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 @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

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

### Changed

Expand Down
102 changes: 101 additions & 1 deletion manta-accounting/src/transfer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ use crate::{
};
use alloc::vec::Vec;
use core::{fmt::Debug, hash::Hash};
use manta_crypto::rand::{CryptoRng, RngCore};
use manta_crypto::{
arkworks::serialize::{
CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write,
},
rand::{CryptoRng, RngCore},
};
use manta_util::{create_seal, seal};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -619,6 +624,101 @@ where
pub to_public: VerifyingContext<C>,
}

impl<C> CanonicalSerialize for MultiVerifyingContext<C>
where
C: Configuration + ?Sized,
VerifyingContext<C>: CanonicalSerialize,
{
#[inline]
fn serialize<W>(&self, mut writer: W) -> Result<(), SerializationError>
where
W: Write,
{
self.to_private.serialize(&mut writer)?;
self.private_transfer.serialize(&mut writer)?;
self.to_public.serialize(&mut writer)?;
Ok(())
}

#[inline]
fn serialized_size(&self) -> usize {
self.to_private.serialized_size()
+ self.private_transfer.serialized_size()
+ self.to_public.serialized_size()
}

#[inline]
fn serialize_uncompressed<W>(&self, mut writer: W) -> Result<(), SerializationError>
where
W: Write,
{
self.to_private.serialize_uncompressed(&mut writer)?;
self.private_transfer.serialize_uncompressed(&mut writer)?;
self.to_public.serialize_uncompressed(&mut writer)?;
Ok(())
}

#[inline]
fn serialize_unchecked<W>(&self, mut writer: W) -> Result<(), SerializationError>
where
W: Write,
{
self.to_private.serialize_unchecked(&mut writer)?;
self.private_transfer.serialize_unchecked(&mut writer)?;
self.to_public.serialize_unchecked(&mut writer)?;
Ok(())
}

#[inline]
fn uncompressed_size(&self) -> usize {
self.to_private.uncompressed_size()
+ self.private_transfer.uncompressed_size()
+ self.to_public.uncompressed_size()
}
}

impl<C> CanonicalDeserialize for MultiVerifyingContext<C>
where
C: Configuration + ?Sized,
VerifyingContext<C>: CanonicalDeserialize,
{
#[inline]
fn deserialize<R>(mut reader: R) -> Result<Self, SerializationError>
where
R: Read,
{
Ok(Self {
to_private: CanonicalDeserialize::deserialize(&mut reader)?,
private_transfer: CanonicalDeserialize::deserialize(&mut reader)?,
to_public: CanonicalDeserialize::deserialize(&mut reader)?,
})
}

#[inline]
fn deserialize_uncompressed<R>(mut reader: R) -> Result<Self, SerializationError>
where
R: Read,
{
Ok(Self {
to_private: CanonicalDeserialize::deserialize_uncompressed(&mut reader)?,
private_transfer: CanonicalDeserialize::deserialize_uncompressed(&mut reader)?,
to_public: CanonicalDeserialize::deserialize_uncompressed(&mut reader)?,
})
}

#[inline]
fn deserialize_unchecked<R>(mut reader: R) -> Result<Self, SerializationError>
where
R: Read,
{
Ok(Self {
to_private: CanonicalDeserialize::deserialize_unchecked(&mut reader)?,
private_transfer: CanonicalDeserialize::deserialize_unchecked(&mut reader)?,
to_public: CanonicalDeserialize::deserialize_unchecked(&mut reader)?,
})
}
}

impl<C> MultiVerifyingContext<C>
where
C: Configuration + ?Sized,
Expand Down
38 changes: 38 additions & 0 deletions manta-accounting/src/transfer/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,41 @@ where
P::extend(input, &self.note);
}
}

/// Unsafe Receiver Ledger
///
/// # Safety
///
/// This unsafe version of the receiver ledger does not perform the
/// [`is_not_registered`](ReceiverLedger::is_not_registered) check before registering a Utxo.
/// Therefore, it must only be used for testing purposes and with trusted inputs.
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
pub mod unsafe_receiver_ledger {
use crate::transfer::{
receiver::{ReceiverLedger, ReceiverPost, ReceiverPostingKey},
utxo::Mint,
};

/// Unsafe Receiver Ledger
pub trait UnsafeReceiverLedger<M>: ReceiverLedger<M>
where
M: Mint,
{
/// Converts `utxo` into a [`ValidUtxo`](ReceiverLedger::ValidUtxo) without checking
/// it isn't already registered in `self`.
fn dont_check_registration(&self, utxo: M::Utxo) -> Self::ValidUtxo;

/// Converts `receiver_post` into a [`ReceiverPostingKey`] without performing
/// the [`is_not_registered`](ReceiverLedger::is_not_registered) check.
fn dont_validate_receiver_post(
&self,
receiver_post: ReceiverPost<M>,
) -> ReceiverPostingKey<M, Self> {
ReceiverPostingKey {
utxo: self.dont_check_registration(receiver_post.utxo),
note: receiver_post.note,
}
}
}
}
54 changes: 54 additions & 0 deletions manta-accounting/src/transfer/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,57 @@ where
P::extend(input, self.nullifier.as_ref());
}
}

/// Unsafe Sender Ledger
///
/// # Safety
///
/// This unsafe version of the sender ledger does not perform the
/// [`has_matching_utxo_accumulator_output`] nor the [`is_unspent`] checks before spending an asset.
/// Therefore, it must only be used for testing purposes and with trusted inputs.
///
/// [`has_matching_utxo_accumulator_output`]: SenderLedger::has_matching_utxo_accumulator_output
/// [`is_unspent`]: SenderLedger::is_unspent
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
pub mod unsafe_sender_ledger {
use crate::transfer::{
sender::{SenderLedger, SenderPost, SenderPostingKey},
utxo::{Spend, UtxoAccumulatorOutput},
};

/// Unsafe Sender Ledger
pub trait UnsafeSenderLedger<S>: SenderLedger<S>
where
S: Spend,
{
/// Transforms `output` into a [`ValidUtxoAccumulatorOutput`] without checking
/// it coincides with one of the [`UtxoAccumulatorOutput`]s in `self`.
///
/// [`ValidUtxoAccumulatorOutput`]: SenderLedger::ValidUtxoAccumulatorOutput
fn dont_check_utxo_accumulator_output(
&self,
output: UtxoAccumulatorOutput<S>,
) -> Self::ValidUtxoAccumulatorOutput;

/// Transforms `nullifier` into a [`ValidNullifier`](SenderLedger::ValidNullifier)
/// without checking that `nullifier` isn't registered in `self` already.
fn dont_check_nullifier(&self, nullifier: S::Nullifier) -> Self::ValidNullifier;

/// Transforms `sender_post` into a [`SenderPostingKey`] without running the
/// [`has_matching_utxo_accumulator_output`] nor the [`is_unspent`] checks before.
///
/// [`has_matching_utxo_accumulator_output`]: SenderLedger::has_matching_utxo_accumulator_output
/// [`is_unspent`]: SenderLedger::is_unspent
fn dont_validate_sender_post(
&self,
sender_post: SenderPost<S>,
) -> SenderPostingKey<S, Self> {
SenderPostingKey::<S, Self> {
utxo_accumulator_output: self
.dont_check_utxo_accumulator_output(sender_post.utxo_accumulator_output),
nullifier: self.dont_check_nullifier(sender_post.nullifier),
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use manta_crypto::{
};
use manta_util::into_array_unchecked;

pub mod unverified_transfers;

/// Samples a distribution over `count`-many values summing to `total`.
///
/// # Warning
Expand Down
Loading