Skip to content

Commit

Permalink
chore: Update light-poseidon to 0.2.0 (#33923)
Browse files Browse the repository at this point in the history
That new release contains an important change which prevents a
potential DDoS.

* Lightprotocol/light-poseidon#32

Invoking `from_bytes_be` function light-poseidon 0.1.1 inverts all
the inputs before performing a check whether their length exceeds
the modulus of the prime field. Therefore, it was prone to an
attack, where a mailicious user could submit long byte slices just
to DDoS the validator, being stuck on inverting large byte sequences.

The update and mentioned change fixes the same issue as #33363 aims
to address.

The new release contains also few other less important changes like:

* Lightprotocol/light-poseidon#37
* Lightprotocol/light-poseidon#38
* Lightprotocol/light-poseidon#39

(cherry picked from commit 67f8daf)

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	programs/sbf/Cargo.lock
  • Loading branch information
vadorovsky authored and mergify[bot] committed Nov 28, 2023
1 parent 67bfb48 commit 38dd0d9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ lazy_static = "1.4.0"
libc = "0.2.148"
libloading = "0.7.4"
libsecp256k1 = "0.6.0"
<<<<<<< HEAD
light-poseidon = "0.1.1"
=======
light-poseidon = "0.2.0"
>>>>>>> 67f8daf6e9 (chore: Update light-poseidon to 0.2.0 (#33923))
log = "0.4.20"
lru = "0.7.7"
lz4 = "1.24.0"
Expand Down
7 changes: 7 additions & 0 deletions programs/sbf/Cargo.lock

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

52 changes: 30 additions & 22 deletions sdk/program/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ pub enum PoseidonSyscallError {
"Invalid length of the input. The length matching the modulus of the prime field is 32."
)]
InvalidInputLength,
#[error("Failed to convert bytest into a prime field element.")]
BytesToPrimeFieldElement,
#[error("Input is larger than the modulus of the prime field.")]
InputLargerThanModulus,
#[error("Failed to convert a vector of bytes into an array.")]
VecToArray,
#[error("Failed to convert the number of inputs from u64 to u8.")]
U64Tou8,
#[error("Failed to convert bytes to BigInt")]
BytesToBigInt,
#[error("Invalid width. Choose a width between 2 and 16 for 1 to 15 inputs.")]
InvalidWidthCircom,
#[error("Unexpected error")]
Expand All @@ -41,10 +45,12 @@ impl From<u64> for PoseidonSyscallError {
3 => PoseidonSyscallError::InvalidNumberOfInputs,
4 => PoseidonSyscallError::EmptyInput,
5 => PoseidonSyscallError::InvalidInputLength,
6 => PoseidonSyscallError::InputLargerThanModulus,
7 => PoseidonSyscallError::VecToArray,
8 => PoseidonSyscallError::U64Tou8,
9 => PoseidonSyscallError::InvalidWidthCircom,
6 => PoseidonSyscallError::BytesToPrimeFieldElement,
7 => PoseidonSyscallError::InputLargerThanModulus,
8 => PoseidonSyscallError::VecToArray,
9 => PoseidonSyscallError::U64Tou8,
10 => PoseidonSyscallError::BytesToBigInt,
11 => PoseidonSyscallError::InvalidWidthCircom,
_ => PoseidonSyscallError::Unexpected,
}
}
Expand All @@ -58,11 +64,13 @@ impl From<PoseidonSyscallError> for u64 {
PoseidonSyscallError::InvalidNumberOfInputs => 3,
PoseidonSyscallError::EmptyInput => 4,
PoseidonSyscallError::InvalidInputLength => 5,
PoseidonSyscallError::InputLargerThanModulus => 6,
PoseidonSyscallError::VecToArray => 7,
PoseidonSyscallError::U64Tou8 => 8,
PoseidonSyscallError::InvalidWidthCircom => 9,
PoseidonSyscallError::Unexpected => 10,
PoseidonSyscallError::BytesToPrimeFieldElement => 6,
PoseidonSyscallError::InputLargerThanModulus => 7,
PoseidonSyscallError::VecToArray => 8,
PoseidonSyscallError::U64Tou8 => 9,
PoseidonSyscallError::BytesToBigInt => 10,
PoseidonSyscallError::InvalidWidthCircom => 11,
PoseidonSyscallError::Unexpected => 12,
}
}
}
Expand Down Expand Up @@ -210,25 +218,25 @@ pub fn hashv(
impl From<PoseidonError> for PoseidonSyscallError {
fn from(error: PoseidonError) -> Self {
match error {
PoseidonError::InvalidNumberOfInputs {
inputs: _,
max_limit: _,
width: _,
} => PoseidonSyscallError::InvalidNumberOfInputs,
PoseidonError::InvalidNumberOfInputs { .. } => {
PoseidonSyscallError::InvalidNumberOfInputs
}
PoseidonError::EmptyInput => PoseidonSyscallError::EmptyInput,
PoseidonError::InvalidInputLength {
len: _,
modulus_bytes_len: _,
} => PoseidonSyscallError::InvalidInputLength,
PoseidonError::InvalidInputLength { .. } => {
PoseidonSyscallError::InvalidInputLength
}
PoseidonError::BytesToPrimeFieldElement { .. } => {
PoseidonSyscallError::BytesToPrimeFieldElement
}
PoseidonError::InputLargerThanModulus => {
PoseidonSyscallError::InputLargerThanModulus
}
PoseidonError::VecToArray => PoseidonSyscallError::VecToArray,
PoseidonError::U64Tou8 => PoseidonSyscallError::U64Tou8,
PoseidonError::InvalidWidthCircom {
width: _,
max_limit: _,
} => PoseidonSyscallError::InvalidWidthCircom,
PoseidonError::BytesToBigInt => PoseidonSyscallError::BytesToBigInt,
PoseidonError::InvalidWidthCircom { .. } => {
PoseidonSyscallError::InvalidWidthCircom
}
}
}
}
Expand Down

0 comments on commit 38dd0d9

Please sign in to comment.