Skip to content

Commit 7f54ab8

Browse files
committed
use safe arith
1 parent 0c62d24 commit 7f54ab8

File tree

1 file changed

+22
-19
lines changed
  • consensus/state_processing/src/per_block_processing/eip4844

1 file changed

+22
-19
lines changed

consensus/state_processing/src/per_block_processing/eip4844/eip4844.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::BlockProcessingError;
22
use eth2_hashing::hash_fixed;
33
use itertools::{EitherOrBoth, Itertools};
4+
use safe_arith::SafeArith;
45
use ssz::Decode;
56
use ssz_types::VariableList;
67
use types::consts::eip4844::{BLOB_TX_TYPE, VERSIONED_HASH_VERSION_KZG};
@@ -39,7 +40,7 @@ pub fn verify_kzg_commitments_against_transactions<T: EthSpec>(
3940
})
4041
.map(|tx| tx_peek_blob_versioned_hashes::<T>(tx));
4142

42-
itertools::process_results(nested_iter, |mut iter| {
43+
itertools::process_results(nested_iter, |iter| {
4344
let zipped_iter = iter
4445
.flatten()
4546
// Need to use `itertools::zip_longest` here because just zipping hides if one iter is shorter
@@ -52,12 +53,12 @@ pub fn verify_kzg_commitments_against_transactions<T: EthSpec>(
5253
// commitments in the block.
5354
EitherOrBoth::Left(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
5455
commitments_processed_in_block: index,
55-
commitments_processed_in_transactions: index + 1,
56+
commitments_processed_in_transactions: index.safe_add(1)?,
5657
}),
5758
// The number of commitments in the block exceeds the number of versioned hashes
5859
// in the blob transactions.
5960
EitherOrBoth::Right(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
60-
commitments_processed_in_block: index + 1,
61+
commitments_processed_in_block: index.safe_add(1)?,
6162
commitments_processed_in_transactions: index,
6263
}),
6364
});
@@ -78,35 +79,37 @@ fn tx_peek_blob_versioned_hashes<T: EthSpec>(
7879
BlockProcessingError,
7980
> {
8081
let tx_len = opaque_tx.len();
81-
let message_offset = 1 + u32::from_ssz_bytes(opaque_tx.get(1..5).ok_or(
82+
let message_offset = 1.safe_add(u32::from_ssz_bytes(opaque_tx.get(1..5).ok_or(
8283
BlockProcessingError::BlobVersionHashIndexOutOfBounds {
8384
length: tx_len,
8485
index: 5,
8586
},
86-
)?)?;
87+
)?)?)?;
8788

8889
let message_offset_usize = message_offset as usize;
8990

90-
// field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 = 156
91-
let blob_versioned_hashes_offset = message_offset
92-
+ u32::from_ssz_bytes(
93-
opaque_tx
94-
.get((message_offset_usize + 156)..(message_offset_usize + 160))
95-
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
96-
length: tx_len,
97-
index: 160,
98-
})?,
99-
)?;
91+
// field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 + 32 = 188
92+
let blob_versioned_hashes_offset = message_offset.safe_add(u32::from_ssz_bytes(
93+
opaque_tx
94+
.get(message_offset_usize.safe_add(188)?..message_offset_usize.safe_add(192)?)
95+
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
96+
length: tx_len,
97+
index: message_offset_usize.safe_add(192)?,
98+
})?,
99+
)?)?;
100100

101-
let num_hashes = (tx_len - blob_versioned_hashes_offset as usize) / 32;
101+
let num_hashes = tx_len
102+
.safe_sub(blob_versioned_hashes_offset as usize)?
103+
.safe_div(32)?;
102104

103105
Ok((0..num_hashes).into_iter().map(move |i| {
104-
let next_version_hash_index = blob_versioned_hashes_offset as usize + (i * 32);
106+
let next_version_hash_index =
107+
(blob_versioned_hashes_offset as usize).safe_add(i.safe_mul(32)?)?;
105108
let bytes = opaque_tx
106-
.get(next_version_hash_index..next_version_hash_index + 32)
109+
.get(next_version_hash_index..next_version_hash_index.safe_add(32)?)
107110
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
108111
length: tx_len,
109-
index: next_version_hash_index as usize + 32,
112+
index: (next_version_hash_index as usize).safe_add(32)?,
110113
})?;
111114
Ok(VersionedHash::from_slice(bytes))
112115
}))

0 commit comments

Comments
 (0)