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

Remove Trapdoor From Circuit #50

Merged
merged 19 commits into from
Apr 13, 2022
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ jobs:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }} && rustup component add clippy
- run: cargo install cargo-hack
- run: cargo hack clippy --feature-powerset --bins --examples --tests --workspace
- run: cargo hack clippy --feature-powerset --workspace
- run: cargo hack clippy --feature-powerset --workspace --bins
- run: cargo hack clippy --feature-powerset --workspace --examples
- run: cargo hack clippy --feature-powerset --workspace --tests
format:
name: Format
runs-on: ubuntu-latest
Expand Down
51 changes: 48 additions & 3 deletions manta-accounting/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ use core::{
use derive_more::{Add, AddAssign, Display, From, Sub, SubAssign, Sum};
use manta_crypto::{
constraint::{Allocator, Secret, ValueSource, Variable},
rand::{CryptoRng, Rand, RngCore, Sample, Standard},
rand::{CryptoRng, Rand, RngCore, Sample},
};
use manta_util::{into_array_unchecked, Array};
use manta_util::{into_array_unchecked, Array, SizeLimit};

#[cfg(feature = "serde")]
use manta_util::serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -127,6 +127,10 @@ where
}
}

impl SizeLimit for AssetId {
const SIZE: usize = Self::SIZE;
}

/// [`AssetValue`] Base Type
pub type AssetValueType = u128;

Expand Down Expand Up @@ -254,6 +258,10 @@ where
}
}

impl SizeLimit for AssetValue {
const SIZE: usize = Self::SIZE;
}

impl Sub<AssetValueType> for AssetValue {
type Output = Self;

Expand Down Expand Up @@ -452,7 +460,7 @@ impl<I, V> From<Asset<I, V>> for (I, V) {

impl Sample for Asset {
#[inline]
fn sample<R>(distribution: Standard, rng: &mut R) -> Self
fn sample<R>(distribution: (), rng: &mut R) -> Self
where
R: CryptoRng + RngCore + ?Sized,
{
Expand All @@ -461,6 +469,10 @@ impl Sample for Asset {
}
}

impl SizeLimit for Asset {
const SIZE: usize = Self::SIZE;
}

impl<I, V> Sub<V> for Asset<I, V>
where
V: SubAssign,
Expand Down Expand Up @@ -682,6 +694,13 @@ impl From<AssetList> for Vec<Asset> {
}
}

impl From<Vec<Asset>> for AssetList {
#[inline]
fn from(vector: Vec<Asset>) -> Self {
Self::from_iter(iter::once(vector))
}
}

impl FromIterator<Asset> for AssetList {
#[inline]
fn from_iter<I>(iter: I) -> Self
Expand All @@ -694,6 +713,32 @@ impl FromIterator<Asset> for AssetList {
}
}

impl FromIterator<AssetList> for AssetList {
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = AssetList>,
{
iter.into_iter().map::<Vec<_>, _>(Into::into).collect()
}
}

impl FromIterator<Vec<Asset>> for AssetList {
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Vec<Asset>>,
{
let mut list = Self::new();
for item in iter {
for asset in item {
list.deposit(asset);
}
}
list
}
}

impl IntoIterator for AssetList {
type Item = Asset;
type IntoIter = vec::IntoIter<Asset>;
Expand Down
54 changes: 32 additions & 22 deletions manta-accounting/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,21 @@ pub trait HierarchicalKeyDerivationScheme {
)
}

/// Borrows `self` rather than consuming it, returning an implementation of
/// [`HierarchicalKeyDerivationScheme`].
#[inline]
fn by_ref(&self) -> &Self {
self
}

/// Maps `self` along a key derivation function.
#[inline]
fn map<K>(self) -> Map<Self, K>
fn map<F>(self, key_derivation_function: F) -> Map<Self, F>
where
Self: Sized,
K: KeyDerivationFunction<Key = Self::SecretKey>,
F: KeyDerivationFunction<Key = Self::SecretKey>,
{
Map::new(self)
Map::new(self, key_derivation_function)
}
}

Expand Down Expand Up @@ -200,69 +207,72 @@ where
)]
#[derive(derivative::Derivative)]
#[derivative(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Map<H, K>
pub struct Map<H, F>
where
H: HierarchicalKeyDerivationScheme,
K: KeyDerivationFunction<Key = H::SecretKey>,
F: KeyDerivationFunction<Key = H::SecretKey>,
{
/// Base Derivation Scheme
base: H,

/// Type Parameter Marker
__: PhantomData<K>,
/// Key Derivation Function
key_derivation_function: F,
}

impl<H, K> Map<H, K>
impl<H, F> Map<H, F>
where
H: HierarchicalKeyDerivationScheme,
K: KeyDerivationFunction<Key = H::SecretKey>,
F: KeyDerivationFunction<Key = H::SecretKey>,
{
/// Builds a new [`Map`] from `base`.
/// Builds a new [`Map`] from `base` and `key_derivation_function`.
#[inline]
pub fn new(base: H) -> Self {
pub fn new(base: H, key_derivation_function: F) -> Self {
Self {
base,
__: PhantomData,
key_derivation_function,
}
}
}

impl<H, K> HierarchicalKeyDerivationScheme for Map<H, K>
impl<H, F> HierarchicalKeyDerivationScheme for Map<H, F>
where
H: HierarchicalKeyDerivationScheme,
K: KeyDerivationFunction<Key = H::SecretKey>,
F: KeyDerivationFunction<Key = H::SecretKey>,
{
const GAP_LIMIT: IndexType = H::GAP_LIMIT;

type SecretKey = K::Output;
type SecretKey = F::Output;

#[inline]
fn derive(&self, account: AccountIndex, kind: Kind, index: KeyIndex) -> Self::SecretKey {
K::derive(&self.base.derive(account, kind, index))
self.key_derivation_function
.derive(&self.base.derive(account, kind, index))
}

#[inline]
fn derive_spend(&self, account: AccountIndex, index: KeyIndex) -> Self::SecretKey {
K::derive(&self.base.derive_spend(account, index))
self.key_derivation_function
.derive(&self.base.derive_spend(account, index))
}

#[inline]
fn derive_view(&self, account: AccountIndex, index: KeyIndex) -> Self::SecretKey {
K::derive(&self.base.derive_view(account, index))
self.key_derivation_function
.derive(&self.base.derive_view(account, index))
}
}

impl<H, K, D> Sample<D> for Map<H, K>
impl<H, F, D> Sample<(D, F)> for Map<H, F>
where
H: HierarchicalKeyDerivationScheme + Sample<D>,
K: KeyDerivationFunction<Key = H::SecretKey>,
F: KeyDerivationFunction<Key = H::SecretKey>,
{
#[inline]
fn sample<R>(distribution: D, rng: &mut R) -> Self
fn sample<R>(distribution: (D, F), rng: &mut R) -> Self
where
R: CryptoRng + RngCore + ?Sized,
{
Self::new(H::sample(distribution, rng))
Self::new(H::sample(distribution.0, rng), distribution.1)
}
}

Expand Down
Loading