Skip to content

Commit 788beb6

Browse files
authored
Deprecate pruning feature (#344)
* trivial leaf vec * changelog
1 parent d5fb350 commit 788beb6

File tree

5 files changed

+224
-16
lines changed

5 files changed

+224
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
### Changed
1010

1111
### Deprecated
12+
- [\#344](https://github.com/Manta-Network/manta-rs/pull/344) Disable pruning feature.
1213

1314
### Removed
1415

manta-crypto/src/merkle_tree/fork.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use crate::merkle_tree::{
2020
capacity,
2121
inner_tree::{BTreeMap, InnerMap, PartialInnerTree},
22-
leaf_map::{LeafMap, LeafVec},
22+
leaf_map::{LeafMap, TrivialLeafVec},
2323
partial::Partial,
2424
path::{CurrentInnerPath, InnerPath},
2525
Configuration, CurrentPath, InnerDigest, Leaf, LeafDigest, Node, Parameters, Parity, Path,
@@ -274,7 +274,7 @@ impl Default for BaseContribution {
274274
Hash(bound = "InnerDigest<C>: Hash, M: Hash, L: Hash"),
275275
PartialEq(bound = "InnerDigest<C>: PartialEq, M: PartialEq, L: PartialEq")
276276
)]
277-
struct Branch<C, M = BTreeMap<C>, L = LeafVec<C>>
277+
struct Branch<C, M = BTreeMap<C>, L = TrivialLeafVec<C>>
278278
where
279279
C: Configuration + ?Sized,
280280
M: Default + InnerMap<C>,
@@ -587,7 +587,7 @@ where
587587
Debug(bound = "P::Weak: Debug, L: Debug, InnerDigest<C>: Debug, M: Debug"),
588588
Default(bound = "L: Default, InnerDigest<C>: Default")
589589
)]
590-
pub struct Fork<C, T, P = pointer::SingleThreaded, M = BTreeMap<C>, L = LeafVec<C>>
590+
pub struct Fork<C, T, P = pointer::SingleThreaded, M = BTreeMap<C>, L = TrivialLeafVec<C>>
591591
where
592592
C: Configuration + ?Sized,
593593
T: Tree<C>,
@@ -839,7 +839,7 @@ where
839839
Hash(bound = "T: Hash, L: Hash, InnerDigest<C>: Hash, M: Hash"),
840840
PartialEq(bound = "T: PartialEq, L: PartialEq, InnerDigest<C>: PartialEq, M: PartialEq")
841841
)]
842-
pub struct ForkedTree<C, T, M = BTreeMap<C>, L = LeafVec<C>>
842+
pub struct ForkedTree<C, T, M = BTreeMap<C>, L = TrivialLeafVec<C>>
843843
where
844844
C: Configuration + ?Sized,
845845
T: Tree<C>,

manta-crypto/src/merkle_tree/leaf_map.rs

Lines changed: 212 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Leaf Map
1818
1919
use crate::merkle_tree::{Configuration, LeafDigest};
20-
use alloc::vec::Vec;
20+
use alloc::{collections::BTreeMap, vec::Vec};
2121
use core::{fmt::Debug, hash::Hash};
2222

2323
#[cfg(feature = "serde")]
@@ -118,6 +118,101 @@ where
118118
}
119119
}
120120

