Skip to content

Commit

Permalink
Merge pull request #2788 from Conflux-Chain/v2.0-stable-merge
Browse files Browse the repository at this point in the history
Release v2.3.4.
  • Loading branch information
peilun-conflux authored Jan 18, 2024
2 parents 89f0ce5 + 76732b9 commit b9befd1
Show file tree
Hide file tree
Showing 95 changed files with 7,820 additions and 2,233 deletions.
270 changes: 160 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "conflux"
version = "2.3.3"
version = "2.3.4"
edition = "2018"
build = "build.rs"

Expand Down Expand Up @@ -104,6 +104,9 @@ sqlite3-sys = { git = "https://github.com/Conflux-Chain/sqlite3-sys.git", rev =
[profile.test]
debug-assertions = true

[profile.test.package]
parity-secp256k1 = { opt-level = 3 }

[profile.bench]
debug-assertions = true
overflow-checks = true
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Conflux-rust is a Rust-based implementation of the Conflux protocol. It is fast and
reliable. Please follow the [Conflux
Documentation](https://doc.confluxnetwork.org/) to
[build](https://doc.confluxnetwork.org/docs/general/run-a-node/compiling-conflux-client)
[build](https://doc.confluxnetwork.org/docs/general/run-a-node/advanced-topics/compiling-conflux-client)
and
[run](https://doc.confluxnetwork.org/docs/general/run-a-node/running-full-node)
[run](https://doc.confluxnetwork.org/docs/general/run-a-node/)
Conflux.

## Contribution
Expand All @@ -20,7 +20,7 @@ significant changes to the Conflux protocol, please submit a
## Unit Tests and Integration Tests

Unit tests come together with the Rust code. They can be invoked via `cargo test --release --all`. See the
[Getting Started](https://doc.confluxnetwork.org/docs/general/run-a-node/running-full-node)
[Getting Started](https://doc.confluxnetwork.org/docs/general/run-a-node/)
page for more information. Integration tests are Python test scripts with the
`_test.py` suffix in the `tests` directory. To run these tests, first compile Conflux
in _release_ mode using `cargo build --release`. Then, you can run all
Expand Down
16 changes: 16 additions & 0 deletions cfx_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@ version = "0.1.0"
[dependencies]
num = "0.2"
cfx-types = { path = "../cfx_types" }
unroll = "0.1.5"
typenum = "1.17.0"

[dev-dependencies]
rand = "0.8.0"
criterion = "0.3.0"
static_assertions = "1.1"
rand_xorshift = "0.3"

[[bench]]
name = "bench"
harness = false

[[bench]]
name = "basic"
harness = false
94 changes: 94 additions & 0 deletions cfx_math/benches/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
extern crate cfx_types;
extern crate criterion;
extern crate rand;
extern crate rand_xorshift;

use cfx_types::U256;
use criterion::{criterion_group, criterion_main, Criterion};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

fn bench_random_input(c: &mut Criterion) {
c.bench_function("u64 input gen", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u64>());
});

c.bench_function("u128 input gen", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>());
});

c.bench_function("u256 input gen", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| U256(rng.gen()));
});
}

fn bench_u256_basic_op(c: &mut Criterion) {
c.bench_function("u256 / u256", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| U256(rng.gen()) / (U256(rng.gen()) >> 64));
});

c.bench_function("u256 / u64", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| U256(rng.gen()) / rng.gen::<u64>());
});

c.bench_function("u256 small quo (4 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| U256(rng.gen()) / (U256(rng.gen()) >> 4));
});

c.bench_function("u256 small quo (2 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| U256(rng.gen()) / (U256(rng.gen()) >> 2));
});

c.bench_function("u256 small quo (0 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| {
let a = U256(rng.gen());
(a >> 1) / a
})
});
}

fn bench_u128_basic_op(c: &mut Criterion) {
c.bench_function("u128 / u128", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>() / (rng.gen::<u128>() >> 32));
});

c.bench_function("u128 / u64", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>() / rng.gen::<u64>() as u128);
});

c.bench_function("u128 small quo (4 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>() / (rng.gen::<u128>() >> 4));
});

