Skip to content

Commit

Permalink
fixed_bytes crate
Browse files Browse the repository at this point in the history
  • Loading branch information
eserilev committed Sep 1, 2024
1 parent 4cd0268 commit 5162316
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 118 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ eth2_keystore = { path = "crypto/eth2_keystore" }
eth2_network_config = { path = "common/eth2_network_config" }
eth2_wallet = { path = "crypto/eth2_wallet" }
execution_layer = { path = "beacon_node/execution_layer" }
fixed_bytes = { path = "consensus/fixed_bytes" }
filesystem = { path = "common/filesystem" }
fork_choice = { path = "consensus/fork_choice" }
genesis = { path = "beacon_node/genesis" }
Expand Down
11 changes: 11 additions & 0 deletions consensus/fixed_bytes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "fixed_bytes"
version = "0.1.0"
authors = ["Eitan Seri-Levi <eitan@sigmaprime.io>"]
edition = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-primitives = { workspace = true }
safe_arith = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use alloy_primitives::{Address, FixedBytes};
use alloy_primitives::FixedBytes;
use safe_arith::SafeArith;

pub type Hash64 = alloy_primitives::B64;
pub type Hash256 = alloy_primitives::B256;
pub type Uint256 = alloy_primitives::U256;
pub type Address = alloy_primitives::Address;

pub trait FixedBytesExtended {
fn from_low_u64_be(value: u64) -> Self;
fn from_low_u64_le(value: u64) -> Self;
Expand All @@ -16,7 +21,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
let start_index = buffer
.len()
.safe_sub(bytes_to_copy)
.expect("byte_to_copy <= buffer.len()");
.expect("bytes_to_copy <= buffer.len()");
// Panic-free because start_index <= buffer.len()
// and bytes_to_copy <= value_bytes.len()
buffer
Expand All @@ -34,7 +39,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
let value_bytes = value.to_le_bytes();
let mut buffer = [0x0; N];
let bytes_to_copy = value_bytes.len().min(buffer.len());
// Panic-free because bytes_to_copy <= buffer.len()
// Panic-free because bytes_to_copy <= buffer.len(),
// and bytes_to_copy <= value_bytes.len()
buffer
.get_mut(..bytes_to_copy)
Expand All @@ -52,7 +57,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
}
}

