From c8e4260efdd7f8a24b189800dcacedeb5e257562 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:47:04 +0100 Subject: [PATCH] fix: remove reliance on invalid decompositions in selector calculation (#9337) Decomposing `hash` into `SELECTOR_SIZE` bytes relies on the compiler not checking the correctness of decompositions performed at compile time. This means the current code does not work at runtime and we're also looking to enforce the correctness of this decomposition at compile-time. I've replaced this decomposition with a truncation into 32 bits as this seems equivalent. --- .../crates/types/src/abis/event_selector.nr | 9 ++------- .../crates/types/src/abis/function_selector.nr | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/event_selector.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/event_selector.nr index b456c2fd03f..0b8fdb604f4 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/event_selector.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/event_selector.nr @@ -1,8 +1,5 @@ -use crate::utils::field::field_from_bytes; use crate::traits::{Serialize, Deserialize, FromField, ToField, Empty}; -global SELECTOR_SIZE: u32 = 4; - pub struct EventSelector { // 1st 4-bytes (big-endian leftmost) of abi-encoding of an event. inner: u32, @@ -53,10 +50,8 @@ impl EventSelector { let bytes = signature.as_bytes(); let hash = crate::hash::poseidon2_hash_bytes(bytes); - // We choose the last SELECTOR_SIZE bytes of the hash to avoid getting the first byte that is not full - let hash_bytes = hash.to_be_bytes::(); - - EventSelector::from_field(field_from_bytes(hash_bytes, true)) + // `hash` is automatically truncated to fit within 32 bits. + EventSelector::from_field(hash) } pub fn zero() -> Self { diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_selector.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_selector.nr index f5a81195504..6722a8f25e9 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_selector.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_selector.nr @@ -1,8 +1,5 @@ -use crate::utils::field::field_from_bytes; use crate::traits::{Serialize, Deserialize, FromField, ToField, Empty}; -global SELECTOR_SIZE: u32 = 4; - pub struct FunctionSelector { // 1st 4-bytes of abi-encoding of function. inner: u32, @@ -53,10 +50,8 @@ impl FunctionSelector { let bytes = signature.as_bytes(); let hash = crate::hash::poseidon2_hash_bytes(bytes); - // We choose the last SELECTOR_SIZE bytes of the hash to avoid getting the first byte that is not full - let hash_bytes = hash.to_be_bytes::(); - - FunctionSelector::from_field(field_from_bytes(hash_bytes, true)) + // `hash` is automatically truncated to fit within 32 bits. + FunctionSelector::from_field(hash) } pub fn zero() -> Self {