-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ed3cb0
commit 3ef52b5
Showing
20 changed files
with
316 additions
and
48 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
imports_granularity = "Crate" |
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,2 @@ | ||
pub(crate) mod recover; | ||
|
||
pub(crate)mod recover; | ||
mod keccak256; |
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,7 +1,8 @@ | ||
use crate::network::specific::{FromBytesRepr, U256}; | ||
use crate::network::from_bytes_repr::{FromBytesRepr, Sanitized}; | ||
use crate::network::specific::U256; | ||
|
||
impl FromBytesRepr<Vec<u8>> for U256 { | ||
fn from_bytes_repr(bytes: Vec<u8>) -> Self { | ||
bytes.as_slice().into() | ||
bytes.sanitized().as_slice().into() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use crate::network::specific::{Bytes, VALUE_SIZE}; | ||
|
||
pub trait FromBytesRepr<T> { | ||
fn from_bytes_repr(bytes: T) -> Self; | ||
} | ||
|
||
pub trait Sanitized { | ||
fn sanitized(self) -> Self; | ||
} | ||
|
||
impl Sanitized for Bytes { | ||
fn sanitized(self) -> Self { | ||
if self.len() <= VALUE_SIZE { | ||
return self; | ||
} | ||
|
||
let index = self.len().max(VALUE_SIZE) - VALUE_SIZE; | ||
let remainder = &self[0..index]; | ||
|
||
if remainder != vec![0; index] { | ||
panic!("Number to big: {:?} digits", self.len()) | ||
} | ||
|
||
self[index..].into() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::network::from_bytes_repr::FromBytesRepr; | ||
use crate::network::specific::{U256, VALUE_SIZE}; | ||
|
||
#[cfg(feature = "network_radix")] | ||
use crate::network::radix::u256_ext::U256Ext; | ||
|
||
#[test] | ||
fn test_from_bytes_repr_single() { | ||
let vec = vec![1]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(1u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_double() { | ||
let vec = vec![1, 2]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(258u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_simple() { | ||
let vec = vec![1, 2, 3]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(66051u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_bigger() { | ||
let vec = vec![101, 202, 255]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(6671103u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_empty() { | ||
let result = U256::from_bytes_repr(Vec::new()); | ||
|
||
assert_eq!(result, U256::from(0u8)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_trailing_zeroes() { | ||
let vec = vec![1, 2, 3, 0]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(16909056u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_leading_zeroes() { | ||
let vec = vec![0, 1, 2, 3]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(66051u32)); | ||
} | ||
|
||
#[allow(clippy::legacy_numeric_constants)] | ||
#[test] | ||
fn test_from_bytes_repr_max() { | ||
let vec = vec![255; VALUE_SIZE]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::max_value()); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_min() { | ||
let vec = vec![0; VALUE_SIZE]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(0u8)); | ||
} | ||
|
||
#[should_panic(expected = "Number to big")] | ||
#[test] | ||
fn test_from_bytes_repr_too_long() { | ||
let x = VALUE_SIZE as u8 + 1; | ||
let vec = (1..=x).collect(); | ||
|
||
U256::from_bytes_repr(vec); | ||
} | ||
|
||
#[allow(clippy::legacy_numeric_constants)] | ||
#[test] | ||
fn test_from_bytes_repr_too_long_but_zeroes() { | ||
let mut vec = vec![255; VALUE_SIZE + 1]; | ||
vec[0] = 0; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::max_value()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::network::from_bytes_repr::{FromBytesRepr, Sanitized}; | ||
use crate::network::specific::{U256}; | ||
|
||
impl FromBytesRepr<Vec<u8>> for U256 { | ||
fn from_bytes_repr(bytes: Vec<u8>) -> Self { | ||
match bytes.len() { | ||
0 => U256::ZERO, | ||
1 => U256::from(bytes[0]), | ||
_ => { | ||
|
||
// TODO: make it cheaper | ||
let mut bytes_le = bytes.sanitized(); | ||
bytes_le.reverse(); | ||
|
||
U256::from_le_bytes(bytes_le.as_slice()) | ||
}} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::network::{error::Error, specific::NetworkSpecific}; | ||
|
||
mod from_bytes_repr; | ||
pub mod u256_ext; | ||
|
||
pub struct Radix; | ||
|
||
impl NetworkSpecific for Radix { | ||
type BytesRepr = Vec<u8>; | ||
type ValueRepr = radix_common::math::bnum_integer::U256; | ||
type _Self = Self; | ||
|
||
const VALUE_SIZE: usize = 32; | ||
|
||
fn print(_text: String) { | ||
#[cfg(all(not(test), feature = "print_debug"))] | ||
{ | ||
println!("{}", _text); | ||
} | ||
|
||
#[cfg(test)] | ||
{ | ||
println!("{}", _text); | ||
} | ||
} | ||
|
||
fn revert(error: Error) -> ! { | ||
#[cfg(not(test))] | ||
{ | ||
panic!("{}", error) | ||
} | ||
|
||
#[cfg(test)] | ||
{ | ||
panic!("{}", error) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
use crate::network::specific::U256; | ||
|
||
pub trait U256Ext { | ||
fn max_value() -> Self; | ||
} | ||
|
||
impl U256Ext for U256 { | ||
fn max_value() -> Self { | ||
Self::MAX | ||
} | ||
} |
Oops, something went wrong.