Skip to content

Commit

Permalink
Add no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Mar 19, 2024
1 parent 23ca3c9 commit 2ad8747
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ jobs:
cache-on-failure: "true"
- name: Build all-features
run: cargo build -p givre --all-features
build-wasm-nostd:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: "true"
- name: Install wasm32-unknown-unknown toolchain
run: rustup target add wasm32-unknown-unknown
- name: Build on wasm32-unknown-unknown (no_std)
run:
(cd wasm/no_std && cargo build --target wasm32-unknown-unknown)
test:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/wasm/no_std/target

/.helix
10 changes: 4 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ members = [
"givre",
"tests",
]

exclude = [
"wasm/no_std",
]

[patch.crates-io.key-share]
git = "https://github.com/dfns/cggmp21"
branch = "no_std"
7 changes: 6 additions & 1 deletion givre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
cggmp21-keygen = { version = "0.1", optional = true }
key-share = "0.2"
key-share = { version = "0.2", default-features = false }

generic-ec = { version = "0.2", default-features = false }

Expand All @@ -27,6 +27,10 @@ serde = { version = "1", default-features = false, features = ["derive"], option
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }

[features]
default = ["std"]

std = ["key-share/std"]

cggmp21-keygen = ["dep:cggmp21-keygen"]
full-signing = ["round-based", "futures"]

Expand All @@ -45,3 +49,4 @@ all-ciphersuites = ["ciphersuite-secp256k1", "ciphersuite-ed25519", "ciphersuite
ciphersuite-secp256k1 = ["generic-ec/curve-secp256k1", "k256", "sha2", "static_assertions"]
ciphersuite-ed25519 = ["generic-ec/curve-ed25519", "sha2"]
ciphersuite-bitcoin = ["ciphersuite-secp256k1"]

3 changes: 3 additions & 0 deletions givre/src/ciphersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//! * [Secp256k1], requires `ciphersuite-secp256k1` feature
//! * [Ed25519], requires `ciphersuite-ed25519` feature
//! * [Bitcoin], requires `ciphersuite-bitcoin` feature
use alloc::vec::Vec;

use generic_ec::{
errors::{InvalidPoint, InvalidScalar},
Curve, NonZero, Point, Scalar, SecretScalar,
Expand Down
6 changes: 6 additions & 0 deletions givre/src/ciphersuite/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Ciphersuite for Bitcoin {
}
}

#[cfg(feature = "std")]
fn challenge_hash() -> sha2::Sha256 {
static PRECOMPUTED: std::sync::OnceLock<sha2::Sha256> = std::sync::OnceLock::new();
PRECOMPUTED
Expand All @@ -93,3 +94,8 @@ fn challenge_hash() -> sha2::Sha256 {
})
.clone()
}
#[cfg(not(feature = "std"))]
fn challenge_hash() -> sha2::Sha256 {
let tag = sha2::Sha256::digest("BIP0340/challenge");
sha2::Sha256::new().chain_update(&tag).chain_update(&tag)
}
3 changes: 3 additions & 0 deletions givre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#![deny(missing_docs)]
#![allow(clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

pub use generic_ec;
#[cfg(feature = "full-signing")]
Expand Down
3 changes: 3 additions & 0 deletions givre/src/signing/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!
//! [Section 5.3]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-15.html#name-signature-share-aggregation
use alloc::vec::Vec;
use core::fmt;

use generic_ec::{NonZero, Point, Scalar};
Expand Down Expand Up @@ -125,6 +126,7 @@ impl fmt::Display for AggregateError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for AggregateError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.0 {
Expand All @@ -143,4 +145,5 @@ impl fmt::Display for InvalidSignature {
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidSignature {}
3 changes: 3 additions & 0 deletions givre/src/signing/round2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!
//! [Section 5.2]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-15.html#name-round-two-signature-share-g
use alloc::vec::Vec;
use core::{fmt, iter};

use generic_ec::{Curve, NonZero, Scalar};
Expand Down Expand Up @@ -239,6 +240,7 @@ impl fmt::Display for Bug {
}
}

#[cfg(feature = "std")]
impl std::error::Error for SigningError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.0 {
Expand All @@ -254,6 +256,7 @@ impl std::error::Error for SigningError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Bug {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
2 changes: 2 additions & 0 deletions givre/src/signing/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use digest::{FixedOutput, Update};
use generic_ec::{Curve, NonZero, Point, Scalar};

Expand Down
Loading

0 comments on commit 2ad8747

Please sign in to comment.