From 587ae28583f65e8cdcec5c43fc146ef71279322b Mon Sep 17 00:00:00 2001 From: Andrey Khmuro Date: Wed, 7 Feb 2024 02:18:39 +0300 Subject: [PATCH] refactor: update repo to be compatible with Winterfell 0.8 --- Cargo.toml | 10 ++++----- src/dsa/rpo_falcon512/keys.rs | 2 +- src/dsa/rpo_falcon512/mod.rs | 2 +- src/dsa/rpo_falcon512/signature.rs | 8 ++++--- src/hash/blake/mod.rs | 2 +- src/hash/rescue/rpo/digest.rs | 15 +++++-------- src/hash/rescue/rpx/digest.rs | 15 +++++-------- src/merkle/index.rs | 2 +- src/merkle/mod.rs | 2 +- src/merkle/path.rs | 4 ++-- src/merkle/smt/full/leaf.rs | 1 - src/merkle/smt/full/mod.rs | 2 -- src/merkle/smt/full/tests.rs | 36 +++++++++++++++--------------- src/rand/mod.rs | 2 +- src/rand/rpo.rs | 2 +- 15 files changed, 49 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 394c7111e..8efd37edb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/dsa/rpo_falcon512/keys.rs b/src/dsa/rpo_falcon512/keys.rs index 6bb94a451..60c4031d6 100644 --- a/src/dsa/rpo_falcon512/keys.rs +++ b/src/dsa/rpo_falcon512/keys.rs @@ -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 // ================================================================================================ diff --git a/src/dsa/rpo_falcon512/mod.rs b/src/dsa/rpo_falcon512/mod.rs index 3ccc266e9..7560bfbfa 100644 --- a/src/dsa/rpo_falcon512/mod.rs +++ b/src/dsa/rpo_falcon512/mod.rs @@ -4,7 +4,7 @@ use crate::{ collections::Vec, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, }, - Felt, StarkField, Word, ZERO, + Felt, Word, ZERO, }; #[cfg(feature = "std")] diff --git a/src/dsa/rpo_falcon512/signature.rs b/src/dsa/rpo_falcon512/signature.rs index 3994e0ec1..3f243dfa0 100644 --- a/src/dsa/rpo_falcon512/signature.rs +++ b/src/dsa/rpo_falcon512/signature.rs @@ -1,6 +1,6 @@ use super::{ - ByteReader, ByteWriter, Deserializable, DeserializationError, NonceBytes, NonceElements, - Polynomial, PublicKeyBytes, Rpo256, Serializable, SignatureBytes, StarkField, Word, MODULUS, N, + ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, NonceBytes, NonceElements, + Polynomial, PublicKeyBytes, Rpo256, Serializable, SignatureBytes, Word, MODULUS, N, SIG_L2_BOUND, ZERO, }; use crate::utils::string::ToString; @@ -182,7 +182,9 @@ 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(); + // we can safely (without overflow) create a new Felt from u64 value here since this value + // contains at most 5 bytes + result[i] = Felt::new(u64::from_le_bytes(buffer)); } result diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index 16af67e75..6862934b5 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -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, diff --git a/src/hash/rescue/rpo/digest.rs b/src/hash/rescue/rpo/digest.rs index 06454584b..801afe5ba 100644 --- a/src/hash/rescue/rpo/digest.rs +++ b/src/hash/rescue/rpo/digest.rs @@ -234,15 +234,12 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpoDigest { type Error = RpoDigestError; fn try_from(value: [u64; DIGEST_SIZE]) -> Result { - if value[0] >= Felt::MODULUS - || value[1] >= Felt::MODULUS - || value[2] >= Felt::MODULUS - || value[3] >= Felt::MODULUS - { - return Err(RpoDigestError::InvalidInteger); - } - - Ok(Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()])) + Ok(Self([ + value[0].try_into().map_err(|_| RpoDigestError::InvalidInteger)?, + value[1].try_into().map_err(|_| RpoDigestError::InvalidInteger)?, + value[2].try_into().map_err(|_| RpoDigestError::InvalidInteger)?, + value[3].try_into().map_err(|_| RpoDigestError::InvalidInteger)?, + ])) } } diff --git a/src/hash/rescue/rpx/digest.rs b/src/hash/rescue/rpx/digest.rs index 80057ca0a..5dd891407 100644 --- a/src/hash/rescue/rpx/digest.rs +++ b/src/hash/rescue/rpx/digest.rs @@ -234,15 +234,12 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpxDigest { type Error = RpxDigestError; fn try_from(value: [u64; DIGEST_SIZE]) -> Result { - if value[0] >= Felt::MODULUS - || value[1] >= Felt::MODULUS - || value[2] >= Felt::MODULUS - || value[3] >= Felt::MODULUS - { - return Err(RpxDigestError::InvalidInteger); - } - - Ok(Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()])) + Ok(Self([ + value[0].try_into().map_err(|_| RpxDigestError::InvalidInteger)?, + value[1].try_into().map_err(|_| RpxDigestError::InvalidInteger)?, + value[2].try_into().map_err(|_| RpxDigestError::InvalidInteger)?, + value[3].try_into().map_err(|_| RpxDigestError::InvalidInteger)?, + ])) } } diff --git a/src/merkle/index.rs b/src/merkle/index.rs index c533fa0d7..a7f7f4fb0 100644 --- a/src/merkle/index.rs +++ b/src/merkle/index.rs @@ -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; diff --git a/src/merkle/mod.rs b/src/merkle/mod.rs index 9b8c7c5f7..125b94a50 100644 --- a/src/merkle/mod.rs +++ b/src/merkle/mod.rs @@ -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 diff --git a/src/merkle/path.rs b/src/merkle/path.rs index 3da78710b..a7cdc5da5 100644 --- a/src/merkle/path.rs +++ b/src/merkle/path.rs @@ -206,14 +206,14 @@ impl Serializable for MerklePath { fn write_into(&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(source: &mut R) -> Result { let count = source.read_u8()?.into(); - let nodes = RpoDigest::read_batch_from(source, count)?; + let nodes = source.read_many::(count)?; Ok(Self { nodes }) } } diff --git a/src/merkle/smt/full/leaf.rs b/src/merkle/smt/full/leaf.rs index 5d3e48591..12aedd65b 100644 --- a/src/merkle/smt/full/leaf.rs +++ b/src/merkle/smt/full/leaf.rs @@ -1,7 +1,6 @@ use core::cmp::Ordering; use crate::utils::{collections::Vec, string::ToString, vec}; -use winter_math::StarkField; use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable}; use super::{Felt, LeafIndex, Rpo256, RpoDigest, SmtLeafError, Word, EMPTY_WORD, SMT_DEPTH}; diff --git a/src/merkle/smt/full/mod.rs b/src/merkle/smt/full/mod.rs index deb8122f2..e895f5497 100644 --- a/src/merkle/smt/full/mod.rs +++ b/src/merkle/smt/full/mod.rs @@ -1,5 +1,3 @@ -use winter_math::StarkField; - use crate::hash::rpo::Rpo256; use crate::merkle::{EmptySubtreeRoots, InnerNodeInfo}; use crate::utils::collections::{BTreeMap, BTreeSet}; diff --git a/src/merkle/smt/full/tests.rs b/src/merkle/smt/full/tests.rs index 08f4183e1..9afc1fbfe 100644 --- a/src/merkle/smt/full/tests.rs +++ b/src/merkle/smt/full/tests.rs @@ -60,7 +60,7 @@ fn test_smt_insert_at_same_key_2() { 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)]); + RpoDigest::from([2_u32.into(), 2_u32.into(), 2_u32.into(), Felt::new(key_msb)]); let key_already_present_index: NodeIndex = LeafIndex::::from(key_already_present).into(); let value_already_present = [ONE + ONE + ONE; WORD_SIZE]; @@ -192,13 +192,13 @@ fn test_smt_removal() { 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)]); + RpoDigest::from([2_u32.into(), 2_u32.into(), 2_u32.into(), Felt::new(raw)]); let key_3: RpoDigest = - RpoDigest::from([3_u64.into(), 3_u64.into(), 3_u64.into(), Felt::new(raw)]); + RpoDigest::from([3_u32.into(), 3_u32.into(), 3_u32.into(), 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_u32.into(); WORD_SIZE]; + let value_3: [Felt; 4] = [3_u32.into(); WORD_SIZE]; // insert key-value 1 { @@ -265,10 +265,10 @@ fn test_smt_path_to_keys_in_same_leaf_are_equal() { 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)]); + RpoDigest::from([2_u32.into(), 2_u32.into(), 2_u32.into(), Felt::new(raw)]); let value_1 = [ONE; WORD_SIZE]; - let value_2 = [2_u64.into(); WORD_SIZE]; + let value_2 = [2_u32.into(); WORD_SIZE]; let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap(); @@ -289,10 +289,10 @@ fn test_empty_leaf_hash() { 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()]); + RpoDigest::from([2_u32.into(), 2_u32.into(), 2_u32.into(), 2_u32.into()]); let value_1 = [ONE; WORD_SIZE]; - let value_2 = [2_u64.into(); WORD_SIZE]; + let value_2 = [2_u32.into(); WORD_SIZE]; let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap(); @@ -304,7 +304,7 @@ fn test_smt_get_value() { // 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()]); + RpoDigest::from([42_u32.into(), 42_u32.into(), 42_u32.into(), 42_u32.into()]); assert_eq!(EMPTY_WORD, smt.get_value(&key_no_value)); } @@ -314,10 +314,10 @@ fn test_smt_get_value() { 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()]); + RpoDigest::from([2_u32.into(), 2_u32.into(), 2_u32.into(), 2_u32.into()]); let value_1 = [ONE; WORD_SIZE]; - let value_2 = [2_u64.into(); WORD_SIZE]; + let value_2 = [2_u32.into(); WORD_SIZE]; let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap(); @@ -348,8 +348,8 @@ fn test_empty_smt_leaf_serialization() { #[test] fn test_single_smt_leaf_serialization() { let single_leaf = SmtLeaf::new_single( - RpoDigest::from([10_u64.into(), 11_u64.into(), 12_u64.into(), 13_u64.into()]), - [1_u64.into(), 2_u64.into(), 3_u64.into(), 4_u64.into()], + RpoDigest::from([10_u32.into(), 11_u32.into(), 12_u32.into(), 13_u32.into()]), + [1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()], ); let mut serialized = single_leaf.to_bytes(); @@ -364,12 +364,12 @@ fn test_single_smt_leaf_serialization() { fn test_multiple_smt_leaf_serialization_success() { let multiple_leaf = SmtLeaf::new_multiple(vec![ ( - RpoDigest::from([10_u64.into(), 11_u64.into(), 12_u64.into(), 13_u64.into()]), - [1_u64.into(), 2_u64.into(), 3_u64.into(), 4_u64.into()], + RpoDigest::from([10_u32.into(), 11_u32.into(), 12_u32.into(), 13_u32.into()]), + [1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()], ), ( - RpoDigest::from([100_u64.into(), 101_u64.into(), 102_u64.into(), 13_u64.into()]), - [11_u64.into(), 12_u64.into(), 13_u64.into(), 14_u64.into()], + RpoDigest::from([100_u32.into(), 101_u32.into(), 102_u32.into(), 13_u32.into()]), + [11_u32.into(), 12_u32.into(), 13_u32.into(), 14_u32.into()], ), ]) .unwrap(); diff --git a/src/rand/mod.rs b/src/rand/mod.rs index e20471d35..68016ddef 100644 --- a/src/rand/mod.rs +++ b/src/rand/mod.rs @@ -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; diff --git a/src/rand/rpo.rs b/src/rand/rpo.rs index 284355a15..92f428877 100644 --- a/src/rand/rpo.rs +++ b/src/rand/rpo.rs @@ -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::{