|
17 | 17 | //! Leaf Map |
18 | 18 |
|
19 | 19 | use crate::merkle_tree::{Configuration, LeafDigest}; |
20 | | -use alloc::vec::Vec; |
| 20 | +use alloc::{collections::BTreeMap, vec::Vec}; |
21 | 21 | use core::{fmt::Debug, hash::Hash}; |
22 | 22 |
|
23 | 23 | #[cfg(feature = "serde")] |
@@ -118,6 +118,101 @@ where |
118 | 118 | } |
119 | 119 | } |
120 | 120 |
|
| 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 | + |
121 | 216 | /// Leaf Vector |
122 | 217 | #[cfg_attr( |
123 | 218 | feature = "serde", |
@@ -219,6 +314,122 @@ where |
219 | 314 | } |
220 | 315 | } |
221 | 316 |
|
| 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 | + |
222 | 433 | /// Leaf Hash Map |
223 | 434 | #[cfg(feature = "std")] |
224 | 435 | #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
|
0 commit comments