Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable no_std support via a default-enabled std feature flag. #205

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,38 @@ jobs:
with:
command: test
args: --all-features

build-nostd:
name: Build target ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
matrix:
target:
- wasm32-wasi
- thumbv7em-none-eabihf
steps:
- uses: actions/checkout@v4
with:
path: crate_root
# We use a synthetic crate to ensure no dev-dependencies are enabled, which can
# be incompatible with some of these targets.
- name: Create synthetic crate for testing
run: cargo init --lib ci-build
- name: Copy Rust version into synthetic crate
run: cp crate_root/rust-toolchain.toml ci-build/
- name: Copy patch directives into synthetic crate
run: |
echo "[patch.crates-io]" >> ./ci-build/Cargo.toml
cat ./crate_root/Cargo.toml | sed "0,/.\+\(patch.crates.\+\)/d" >> ./ci-build/Cargo.toml
- name: Add no_std pragma to lib.rs
run: |
echo "#![no_std]" > ./ci-build/src/lib.rs
- name: Add redjubjub as a dependency of the synthetic crate
working-directory: ./ci-build
run: cargo add --no-default-features --path ../crate_root
- name: Add target
working-directory: ./ci-build
run: rustup target add ${{ matrix.target }}
- name: Build for target
working-directory: ./ci-build
run: cargo build --verbose --target ${{ matrix.target }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

Entries are listed in reverse chronological order.

## Unreleased

* Enable `no_std` use via a default-enabled `std` feature flag.

## 0.7.0

* Update the `reddsa` dependency to version 0.5.0.
* MSRV is now 1.65

## 0.6.0

Expand Down
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "redjubjub"
edition = "2018"
edition = "2021"
# When releasing to crates.io:
# - Update CHANGELOG.md
# - Create git tag.
Expand All @@ -17,10 +17,10 @@ description = "A standalone implementation of the RedJubjub signature scheme."
features = ["nightly"]

[dependencies]
reddsa = "0.5.0"
rand_core = "0.6"
serde = { version = "1", optional = true, features = ["derive"] }
thiserror = "1.0"
reddsa = { version = "0.5.0", default-features = false, features = ["alloc"] }
rand_core = { version = "0.6", default-features = false }
serde = { version = "1", optional = true, default-features = false, features = ["derive"] }
thiserror = { version = "1.0", optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

[dev-dependencies]
Expand All @@ -34,7 +34,8 @@ serde_json = "1.0"

[features]
nightly = []
default = ["serde"]
default = ["serde", "std"]
std = ["dep:thiserror", "reddsa/std"]

[[bench]]
name = "bench"
Expand Down
10 changes: 6 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
// - Deirdre Connolly <deirdre@zfnd.org>
// - Henry de Valence <hdevalence@hdevalence.ca>

#[cfg(feature = "std")]
use thiserror::Error;

/// An error related to RedJubJub signatures.
#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum Error {
/// The encoding of a signing key was malformed.
#[error("Malformed signing key encoding.")]
#[cfg_attr(feature = "std", error("Malformed signing key encoding."))]
MalformedSigningKey,
/// The encoding of a verification key was malformed.
#[error("Malformed verification key encoding.")]
#[cfg_attr(feature = "std", error("Malformed verification key encoding."))]
MalformedVerificationKey,
/// Signature verification failed.
#[error("Invalid signature.")]
#[cfg_attr(feature = "std", error("Invalid signature."))]
InvalidSignature,
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
#![no_std]

#[cfg(feature = "std")]
extern crate std;

pub mod batch;
mod error;
Expand Down Expand Up @@ -53,7 +57,7 @@ impl SigType for SpendAuth {}

pub(crate) mod private {
use super::*;
pub trait Sealed: Copy + Clone + Eq + PartialEq + std::fmt::Debug {
pub trait Sealed: Copy + Clone + Eq + PartialEq + core::fmt::Debug {
type RedDSASigType: reddsa::SigType;
}
impl Sealed for Binding {
Expand Down
2 changes: 1 addition & 1 deletion src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Deirdre Connolly <deirdre@zfnd.org>
// - Henry de Valence <hdevalence@hdevalence.ca>

use std::convert::{TryFrom, TryInto};
use core::convert::{TryFrom, TryInto};

use crate::{Error, Randomizer, SigType, Signature, SpendAuth, VerificationKey};

Expand Down
4 changes: 2 additions & 2 deletions src/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Deirdre Connolly <deirdre@zfnd.org>
// - Henry de Valence <hdevalence@hdevalence.ca>

use std::{convert::TryFrom, hash::Hash};
use core::{convert::TryFrom, hash::Hash};

use crate::{Error, Randomizer, SigType, Signature, SpendAuth};

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<T: SigType> TryFrom<[u8; 32]> for VerificationKey<T> {
type Error = Error;

fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
use std::convert::TryInto;
use core::convert::TryInto;
VerificationKeyBytes::from(bytes).try_into()
}
}
Expand Down