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,

source

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

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

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

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

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

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

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

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

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

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,

source§

fn clone(&self) -> Accumulator<T, D>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, D> Debug for Accumulator<T, D>where
T: for<'a> BigInt<'a> + Debug,
D: Map + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T, D> Display for Accumulator<T, D>where
T: for<'a> BigInt<'a>,
D: Map,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, D> RefUnwindSafe for Accumulator<T, D>where
D: RefUnwindSafe,
T: RefUnwindSafe,

§

impl<T, D> Send for Accumulator<T, D>where
D: Send,

§

impl<T, D> Sync for Accumulator<T, D>where
D: Sync,

§

impl<T, D> Unpin for Accumulator<T, D>where
D: Unpin,
T: Unpin,

§

impl<T, D> UnwindSafe for Accumulator<T, D>where
D: UnwindSafe,
T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere
T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere
T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.