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

fix: remove reliance on invalid decompositions in selector calculation #9337

Merged
merged 1 commit into from
Oct 23, 2024
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
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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::<SELECTOR_SIZE>();

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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::<SELECTOR_SIZE>();

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 {
Expand Down
Loading