RistrettoHash

Struct RistrettoHash 

Source
pub struct RistrettoHash<H> { /* private fields */ }
Expand description

RistrettoHash represents a hash function for multi-sets.

A multi-set is a collection of objects, where any given object may appear multiple times. This hash function will accept the objects in different orders, and with different groupings, but always return the same result for the same multi-set.

This allows incrementally calculating such a multi-set hash without keeping the entire set in memory.

This struct is parameterized by a hash function with 512 bits of output. For example, SHA-512.

This is called RistrettoHash, because this function internally uses the Ristretto group to implement its commutative hashing.

§Examples

In the usual case, you add in different byte slices, with multiplicity For example, to hash the set {cat, cat, dog, dog}, you could do:

use digest::Digest;
use sha2::Sha512;

let mut hash = RistrettoHash::<Sha512>::default();
hash.add(b"cat", 2);
hash.add(b"dog", 2);

This is equivalent to hashing the same number of objects, with a different grouping and order:

use digest::Digest;

let mut hash = RistrettoHash::<Sha512>::default();
hash.add(b"dog", 1);
hash.add(b"cat", 1);
hash.add(b"cat", 1);
hash.add(b"dog", 1);

You can also hash objects in multiple pieces using the update method:

use digest::Digest;

let mut hash = RistrettoHash::<Sha512>::default();
hash.update(b"d");
hash.update(b"og");
hash.end_update(2);
hash.add(b"cat", 2);

If you use update, you must call end_update before adding another object, or calling finalize to get the output of the hash function.

Implementations§

Source§

impl<H: Digest<OutputSize = U64> + Default> RistrettoHash<H>

Source

pub fn add(&mut self, data: impl AsRef<[u8]>, multiplicity: u64)

This function adds a complete object to the hash.

This function takes a multiplicity, which is equivalent to calling the function multiple times, but is much more efficient.

Source

pub fn end_update(&mut self, multiplicity: u64)

This function should be called to mark the end of an object provided with update.

This must always be called after calls to update, otherwise panics will happen when finalizing or adding new objects.

If called without any prior calls to update, this function is equivalent to calling add with an empty slice.

Trait Implementations§

Source§

impl<H: Clone> Clone for RistrettoHash<H>

Source§

fn clone(&self) -> RistrettoHash<H>

Returns a duplicate 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<H: Default> Default for RistrettoHash<H>

Source§

fn default() -> RistrettoHash<H>

Returns the “default value” for a type. Read more
Source§

impl<H: Reset> FixedOutput for RistrettoHash<H>

Source§

type OutputSize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>

Output size for fixed output digest
Source§

fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>)

Write result into provided array and consume the hasher instance.
Source§

fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>)

Write result into provided array and reset the hasher instance.
Source§

fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>
where Self: Sized,

Retrieve result and consume the hasher instance.
Source§

fn finalize_fixed_reset(&mut self) -> GenericArray<u8, Self::OutputSize>

Retrieve result and reset the hasher instance.
Source§

impl<H: Reset> Reset for RistrettoHash<H>

Source§

fn reset(&mut self)

Reset hasher instance to its initial state and return current state.
Source§

impl<H: Update> Update for RistrettoHash<H>

Source§

fn update(&mut self, data: impl AsRef<[u8]>)

update hashes in part of an object.

This method is used to hash an object in multiple different parts. These updates must be finished by calling end_update to mark the end of the object, and its multiplicity.

Failing to call end_update before adding a new object with add or finalizing the hash will panic.

Source§

fn chain(self, data: impl AsRef<[u8]>) -> Self
where Self: Sized,

Digest input data in a chained manner.

Auto Trait Implementations§

§

impl<H> Freeze for RistrettoHash<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for RistrettoHash<H>
where H: RefUnwindSafe,

§

impl<H> Send for RistrettoHash<H>
where H: Send,

§

impl<H> Sync for RistrettoHash<H>
where H: Sync,

§

impl<H> Unpin for RistrettoHash<H>
where H: Unpin,

§

impl<H> UnwindSafe for RistrettoHash<H>
where H: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<D> Digest for D

Source§

type OutputSize = <D as FixedOutput>::OutputSize

Output size for Digest
Source§

fn new() -> D

Create new hasher instance
Source§

fn update(&mut self, data: impl AsRef<[u8]>)

Digest data, updating the internal state. Read more
Source§

fn chain(self, data: impl AsRef<[u8]>) -> D

Digest input data in a chained manner.
Source§

fn finalize(self) -> GenericArray<u8, <D as Digest>::OutputSize>

Retrieve result and consume hasher instance.
Source§

fn finalize_reset(&mut self) -> GenericArray<u8, <D as Digest>::OutputSize>

Retrieve result and reset hasher instance. Read more
Source§

fn reset(&mut self)

Reset hasher instance to its initial state.
Source§

fn output_size() -> usize

Get output size of the hasher
Source§

fn digest(data: &[u8]) -> GenericArray<u8, <D as Digest>::OutputSize>

Convenience function to compute hash of the data. It will handle hasher creation, data feeding and finalization. Read more
Source§

impl<D> DynDigest for D
where D: Update + FixedOutput + Reset + Clone + 'static,

Source§

fn update(&mut self, data: &[u8])

Digest input data. Read more
Source§

fn finalize_reset(&mut self) -> Box<[u8]>

Retrieve result and reset hasher instance
Source§

fn finalize(self: Box<D>) -> Box<[u8]>

Retrieve result and consume boxed hasher instance
Source§

fn reset(&mut self)

Reset hasher instance to its initial state.
Source§

fn output_size(&self) -> usize

Get output size of the hasher
Source§

fn box_clone(&self) -> Box<dyn DynDigest>

Clone hasher state into a boxed trait object
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.