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

poseidon: replaced unimplemented with error and added solana feature flag #9

Merged
merged 5 commits into from
Apr 9, 2023
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ of generating the round constants.
Parameters provided by the library are:

* x^5 S-boxes
* t = 2 - 17 (for 1 to 16 inputs)
* 8 full rounds and partial rounds depending on t [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68]
* t = 2 - 17 (for 1 to 15 inputs)
* 8 full rounds and partial rounds depending on t [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64]

The parameters can be generated with:
```$ cargo xtask generate-poseidon-parameters``
Expand Down Expand Up @@ -89,7 +89,7 @@ but it was also inspired by the following ones:
### Performance

This repository contains a benchmark measuring the performance of this
Poseidon implementation for given 1 - 16 random 32 bytes inputs.
Poseidon implementation for given 1 - 15 random 32 bytes inputs.

To run them, simply use:

Expand Down
4 changes: 4 additions & 0 deletions light-poseidon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ keywords = ["cryptography", "hash", "poseidon", "zero-knowledge", "zkSNARK"]
license = "Apache-2.0"
edition = "2021"

[features]
# limits the width of poseidon paramters to 13 (12 inputs)
width_limit_13 = []

[dependencies]
ark-bn254 = "0.4.0"

Expand Down
8 changes: 4 additions & 4 deletions light-poseidon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! Parameters provided by the library are:
//!
//! * x^5 S-boxes
//! * t = 2 - 17 (for 1 to 16 inputs)
//! * 8 full rounds and partial rounds depending on t [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68]
//! * t = 2 - 17 (for 1 to 15 inputs)
//! * 8 full rounds and partial rounds depending on t [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64]
//! The parameters can be generated with:
//! ```$ cargo xtask generate-poseidon-parameters``
//! # Output type
Expand Down Expand Up @@ -174,7 +174,7 @@ pub mod parameters;
pub const HASH_LEN: usize = 32;
pub const MAX_X5_LEN: usize = 16;

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum PoseidonError {
#[error("Invalid number of inputs: {inputs}, the maximum limit is {max_limit} ({width} - 1)")]
InvalidNumberOfInputs {
Expand Down Expand Up @@ -428,7 +428,7 @@ impl<F: PrimeField> Poseidon<F> {

let params = crate::parameters::bn254_x5::get_poseidon_parameters::<Fr>(
(width).try_into().map_err(|_| PoseidonError::U64Tou8)?,
);
)?;
Ok(Poseidon::<Fr>::new(params))
}
}
109 changes: 75 additions & 34 deletions light-poseidon/src/parameters/bn254_x5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const PARTIAL_ROUNDS: [usize; 15] =
[56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64];
pub const ALPHA: u64 = 5;

use crate::PoseidonParameters;
use crate::{PoseidonError, PoseidonParameters};
/// Returns Poseidon parameters for the BN254 curve with the following
/// properties:
///
Expand All @@ -31,11 +31,22 @@ use crate::PoseidonParameters;
/// The argument of this macro is a type which implements
/// [`ark_ff::PrimeField`](ark_ff::PrimeField).
use ark_ff::PrimeField;
// to avoid warnings when width_limit_13 feature is used
#[allow(unused_variables)]
pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInteger256>>(
t: u8,
) -> PoseidonParameters<F> {
) -> Result<PoseidonParameters<F>, PoseidonError> {
if t == 0_u8 {
unimplemented!()
#[cfg(not(feature = "width_limit_13"))]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 16usize,
});
#[cfg(feature = "width_limit_13")]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 13usize,
});
} else if 2 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -837,14 +848,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[0],
t.into(),
ALPHA,
);
));
} else if 3 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -2080,14 +2091,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[1],
t.into(),
ALPHA,
);
));
} else if 4 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -3733,14 +3744,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[2],
t.into(),
ALPHA,
);
));
} else if 5 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -5946,14 +5957,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[3],
t.into(),
ALPHA,
);
));
} else if 6 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -8635,14 +8646,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[4],
t.into(),
ALPHA,
);
));
} else if 7 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -11938,14 +11949,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[5],
t.into(),
ALPHA,
);
));
} else if 8 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -15807,14 +15818,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[6],
t.into(),
ALPHA,
);
));
} else if 9 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -20158,14 +20169,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[7],
t.into(),
ALPHA,
);
));
} else if 10 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -24871,14 +24882,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[8],
t.into(),
ALPHA,
);
));
} else if 11 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -30516,14 +30527,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[9],
t.into(),
ALPHA,
);
));
} else if 12 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -36313,14 +36324,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[10],
t.into(),
ALPHA,
);
));
} else if 13 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -43060,14 +43071,14 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[11],
t.into(),
ALPHA,
);
));
} else if 14 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -50829,14 +50840,21 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "width_limit_13")]
return Err(PoseidonError::InvalidWidthCircom {
width: 14 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "width_limit_13"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[12],
t.into(),
ALPHA,
);
));
} else if 15 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -58342,14 +58360,21 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "width_limit_13")]
return Err(PoseidonError::InvalidWidthCircom {
width: 15 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "width_limit_13"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[13],
t.into(),
ALPHA,
);
));
} else if 16 == t {
let ark = vec![
F::from(ark_ff::BigInteger256::new([
Expand Down Expand Up @@ -66835,15 +66860,31 @@ pub fn get_poseidon_parameters<F: PrimeField + std::convert::From<ark_ff::BigInt
])),
],
];
return crate::PoseidonParameters::new(
#[cfg(feature = "width_limit_13")]
return Err(PoseidonError::InvalidWidthCircom {
width: 16 as usize,
max_limit: 13usize,
});

#[cfg(not(feature = "width_limit_13"))]
return Ok(crate::PoseidonParameters::new(
ark,
mds,
FULL_ROUNDS,
PARTIAL_ROUNDS[14],
t.into(),
ALPHA,
);
));
} else {
unimplemented!();
#[cfg(not(feature = "width_limit_13"))]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 16usize,
});
#[cfg(feature = "width_limit_13")]
return Err(PoseidonError::InvalidWidthCircom {
width: t as usize,
max_limit: 13usize,
});
}
}
Loading