c.bench_function("u128 small quo (2 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>() / (rng.gen::<u128>() >> 2));
});

c.bench_function("u128 small quo (0 bit)", move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| {
let a = rng.gen::<u128>();
(a >> 1) / a
})
});
}

criterion_group!(
benches,
bench_random_input,
bench_u128_basic_op,
bench_u256_basic_op
);
criterion_main!(benches);
107 changes: 107 additions & 0 deletions cfx_math/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
extern crate cfx_math;
extern crate cfx_types;
extern crate criterion;
extern crate num;
extern crate rand;
extern crate rand_xorshift;
extern crate typenum;

use cfx_math::{
nth_inv_root,
nth_root::{nth_root, RootDegree, RootInvParams},
};
use cfx_types::U256;
use criterion::{criterion_group, criterion_main, Criterion};
use num::integer::Roots;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

fn bench_nth_root<N: RootDegree>(c: &mut Criterion) {
c.bench_function(&format!("u64 {}-th root", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| nth_root::<N, u64>(rng.gen()));
});

c.bench_function(&format!("u64 {}-th baseline", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u64>().nth_root(N::U32));
});

c.bench_function(&format!("u128 {}-th root", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| nth_root::<N, u128>(rng.gen()));
});
c.bench_function(&format!("u128 {}-th baseline", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| rng.gen::<u128>().nth_root(N::U32));
});

c.bench_function(&format!("u192 {}-th root", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| nth_root::<N, U256>(U256(rng.gen()) >> 64));
});

c.bench_function(&format!("u256 {}-th root", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| nth_root::<N, U256>(U256(rng.gen())));
});
}

fn bench_nth_inv_root<N: RootDegree>(c: &mut Criterion)
where
(N, typenum::U10): RootInvParams,
(N, typenum::U15): RootInvParams,
(N, typenum::U20): RootInvParams,
{
c.bench_function(&format!("{}-th inv root (10 bit)", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| {
nth_inv_root::<N, typenum::U10>(U256::from(rng.gen::<u64>()))
});
});

c.bench_function(&format!("{}-th inv root (15 bit)", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| {
nth_inv_root::<N, typenum::U15>(U256::from(rng.gen::<u64>()))
});
});

c.bench_function(&format!("{}-th inv root (20 bit)", N::USIZE), move |b| {
let mut rng = XorShiftRng::from_seed([0u8; 16]);
b.iter(|| {
nth_inv_root::<N, typenum::U20>(U256::from(rng.gen::<u64>()))
});
});
}

fn bench_multiple_nth_root(c: &mut Criterion) {
bench_nth_root::<typenum::U2>(c);
bench_nth_root::<typenum::U3>(c);
bench_nth_root::<typenum::U4>(c);
bench_nth_root::<typenum::U5>(c);
bench_nth_root::<typenum::U8>(c);
bench_nth_root::<typenum::U10>(c);
bench_nth_root::<typenum::U12>(c);

// c.bench_function(&format!("u256 another sqrt"), move |b| {
// b.iter(|| cfx_math::sqrt_u256(U256(rand::random::<[u64; 4]>())));
// });
}

fn bench_multiple_nth_inv_root(c: &mut Criterion) {
bench_nth_inv_root::<typenum::U2>(c);
bench_nth_inv_root::<typenum::U3>(c);
bench_nth_inv_root::<typenum::U4>(c);
bench_nth_inv_root::<typenum::U5>(c);
bench_nth_inv_root::<typenum::U8>(c);
bench_nth_inv_root::<typenum::U10>(c);
bench_nth_inv_root::<typenum::U12>(c);
}

criterion_group!(
benches,
bench_multiple_nth_inv_root,
bench_multiple_nth_root
);
criterion_main!(benches);
8 changes: 8 additions & 0 deletions cfx_math/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
extern crate cfx_types;
extern crate num;
#[cfg(test)]
extern crate static_assertions;
extern crate typenum;
extern crate unroll;

pub mod nth_root;

pub use nth_root::{nth_inv_root, nth_root};

use cfx_types::U256;
use num::integer::Roots;
Expand Down
Loading

0 comments on commit b9befd1

Please sign in to comment.