Struct clacc::Update

source ·
pub struct Update<T>where
    T: BigInt,
{ /* private fields */ }
Expand description

A sum of updates to be applied to witnesses.

Implementations§

Create a new batched update.

Absorb an element that must be added to a witness.

Absorb an element that must be deleted from a witness.

Undo an absorbed element’s addition into an update.

Undo an absorbed element’s deletion from an update.

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());

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());
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.