Browse Source

Merge pull request #10 from 0xPolygonMiden/implement-no-std

feat: add no-std support
al-gkr-basic-workflow
Bobbin Threadbare 2 years ago
committed by GitHub
parent
commit
6cf3b07e3f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 19 deletions
  1. +35
    -6
      .github/workflows/ci.yml
  2. +7
    -3
      Cargo.toml
  3. +4
    -4
      src/hash/rpo/digest.rs
  4. +7
    -0
      src/lib.rs
  5. +2
    -2
      src/merkle/merkle_path_set.rs
  6. +3
    -3
      src/merkle/merkle_tree.rs
  7. +1
    -1
      src/merkle/mod.rs

+ 35
- 6
.github/workflows/ci.yml

@ -7,14 +7,39 @@ on:
types: [opened, repoened, synchronize] types: [opened, repoened, synchronize]
jobs: 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: test:
name: Test Rust ${{matrix.toolchain}} on ${{matrix.os}}
name: Test ${{matrix.toolchain}} on ${{matrix.os}} with ${{matrix.features}}
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]
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

+ 7
- 3
Cargo.toml

@ -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_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] [dev-dependencies]
proptest = "1.0.0" proptest = "1.0.0"

+ 4
- 4
src/hash/rpo/digest.rs

@ -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(
"Value not in the appropriate range".to_owned(),
));
return Err(DeserializationError::InvalidValue(String::from(
"Value not in the appropriate range",
)));
} }
*inner = Felt::new(e); *inner = Felt::new(e);
} }

+ 7
- 0
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_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,
}; };

+ 2
- 2
src/merkle/merkle_path_set.rs

@ -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)),

+ 3
- 3
src/merkle/merkle_tree.rs

@ -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]);

+ 1
- 1
src/merkle/mod.rs

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

Loading…
Cancel
Save