Struct clacc::Accumulator
source · pub struct Accumulator<T: BigInt> { /* 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 bit depth in the
accumulation z
will never exceed the bit depth in the modulus n
.
Implementations§
source§impl<T: BigInt> Accumulator<T>
impl<T: BigInt> Accumulator<T>
sourcepub fn with_private_key(n: &T, d: &T) -> Self
pub fn with_private_key(n: &T, d: &T) -> Self
Initialize an accumulator from private key parameters n
the modulus
and d
ϕ(n). 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;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let acc = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
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;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
sourcepub fn get_public_key(&self) -> T
pub fn get_public_key(&self) -> T
Get an accumulator’s public key.
sourcepub fn add(&mut self, x: &T) -> T
pub fn add(&mut self, x: &T) -> T
Add an element to the accumulator. The element x
must be a prime.
The returned value is a witness to the element’s addition.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
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;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let mut acc = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
let w = acc.add(&x);
assert!(acc.verify(&x, &w).is_ok());
sourcepub fn del(&mut self, x: &T) -> Result<T, MissingPrivateKeyError>
pub fn del(&mut self, x: &T) -> Result<T, MissingPrivateKeyError>
Delete an element from an accumulator. The element x
must be a prime.
The returned value is the accumulation less the element.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let mut acc = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(7);
let w = acc.add(&x);
assert!(acc.del(&x).is_ok());
assert!(acc.verify(&x, &w).is_err());
This will only succeed with an accumulator constructed from a private key.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice())
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
acc.add(&x);
assert!(acc.del(&x).is_err());
sourcepub fn prove(&self, x: &T) -> Result<T, MissingPrivateKeyError>
pub fn prove(&self, x: &T) -> Result<T, MissingPrivateKeyError>
Generate a witness to an element’s addition to the accumulation.
The element x
must be a prime.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let mut acc = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(7);
acc.add(&x);
let u = acc.prove(&x).unwrap();
assert!(acc.verify(&x, &u).is_ok());
This will only succeed with an accumulator constructed from a private key.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(7);
acc.add(&x);
assert!(acc.prove(&x).is_err());
sourcepub fn verify(&self, x: &T, w: &T) -> Result<(), ElementNotFoundError>
pub fn verify(&self, x: &T, w: &T) -> Result<(), ElementNotFoundError>
Verify an element is a member of the accumulator. The element x
must
be a prime.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
let u = acc.add(&x);
assert!(acc.verify(&x, &u).is_ok());
This works with accumulators constructed from a public key or a private key.
use clacc::Accumulator;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let mut acc = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
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;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
let y = <BigInt as clacc::BigInt>::from_i64(5);
// Add an element.
acc.add(&x);
// Save the current accumulation. This value is effectively
// a witness for the next element added.
let w = acc.get_value().clone();
// Add another element.
acc.add(&y);
// 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;
use num_bigint::BigInt;
let n = vec![0x0c, 0xa1];
let d = vec![0x0c, 0x30];
let mut acc_prv = Accumulator::<BigInt>::with_private_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
&<BigInt as clacc::BigInt>::from_bytes_be(d.as_slice()),
);
let mut acc_pub = Accumulator::<BigInt>::with_public_key(
&<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);
let x = <BigInt as clacc::BigInt>::from_i64(3);
let w = acc_prv.add(&x);
acc_pub.set_value(&acc_prv.get_value());
assert!(acc_prv.verify(&x, &w).is_ok());