From 3b9d515d0074fa46e853de31650882e6ba77702c Mon Sep 17 00:00:00 2001 From: Victor Lopez Date: Tue, 22 Nov 2022 13:35:27 +0100 Subject: [PATCH] feat: add no-std support closes #5 --- .github/workflows/ci.yml | 41 ++++++++++++++++++++++++++++++----- Cargo.toml | 10 ++++++--- src/hash/rpo/digest.rs | 8 +++---- src/lib.rs | 7 ++++++ src/merkle/merkle_path_set.rs | 4 ++-- src/merkle/merkle_tree.rs | 6 ++--- src/merkle/mod.rs | 2 +- 7 files changed, 59 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4924f7c..6ee9fb3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,14 +7,39 @@ on: types: [opened, repoened, synchronize] jobs: + build: + name: Build ${{matrix.toolchain}} on ${{matrix.os}} with ${{matrix.args}} + runs-on: ${{matrix.os}}-latest + strategy: + fail-fast: false + matrix: + toolchain: [stable, nightly] + os: [ubuntu] + target: [wasm32-unknown-unknown] + args: [--no-default-features --target wasm32-unknown-unknown] + steps: + - uses: actions/checkout@main + - name: Install rust + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{matrix.toolchain}} + override: true + - run: rustup target add ${{matrix.target}} + - name: Test + uses: actions-rs/cargo@v1 + with: + command: build + args: ${{matrix.args}} + test: - name: Test Rust ${{matrix.toolchain}} on ${{matrix.os}} + name: Test ${{matrix.toolchain}} on ${{matrix.os}} with ${{matrix.features}} runs-on: ${{matrix.os}}-latest strategy: fail-fast: false matrix: toolchain: [stable, nightly] os: [ubuntu] + features: [--all-features, --no-default-features] steps: - uses: actions/checkout@main - name: Install rust @@ -26,25 +51,29 @@ jobs: uses: actions-rs/cargo@v1 with: command: test + args: ${{matrix.features}} clippy: - name: Clippy + name: Clippy with ${{matrix.features}} runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + features: [--all-features, --no-default-features] steps: - uses: actions/checkout@main - - name: Install minimal stable with clippy + - name: Install minimal nightly with clippy uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: stable + toolchain: nightly components: clippy override: true - - name: Clippy uses: actions-rs/cargo@v1 with: command: clippy - args: --all -- -D clippy::all -D warnings + args: --all ${{matrix.features}} -- -D clippy::all -D warnings rustfmt: name: rustfmt diff --git a/Cargo.toml b/Cargo.toml index 01d456d..531e0ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,14 @@ categories = ["cryptography", "no-std"] keywords = ["miden", "crypto", "hash", "merkle"] edition = "2021" +[features] +default = ["std", "winter_crypto/default", "winter_math/default", "winter_utils/default"] +std = ["winter_crypto/std", "winter_math/std", "winter_utils/std"] + [dependencies] -winter_crypto = { version = "0.4.1", package = "winter-crypto" } -winter_math = { version = "0.4.1", package = "winter-math" } -winter_utils = { version = "0.4.1", package = "winter-utils" } +winter_crypto = { version = "0.4.1", package = "winter-crypto", default-features = false } +winter_math = { version = "0.4.1", package = "winter-math", default-features = false } +winter_utils = { version = "0.4.1", package = "winter-utils", default-features = false } [dev-dependencies] proptest = "1.0.0" diff --git a/src/hash/rpo/digest.rs b/src/hash/rpo/digest.rs index ce31f3e..f4e0aee 100644 --- a/src/hash/rpo/digest.rs +++ b/src/hash/rpo/digest.rs @@ -1,7 +1,7 @@ use super::DIGEST_SIZE; use crate::{ ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Serializable, - StarkField, ZERO, + StarkField, String, ZERO, }; use core::ops::Deref; @@ -59,9 +59,9 @@ impl Deserializable for RpoDigest256 { for inner in inner.iter_mut() { let e = source.read_u64()?; if e >= Felt::MODULUS { - return Err(DeserializationError::InvalidValue( - "Value not in the appropriate range".to_owned(), - )); + return Err(DeserializationError::InvalidValue(String::from( + "Value not in the appropriate range", + ))); } *inner = Felt::new(e); } diff --git a/src/lib.rs b/src/lib.rs index 3f7a48e..edb814e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,9 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +#[cfg_attr(test, macro_use)] +extern crate alloc; + pub use winter_crypto::{Digest, ElementHasher, Hasher as HashFn}; pub use winter_math::{ fields::{f64::BaseElement as Felt, QuadExtension}, @@ -5,6 +11,7 @@ pub use winter_math::{ }; pub use winter_utils::{ collections::{BTreeMap, Vec}, + string::String, uninit_vector, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SliceReader, }; diff --git a/src/merkle/merkle_path_set.rs b/src/merkle/merkle_path_set.rs index 6ab7dde..9221757 100644 --- a/src/merkle/merkle_path_set.rs +++ b/src/merkle/merkle_path_set.rs @@ -57,7 +57,7 @@ impl MerklePathSet { let pos = 2u64.pow(self.total_depth) + index; // Index of the leaf path in map. Paths of neighboring leaves are stored in one key-value pair - let half_pos = (pos / 2) as u64; + let half_pos = pos / 2; let mut extended_path = path; if is_even(pos) { @@ -104,7 +104,7 @@ impl MerklePathSet { } let pos = 2u64.pow(depth) + index; - let index = (pos / 2) as u64; + let index = pos / 2; match self.paths.get(&index) { None => Err(MerkleError::NodeNotInSet(index)), diff --git a/src/merkle/merkle_tree.rs b/src/merkle/merkle_tree.rs index 4a2953a..e367a1c 100644 --- a/src/merkle/merkle_tree.rs +++ b/src/merkle/merkle_tree.rs @@ -1,7 +1,7 @@ use super::MerkleError; use crate::{ hash::{merge, Digest}, - log2, uninit_vector, Felt, FieldElement, Word, + log2, uninit_vector, Felt, FieldElement, Vec, Word, }; use core::slice; @@ -80,7 +80,7 @@ impl MerkleTree { return Err(MerkleError::InvalidIndex(depth, index)); } - let pos = 2usize.pow(depth as u32) + (index as usize); + let pos = 2_usize.pow(depth) + (index as usize); Ok(self.nodes[pos]) } @@ -102,7 +102,7 @@ impl MerkleTree { } let mut path = Vec::with_capacity(depth as usize); - let mut pos = 2usize.pow(depth as u32) + (index as usize); + let mut pos = 2_usize.pow(depth) + (index as usize); while pos > 1 { path.push(self.nodes[pos ^ 1]); diff --git a/src/merkle/mod.rs b/src/merkle/mod.rs index 63e5488..38b4075 100644 --- a/src/merkle/mod.rs +++ b/src/merkle/mod.rs @@ -1,4 +1,4 @@ -use crate::Word; +use crate::{Vec, Word}; #[cfg(test)] use crate::{Felt, ZERO};