121+
/// Trivial Leaf Vector
122+
///
123+
/// This struct implements [`LeafMap`] in the most trivial way possible, i.e.,
124+
/// it does not mark nor remove any leaves.
125+
#[cfg_attr(
126+
feature = "serde",
127+
derive(Deserialize, Serialize),
128+
serde(
129+
bound(
130+
deserialize = "LeafDigest<C>: Deserialize<'de>",
131+
serialize = "LeafDigest<C>: Serialize"
132+
),
133+
crate = "manta_util::serde",
134+
deny_unknown_fields
135+
)
136+
)]
137+
#[derive(derivative::Derivative)]
138+
#[derivative(
139+
Clone(bound = "LeafDigest<C>: Clone"),
140+
Debug(bound = "LeafDigest<C>: Debug"),
141+
Default(bound = "LeafDigest<C>: Default"),
142+
Eq(bound = "LeafDigest<C>: Eq"),
143+
Hash(bound = "LeafDigest<C>: Hash"),
144+
PartialEq(bound = "LeafDigest<C>: PartialEq")
145+
)]
146+
pub struct TrivialLeafVec<C>(Vec<LeafDigest<C>>)
147+
where
148+
C: Configuration + ?Sized;
149+
150+
impl<C> LeafMap<C> for TrivialLeafVec<C>
151+
where
152+
C: Configuration + ?Sized,
153+
LeafDigest<C>: PartialEq,
154+
{
155+
#[inline]
156+
fn len(&self) -> usize {
157+
self.0.len()
158+
}
159+
160+
#[inline]
161+
fn get(&self, index: usize) -> Option<&LeafDigest<C>> {
162+
self.0.get(index)
163+
}
164+
165+
#[inline]
166+
fn current_index(&self) -> Option<usize> {
167+
if self.is_empty() {
168+
None
169+
} else {
170+
Some(self.len() - 1)
171+
}
172+
}
173+
174+
#[inline]
175+
fn position(&self, leaf_digest: &LeafDigest<C>) -> Option<usize> {
176+
self.0.iter().position(|l| l == leaf_digest)
177+
}
178+
179+
#[inline]
180+
fn push(&mut self, leaf_digest: LeafDigest<C>) {
181+
self.0.push(leaf_digest);
182+
}
183+
184+
#[inline]
185+
fn extend(&mut self, leaf_digests: Vec<LeafDigest<C>>) {
186+
self.0.extend(leaf_digests)
187+
}
188+
189+
#[inline]
190+
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
191+
Self(leaf_digests)
192+
}
193+
194+
#[inline]
195+
fn into_leaf_digests(self) -> Vec<LeafDigest<C>> {
196+
self.0
197+
}
198+
199+
#[inline]
200+
fn mark(&mut self, index: usize) {
201+
let _ = index;
202+
}
203+
204+
#[inline]
205+
fn is_marked(&self, index: usize) -> Option<bool> {
206+
self.get(index).map(|_| false)
207+
}
208+
209+
#[inline]
210+
fn remove(&mut self, index: usize) -> bool {
211+
let _ = index;
212+
false
213+
}
214+
}
215+
121216
/// Leaf Vector
122217
#[cfg_attr(
123218
feature = "serde",
@@ -219,6 +314,122 @@ where
219314
}
220315
}
221316

317+
/// Leaf BTree Map
318+
#[cfg_attr(
319+
feature = "serde",
320+
derive(Deserialize, Serialize),
321+
serde(
322+
bound(
323+
deserialize = "LeafDigest<C>: Deserialize<'de>",
324+
serialize = "LeafDigest<C>: Serialize"
325+
),
326+
crate = "manta_util::serde",
327+
deny_unknown_fields
328+
)
329+
)]
330+
#[derive(derivative::Derivative)]
331+
#[derivative(
332+
Clone(bound = "LeafDigest<C>: Clone"),
333+
Debug(bound = "LeafDigest<C>: Debug"),
334+
Default(bound = "LeafDigest<C>: Default"),
335+
Eq(bound = "LeafDigest<C>: Eq"),
336+
PartialEq(bound = "LeafDigest<C>: PartialEq")
337+
)]
338+
pub struct LeafBTreeMap<C>
339+
where
340+
C: Configuration + ?Sized,
341+
{
342+
/// Hash map of marked leaf digests
343+
map: BTreeMap<usize, (bool, LeafDigest<C>)>,
344+
345+
/// Last index
346+
last_index: Option<usize>,
347+
}
348+
349+
impl<C> LeafMap<C> for LeafBTreeMap<C>
350+
where
351+
C: Configuration + ?Sized,
352+
LeafDigest<C>: PartialEq,
353+
{
354+
#[inline]
355+
fn len(&self) -> usize {
356+
self.map.len()
357+
}
358+
359+
#[inline]
360+
fn get(&self, index: usize) -> Option<&LeafDigest<C>> {
361+
Some(&self.map.get(&index)?.1)
362+
}
363+
364+
#[inline]
365+
fn current_index(&self) -> Option<usize> {
366+
self.last_index
367+
}
368+
369+
#[inline]
370+
fn position(&self, leaf_digest: &LeafDigest<C>) -> Option<usize> {
371+
self.map.iter().position(|(_, (_, l))| l == leaf_digest)
372+
}
373+
374+
#[inline]
375+
fn push(&mut self, leaf_digest: LeafDigest<C>) {
376+
self.last_index = Some(self.last_index.map(|index| index + 1).unwrap_or(0));
377+
self.map.insert(
378+
self.last_index
379+
.expect("This cannot fail because of the computation above."),
380+
(false, leaf_digest),
381+
);
382+
}
383+
384+
#[inline]
385+
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
386+
let digest_count = leaf_digests.len();
387+
if digest_count == 0 {
388+
Self {
389+
map: Default::default(),
390+
last_index: None,
391+
}
392+
} else {
393+
Self {
394+
map: leaf_digests
395+
.into_iter()
396+
.map(|x| (false, x))
397+
.enumerate()
398+
.collect::<BTreeMap<usize, (bool, LeafDigest<C>)>>(),
399+
last_index: Some(digest_count - 1),
400+
}
401+
}
402+
}
403+
404+
#[inline]
405+
fn into_leaf_digests(self) -> Vec<LeafDigest<C>> {
406+
self.map
407+
.into_iter()
408+
.map(|(_, (_, digest))| digest)
409+
.collect()
410+
}
411+
412+
#[inline]
413+
fn mark(&mut self, index: usize) {
414+
if let Some((b, _)) = self.map.get_mut(&index) {
415+
*b = true
416+
};
417+
}
418+
419+
#[inline]
420+
fn is_marked(&self, index: usize) -> Option<bool> {
421+
Some(self.map.get(&index)?.0)
422+
}
423+
424+
#[inline]
425+
fn remove(&mut self, index: usize) -> bool {
426+
match self.last_index {
427+
Some(current_index) if index == current_index => false,
428+
_ => !matches!(self.map.remove(&index), None),
429+
}
430+
}
431+
}
432+
222433
/// Leaf Hash Map
223434
#[cfg(feature = "std")]
224435
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

