-
Notifications
You must be signed in to change notification settings - Fork 87
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
Avoid possible truncation of higher bits #619
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00cfd8f
Avoid possible truncation of higher bits
xgreenx fcab2e0
Update CHANGELOG.md
xgreenx e2348a7
`UtxoId` uses `TxId` + ``u8` - the index in the outputs array. So it …
xgreenx 758df7b
Merge branch 'master' into feature/avoid-cast-where-possible
xgreenx ea0324d
Apply comments
xgreenx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ pub enum Bit { | |
} | ||
|
||
trait GetBit { | ||
fn get_bit(&self, bit_index: usize) -> Option<Bit>; | ||
fn get_bit(&self, bit_index: u32) -> Option<Bit>; | ||
} | ||
|
||
impl GetBit for u8 { | ||
fn get_bit(&self, bit_index: usize) -> Option<Bit> { | ||
fn get_bit(&self, bit_index: u32) -> Option<Bit> { | ||
if bit_index < 8 { | ||
let mask = 1 << (7 - bit_index); | ||
let bit = self & mask; | ||
|
@@ -24,21 +24,21 @@ impl GetBit for u8 { | |
} | ||
|
||
pub trait Msb { | ||
fn get_bit_at_index_from_msb(&self, index: usize) -> Option<Bit>; | ||
fn common_prefix_count(&self, other: &Self) -> usize; | ||
fn get_bit_at_index_from_msb(&self, index: u32) -> Option<Bit>; | ||
fn common_prefix_count(&self, other: &Self) -> u32; | ||
} | ||
|
||
impl<const N: usize> Msb for [u8; N] { | ||
fn get_bit_at_index_from_msb(&self, index: usize) -> Option<Bit> { | ||
fn get_bit_at_index_from_msb(&self, index: u32) -> Option<Bit> { | ||
// The byte that contains the bit | ||
let byte_index = index / 8; | ||
// The bit within the containing byte | ||
let byte_bit_index = index % 8; | ||
self.get(byte_index) | ||
self.get(byte_index as usize) | ||
.and_then(|byte| byte.get_bit(byte_bit_index)) | ||
} | ||
|
||
fn common_prefix_count(&self, other: &Self) -> usize { | ||
fn common_prefix_count(&self, other: &Self) -> u32 { | ||
let mut count = 0; | ||
for (byte1, byte2) in self.iter().zip(other.iter()) { | ||
// For each pair of bytes, compute the similarity of each byte using | ||
|
@@ -49,10 +49,11 @@ impl<const N: usize> Msb for [u8; N] { | |
break | ||
} | ||
} | ||
count as usize | ||
count | ||
} | ||
} | ||
|
||
#[allow(clippy::cast_possible_truncation)] | ||
#[cfg(test)] | ||
mod test { | ||
use crate::common::{ | ||
|
@@ -66,7 +67,7 @@ mod test { | |
|
||
#[test] | ||
fn test_msb_for_bytes_1() { | ||
const NUM_BITS: usize = size_of::<Bytes1>() * 8; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the |
||
const NUM_BITS: u32 = size_of::<Bytes1>() as u32 * 8; | ||
|
||
let bytes: Bytes1 = [0b10101010]; | ||
let expected_n = u8::from_be_bytes(bytes); | ||
|
@@ -83,7 +84,7 @@ mod test { | |
|
||
#[test] | ||
fn test_msb_for_bytes_2() { | ||
const NUM_BITS: usize = size_of::<Bytes2>() * 8; | ||
const NUM_BITS: u32 = size_of::<Bytes2>() as u32 * 8; | ||
|
||
let bytes: Bytes2 = [0b10101010, 0b10101010]; | ||
let expected_n = u16::from_be_bytes(bytes); | ||
|
@@ -100,7 +101,7 @@ mod test { | |
|
||
#[test] | ||
fn test_msb_for_bytes_4() { | ||
const NUM_BITS: usize = size_of::<Bytes4>() * 8; | ||
const NUM_BITS: u32 = size_of::<Bytes4>() as u32 * 8; | ||
|
||
let bytes: Bytes4 = [0b10101010, 0b10101010, 0b10101010, 0b10101010]; | ||
let expected_n = u32::from_be_bytes(bytes); | ||
|
@@ -117,7 +118,7 @@ mod test { | |
|
||
#[test] | ||
fn test_msb_for_bytes_8() { | ||
const NUM_BITS: usize = size_of::<Bytes8>() * 8; | ||
const NUM_BITS: u32 = size_of::<Bytes8>() as u32 * 8; | ||
|
||
let bytes: Bytes8 = [ | ||
0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b10101010, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![no_std] | ||
#![deny(clippy::cast_possible_truncation)] | ||
#![deny(unsafe_code)] | ||
#![deny(unused_crate_dependencies)] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#![allow(clippy::cast_possible_truncation)] | ||
use super::PREDICATE_PARAMS; | ||
|
||
use fuel_crypto::{ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes to
fuel-merkle
necessary? It feels like we're changing library logic to fit business logic. A library should promise correctness under all targets it supports, including all 32 bit and 64 bit targets. If it has internal problems with truncation, that should be fixed in the library. But this change appears to be changing the library interface to accommodate changes to external libraries.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a private trait, so it is an internal detail of how Merkle tree works=)