From 36029e6c9614de0d78a0183d6cdbe8cde94cd62e Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Wed, 31 Jan 2024 16:06:20 -0800 Subject: [PATCH 1/5] Make std optional This feature gates the entire crate, but works --- Cargo.toml | 33 ++++++++++++++++++++++----------- src/batch.rs | 1 - src/lib.rs | 35 +++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dad0cb5..4abc0cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,19 +9,23 @@ description = "decaf377-rdsa is a randomizable signature scheme using the decaf3 repository = "https://github.com/penumbra-zone/decaf377-rdsa" [dependencies] -blake2b_simd = "0.5" +# No Alloc, No Std +blake2b_simd = { version = "0.5", default-features = false } byteorder = "1.3" -decaf377 = { version = "0.5", default-features = false } -digest = "0.9" -rand_core = "0.6" -serde = { version = "1", optional = true, features = ["derive"] } +cfg-if = "1.0" +decaf377 = { git = "https://github.com/penumbra-zone/decaf377", tag = "v0.8.0", default-features = false } +digest = { version = "0.9", default-features = false } +rand_core = { version = "0.6", default-features = false } thiserror = "1.0" -ark-serialize = "0.4" -ark-ff = { version = "0.4", default-features = false } -hex = "0.4" +hex = { version = "0.4", default-features = false } +# Only to satisfy Cargo +zeroize = { version = "1.7", default-features = false } +# Alloc, No Std +ark-ff = { version = "0.4", optional = true, default-features = false } +ark-serialize = { version = "0.4", optional = true } +# Std +serde = { version = "1", optional = true, features = ["derive"] } -# Frost deps -# TODO: make optional ? use published versions? [dev-dependencies] bincode = "1" criterion = "0.3" @@ -37,5 +41,12 @@ harness = false [features] default = ["serde", "std"] -std = ["ark-ff/std"] +alloc = ["ark-ff", "ark-serialize"] +std = ["alloc", "ark-ff/std", "blake2b_simd/std", "byteorder/std", "decaf377/arkworks", "digest/std", "hex/std", "rand_core/std"] parallel = ["ark-ff/parallel", "decaf377/parallel"] + +# Create profile for running checks in CI that are mostly "release" mode, +# but also checking the `debug_assert `lines. +[profile.ci] +inherits = "release" +debug-assertions = true diff --git a/src/batch.rs b/src/batch.rs index cf389aa..46cef1f 100644 --- a/src/batch.rs +++ b/src/batch.rs @@ -10,7 +10,6 @@ use std::convert::TryFrom; -use ark_ff::Zero; use decaf377::{Element, FieldExt, Fr}; use rand_core::{CryptoRng, RngCore}; diff --git a/src/lib.rs b/src/lib.rs index d9a6762..44267e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,27 @@ +#![cfg_attr(not(feature = "std"), no_std)] #![doc = include_str!("../README.md")] +use cfg_if::cfg_if; -pub mod batch; +cfg_if! { + if #[cfg(feature = "std")] { + pub mod batch; -mod domain; -mod error; -mod hash; -mod signature; -mod signing_key; -mod verification_key; + mod domain; + mod error; + mod hash; + mod signature; + mod signing_key; + mod verification_key; -use hash::HStar; + use hash::HStar; -pub use domain::{Binding, Domain, SpendAuth}; -pub use error::Error; -pub use signature::Signature; -pub use signing_key::SigningKey; -pub use verification_key::{VerificationKey, VerificationKeyBytes}; + pub use domain::{Binding, Domain, SpendAuth}; + pub use error::Error; + pub use signature::Signature; + pub use signing_key::SigningKey; + pub use verification_key::{VerificationKey, VerificationKeyBytes}; -pub use decaf377::Fr; + pub use decaf377::Fr; + } else { + } +} From c4382c2ac4a48c9592741f9d432d46800d05ab1b Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Wed, 31 Jan 2024 16:08:00 -0800 Subject: [PATCH 2/5] Add CI jobs to run tests, and check no_std no_alloc --- .github/workflows/rust.yml | 85 ++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f978ba7..704bea2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,4 +1,4 @@ -on: [push, pull_request] +on: [pull_request] name: Rust CI @@ -7,37 +7,55 @@ jobs: name: Check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - uses: Swatinem/rust-cache@v1 - - uses: actions-rs/cargo@v1 - with: - command: check + - uses: actions/checkout@v4 + - name: Install rust toolchain + uses: dtolnay/rust-toolchain@stable + - name: Load rust cache + uses: astriaorg/buildjet-rust-cache@v2.5.1 + - name: Run cargo check, failing on warnings + run: cargo check --profile ci --all-targets + # It'd be nice to fail on warnings, but we're not ready for that. + # env: + # RUSTFLAGS: "-D warnings" test: name: Test Suite + runs-on: buildjet-16vcpu-ubuntu-2204 + steps: + - uses: actions/checkout@v4 + - name: Install rust toolchain + uses: dtolnay/rust-toolchain@stable + - name: Install nextest + uses: taiki-e/install-action@nextest + - name: Load rust cache + uses: astriaorg/buildjet-rust-cache@v2.5.1 + - name: Run tests with nextest + run: cargo nextest run --cargo-profile ci --profile ci + env: + CARGO_TERM_COLOR: always + + build_no_alloc: + name: build without alloc runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true + - run: rustup target add thumbv7em-none-eabihf - uses: Swatinem/rust-cache@v1 - uses: actions-rs/cargo@v1 with: - command: test + command: build + args: --target thumbv7em-none-eabihf --no-default-features fmt: name: Rustfmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -50,19 +68,26 @@ jobs: command: fmt args: --all -- --check - # clippy: - # name: Clippy - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v2 - # - uses: actions-rs/toolchain@v1 - # with: - # profile: minimal - # toolchain: stable - # override: true - # - uses: Swatinem/rust-cache@v1 - # - run: rustup component add clippy - # - uses: actions-rs/cargo@v1 - # with: - # command: clippy - # args: -- -D warnings + no-std: + name: no_std compatibility check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - uses: Swatinem/rust-cache@v1 + - uses: actions-rs/cargo@v1 + with: + command: check + args: --no-default-features + - uses: actions-rs/cargo@v1 + with: + command: build + args: --no-default-features + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features From 2e94412e2be364a00e68882b98a82c8f1539a9c0 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Thu, 1 Feb 2024 12:40:05 -0800 Subject: [PATCH 3/5] Fix CI check --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 704bea2..966264c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,7 +30,7 @@ jobs: - name: Load rust cache uses: astriaorg/buildjet-rust-cache@v2.5.1 - name: Run tests with nextest - run: cargo nextest run --cargo-profile ci --profile ci + run: cargo nextest run --cargo-profile ci env: CARGO_TERM_COLOR: always From 3894ce069658e777743126051268ea35a6849f21 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Thu, 1 Feb 2024 13:56:46 -0800 Subject: [PATCH 4/5] Reconfigure some dependencies to make no-std easier --- Cargo.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4abc0cf..be5b92c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,10 @@ repository = "https://github.com/penumbra-zone/decaf377-rdsa" [dependencies] # No Alloc, No Std blake2b_simd = { version = "0.5", default-features = false } -byteorder = "1.3" cfg-if = "1.0" decaf377 = { git = "https://github.com/penumbra-zone/decaf377", tag = "v0.8.0", default-features = false } digest = { version = "0.9", default-features = false } rand_core = { version = "0.6", default-features = false } -thiserror = "1.0" hex = { version = "0.4", default-features = false } # Only to satisfy Cargo zeroize = { version = "1.7", default-features = false } @@ -25,6 +23,7 @@ ark-ff = { version = "0.4", optional = true, default-features = false } ark-serialize = { version = "0.4", optional = true } # Std serde = { version = "1", optional = true, features = ["derive"] } +thiserror = { version = "1.0", optional = true } [dev-dependencies] bincode = "1" @@ -42,7 +41,7 @@ harness = false [features] default = ["serde", "std"] alloc = ["ark-ff", "ark-serialize"] -std = ["alloc", "ark-ff/std", "blake2b_simd/std", "byteorder/std", "decaf377/arkworks", "digest/std", "hex/std", "rand_core/std"] +std = ["alloc", "ark-ff/std", "blake2b_simd/std", "decaf377/arkworks", "digest/std", "hex/std", "rand_core/std", "thiserror"] parallel = ["ark-ff/parallel", "decaf377/parallel"] # Create profile for running checks in CI that are mostly "release" mode, From 68f38078b17945a6ef49e481643d2e85f72ce4a0 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Thu, 1 Feb 2024 14:02:59 -0800 Subject: [PATCH 5/5] Remove no_std tests Tests should always be run with std --- .github/workflows/rust.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 966264c..794a2e4 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -87,7 +87,3 @@ jobs: with: command: build args: --no-default-features - - uses: actions-rs/cargo@v1 - with: - command: test - args: --no-default-features