manta-crypto/src/merkle_tree/partial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use crate::merkle_tree::{
2222
capacity,
2323
inner_tree::{BTreeMap, InnerMap, InnerNodeIter, PartialInnerTree},
24-
leaf_map::{LeafMap, LeafVec},
24+
leaf_map::{LeafMap, TrivialLeafVec},
2525
node::{NodeRange, Parity},
2626
Configuration, CurrentPath, InnerDigest, Leaf, LeafDigest, MerkleTree, Node, Parameters, Path,
2727
PathError, Root, Tree, WithProofs,
@@ -57,7 +57,7 @@ pub type PartialMerkleTree<C, M = BTreeMap<C>> = MerkleTree<C, Partial<C, M>>;
5757
Hash(bound = "L: Hash, InnerDigest<C>: Hash, M: Hash"),
5858
PartialEq(bound = "L: PartialEq, InnerDigest<C>: PartialEq, M: PartialEq")
5959
)]
60-
pub struct Partial<C, M = BTreeMap<C>, L = LeafVec<C>>
60+
pub struct Partial<C, M = BTreeMap<C>, L = TrivialLeafVec<C>>
6161
where
6262
C: Configuration + ?Sized,
6363
M: InnerMap<C>,

manta-pay/src/config/utxo.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
GroupCurve, GroupVar,
2626
},
2727
crypto::{
28-
encryption::aes,
28+
encryption::aes::{self, FixedNonceAesGcm},
2929
poseidon::{self, encryption::BlockArray, hash::Hasher, ParameterFieldType},
3030
},
3131
};
@@ -857,8 +857,7 @@ impl<COM> encryption::Encrypt for IncomingAESEncryptionScheme<COM> {
857857
plaintext: &Self::Plaintext,
858858
compiler: &mut (),
859859
) -> Self::Ciphertext {
860-
let aes = AES::default();
861-
aes.encrypt(encryption_key, randomness, header, plaintext, compiler)
860+
FixedNonceAesGcm.encrypt(encryption_key, randomness, header, plaintext, compiler)
862861
}
863862
}
864863

@@ -870,8 +869,7 @@ impl<COM> encryption::Decrypt for IncomingAESEncryptionScheme<COM> {
870869
ciphertext: &Self::Ciphertext,
871870
compiler: &mut (),
872871
) -> Self::DecryptedPlaintext {
873-
let aes = AES::default();
874-
aes.decrypt(decryption_key, header, ciphertext, compiler)
872+
FixedNonceAesGcm.decrypt(decryption_key, header, ciphertext, compiler)
875873
}
876874
}
877875

@@ -1610,8 +1608,7 @@ impl<COM> encryption::Encrypt for OutgoingAESEncryptionScheme<COM> {
16101608
plaintext: &Self::Plaintext,
16111609
compiler: &mut (),
16121610
) -> Self::Ciphertext {
1613-
let aes = OutAes::default();
1614-
aes.encrypt(encryption_key, randomness, header, plaintext, compiler)
1611+
FixedNonceAesGcm.encrypt(encryption_key, randomness, header, plaintext, compiler)
16151612
}
16161613
}
16171614

@@ -1623,8 +1620,7 @@ impl<COM> encryption::Decrypt for OutgoingAESEncryptionScheme<COM> {
16231620
ciphertext: &Self::Ciphertext,
16241621
compiler: &mut (),
16251622
) -> Self::DecryptedPlaintext {
1626-
let aes = OutAes::default();
1627-
aes.decrypt(decryption_key, header, ciphertext, compiler)
1623+
FixedNonceAesGcm.decrypt(decryption_key, header, ciphertext, compiler)
16281624
}
16291625
}
16301626

0 commit comments

Comments
 (0)