Skip to content

Commit 92ce32a

Browse files
authored
fix: Enforce byte input length for the given prime field (#50)
Before this change, we were enforcing only the upper limit of the byte input lenghts in `hash_bytes_be` and `hash_bytes_le` methods. The limit is indicated by the amount of bytes needed to represent the modulus of the given prime field. For the `Fr` field, the limit is 32 bytes. At the same time, we were allowing smaller byte slices. For example, we were allowing either a full 32-byte array with explicit padding: ``` [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ] ``` Or smaller arrays with smaller amount of leading or trailing bytes (depending on endianness): ``` [0, 0, 0, 0, 0, 0, 0, 1] [0, 0, 0, 1] [0, 1] [1] ``` All these inputs produce the same hashes. To avoid confusion, do not allow smaller inputs the modulus and make padding mandatory.
1 parent 50b40cb commit 92ce32a

2 files changed

Lines changed: 220 additions & 140 deletions

File tree

light-poseidon/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ where
459459
if input.is_empty() {
460460
return Err(PoseidonError::EmptyInput);
461461
}
462-
if input.len() > modulus_bytes_len {
462+
if input.len() != modulus_bytes_len {
463463
return Err(PoseidonError::InvalidInputLength {
464464
len: input.len(),
465465
modulus_bytes_len,

0 commit comments

Comments
 (0)