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> Clone for Poseidon<'a, F, A>where
F: PrimeField + Clone,
A: Arity<F> + Clone,
A::ConstantsSize: Clone,
impl<'a, F, A> Clone for Poseidon<'a, F, A>where F: PrimeField + Clone, A: Arity<F> + Clone, A::ConstantsSize: Clone,
source§impl<'a, F, A> Debug for Poseidon<'a, F, A>where
F: PrimeField + Debug,
A: Arity<F> + Debug,
A::ConstantsSize: Debug,
impl<'a, F, A> Debug for Poseidon<'a, F, A>where F: PrimeField + Debug, A: Arity<F> + Debug, A::ConstantsSize: Debug,
source§impl<'a, F, A> PartialEq for Poseidon<'a, F, A>where
F: PrimeField + PartialEq,
A: Arity<F> + PartialEq,
A::ConstantsSize: PartialEq,
impl<'a, F, A> PartialEq for Poseidon<'a, F, A>where F: PrimeField + PartialEq, A: Arity<F> + PartialEq, A::ConstantsSize: PartialEq,
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> Eq for Poseidon<'a, F, A>where F: PrimeField + Eq, A: Arity<F> + Eq, A::ConstantsSize: Eq,
impl<'a, F, A> StructuralEq for Poseidon<'a, F, A>where F: PrimeField, A: Arity<F>,
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> 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
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§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.§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.§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.§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.§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.§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.§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.§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.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§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 more§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 more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§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,
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,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
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,
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,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
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,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
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,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read more§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.§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.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.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,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.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,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.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,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.