arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
feat: add no-std support

closes #5
Victor Lopez victor@codx.io
Tue, 22 Nov 2022 13:35:27 +0100
commit

3b9d515d0074fa46e853de31650882e6ba77702c

parent

7395697a680c0df9a6449aec815fb126f99e0602

M .github/workflows/ci.yml → .github/workflows/ci.yml

@@ -7,14 +7,39 @@ pull_request:

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 @@ - name: Test

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
M Cargo.toml → 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"
M src/hash/rpo/digest.rs → 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 @@ let mut inner: [Felt; DIGEST_SIZE] = [ZERO; DIGEST_SIZE];

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); }
M src/lib.rs → 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 @@ log2, ExtensionOf, FieldElement, StarkField,

}; pub use winter_utils::{ collections::{BTreeMap, Vec}, + string::String, uninit_vector, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SliceReader, };
M src/merkle/merkle_path_set.rs → src/merkle/merkle_path_set.rs

@@ -57,7 +57,7 @@ // Actual number of node in tree

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 @@ return Err(MerkleError::InvalidDepth(self.total_depth, depth));

} 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)),
M src/merkle/merkle_tree.rs → 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 @@ if index >= 2u64.pow(depth) {

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 @@ return Err(MerkleError::InvalidIndex(depth, index));

} 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]);
M src/merkle/mod.rs → src/merkle/mod.rs

@@ -1,4 +1,4 @@

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