Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion halo2_poseidon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "halo2_poseidon"
version = "0.1.0"
version = "0.1.1"
authors = [
"Jack Grigg <[email protected]>",
"Daira Emma Hopwood <[email protected]>",
Expand Down
41 changes: 41 additions & 0 deletions halo2_poseidon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,31 @@ pub trait Domain<F: Field, const RATE: usize> {
fn padding(input_len: usize) -> Self::Padding;
}

/// A Poseidon hash function used with variable input length.
///
/// Domain specified in [ePrint 2019/458 section 4.2](https://eprint.iacr.org/2019/458.pdf).
#[derive(Clone, Copy, Debug)]
pub struct VariableLength;

impl<F: PrimeField, const RATE: usize> Domain<F, RATE> for VariableLength {
type Padding = iter::Chain<iter::Once<F>, iter::Take<iter::Repeat<F>>>;

fn name() -> String {
"VariableLength".into()
}

fn initial_capacity_element() -> F {
// Capacity value is $2^64 + (o-1)$ where o is the output length.
// We hard-code an output length of 1.
F::from_u128(1u128 << 64)
}

fn padding(input_len: usize) -> Self::Padding {
let k = (input_len + RATE - 1) / RATE;
iter::once(F::ONE).chain(iter::repeat(F::ZERO).take(k * RATE - input_len - 1))
}
}

/// A Poseidon hash function used with constant input length.
///
/// Domain specified in [ePrint 2019/458 section 4.2](https://eprint.iacr.org/2019/458.pdf).
Expand Down Expand Up @@ -450,6 +475,22 @@ impl<F: Field, S: Spec<F, T, RATE>, D: Domain<F, RATE>, const T: usize, const RA
}
}

impl<F: PrimeField, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>
Hash<F, S, VariableLength, T, RATE>
{
/// Hashes the given input.
pub fn hash(mut self, message: &[F]) -> F {
for value in message
.into_iter()
.map(|value| *value)
.chain(<VariableLength as Domain<F, RATE>>::padding(message.len()))
{
self.sponge.absorb(value);
}
self.sponge.finish_absorbing().squeeze()
}
}

impl<F: PrimeField, S: Spec<F, T, RATE>, const T: usize, const RATE: usize, const L: usize>
Hash<F, S, ConstantLength<L>, T, RATE>
{
Expand Down