pub struct Update<T>where
T: BigInt,{ /* private fields */ }Expand description
A sum of updates to be applied to witnesses.
Implementations§
source§impl<T> Update<T>where
T: BigInt,
impl<T> Update<T>where
T: BigInt,
sourcepub fn add<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
pub fn add<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
Absorb an element that must be added to a witness.
sourcepub fn del<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
pub fn del<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
Absorb an element that must be deleted from a witness.
sourcepub fn undo_add<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
pub fn undo_add<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
Undo an absorbed element’s addition into an update.
sourcepub fn undo_del<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
pub fn undo_del<'a, M, N, S, V>(&mut self, v: &'a V, w: &Witness<T>)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
Undo an absorbed element’s deletion from an update.
sourcepub fn update_witness<'a, M, N, S, V>(
&self,
acc: &Accumulator<T>,
v: &'a V,
w: &Witness<T>
) -> Witness<T>where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
pub fn update_witness<'a, M, N, S, V>(
&self,
acc: &Accumulator<T>,
v: &'a V,
w: &Witness<T>
) -> Witness<T>where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
Update a witness. The update will include all additions and deletions previously absorbed into this update struct.
use clacc::{
Accumulator, Update, RawSerializer,
blake2::Mapper,
gmp::BigInt,
typenum::U16,
};
// In this example, the update will include a deletion, so the
// accumulator must be created with a private key.
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into()
);
// Create the static element.
let xs = b"abc".to_vec();
// Create the deletion.
let xd = b"def".to_vec();
// Create the addition.
let xa = b"ghi".to_vec();
// Add the deletion element.
acc.add::<Mapper, U16, RawSerializer, _>(&xd);
// Add the static element to the accumulator.
let mut wxs = acc.add::<Mapper, U16, RawSerializer, _>(&xs);
// Delete the deletion element from the accumulator.
let wxd = acc.prove::<Mapper, U16, RawSerializer, _>(&xd).unwrap();
acc.del::<Mapper, U16, RawSerializer, _>(&xd, &wxd).unwrap();
// Create an update object and absorb the addition and deletion.
let mut u = Update::new();
u.del::<Mapper, U16, RawSerializer, _>(&xd, &wxd);
u.add::<Mapper, U16, RawSerializer, _>(
&xa,
&acc.add::<Mapper, U16, RawSerializer, _>(&xa),
);
// Update the static element's witness.
wxs = u.update_witness::<Mapper, U16, RawSerializer, _>(
&acc,
&xs,
&wxs,
);
assert!(acc.verify::<Mapper, U16, RawSerializer, _>(
&xs,
&wxs,
).is_ok());sourcepub fn update_witnesses<'a, M, N, S, V, I>(
&self,
acc: &Accumulator<T>,
additions: Arc<Mutex<I>>,
staticels: Arc<Mutex<I>>
)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
I: Iterator<Item = &'a mut (V, Witness<T>)> + 'a + Send,
pub fn update_witnesses<'a, M, N, S, V, I>(
&self,
acc: &Accumulator<T>,
additions: Arc<Mutex<I>>,
staticels: Arc<Mutex<I>>
)where
M: Mapper,
N: ArrayLength<u8>,
V: 'a,
S: ElementSerializer<V>,
I: Iterator<Item = &'a mut (V, Witness<T>)> + 'a + Send,
Thread-safe method that updates multiple witnesses.
This method operates on atomic references to iterators over collections of element-witness pairs so that the application can invoke it concurrently. All concurrently running invocations will finish when the iterators have reached the end of their collections. A usage example can be found in this crate’s benchmarks.
It is assumed that the additional elements have been absorbed by the update and that their witnesses are the accumulator’s value before any of the additions or deletions absorbed by this update were applied. Updating the witnesses for each of these additional elements is thus acheived by simply removing its respective element from the update and applying the result to its witness.
use clacc::{
Accumulator, Update, Witness, RawSerializer as Raw,
blake2::Mapper as Map,
gmp::BigInt,
typenum::U16,
};
use rand::RngCore;
use std::sync::{Arc, Mutex};
// Create elements.
const BUCKET_SIZE: usize = 20;
const DELETIONS_COUNT: usize = 2;
const ADDITIONS_COUNT: usize = 10;
const STATICELS_COUNT: usize = BUCKET_SIZE - DELETIONS_COUNT;
let mut deletions: Vec<(Vec<u8>, Witness<BigInt>)> = vec![
Default::default(); DELETIONS_COUNT
];
let mut additions: Vec<(Vec<u8>, Witness<BigInt>)> = vec![
Default::default(); ADDITIONS_COUNT
];
let mut staticels: Vec<(Vec<u8>, Witness<BigInt>)> = vec![
Default::default(); STATICELS_COUNT
];
let mut rng = rand::thread_rng();
let mut bytes = vec![0; 8];
for deletion in deletions.iter_mut() {
rng.fill_bytes(&mut bytes);
deletion.0 = bytes.clone();
}
for addition in additions.iter_mut() {
rng.fill_bytes(&mut bytes);
addition.0 = bytes.clone();
}
for staticel in staticels.iter_mut() {
rng.fill_bytes(&mut bytes);
staticel.0 = bytes.clone();
}
// Create accumulator with private key.
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into()
);
// Accumulate elements.
for deletion in deletions.iter() {
acc.add::<Map, U16, Raw, _>(&deletion.0);
}
for stat in staticels.iter() {
acc.add::<Map, U16, Raw, _>(&stat.0);
}
// Generate witnesses for static elements.
for stat in staticels.iter_mut() {
stat.1 = acc.prove::<Map, U16, Raw, _>(
&stat.0,
).unwrap();
}
// Save accumulation at current state.
let prev = acc.clone();
// Accumulate deletions.
for del in deletions.iter_mut() {
del.1 = acc.prove::<Map, U16, Raw, _>(&del.0).unwrap();
acc.del::<Map, U16, Raw, _>(&del.0, &del.1).unwrap();
}
// Accumulate additions.
for addition in additions.iter_mut() {
addition.1 = acc.add::<Map, U16, Raw, _>(&addition.0);
// Use the saved accumulation as the witness value.
addition.1.set_value(prev.get_value());
}
// Batch updates.
let mut update = Update::new();
for deletion in deletions.iter() {
update.del::<Map, U16, Raw, _>(
&deletion.0,
&deletion.1,
);
}
for addition in additions.iter() {
update.add::<Map, U16, Raw, _>(
&addition.0,
&addition.1,
);
}
// Update all witnesses.
update.update_witnesses::<Map, U16, Raw, _, _>(
&acc,
Arc::new(Mutex::new(additions.iter_mut())),
Arc::new(Mutex::new(staticels.iter_mut())),
);
// Verify all updated witnesses.
for addition in additions.iter() {
assert!(acc.verify::<Map, U16, Raw, _>(
&addition.0,
&addition.1,
).is_ok());
}
for staticel in staticels.iter() {
assert!(acc.verify::<Map, U16, Raw, _>(
&staticel.0,
&staticel.1,
).is_ok());
}