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(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;
use num_bigint::BigInt;
let p = vec![0x3d];
let q = vec![0x35];
let acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.as_slice()),
);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,
};
use num_bigint::BigInt;
use rand::RngCore;
let mut rng = rand::thread_rng();
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;
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.
use clacc::Accumulator;
use num_bigint::BigInt;
let p = vec![0x3d];
let q = vec![0x35];
let n = vec![0x0c, 0xa1];
let mut acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.as_slice()),
);
assert_eq!(
acc.get_public_key(),
<BigInt as clacc::BigInt>::from_bytes_be(n.as_slice()),
);sourcepub fn add(&mut self, x: &T) -> T
pub fn add(&mut self, x: &T) -> T
Add a prime to an accumulator.
use clacc::Accumulator;
use num_bigint::BigInt;
use rand::RngCore;
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 p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.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, Error>
pub fn del(&mut self, x: &T) -> Result<T, Error>
Delete a prime from an accumulator.
use clacc::Accumulator;
use num_bigint::BigInt;
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.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, Error>
pub fn prove(&self, x: &T) -> Result<T, Error>
Generate a witness to a prime’s addition to the accumulation.
use clacc::Accumulator;
use num_bigint::BigInt;
let p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.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<(), Error>
pub fn verify(&self, x: &T, w: &T) -> Result<(), Error>
Verify a prime is a member of an accumulator.
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 p = vec![0x3d];
let q = vec![0x35];
let mut acc = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.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 p = vec![0x3d];
let q = vec![0x35];
let mut acc_prv = Accumulator::<BigInt>::with_private_key(
<BigInt as clacc::BigInt>::from_bytes_be(p.as_slice()),
<BigInt as clacc::BigInt>::from_bytes_be(q.as_slice()),
);
let n = vec![0x0c, 0xa1];
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());Trait Implementations§
Auto Trait Implementations§
impl<T> RefUnwindSafe for Accumulator<T>where T: RefUnwindSafe,
impl<T> Send for Accumulator<T>
impl<T> Sync for Accumulator<T>
impl<T> Unpin for Accumulator<T>where T: Unpin,
impl<T> UnwindSafe for Accumulator<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_mut() into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut() only in debug builds, and is erased in release
builds.