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>,
impl<'a, F, A> Poseidon<'a, F, A>where
F: PrimeField,
A: Arity<F>,
sourcepub fn new(constants: &'a PoseidonConstants<F, A>) -> Self
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);
}
sourcepub fn new_with_preimage(
preimage: &[F],
constants: &'a PoseidonConstants<F, A>,
) -> Self
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);
sourcepub fn set_preimage(&mut self, preimage: &[F])
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
sourcepub fn input(&mut self, element: F) -> Result<usize, Error>
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 !!!
sourcepub fn hash_in_mode(&mut self, mode: HashMode) -> F
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
sourcepub fn hash(&mut self) -> F
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
sourcepub fn extract_output(&self) -> F
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
sourcepub fn hash_optimized_static(&mut self) -> F
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> PartialEq for Poseidon<'a, F, A>
impl<'a, F, A> PartialEq for Poseidon<'a, F, A>
source§impl<'a, Scalar, A> SizedWitness<Scalar> for Poseidon<'a, Scalar, A>where
Scalar: PrimeField,
A: Arity<Scalar>,
impl<'a, Scalar, A> SizedWitness<Scalar> for Poseidon<'a, Scalar, A>where
Scalar: PrimeField,
A: Arity<Scalar>,
fn num_constraints(&self) -> usize
fn num_inputs(&self) -> usize
fn num_aux(&self) -> usize
fn generate_witness_into( &mut self, aux: &mut [Scalar], _inputs: &mut [Scalar], ) -> Scalar
fn generate_witness(&mut self) -> (Vec<Scalar>, Vec<Scalar>, Scalar)
fn generate_witness_into_cs<CS>(&mut self, cs: &mut CS) -> Scalarwhere
CS: ConstraintSystem<Scalar>,
impl<'a, F, A> StructuralPartialEq for Poseidon<'a, F, A>where
F: PrimeField,
A: Arity<F>,
Auto Trait Implementations§
impl<'a, F, A> Freeze for Poseidon<'a, F, A>
impl<'a, F, A> RefUnwindSafe for Poseidon<'a, F, A>where
<<A as Arity<F>>::ConstantsSize as ArrayLength<F>>::ArrayType: RefUnwindSafe,
F: RefUnwindSafe,
A: 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>
impl<'a, F, A> UnwindSafe for Poseidon<'a, F, A>where
<<A as Arity<F>>::ConstantsSize as ArrayLength<F>>::ArrayType: UnwindSafe,
F: UnwindSafe + RefUnwindSafe,
A: RefUnwindSafe,
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§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.source§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.source§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.source§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.source§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.source§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.source§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.source§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.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§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 moresource§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 moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§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.source§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.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.