impl FixedBytesExtended for Address {
impl FixedBytesExtended for alloy_primitives::Address {
fn from_low_u64_be(value: u64) -> Self {
FixedBytes::<20>::from_low_u64_be(value).into()
}
Expand Down
1 change: 1 addition & 0 deletions consensus/merkle_proof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = { workspace = true }
alloy-primitives = { workspace = true }
ethereum_hashing = { workspace = true }
safe_arith = { workspace = true }
fixed_bytes = { workspace = true }

[dev-dependencies]
quickcheck = { workspace = true }
Expand Down
57 changes: 3 additions & 54 deletions consensus/merkle_proof/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
use alloy_primitives::FixedBytes;
use ethereum_hashing::{hash, hash32_concat, ZERO_HASHES};
use safe_arith::{ArithError, SafeArith};
use safe_arith::ArithError;
use std::sync::LazyLock;

type H256 = alloy_primitives::B256;

pub trait FixedBytesExtended {
fn from_low_u64_be(value: u64) -> Self;
fn from_low_u64_le(value: u64) -> Self;
fn zero() -> Self;
}

impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
fn from_low_u64_be(value: u64) -> Self {
let value_bytes = value.to_be_bytes();
let mut buffer = [0x0; N];
let bytes_to_copy = value_bytes.len().min(buffer.len());
// Panic-free because bytes_to_copy <= buffer.len()
let start_index = buffer
.len()
.safe_sub(bytes_to_copy)
.expect("bytes_to_copy <= buffer.len()");
// Panic-free because start_index <= buffer.len()
// and bytes_to_copy <= value_bytes.len()
buffer
.get_mut(start_index..)
.expect("start_index <= buffer.len()")
.copy_from_slice(
value_bytes
.get(..bytes_to_copy)
.expect("bytes_to_copy <= value_byte.len()"),
);
Self::from(buffer)
}

fn from_low_u64_le(value: u64) -> Self {
let value_bytes = value.to_le_bytes();
let mut buffer = [0x0; N];
let bytes_to_copy = value_bytes.len().min(buffer.len());
// Panic-free because bytes_to_copy <= buffer.len()
// and bytes_to_copy <= value_bytes.len()
buffer
.get_mut(..bytes_to_copy)
.expect("bytes_to_copy <= buffer.len()")
.copy_from_slice(
value_bytes
.get(..bytes_to_copy)
.expect("bytes_to_copy <= value_byte.len()"),
);
Self::from(buffer)
}

fn zero() -> Self {
Self::ZERO
}
}
type H256 = fixed_bytes::Hash256;
pub use fixed_bytes::FixedBytesExtended;

const MAX_TREE_DEPTH: usize = 32;
const EMPTY_SLICE: &[H256] = &[];
Expand Down
1 change: 1 addition & 0 deletions consensus/swap_or_not_shuffle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ criterion = { workspace = true }
[dependencies]
alloy-primitives = { workspace = true }
ethereum_hashing = { workspace = true }
fixed_bytes = { workspace = true }

[features]
arbitrary = ["alloy-primitives/arbitrary"]
Expand Down
2 changes: 1 addition & 1 deletion consensus/swap_or_not_shuffle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ mod shuffle_list;
pub use compute_shuffled_index::compute_shuffled_index;
pub use shuffle_list::shuffle_list;

type Hash256 = alloy_primitives::B256;
type Hash256 = fixed_bytes::Hash256;
1 change: 1 addition & 0 deletions consensus/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ maplit = { workspace = true }
alloy-rlp = { version = "0.3.4", features = ["derive"] }
milhouse = { workspace = true }
rpds = { workspace = true }
fixed_bytes = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use self::committee_cache::get_active_validator_indices;
use crate::fixed_bytes::FixedBytesExtended;
use crate::historical_summary::HistoricalSummary;
use crate::test_utils::TestRandom;
use crate::FixedBytesExtended;
use crate::*;
use compare_fields::CompareFields;
use compare_fields_derive::CompareFields;
Expand Down
9 changes: 4 additions & 5 deletions consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub mod eth_spec;
pub mod execution_block_hash;
pub mod execution_payload;
pub mod execution_payload_header;
pub mod fixed_bytes;
pub mod fork;
pub mod fork_data;
pub mod fork_name;
Expand Down Expand Up @@ -170,7 +169,6 @@ pub use crate::execution_payload_header::{
ExecutionPayloadHeaderDeneb, ExecutionPayloadHeaderElectra, ExecutionPayloadHeaderRef,
ExecutionPayloadHeaderRefMut,
};
pub use crate::fixed_bytes::FixedBytesExtended;
pub use crate::fork::Fork;
pub use crate::fork_context::ForkContext;
pub use crate::fork_data::ForkData;
Expand Down Expand Up @@ -254,11 +252,12 @@ pub use crate::voluntary_exit::VoluntaryExit;
pub use crate::withdrawal::Withdrawal;
pub use crate::withdrawal_credentials::WithdrawalCredentials;
pub use crate::withdrawal_request::WithdrawalRequest;
pub use fixed_bytes::FixedBytesExtended;

pub type CommitteeIndex = u64;
pub type Hash256 = alloy_primitives::B256;
pub type Uint256 = alloy_primitives::U256;
pub type Address = alloy_primitives::Address;
pub type Hash256 = fixed_bytes::Hash256;
pub type Uint256 = fixed_bytes::Uint256;
pub type Address = fixed_bytes::Address;
pub type ForkVersion = [u8; 4];
pub type BLSFieldElement = Uint256;
pub type Blob<E> = FixedVector<u8, <E as EthSpec>::BytesPerBlob>;
Expand Down
1 change: 1 addition & 0 deletions crypto/bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ arbitrary = { workspace = true }
zeroize = { workspace = true }
blst = { version = "0.3.3", optional = true }
safe_arith = { workspace = true }
fixed_bytes = { workspace = true }

[features]
arbitrary = []
Expand Down
55 changes: 2 additions & 53 deletions crypto/bls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,70 +33,19 @@ mod zeroize_hash;

pub mod impls;

use alloy_primitives::FixedBytes;
pub use generic_public_key::{
INFINITY_PUBLIC_KEY, PUBLIC_KEY_BYTES_LEN, PUBLIC_KEY_UNCOMPRESSED_BYTES_LEN,
};
pub use generic_secret_key::SECRET_KEY_BYTES_LEN;
pub use generic_signature::{INFINITY_SIGNATURE, SIGNATURE_BYTES_LEN};
pub use get_withdrawal_credentials::get_withdrawal_credentials;
use safe_arith::SafeArith;
pub use zeroize_hash::ZeroizeHash;

#[cfg(feature = "supranational")]
use blst::BLST_ERROR as BlstError;

pub type Hash256 = alloy_primitives::B256;
pub trait FixedBytesExtended {
fn from_low_u64_be(value: u64) -> Self;
fn from_low_u64_le(value: u64) -> Self;
fn zero() -> Self;
}

impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
fn from_low_u64_be(value: u64) -> Self {
let value_bytes = value.to_be_bytes();
let mut buffer = [0x0; N];
let bytes_to_copy = value_bytes.len().min(buffer.len());
// Panic-free because bytes_to_copy <= buffer.len()
let start_index = buffer
.len()
.safe_sub(bytes_to_copy)
.expect("bytes_to_copy <= buffer.len()");
// Panic-free because start_index <= buffer.len()
// and bytes_to_copy <= value_bytes.len()
buffer
.get_mut(start_index..)
.expect("start_index <= buffer.len()")
.copy_from_slice(
value_bytes
.get(..bytes_to_copy)
.expect("bytes_to_copy <= value_byte.len()"),
);
Self::from(buffer)
}

fn from_low_u64_le(value: u64) -> Self {
let value_bytes = value.to_le_bytes();
let mut buffer = [0x0; N];
let bytes_to_copy = value_bytes.len().min(buffer.len());
// Panic-free because bytes_to_copy <= buffer.len(),
// and bytes_to_copy <= value_bytes.len()
buffer
.get_mut(..bytes_to_copy)
.expect("bytes_to_copy <= buffer.len()")
.copy_from_slice(
value_bytes
.get(..bytes_to_copy)
.expect("bytes_to_copy <= value_byte.len()"),
);
Self::from(buffer)
}

fn zero() -> Self {
Self::ZERO
}
}
pub type Hash256 = fixed_bytes::Hash256;
pub use fixed_bytes::FixedBytesExtended;

#[derive(Clone, Debug, PartialEq)]
pub enum Error {
Expand Down

0 comments on commit 5162316

Please sign in to comment.