Skip to content

Commit

Permalink
refactor: update repo to be compatible with Winterfell 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Feb 6, 2024
1 parent a80e2b1 commit 3b59d3f
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 46 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ std = [
blake3 = { version = "1.5", default-features = false }
clap = { version = "4.4", features = ["derive"], optional = true }
libc = { version = "0.2", default-features = false, optional = true }
rand_utils = { version = "0.7", package = "winter-rand-utils", optional = true }
rand_utils = { version = "0.8", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", features = ["derive"], default-features = false, optional = true }
winter_crypto = { version = "0.7", package = "winter-crypto", default-features = false }
winter_math = { version = "0.7", package = "winter-math", default-features = false }
winter_utils = { version = "0.7", package = "winter-utils", default-features = false }
winter_crypto = { version = "0.8", package = "winter-crypto", default-features = false }
winter_math = { version = "0.8", package = "winter-math", default-features = false }
winter_utils = { version = "0.8", package = "winter-utils", default-features = false }

[dev-dependencies]
seq-macro = { version = "0.3" }
criterion = { version = "0.5", features = ["html_reports"] }
proptest = "1.4"
rand_utils = { version = "0.7", package = "winter-rand-utils" }
rand_utils = { version = "0.8", package = "winter-rand-utils" }

[build-dependencies]
cc = { version = "1.0", features = ["parallel"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/dsa/rpo_falcon512/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};

#[cfg(feature = "std")]
use super::{ffi, NonceBytes, StarkField, NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
use super::{ffi, NonceBytes, NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};

// PUBLIC KEY
// ================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/dsa/rpo_falcon512/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
collections::Vec, ByteReader, ByteWriter, Deserializable, DeserializationError,
Serializable,
},
Felt, StarkField, Word, ZERO,
Felt, Word, ZERO,
};

#[cfg(feature = "std")]
Expand Down
4 changes: 2 additions & 2 deletions src/dsa/rpo_falcon512/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, NonceBytes, NonceElements,
Polynomial, PublicKeyBytes, Rpo256, Serializable, SignatureBytes, StarkField, Word, MODULUS, N,
Polynomial, PublicKeyBytes, Rpo256, Serializable, SignatureBytes, Word, MODULUS, N,
SIG_L2_BOUND, ZERO,
};
use crate::utils::string::ToString;
Expand Down Expand Up @@ -182,7 +182,7 @@ fn decode_nonce(nonce: &NonceBytes) -> NonceElements {
let mut result = [ZERO; 8];
for (i, bytes) in nonce.chunks(5).enumerate() {
buffer[..5].copy_from_slice(bytes);
result[i] = u64::from_le_bytes(buffer).into();
result[i] = u64::from_le_bytes(buffer).try_into().unwrap();
}

result
Expand Down
2 changes: 1 addition & 1 deletion src/hash/blake/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Digest, ElementHasher, Felt, FieldElement, Hasher, StarkField};
use super::{Digest, ElementHasher, Felt, FieldElement, Hasher};
use crate::utils::{
bytes_to_hex_string, hex_to_bytes, string::String, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
Expand Down
7 changes: 6 additions & 1 deletion src/hash/rescue/rpo/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpoDigest {
return Err(RpoDigestError::InvalidInteger);
}

Ok(Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()]))
Ok(Self([
value[0].try_into().unwrap(),
value[1].try_into().unwrap(),
value[2].try_into().unwrap(),
value[3].try_into().unwrap(),
]))
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/hash/rescue/rpx/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpxDigest {
return Err(RpxDigestError::InvalidInteger);
}

Ok(Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()]))
Ok(Self([
value[0].try_into().unwrap(),
value[1].try_into().unwrap(),
value[2].try_into().unwrap(),
value[3].try_into().unwrap(),
]))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/merkle/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Felt, MerkleError, RpoDigest, StarkField};
use super::{Felt, MerkleError, RpoDigest};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use core::fmt::Display;

Expand Down
2 changes: 1 addition & 1 deletion src/merkle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::{
hash::rpo::{Rpo256, RpoDigest},
utils::collections::{vec, BTreeMap, BTreeSet, KvMap, RecordingMap, Vec},
Felt, StarkField, Word, EMPTY_WORD, ZERO,
Felt, Word, EMPTY_WORD, ZERO,
};

