Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Poseidon hash rounds, following 2019/458. #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 7 additions & 5 deletions src/circuit/poseidon_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn poseidon_mimc_round<E: PoseidonEngine<SBox = QuinticSBox<E> >, CS>(
let r_f = params.r_f();
let r_p = params.r_p();
let t = params.t();
let pre_full_rounds = r_f - r_f / 2;

fn add_round_constants<E: PoseidonEngine, CS>(
params: &E::Params,
Expand All @@ -286,7 +287,7 @@ fn poseidon_mimc_round<E: PoseidonEngine<SBox = QuinticSBox<E> >, CS>(

// do releated applications of MDS and then round constants and s-boxes

for full_round in 0..(r_f-1) {
for full_round in 0..(pre_full_rounds - 1) {
let s_box_applied = E::SBox::apply_sbox(
cs.namespace(|| format!("apply s-box for full round {}", full_round)),
&state[..]
Expand Down Expand Up @@ -316,7 +317,7 @@ fn poseidon_mimc_round<E: PoseidonEngine<SBox = QuinticSBox<E> >, CS>(
// transformation and add first round constants before going through partial rounds
{
let s_box_applied = E::SBox::apply_sbox(
cs.namespace(|| format!("apply s-box for full round {}", r_f-1)),
cs.namespace(|| format!("apply s-box for full round {}", pre_full_rounds-1)),
&state[..]
)?;

Expand Down Expand Up @@ -376,13 +377,13 @@ fn poseidon_mimc_round<E: PoseidonEngine<SBox = QuinticSBox<E> >, CS>(
linear_transformation_results.push(linear_applied);
}

add_round_constants::<E, CS>(params, &mut linear_transformation_results[..], r_f, true);
add_round_constants::<E, CS>(params, &mut linear_transformation_results[..], pre_full_rounds, true);
state = linear_transformation_results;

round += 1;
}

for full_round in r_f..(2*r_f - 1) {
for full_round in pre_full_rounds..(r_f - 1) {
let s_box_applied = E::SBox::apply_sbox(
cs.namespace(|| format!("apply s-box for full round {}", full_round)),
&state[..]
Expand All @@ -403,9 +404,10 @@ fn poseidon_mimc_round<E: PoseidonEngine<SBox = QuinticSBox<E> >, CS>(
}

// for a final round we only apply s-box
let full_round = r_f - 1;

let state = E::SBox::apply_sbox(
cs.namespace(|| format!("apply s-box for full round {}", 2*r_f - 1)),
cs.namespace(|| format!("apply s-box for full round {}", full_round)),
&state[..]
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/poseidon/bn256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Bn256PoseidonParams {
pub fn new<H: GroupHasher>() -> Self {
let t = 6u32;
let r_f = 8u32;
let r_p = 84u32;
let r_p = 57u32;
let security_level = 126u32;

Self::new_for_params::<H>(t, r_f, r_p, security_level)
Expand Down
8 changes: 4 additions & 4 deletions src/poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ pub fn poseidon_mimc<E: PoseidonEngine>(

let r_f = params.r_f();
let r_p = params.r_p();
let pre_full_rounds = r_f - r_f / 2;

for full_round in 0..r_f {
for full_round in 0..pre_full_rounds {
let round_constants = params.full_round_key(full_round);
for (el, c) in state.iter_mut().zip(round_constants.iter()) {
el.add_assign(c);
Expand Down Expand Up @@ -211,8 +212,7 @@ pub fn poseidon_mimc<E: PoseidonEngine>(

// reference implementation says that last round does not have matrix muptiplication step,
// that is true due to ease of inversion
for full_round in r_f..(2*r_f - 1) {
// for full_round in r_f..(2*r_f) {
for full_round in pre_full_rounds..(r_f - 1) {
let round_constants = params.full_round_key(full_round);
for (el, c) in state.iter_mut().zip(round_constants.iter()) {
el.add_assign(c);
Expand All @@ -231,7 +231,7 @@ pub fn poseidon_mimc<E: PoseidonEngine>(
}

{
let full_round = 2*r_f - 1;
let full_round = r_f - 1;
let round_constants = params.full_round_key(full_round);
for (el, c) in state.iter_mut().zip(round_constants.iter()) {
el.add_assign(c);
Expand Down