Struct neptune::poseidon::Poseidon

source ·
pub struct Poseidon<'a, F, A = U2>where
    F: PrimeField,
    A: Arity<F>,{
    pub elements: GenericArray<F, A::ConstantsSize>,
    /* private fields */
}
Expand description

Holds preimage, some utility offsets and counters along with the reference to PoseidonConstants required for hashing. Poseidon is parameterized by ff::PrimeField and Arity, which should be similar to PoseidonConstants.

Poseidon accepts input elements set with length equal or less than Arity.

Fields§

§elements: GenericArray<F, A::ConstantsSize>

the elements to permute

Implementations§

source§

impl<'a, F, A> Poseidon<'a, F, A>where F: PrimeField, A: Arity<F>,

source

pub fn new(constants: &'a PoseidonConstants<F, A>) -> Self

Creates Poseidon instance using provided PoseidonConstants as input. Underlying set of elements are initialized and domain_tag from PoseidonConstants is used as zero element in the set. Therefore, hashing is eventually performed over Arity + 1 elements in fact, while Arity elements are occupied by preimage data.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U8;

let constants: PoseidonConstants<Fp, U8> = PoseidonConstants::new();
let poseidon = Poseidon::<Fp, U8>::new(&constants);

assert_eq!(poseidon.elements.len(), 9);
for index in 1..9 {
    assert_eq!(poseidon.elements[index], Fp::ZERO);
}
source

pub fn new_with_preimage( preimage: &[F], constants: &'a PoseidonConstants<F, A> ) -> Self

Creates Poseidon instance using provided preimage and PoseidonConstants as input. Doesn’t support PoseidonConstants with HashType::VariableLength. It is assumed that size of input preimage set can’t be greater than Arity.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let preimage_set_length = 1;
let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new_constant_length(preimage_set_length);

let preimage = vec![Fp::from(u64::MAX); preimage_set_length];

let mut poseidon = Poseidon::<Fp, U2>::new_with_preimage(&preimage, &constants);

assert_eq!(constants.width(), 3);
assert_eq!(poseidon.elements.len(), constants.width());
assert_eq!(poseidon.elements[1], Fp::from(u64::MAX));
assert_eq!(poseidon.elements[2], Fp::ZERO);
source

pub fn set_preimage(&mut self, preimage: &[F])

Replaces the elements with the provided optional items.

Panics

Panics if the provided slice is not equal to the arity.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new_constant_length(1);
let preimage = vec![Fp::from(u64::MAX)];

let mut poseidon = Poseidon::<Fp, U2>::new_with_preimage(&preimage, &constants);

assert_eq!(poseidon.elements.len(), constants.width());
assert_eq!(poseidon.elements[1], Fp::from(u64::MAX));
assert_eq!(poseidon.elements[2], Fp::ZERO);

let preimage = vec![Fp::from(u64::MIN), Fp::from(u64::MIN)];
poseidon.set_preimage(&preimage);
assert_eq!(poseidon.elements.len(), constants.width());
assert_eq!(poseidon.elements[1], Fp::from(u64::MIN)); // Now it's u64::MIN
assert_eq!(poseidon.elements[2], Fp::from(u64::MIN)); // Now it's u64::MIN
source

pub fn reset(&mut self)

Restore the initial state

source

pub fn input(&mut self, element: F) -> Result<usize, Error>

Adds one more field element of preimage to the underlying Poseidon buffer for further hashing. The returned usize represents the element position (within arity) for the input operation. Returns Error::FullBuffer if no more elements can be added for hashing.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new_constant_length(1);

let mut poseidon = Poseidon::<Fp, U2>::new(&constants);
assert_eq!(poseidon.elements.len(), constants.width());
assert_eq!(poseidon.elements[1], Fp::ZERO);
assert_eq!(poseidon.elements[2], Fp::ZERO);

let pos = poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

assert_eq!(pos, 1);
assert_eq!(poseidon.elements[1], Fp::from(u64::MAX));
assert_eq!(poseidon.elements[2], Fp::ZERO);

let pos = poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

assert_eq!(pos, 2);
assert_eq!(poseidon.elements[1], Fp::from(u64::MAX));
assert_eq!(poseidon.elements[2], Fp::from(u64::MAX));

// poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element"); // panic !!!
source

pub fn hash_in_mode(&mut self, mode: HashMode) -> F

Performs hashing using underlying Poseidon buffer of the preimage’ field elements using provided HashMode. Always outputs digest expressed as a single field element of concrete type specified upon PoseidonConstants and Poseidon instantiations.

Example
use neptune::poseidon::{HashMode, PoseidonConstants};
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new();

let mut poseidon = Poseidon::<Fp, U2>::new(&constants);

poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

let digest = poseidon.hash_in_mode(HashMode::Correct);

assert_ne!(digest, Fp::ZERO); // digest has `Fp` type
source

pub fn hash(&mut self) -> F

Performs hashing using underlying Poseidon buffer of the preimage’ field elements in default (optimized) mode. Always outputs digest expressed as a single field element of concrete type specified upon PoseidonConstants and Poseidon instantiations.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new();

let mut poseidon = Poseidon::<Fp, U2>::new(&constants);

poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

let digest = poseidon.hash();

assert_ne!(digest, Fp::ZERO); // digest has `Fp` type
source

pub fn extract_output(&self) -> F

Returns 1-th element from underlying Poseidon buffer. This function is important, since according to Poseidon design, after performing hashing, output digest will be stored at 1-st place of underlying buffer.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new();

let mut poseidon = Poseidon::<Fp, U2>::new(&constants);

poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

let output = poseidon.extract_output();

assert_eq!(poseidon.elements[1], Fp::from(u64::MAX));
assert_eq!(output, Fp::from(u64::MAX)); // output == input

let digest = poseidon.hash();

let output = poseidon.extract_output();

assert_eq!(poseidon.elements[1], output);
assert_eq!(digest, output); // output == digest
source

pub fn hash_optimized_static(&mut self) -> F

Performs hashing using underlying Poseidon buffer of the preimage’ field elements using HashMode::OptimizedStatic mode. Always outputs digest expressed as a single field element of concrete type specified upon PoseidonConstants and Poseidon instantiations.

Example
use neptune::poseidon::PoseidonConstants;
use neptune::poseidon::Poseidon;
use pasta_curves::Fp;
use ff::Field;
use generic_array::typenum::U2;

let constants: PoseidonConstants<Fp, U2> = PoseidonConstants::new();

let mut poseidon = Poseidon::<Fp, U2>::new(&constants);

poseidon.input(Fp::from(u64::MAX)).expect("can't add one more element");

let digest = poseidon.hash_optimized_static();

assert_ne!(digest, Fp::ZERO); // digest has `Fp` type

Trait Implementations§

source§

impl<'a, F, A> Clone for Poseidon<'a, F, A>where F: PrimeField + Clone, A: Arity<F> + Clone, A::ConstantsSize: Clone,

source§

fn clone(&self) -> Poseidon<'a, F, A>

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<'a, F, A> Debug for Poseidon<'a, F, A>where F: PrimeField + Debug, A: Arity<F> + Debug, A::ConstantsSize: Debug,

source§

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

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

impl<'a, F, A> PartialEq for Poseidon<'a, F, A>where F: PrimeField + PartialEq, A: Arity<F> + PartialEq, A::ConstantsSize: PartialEq,

source§

fn eq(&self, other: &Poseidon<'a, F, A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Scalar, A> SizedWitness<Scalar> for Poseidon<'a, Scalar, A>where Scalar: PrimeField, A: Arity<Scalar>,

source§

fn num_constraints(&self) -> usize

source§

fn num_inputs(&self) -> usize

source§

fn num_aux(&self) -> usize

source§

fn generate_witness_into( &mut self, aux: &mut [Scalar], _inputs: &mut [Scalar] ) -> Scalar

source§

fn generate_witness(&mut self) -> (Vec<Scalar>, Vec<Scalar>, Scalar)

source§

fn generate_witness_into_cs<CS>(&mut self, cs: &mut CS) -> Scalarwhere CS: ConstraintSystem<Scalar>,

source§

impl<'a, F, A> Eq for Poseidon<'a, F, A>where F: PrimeField + Eq, A: Arity<F> + Eq, A::ConstantsSize: Eq,

source§

impl<'a, F, A> StructuralEq for Poseidon<'a, F, A>where F: PrimeField, A: Arity<F>,

source§

impl<'a, F, A> StructuralPartialEq for Poseidon<'a, F, A>where F: PrimeField, A: Arity<F>,

Auto Trait Implementations§

§

impl<'a, F, A> RefUnwindSafe for Poseidon<'a, F, A>where A: RefUnwindSafe, F: RefUnwindSafe, <<A as Arity<F>>::ConstantsSize as ArrayLength<F>>::ArrayType: RefUnwindSafe,

§

impl<'a, F, A> Send for Poseidon<'a, F, A>where A: Sync,

§

impl<'a, F, A> Sync for Poseidon<'a, F, A>where A: Sync,

§

impl<'a, F, A> Unpin for Poseidon<'a, F, A>where F: Unpin, <<A as Arity<F>>::ConstantsSize as ArrayLength<F>>::ArrayType: Unpin,

§

impl<'a, F, A> UnwindSafe for Poseidon<'a, F, A>where A: RefUnwindSafe, F: UnwindSafe + RefUnwindSafe, <<A as Arity<F>>::ConstantsSize as ArrayLength<F>>::ArrayType: 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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> Twhere Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. 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 Twhere 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.

§

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

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,

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

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

Borrows self, then passes self.borrow() into the pipe function. Read more
§

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,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

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

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> Rwhere Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Immutable access to the 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,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Immutable access to the 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,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

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

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

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

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

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
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
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. 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.
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.
source§

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

Performs the conversion.