feat: add no-std support

closes #5
This commit is contained in:
Victor Lopez 2022-11-22 13:35:27 +01:00
parent 7395697a68
commit 3b9d515d00
No known key found for this signature in database
GPG key ID: DE1CBCF58160A41D
7 changed files with 60 additions and 20 deletions

View file

@ -7,14 +7,39 @@ on:
types: [opened, repoened, synchronize] types: [opened, repoened, synchronize]
jobs: jobs:
test: build:
name: Test Rust ${{matrix.toolchain}} on ${{matrix.os}} name: Build ${{matrix.toolchain}} on ${{matrix.os}} with ${{matrix.args}}
runs-on: ${{matrix.os}}-latest runs-on: ${{matrix.os}}-latest
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
toolchain: [stable, nightly] toolchain: [stable, nightly]
os: [ubuntu] 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 ${{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: steps:
- uses: actions/checkout@main - uses: actions/checkout@main
- name: Install rust - name: Install rust
@ -26,25 +51,29 @@ jobs:
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: test command: test
args: ${{matrix.features}}
clippy: clippy:
name: Clippy name: Clippy with ${{matrix.features}}
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
features: [--all-features, --no-default-features]
steps: steps:
- uses: actions/checkout@main - uses: actions/checkout@main
- name: Install minimal stable with clippy - name: Install minimal nightly with clippy
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
profile: minimal profile: minimal
toolchain: stable toolchain: nightly
components: clippy components: clippy
override: true override: true
- name: Clippy - name: Clippy
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: clippy command: clippy
args: --all -- -D clippy::all -D warnings args: --all ${{matrix.features}} -- -D clippy::all -D warnings
rustfmt: rustfmt:
name: rustfmt name: rustfmt

View file

@ -10,10 +10,14 @@ categories = ["cryptography", "no-std"]
keywords = ["miden", "crypto", "hash", "merkle"] keywords = ["miden", "crypto", "hash", "merkle"]
edition = "2021" 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] [dependencies]
winter_crypto = { version = "0.4.1", package = "winter-crypto" } winter_crypto = { version = "0.4.1", package = "winter-crypto", default-features = false }
winter_math = { version = "0.4.1", package = "winter-math" } winter_math = { version = "0.4.1", package = "winter-math", default-features = false }
winter_utils = { version = "0.4.1", package = "winter-utils" } winter_utils = { version = "0.4.1", package = "winter-utils", default-features = false }
[dev-dependencies] [dev-dependencies]
proptest = "1.0.0" proptest = "1.0.0"

View file

@ -1,7 +1,7 @@
use super::DIGEST_SIZE; use super::DIGEST_SIZE;
use crate::{ use crate::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Serializable, ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Serializable,
StarkField, ZERO, StarkField, String, ZERO,
}; };
use core::ops::Deref; use core::ops::Deref;
@ -59,9 +59,9 @@ impl Deserializable for RpoDigest256 {
for inner in inner.iter_mut() { for inner in inner.iter_mut() {
let e = source.read_u64()?; let e = source.read_u64()?;
if e >= Felt::MODULUS { if e >= Felt::MODULUS {
return Err(DeserializationError::InvalidValue( return Err(DeserializationError::InvalidValue(String::from(
"Value not in the appropriate range".to_owned(), "Value not in the appropriate range",
)); )));
} }
*inner = Felt::new(e); *inner = Felt::new(e);
} }

View file

@ -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_crypto::{Digest, ElementHasher, Hasher as HashFn};
pub use winter_math::{ pub use winter_math::{
fields::{f64::BaseElement as Felt, QuadExtension}, fields::{f64::BaseElement as Felt, QuadExtension},
@ -5,6 +11,7 @@ pub use winter_math::{
}; };
pub use winter_utils::{ pub use winter_utils::{
collections::{BTreeMap, Vec}, collections::{BTreeMap, Vec},
string::String,
uninit_vector, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, uninit_vector, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
SliceReader, SliceReader,
}; };

View file

@ -57,7 +57,7 @@ impl MerklePathSet {
let pos = 2u64.pow(self.total_depth) + index; 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 // 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; let mut extended_path = path;
if is_even(pos) { if is_even(pos) {
@ -104,7 +104,7 @@ impl MerklePathSet {
} }
let pos = 2u64.pow(depth) + index; let pos = 2u64.pow(depth) + index;
let index = (pos / 2) as u64; let index = pos / 2;
match self.paths.get(&index) { match self.paths.get(&index) {
None => Err(MerkleError::NodeNotInSet(index)), None => Err(MerkleError::NodeNotInSet(index)),

View file

@ -1,7 +1,7 @@
use super::MerkleError; use super::MerkleError;
use crate::{ use crate::{
hash::{merge, Digest}, hash::{merge, Digest},
log2, uninit_vector, Felt, FieldElement, Word, log2, uninit_vector, Felt, FieldElement, Vec, Word,
}; };
use core::slice; use core::slice;
@ -80,7 +80,7 @@ impl MerkleTree {
return Err(MerkleError::InvalidIndex(depth, index)); 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]) Ok(self.nodes[pos])
} }
@ -102,7 +102,7 @@ impl MerkleTree {
} }
let mut path = Vec::with_capacity(depth as usize); 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 { while pos > 1 {
path.push(self.nodes[pos ^ 1]); path.push(self.nodes[pos ^ 1]);

View file

@ -1,4 +1,4 @@
use crate::Word; use crate::{Vec, Word};
#[cfg(test)] #[cfg(test)]
use crate::{Felt, ZERO}; use crate::{Felt, ZERO};