From 83e37183466b8466e80c05f66bd3178ab89c30ff Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 22 Oct 2024 17:53:38 +0100 Subject: [PATCH] fix: remove reliance on invalid decompositions in selector calculation --- .../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 {