// REEXPORTS
Expand Down
4 changes: 2 additions & 2 deletions src/merkle/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ impl Serializable for MerklePath {
fn write_into<W: winter_utils::ByteWriter>(&self, target: &mut W) {
assert!(self.nodes.len() <= u8::MAX.into(), "Length enforced in the constructor");
target.write_u8(self.nodes.len() as u8);
self.nodes.write_into(target);
target.write_many(&self.nodes);
}
}

impl Deserializable for MerklePath {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let count = source.read_u8()?.into();
let nodes = RpoDigest::read_batch_from(source, count)?;
let nodes = source.read_many::<RpoDigest>(count)?;
Ok(Self { nodes })
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/merkle/smt/full/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::cmp::Ordering;

use winter_math::StarkField;

use crate::hash::rpo::Rpo256;
use crate::merkle::{EmptySubtreeRoots, InnerNodeInfo};
use crate::utils::{
Expand Down
66 changes: 47 additions & 19 deletions src/merkle/smt/full/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ fn test_smt_insert_at_same_key_2() {
// The most significant u64 used for both keys (to ensure they map to the same leaf)
let key_msb: u64 = 42;

let key_already_present: RpoDigest =
RpoDigest::from([2_u64.into(), 2_u64.into(), 2_u64.into(), Felt::new(key_msb)]);
let key_already_present: RpoDigest = RpoDigest::from([
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
Felt::new(key_msb),
]);
let key_already_present_index: NodeIndex =
LeafIndex::<SMT_DEPTH>::from(key_already_present).into();
let value_already_present = [ONE + ONE + ONE; WORD_SIZE];
Expand Down Expand Up @@ -185,14 +189,22 @@ fn test_smt_removal() {
let raw = 0b_01101001_01101100_00011111_11111111_10010110_10010011_11100000_00000000_u64;

let key_1: RpoDigest = RpoDigest::from([ONE, ONE, ONE, Felt::new(raw)]);
let key_2: RpoDigest =
RpoDigest::from([2_u64.into(), 2_u64.into(), 2_u64.into(), Felt::new(raw)]);
let key_3: RpoDigest =
RpoDigest::from([3_u64.into(), 3_u64.into(), 3_u64.into(), Felt::new(raw)]);
let key_2: RpoDigest = RpoDigest::from([
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
Felt::new(raw),
]);
let key_3: RpoDigest = RpoDigest::from([
3_u64.try_into().unwrap(),
3_u64.try_into().unwrap(),
3_u64.try_into().unwrap(),
Felt::new(raw),
]);

let value_1 = [ONE; WORD_SIZE];
let value_2 = [2_u64.into(); WORD_SIZE];
let value_3: [Felt; 4] = [3_u64.into(); WORD_SIZE];
let value_2 = [2_u64.try_into().unwrap(); WORD_SIZE];
let value_3: [Felt; 4] = [3_u64.try_into().unwrap(); WORD_SIZE];

// insert key-value 1
{
Expand Down Expand Up @@ -258,11 +270,15 @@ fn test_smt_path_to_keys_in_same_leaf_are_equal() {
let raw = 0b_01101001_01101100_00011111_11111111_10010110_10010011_11100000_00000000_u64;

let key_1: RpoDigest = RpoDigest::from([ONE, ONE, ONE, Felt::new(raw)]);
let key_2: RpoDigest =
RpoDigest::from([2_u64.into(), 2_u64.into(), 2_u64.into(), Felt::new(raw)]);
let key_2: RpoDigest = RpoDigest::from([
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
Felt::new(raw),
]);

let value_1 = [ONE; WORD_SIZE];
let value_2 = [2_u64.into(); WORD_SIZE];
let value_2 = [2_u64.try_into().unwrap(); WORD_SIZE];

let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap();

Expand All @@ -282,11 +298,15 @@ fn test_empty_leaf_hash() {
#[test]
fn test_smt_get_value() {
let key_1: RpoDigest = RpoDigest::from([ONE, ONE, ONE, ONE]);
let key_2: RpoDigest =
RpoDigest::from([2_u64.into(), 2_u64.into(), 2_u64.into(), 2_u64.into()]);
let key_2: RpoDigest = RpoDigest::from([
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
]);

let value_1 = [ONE; WORD_SIZE];
let value_2 = [2_u64.into(); WORD_SIZE];
let value_2 = [2_u64.try_into().unwrap(); WORD_SIZE];

let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap();

Expand All @@ -297,8 +317,12 @@ fn test_smt_get_value() {
assert_eq!(value_2, returned_value_2);

// Check that a key with no inserted value returns the empty word
let key_no_value =
RpoDigest::from([42_u64.into(), 42_u64.into(), 42_u64.into(), 42_u64.into()]);
let key_no_value = RpoDigest::from([
42_u64.try_into().unwrap(),
42_u64.try_into().unwrap(),
42_u64.try_into().unwrap(),
42_u64.try_into().unwrap(),
]);

assert_eq!(EMPTY_WORD, smt.get_value(&key_no_value));
}
Expand All @@ -307,11 +331,15 @@ fn test_smt_get_value() {
#[test]
fn test_smt_entries() {
let key_1: RpoDigest = RpoDigest::from([ONE, ONE, ONE, ONE]);
let key_2: RpoDigest =
RpoDigest::from([2_u64.into(), 2_u64.into(), 2_u64.into(), 2_u64.into()]);
let key_2: RpoDigest = RpoDigest::from([
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
2_u64.try_into().unwrap(),
]);

let value_1 = [ONE; WORD_SIZE];
let value_2 = [2_u64.into(); WORD_SIZE];
let value_2 = [2_u64.try_into().unwrap(); WORD_SIZE];

let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap();

Expand Down
2 changes: 0 additions & 2 deletions src/merkle/smt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use winter_math::StarkField;

use crate::{
hash::rpo::{Rpo256, RpoDigest},
Word,
Expand Down
2 changes: 1 addition & 1 deletion src/merkle/tiered_smt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
BTreeMap, BTreeSet, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex,
Rpo256, RpoDigest, StarkField, Vec, Word,
Rpo256, RpoDigest, Vec, Word,
};
use crate::utils::vec;
use core::{cmp, ops::Deref};
Expand Down
6 changes: 3 additions & 3 deletions src/merkle/tiered_smt/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,13 +721,13 @@ fn tsmt_get_proof_single_element_64() {
let mut smt = TieredSmt::default();

let raw_a = 0b_00000000_00000001_00000000_00000001_00000000_00000001_00000000_00000001_u64;
let key_a = [ONE, ONE, ONE, raw_a.into()].into();
let key_a = [ONE, ONE, ONE, raw_a.try_into().unwrap()].into();
let value_a = [ONE, ONE, ONE, ONE];
smt.insert(key_a, value_a);

// push element `a` to depth 64, by inserting another value that shares the 48-bit prefix
let raw_b = 0b_00000000_00000001_00000000_00000001_00000000_00000001_00000000_00000000_u64;
let key_b = [ONE, ONE, ONE, raw_b.into()].into();
let key_b = [ONE, ONE, ONE, raw_b.try_into().unwrap()].into();
smt.insert(key_b, [ONE, ONE, ONE, ONE]);

// verify the proof for element `a`
Expand All @@ -742,7 +742,7 @@ fn tsmt_get_proof_single_element_64() {

// check that a key that shared the 64-bit prefix with `a`, but is not inserted, also has a
// valid membership proof for the empty word
let key = [ONE, ONE, ZERO, raw_a.into()].into();
let key = [ONE, ONE, ZERO, raw_a.try_into().unwrap()].into();
let proof = smt.prove(key);
assert!(proof.verify_membership(&key, &EMPTY_WORD, &smt.root()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/merkle/tiered_smt/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{get_key_prefix, BTreeMap, LeafNodeIndex, RpoDigest, StarkField, Vec, Word};
use super::{get_key_prefix, BTreeMap, LeafNodeIndex, RpoDigest, Vec, Word};
use crate::utils::vec;
use core::{
cmp::{Ord, Ordering},
Expand Down
2 changes: 1 addition & 1 deletion src/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pub use winter_crypto::{DefaultRandomCoin as WinterRandomCoin, RandomCoin, RandomCoinError};

use crate::{Felt, FieldElement, StarkField, Word, ZERO};
use crate::{Felt, FieldElement, Word, ZERO};

mod rpo;
pub use rpo::RpoRandomCoin;
Expand Down
2 changes: 1 addition & 1 deletion src/rand/rpo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Felt, FeltRng, FieldElement, StarkField, Word, ZERO};
use super::{Felt, FeltRng, FieldElement, Word, ZERO};
use crate::{
hash::rpo::{Rpo256, RpoDigest},
utils::{
Expand Down

0 comments on commit 3b59d3f

Please sign in to comment.