Skip to content

Commit

Permalink
Update dependencies and bench against ruint
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Rodrigues Lordello committed Aug 29, 2023
1 parent d8a2c42 commit 211f35a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ as a reference.
In order to start fuzzing:

```sh
cargo install afl
cargo install --force cargo-afl
cargo run -p ethnum-fuzz --bin init target/fuzz
cargo afl build -p ethnum-fuzz --bin fuzz
cargo afl fuzz -i target/fuzz/in -o target/fuzz/out target/debug/fuzz
Expand Down
3 changes: 2 additions & 1 deletion bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ llvm-intrinsics = ["ethnum/llvm-intrinsics"]

[dependencies]
ethnum = { path = ".." }
criterion = "0.4"
criterion = "0.5"
primitive-types = { version = "0.12", optional = true }
ruint = { version = "1", optional = true }
26 changes: 19 additions & 7 deletions bench/benches/num.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
#[cfg(not(feature = "primitive-types"))]
#[cfg(not(any(feature = "primitive-types", feature = "ruint")))]
use ethnum::U256;
#[cfg(feature = "primitive-types")]
use primitive_types::U256;
#[cfg(feature = "ruint")]
use ruint::aliases::U256;

#[cfg(not(feature = "ruint"))]
type Shift = u32;
#[cfg(feature = "ruint")]
type Shift = usize;

#[inline(always)]
const fn sh(n: Shift) -> Shift {
n
}

fn arithmetic(c: &mut Criterion) {
let nums = [
(U256::from(0x00017eb02a11f4a9443abc5058e1c2c2_u128) << 128_u32)
(U256::from(0x00017eb02a11f4a9443abc5058e1c2c2_u128) << sh(128))
+ U256::from(0x3540ba08c848a6eb3a1e1415b0000000_u128),
(U256::from(0x0000000007a5c694c4fb15944398653f_u128) << 128_u32)
(U256::from(0x0000000007a5c694c4fb15944398653f_u128) << sh(128))
+ U256::from(0x724f5c482676cba8ea4e698d75210fe0_u128),
(U256::from(0x0000000000000000024e9ffa7e0bba23_u128) << 128_u32)
(U256::from(0x0000000000000000024e9ffa7e0bba23_u128) << sh(128))
+ U256::from(0x451a0df036962a5b327f93054732380a_u128),
(U256::from(0x0000000000000000000000000647a49c_u128) << 128_u32)
(U256::from(0x0000000000000000000000000647a49c_u128) << sh(128))
+ U256::from(0xf1055ae531427db60296077b1863d256_u128),
U256::from(0x000f4187ab979b49ad893d525a13a5aa_u128),
U256::from(0x000000000edac72a3447ed506fccc42c_u128),
Expand Down Expand Up @@ -61,7 +73,7 @@ fn arithmetic(c: &mut Criterion) {
b.iter(|| black_box(nums[0]) - black_box(nums[1]))
});

for (name, shift) in [("short", 21_u32), ("long", 176_u32)] {
for (name, shift) in [("short", sh(21)), ("long", sh(176))] {
c.bench_with_input(BenchmarkId::new("U256::shl", name), &shift, |b, &s| {
b.iter(|| black_box(nums[0]) << black_box(s))
});
Expand Down Expand Up @@ -90,7 +102,7 @@ fn arithmetic(c: &mut Criterion) {
b.iter(|| black_box(x).leading_zeros())
});

#[cfg(not(feature = "primitive-types"))]
#[cfg(not(any(feature = "primitive-types", feature = "ruint")))]
c.bench_with_input(
BenchmarkId::new("U256::cttz", name(x)),
&x.swap_bytes(),
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
llvm-intrinsics = ["ethnum/llvm-intrinsics"]

[dependencies]
afl = "0.12"
afl = "0.14"
arbitrary = { version = "1", features = ["derive"] }
ethnum = { path = ".." }
num = "0.4"
6 changes: 5 additions & 1 deletion intrinsics/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.replace("dereferenceable(16)", "dereferenceable(32)")
// TODO(nlordell): Figure out why Clang doesn't like these
.replace("mustprogress ", "")
.replace("noundef ", "");
.replace("noundef ", "")
.replace("memory(argmem: readwrite) ", "")
.replace("memory(argmem: read) ", "")
.replace("memory(none) ", "")
.replace(", !!1", "");
let path = out_dir.join("intrinsics.ll");
fs::write(&path, source)?;
path
Expand Down
5 changes: 1 addition & 4 deletions macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ impl FromLiteral for Uint {

fn mul_radix(&self, radix: u32) -> Option<Self> {
let radix = radix as u128;
let (lh, ll) = (
(self.lo >> 64) as u128,
(self.lo & (u64::MAX as u128)) as u128,
);
let (lh, ll) = (self.lo >> 64, self.lo & (u64::MAX as u128));

let llx = ll * radix;
let lhx = lh * radix;
Expand Down

0 comments on commit 211f35a

Please sign in to comment.