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>

source

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

pub fn get_public_key(&self) -> T

Get an accumulator’s public key.

source

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

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

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

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

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

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

Trait Implementations§

source§

impl<T: Clone + BigInt> Clone for Accumulator<T>

source§

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

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

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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> ToOwned for T
where 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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.