Skip to content

Commit

Permalink
feature options 'num-integer-backend' and 'rug-backend' for mithril-c…
Browse files Browse the repository at this point in the history
…ommon and mithril-client crates
  • Loading branch information
Mr-Leshiy committed Sep 2, 2024
1 parent cbd65d0 commit d282bba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
14 changes: 10 additions & 4 deletions mithril-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ uuid = { version = "1.7.0", features = ["v4"] }
zstd = { version = "0.13.0", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
mithril-common = { path = "../mithril-common", version = "=0.4", features = [
mithril-common = { path = "../mithril-common", version = "=0.4", default-features = false, features = [
"fs",
] }

[target.'cfg(target_family = "wasm")'.dependencies]
getrandom = { version = "0.2.12", features = ["js"] }
mithril-common = { path = "../mithril-common", version = "=0.4" }
mithril-common = { path = "../mithril-common", version = "=0.4", default-features = false }
reqwest = { version = "0.12.4", default-features = false, features = [
"charset",
"http2",
Expand All @@ -68,7 +68,7 @@ reqwest = { version = "0.12.4", default-features = false, features = [
[dev-dependencies]
httpmock = "0.7.0"
indicatif = { version = "0.17.7", features = ["tokio"] }
mithril-common = { path = "../mithril-common", version = "=0.4", features = [
mithril-common = { path = "../mithril-common", version = "=0.4", default-features = false, features = [
"random",
"test_tools",
] }
Expand All @@ -81,7 +81,7 @@ warp = "0.3.6"

[features]
# Include native-tls in reqwest by default
default = ["native-tls"]
default = ["native-tls", "rug-backend"]

# Full feature set
full = ["fs"]
Expand All @@ -102,6 +102,12 @@ rustls-tls-manual-roots = ["reqwest/rustls-tls-manual-roots"]
rustls-tls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
rustls-tls-native-roots = ["reqwest/rustls-tls-native-roots"]

# Enables `rug-backend` features for `mithril-common` dependency
rug-backend = ["mithril-common/rug-backend"]
# Enables `num-integer-backend` features for `mithril-common` dependency
# by default it's `rug-backend`
num-integer-backend = ["mithril-common/num-integer-backend"]

[package.metadata.docs.rs]
all-features = true
# enable unstable features in the documentation
Expand Down
11 changes: 11 additions & 0 deletions mithril-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ async fn main() -> mithril_client::MithrilResult<()> {
}
```

## Different arithmetic libraries

Under the hood mithril client could use different arithmetic libraries:

- [rug](https://crates.io/crates/rug)
- [num](https://crates.io/crates/num)

You can switch beetwen them by using `rug-backend` and `num-integer-backend` respectively.
For Windows and WASM target platforms only `num-integer-backend` is available,
for others `rug-backend` is a default option.

## Getting Help

First, check our [Developer documentation](https://mithril.network/doc/manual/developer-docs/nodes/mithril-client-library).
Expand Down
23 changes: 8 additions & 15 deletions mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ kes-summed-ed25519 = { version = "0.2.1", features = [
"serde_enabled",
"sk_clone_enabled",
] }
mithril-stm = { path = "../mithril-stm", version = "0.3", default-features = false }
nom = "7.1.3"
pallas-addresses = { version = "0.29.0", optional = true }
pallas-codec = { version = "0.29.0", optional = true }
Expand All @@ -73,21 +74,7 @@ typetag = "0.2.15"
walkdir = "2.4.0"
warp = { version = "0.3.6", optional = true }

[target.'cfg(target_family = "unix")'.dependencies]
# only unix supports the default rug backend
mithril-stm = { path = "../mithril-stm", version = "0.3" }

[target.'cfg(windows)'.dependencies]
# Windows doesn't support rug backend, fallback to num-integer
mithril-stm = { path = "../mithril-stm", version = "0.3", default-features = false, features = [
"num-integer-backend",
] }

[target.'cfg(target_family = "wasm")'.dependencies]
# WASM doesn't support rug backend, fallback to num-integer
mithril-stm = { path = "../mithril-stm", version = "0.3", default-features = false, features = [
"num-integer-backend",
] }
wasm-bindgen = "0.2.90"

[dev-dependencies]
Expand All @@ -105,7 +92,7 @@ tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "time"] }
mithril-build-script = { path = "../internal/mithril-build-script", version = "=0.2" }

[features]
default = []
default = ["rug-backend"]

# Full feature set
full = ["random", "fs", "test_tools"]
Expand All @@ -121,6 +108,12 @@ fs = [
"dep:pallas-traverse",
]

# Enables `rug-backend` features for `mithril-stm` dependency
rug-backend = ["mithril-stm/rug-backend"]
# Enables `num-integer-backend` features for `mithril-stm` dependency
# by default it's `rug-backend`
num-integer-backend = ["mithril-stm/num-integer-backend"]

# Disable signer certification, to be used only for tests
allow_skip_signer_certification = []
# Enable all tests tools
Expand Down
17 changes: 13 additions & 4 deletions mithril-stm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@ blake2 = "0.10.6"
# Enforce blst portable feature for runtime detection of Intel ADX instruction set.
blst = { version = "0.3.11", features = ["portable"] }
digest = { version = "0.10.7", features = ["alloc"] }
num-bigint = { version = "0.4.4", optional = true }
num-rational = { version = "0.4.1", optional = true }
num-traits = { version = "0.2.16", optional = true }
rand_core = "0.6.4"
rayon = "1.8.0"
rug = { version = "1.22.0", optional = true }
serde = { version = "1.0.188", features = ["rc", "derive"] }
thiserror = "1.0.49"

[target.'cfg(not(any(target_family = "wasm", windows)))'.dependencies]
# only unix supports the rug backend
rug = { version = "1.22.0", optional = true }
num-bigint = { version = "0.4.4", optional = true }
num-rational = { version = "0.4.1", optional = true }
num-traits = { version = "0.2.16", optional = true }

[target.'cfg(any(target_family = "wasm", windows))'.dependencies]
# WASM and Windows don't support rug backend, fallback to num-integer only
num-bigint = { version = "0.4.4" }
num-rational = { version = "0.4.1" }
num-traits = { version = "0.2.16" }

[dev-dependencies]
bincode = "1.3.3"
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
10 changes: 5 additions & 5 deletions mithril-stm/src/eligibility_check.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::stm::Stake;
#[cfg(feature = "num-integer-backend")]
#[cfg(any(feature = "num-integer-backend", target_family = "wasm", windows))]
use {
num_bigint::{BigInt, Sign},
num_rational::Ratio,
num_traits::{One, Signed},
std::ops::Neg,
};

#[cfg(feature = "num-integer-backend")]
#[cfg(any(feature = "num-integer-backend", target_family = "wasm", windows))]
/// Checks that ev is successful in the lottery. In particular, it compares the output of `phi`
/// (a real) to the output of `ev` (a hash). It uses the same technique used in the
/// [Cardano ledger](https://github.com/input-output-hk/cardano-ledger/). In particular,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) fn ev_lt_phi(phi_f: f64, ev: [u8; 64], stake: Stake, total_stake: Sta
taylor_comparison(1000, q, x)
}

#[cfg(feature = "num-integer-backend")]
#[cfg(any(feature = "num-integer-backend", target_family = "wasm", windows))]
/// Checks if cmp < exp(x). Uses error approximation for an early stop. Whenever the value being
/// compared, `cmp`, is smaller (or greater) than the current approximation minus an `error_term`
/// (plus an `error_term` respectively), then we stop approximating. The choice of the `error_term`
Expand Down Expand Up @@ -82,7 +82,7 @@ fn taylor_comparison(bound: usize, cmp: Ratio<BigInt>, x: Ratio<BigInt>) -> bool
false
}

#[cfg(not(feature = "num-integer-backend"))]
#[cfg(not(any(feature = "num-integer-backend", target_family = "wasm", windows)))]
/// The crate `rug` has sufficient optimizations to not require a taylor approximation with early
/// stop. The difference between the current implementation and the one using the optimization
/// above is around 10% faster. We perform the computations with 117 significant bits of
Expand Down Expand Up @@ -146,7 +146,7 @@ mod tests {
assert_eq!(quick_result, result);
}

#[cfg(feature = "num-integer-backend")]
#[cfg(any(feature = "num-integer-backend", target_family = "wasm", windows))]
#[test]
/// Checking the early break of Taylor computation
fn early_break_taylor(
Expand Down

0 comments on commit d282bba

Please sign in to comment.