Struct clacc::Accumulator
source · pub struct Accumulator<T, D = D128>where
T: for<'a> BigInt<'a>,
D: Map,{ /* private fields */ }
Expand description
An accumulator.
Elements may be added and deleted from the acculumator without increasing
the size of its internal parameters. That is, the number of digits in the
accumulation z
will never exceed the number of digits in the modulus
n
.
Implementations§
source§impl<T, D> Accumulator<T, D>where
T: for<'a> BigInt<'a>,
D: Map,
impl<T, D> Accumulator<T, D>where
T: for<'a> BigInt<'a>,
D: Map,
sourcepub fn with_private_key(p: T, q: T) -> Self
pub fn with_private_key(p: T, q: T) -> Self
Initialize an accumulator from private key parameters. All accumulators are able to add elements and verify witnesses. An accumulator constructed from a private key is able to delete elements and prove elements after their addition.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
sourcepub fn with_random_key<F: FnMut(&mut [u8])>(
fill_bytes: F,
key_bits: Option<usize>
) -> (Self, T, T)
pub fn with_random_key<F: FnMut(&mut [u8])>(
fill_bytes: F,
key_bits: Option<usize>
) -> (Self, T, T)
Create an accumulator from a randomly generated private key and return it along with the generated key parameters.
If key_bits
is None
, the bit size of the generated modulus is
3072.
use clacc::{Accumulator, BigInt as BigIntTrait, gmp::BigInt};
use rand::RngCore;
let mut rng = rand::thread_rng();
let acc = Accumulator::<BigInt>::with_random_key(
|bytes| rng.fill_bytes(bytes),
None,
).0;
assert_eq!(acc.get_public_key().size_in_bits(), 3072);
let acc = Accumulator::<BigInt>::with_random_key(
|bytes| rng.fill_bytes(bytes),
Some(256),
).0;
assert_eq!(acc.get_public_key().size_in_bits(), 256);
sourcepub fn with_public_key(n: T) -> Self
pub fn with_public_key(n: T) -> Self
Initialize an accumulator from a public key. An accumulator constructed from a public key is only able to add elements and verify witnesses.
use clacc::{Accumulator, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into(),
);
sourcepub fn get_public_key(&self) -> T
pub fn get_public_key(&self) -> T
Get an accumulator’s public key.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
assert_eq!(acc.get_public_key(), n.as_slice().into());
sourcepub fn add<'a, V>(&mut self, v: &'a V) -> Witness<T>where
V: 'a + Clone + Into<Vec<u8>>,
pub fn add<'a, V>(&mut self, v: &'a V) -> Witness<T>where
V: 'a + Clone + Into<Vec<u8>>,
Add an element to an accumulator.
use clacc::{Accumulator, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into()
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.verify(&x, &w).is_ok());
This works with accumulators constructed from a public key or a private key.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.verify(&x, &w).is_ok());
sourcepub fn del<'a, V>(&mut self, v: &'a V, w: &Witness<T>) -> Result<T, &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
pub fn del<'a, V>(&mut self, v: &'a V, w: &Witness<T>) -> Result<T, &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
Delete an element from an accumulator.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.del(&x, &w).is_ok());
assert!(acc.verify(&x, &w).is_err());
assert!(acc.del(&x, &w).is_err());
This will only succeed with an accumulator constructed from a private key.
use clacc::{Accumulator, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into(),
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.del(&x, &w).is_err());
sourcepub fn prove<'a, V>(&self, v: &'a V) -> Result<Witness<T>, &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
pub fn prove<'a, V>(&self, v: &'a V) -> Result<Witness<T>, &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
Generate a witness to an element’s addition to the accumulation.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
let x = b"abc".to_vec();
acc.add(&x);
let w = acc.prove(&x).unwrap();
assert!(acc.verify(&x, &w).is_ok());
This will only succeed with an accumulator constructed from a private key.
use clacc::{Accumulator, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into(),
);
let x = b"abc".to_vec();
acc.add(&x);
assert!(acc.prove(&x).is_err());
sourcepub fn verify<'a, V>(&self, v: &'a V, w: &Witness<T>) -> Result<(), &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
pub fn verify<'a, V>(&self, v: &'a V, w: &Witness<T>) -> Result<(), &'static str>where
V: 'a + Clone + Into<Vec<u8>>,
Verify an element is a member of an accumulator.
use clacc::{Accumulator, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into(),
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.verify(&x, &w).is_ok());
This works with accumulators constructed from a public key or a private key.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
let x = b"abc".to_vec();
let w = acc.add(&x);
assert!(acc.verify(&x, &w).is_ok());
sourcepub fn get_value(&self) -> T
pub fn get_value(&self) -> T
Return the accumulation value as a BigInt
.
use clacc::{Accumulator, Witness, gmp::BigInt};
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
n.as_slice().into(),
);
let x = b"abc".to_vec();
let y = b"def".to_vec();
// Add an element.
acc.add(&x);
// Save the current accumulation. This value is effectively
// a witness for the next element added.
let u = acc.get_value().clone();
// Add another element.
let nonce = acc.add(&y).nonce;
let w = Witness {
u: u,
nonce: nonce,
};
// Verify that `w` is a witness for `y`.
assert!(acc.verify(&y, &w).is_ok());
sourcepub fn set_value(&mut self, z: T)
pub fn set_value(&mut self, z: T)
Set the accumulation value from a BigInt
.
use clacc::{Accumulator, gmp::BigInt};
let p = vec![0x3d];
let q = vec![0x35];
let mut acc_prv = Accumulator::<BigInt>::with_private_key(
p.as_slice().into(),
q.as_slice().into(),
);
let n = vec![0x0c, 0xa1];
let mut acc_pub = Accumulator::<BigInt>::with_public_key(
n.as_slice().into()
);
let x = b"abc".to_vec();
let w = acc_prv.add(&x);
acc_pub.set_value(acc_prv.get_value());
assert!(acc_prv.verify(&x, &w).is_ok());
Trait Implementations§
source§impl<T, D> Clone for Accumulator<T, D>where
T: for<'a> BigInt<'a> + Clone,
D: Map + Clone,
impl<T, D> Clone for Accumulator<T, D>where
T: for<'a> BigInt<'a> + Clone,
D: Map + Clone,
source§fn clone(&self) -> Accumulator<T, D>
fn clone(&self) -> Accumulator<T, D>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more