From ca5760c809e52f9af70a29bd2a33ff432c82cb52 Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:15:23 +0200 Subject: [PATCH 1/5] wip: refer to next branch --- Cargo.lock | 6 ++---- Cargo.toml | 10 +++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35bbed75..3e2331da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1146,8 +1146,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winter-crypto" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00fbb724d2d9fbfd3aa16ea27f5e461d4fe1d74b0c9e0ed1bf79e9e2a955f4d5" +source = "git+https://github.com/facebook/winterfell?branch=next#9a9377fe1fab12dc0a54f258856e40ac960ddac2" dependencies = [ "blake3", "sha3", @@ -1168,8 +1167,7 @@ dependencies = [ [[package]] name = "winter-rand-utils" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b827c901ab0c316d89812858ff451d60855c0a5c7ae734b098c62a28624181" +source = "git+https://github.com/facebook/winterfell?branch=next#9a9377fe1fab12dc0a54f258856e40ac960ddac2" dependencies = [ "rand", "winter-utils", diff --git a/Cargo.toml b/Cargo.toml index cb7a6af1..9e79c798 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,12 +52,12 @@ num = { version = "0.4", default-features = false, features = ["alloc", "libm"] num-complex = { version = "0.4", default-features = false } rand = { version = "0.8", default-features = false } rand_core = { version = "0.6", default-features = false } -rand-utils = { version = "0.9", package = "winter-rand-utils", optional = true } +rand-utils = { git = "https://github.com/facebook/winterfell", branch = "next", package = "winter-rand-utils", optional = true } serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] } sha3 = { version = "0.10", default-features = false } -winter-crypto = { version = "0.9", default-features = false } -winter-math = { version = "0.9", default-features = false } -winter-utils = { version = "0.9", default-features = false } +winter-crypto = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } +winter-math = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } +winter-utils = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } [dev-dependencies] criterion = { version = "0.5", features = ["html_reports"] } @@ -65,7 +65,7 @@ getrandom = { version = "0.2", features = ["js"] } hex = { version = "0.4", default-features = false, features = ["alloc"] } proptest = "1.5" rand_chacha = { version = "0.3", default-features = false } -rand-utils = { version = "0.9", package = "winter-rand-utils" } +rand-utils = { git = "https://github.com/facebook/winterfell", branch = "next", package = "winter-rand-utils" } seq-macro = { version = "0.3" } [build-dependencies] From a323d76ca2a7ea7c4d0e7915e1abd4e3a002ac86 Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:26:54 +0100 Subject: [PATCH 2/5] chore: use new Winterfell release --- Cargo.lock | 82 +++++++++++++++++++------------------- Cargo.toml | 10 ++--- src/hash/blake/mod.rs | 16 ++++++++ src/hash/rescue/rpo/mod.rs | 7 ++++ src/hash/rescue/rpx/mod.rs | 7 ++++ 5 files changed, 77 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e2331da..120ecbde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstream" -version = "0.6.15" +version = "0.6.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338" dependencies = [ "anstyle", "anstyle-parse", @@ -34,36 +34,36 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" +checksum = "8365de52b16c035ff4fcafe0092ba9390540e3e352870ac09933bebcaa2c8c56" [[package]] name = "anstyle-parse" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.4" +version = "3.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -147,9 +147,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.1.30" +version = "1.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" +checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" dependencies = [ "jobserver", "libc", @@ -231,9 +231,9 @@ checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "colorchoice" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "constant_time_eq" @@ -495,9 +495,9 @@ checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "libm" -version = "0.2.8" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +checksum = "a00419de735aac21d53b0de5ce2c03bd3627277cf471300f27ebc89f7d828047" [[package]] name = "linux-raw-sys" @@ -668,9 +668,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.88" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] @@ -771,9 +771,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", @@ -800,9 +800,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rustix" -version = "0.38.37" +version = "0.38.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" dependencies = [ "bitflags", "errno", @@ -846,18 +846,18 @@ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4" [[package]] name = "serde" -version = "1.0.210" +version = "1.0.213" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.213" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" dependencies = [ "proc-macro2", "quote", @@ -866,9 +866,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.129" +version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dbcf9b78a125ee667ae19388837dd12294b858d101fdd393cb9d5501ef09eb2" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ "itoa", "memchr", @@ -900,9 +900,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.79" +version = "2.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" dependencies = [ "proc-macro2", "quote", @@ -1145,8 +1145,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winter-crypto" -version = "0.9.0" -source = "git+https://github.com/facebook/winterfell?branch=next#9a9377fe1fab12dc0a54f258856e40ac960ddac2" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f9b62f424c65828678ed8e08faf90e7cfc91821453ed26521e0e0c561d5a630" dependencies = [ "blake3", "sha3", @@ -1156,9 +1157,9 @@ dependencies = [ [[package]] name = "winter-math" -version = "0.9.3" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0e685b3b872d82e58a86519294a814b7bc7a4d3cd2c93570a7d80c0c5a1aba" +checksum = "16f3eb5a52d26a46037289f7f4153eb3528fc72c896d9f3b4037df0e102f83d4" dependencies = [ "serde", "winter-utils", @@ -1166,8 +1167,9 @@ dependencies = [ [[package]] name = "winter-rand-utils" -version = "0.9.0" -source = "git+https://github.com/facebook/winterfell?branch=next#9a9377fe1fab12dc0a54f258856e40ac960ddac2" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93be285803ef6d760974490097f1ddd8d2a749632361ff475aac8c4d2c10fb12" dependencies = [ "rand", "winter-utils", @@ -1175,9 +1177,9 @@ dependencies = [ [[package]] name = "winter-utils" -version = "0.9.3" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "961e81e9388877a25db1c034ba38253de2055f569633ae6a665d857a0556391b" +checksum = "5c548030dab203e9b0e5060ce791f5aa84f7672718d8e1cc5e41204d23e1d8dc" [[package]] name = "zerocopy" diff --git a/Cargo.toml b/Cargo.toml index 9e79c798..93310c91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,12 +52,12 @@ num = { version = "0.4", default-features = false, features = ["alloc", "libm"] num-complex = { version = "0.4", default-features = false } rand = { version = "0.8", default-features = false } rand_core = { version = "0.6", default-features = false } -rand-utils = { git = "https://github.com/facebook/winterfell", branch = "next", package = "winter-rand-utils", optional = true } +rand-utils = { version = "0.10", package = "winter-rand-utils", optional = true } serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] } sha3 = { version = "0.10", default-features = false } -winter-crypto = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } -winter-math = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } -winter-utils = { git = "https://github.com/facebook/winterfell", branch = "next", default-features = false } +winter-crypto = { version = "0.10", default-features = false } +winter-math = { version = "0.10", default-features = false } +winter-utils = { version = "0.10", default-features = false } [dev-dependencies] criterion = { version = "0.5", features = ["html_reports"] } @@ -65,7 +65,7 @@ getrandom = { version = "0.2", features = ["js"] } hex = { version = "0.4", default-features = false, features = ["alloc"] } proptest = "1.5" rand_chacha = { version = "0.3", default-features = false } -rand-utils = { git = "https://github.com/facebook/winterfell", branch = "next", package = "winter-rand-utils" } +rand-utils = { version = "0.10", package = "winter-rand-utils" } seq-macro = { version = "0.3" } [build-dependencies] diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index 30ed8482..47b80917 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -4,6 +4,7 @@ use core::{ ops::Deref, slice::from_raw_parts, }; +use std::vec::Vec; use super::{Digest, ElementHasher, Felt, FieldElement, Hasher}; use crate::utils::{ @@ -114,6 +115,11 @@ impl Hasher for Blake3_256 { Self::hash(prepare_merge(values)) } + fn merge_many(values: &[Self::Digest]) -> Self::Digest { + let bytes: Vec = values.iter().flat_map(|v| v.as_bytes()).collect(); + Blake3Digest(blake3::hash(&bytes).into()) + } + fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { let mut hasher = blake3::Hasher::new(); hasher.update(&seed.0); @@ -174,6 +180,11 @@ impl Hasher for Blake3_192 { Blake3Digest(*shrink_bytes(&blake3::hash(bytes).into())) } + fn merge_many(values: &[Self::Digest]) -> Self::Digest { + let bytes: Vec = values.iter().flat_map(|v| v.as_bytes()).collect(); + Blake3Digest(*shrink_bytes(&blake3::hash(&bytes).into())) + } + fn merge(values: &[Self::Digest; 2]) -> Self::Digest { Self::hash(prepare_merge(values)) } @@ -242,6 +253,11 @@ impl Hasher for Blake3_160 { Self::hash(prepare_merge(values)) } + fn merge_many(values: &[Self::Digest]) -> Self::Digest { + let bytes: Vec = values.iter().flat_map(|v| v.as_bytes()).collect(); + Blake3Digest(*shrink_bytes(&blake3::hash(&bytes).into())) + } + fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { let mut hasher = blake3::Hasher::new(); hasher.update(&seed.0); diff --git a/src/hash/rescue/rpo/mod.rs b/src/hash/rescue/rpo/mod.rs index 0d1d87bc..31ca6f35 100644 --- a/src/hash/rescue/rpo/mod.rs +++ b/src/hash/rescue/rpo/mod.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::ops::Range; use super::{ @@ -164,6 +165,12 @@ impl Hasher for Rpo256 { RpoDigest::new(state[DIGEST_RANGE].try_into().unwrap()) } + fn merge_many(values: &[Self::Digest]) -> Self::Digest { + let elements: Vec = + Self::Digest::digests_as_elements(values.iter()).copied().collect(); + Self::hash_elements(&elements) + } + fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { // initialize the state as follows: // - seed is copied into the first 4 elements of the rate portion of the state. diff --git a/src/hash/rescue/rpx/mod.rs b/src/hash/rescue/rpx/mod.rs index 8c6c53b6..36e381b5 100644 --- a/src/hash/rescue/rpx/mod.rs +++ b/src/hash/rescue/rpx/mod.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::ops::Range; use super::{ @@ -170,6 +171,12 @@ impl Hasher for Rpx256 { RpxDigest::new(state[DIGEST_RANGE].try_into().unwrap()) } + fn merge_many(values: &[Self::Digest]) -> Self::Digest { + let elements: Vec = + Self::Digest::digests_as_elements(values.iter()).copied().collect(); + Self::hash_elements(&elements) + } + fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { // initialize the state as follows: // - seed is copied into the first 4 elements of the rate portion of the state. From 21ce15e915a64e4b8ca612f0be5c8e2a2067001f Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:03:26 +0100 Subject: [PATCH 3/5] fix: ci --- CHANGELOG.md | 1 + src/hash/blake/mod.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ee11d27..f0ae728b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.11.0 (2024-10-17) +- Updated Winterfell dependency to v0.10 (#338). - [BREAKING]: renamed `Mmr::open()` into `Mmr::open_at()` and `Mmr::peaks()` into `Mmr::peaks_at()` (#234). - Added `Mmr::open()` and `Mmr::peaks()` which rely on `Mmr::open_at()` and `Mmr::peaks()` respectively (#234). - Standardized CI and Makefile across Miden repos (#323). diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index 47b80917..1b859c64 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -4,7 +4,7 @@ use core::{ ops::Deref, slice::from_raw_parts, }; -use std::vec::Vec; +use alloc::vec::Vec; use super::{Digest, ElementHasher, Felt, FieldElement, Hasher}; use crate::utils::{ From 7f46e2134832007bc33cc6f40b0102ba835be663 Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:06:36 +0100 Subject: [PATCH 4/5] fix: clippy --- src/hash/blake/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index 1b859c64..de42f832 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -1,10 +1,9 @@ -use alloc::string::String; +use alloc::{string::String, vec::Vec}; use core::{ mem::{size_of, transmute, transmute_copy}, ops::Deref, slice::from_raw_parts, }; -use alloc::vec::Vec; use super::{Digest, ElementHasher, Felt, FieldElement, Hasher}; use crate::utils::{ From 95127416b2cca2f9718302d6419e768ac76ed850 Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Tue, 29 Oct 2024 07:33:34 +0100 Subject: [PATCH 5/5] chore: remove extra allocations in merge_many --- src/hash/blake/mod.rs | 13 ++++++++++--- src/hash/rescue/rpo/digest.rs | 10 ++++++++-- src/hash/rescue/rpo/mod.rs | 9 +++------ src/hash/rescue/rpx/digest.rs | 10 ++++++++-- src/hash/rescue/rpx/mod.rs | 9 +++------ 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index de42f832..88ffe089 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -2,7 +2,7 @@ use alloc::{string::String, vec::Vec}; use core::{ mem::{size_of, transmute, transmute_copy}, ops::Deref, - slice::from_raw_parts, + slice::{self, from_raw_parts}, }; use super::{Digest, ElementHasher, Felt, FieldElement, Hasher}; @@ -33,6 +33,14 @@ const DIGEST20_BYTES: usize = 20; #[cfg_attr(feature = "serde", serde(into = "String", try_from = "&str"))] pub struct Blake3Digest([u8; N]); +impl Blake3Digest { + pub fn digests_as_bytes(digests: &[Blake3Digest]) -> &[u8] { + let p = digests.as_ptr(); + let len = digests.len() * N; + unsafe { slice::from_raw_parts(p as *const u8, len) } + } +} + impl Default for Blake3Digest { fn default() -> Self { Self([0; N]) @@ -115,8 +123,7 @@ impl Hasher for Blake3_256 { } fn merge_many(values: &[Self::Digest]) -> Self::Digest { - let bytes: Vec = values.iter().flat_map(|v| v.as_bytes()).collect(); - Blake3Digest(blake3::hash(&bytes).into()) + Blake3Digest(blake3::hash(Blake3Digest::digests_as_bytes(values)).into()) } fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { diff --git a/src/hash/rescue/rpo/digest.rs b/src/hash/rescue/rpo/digest.rs index d79a5adf..67d73e31 100644 --- a/src/hash/rescue/rpo/digest.rs +++ b/src/hash/rescue/rpo/digest.rs @@ -1,5 +1,5 @@ use alloc::string::String; -use core::{cmp::Ordering, fmt::Display, ops::Deref}; +use core::{cmp::Ordering, fmt::Display, ops::Deref, slice}; use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO}; use crate::{ @@ -34,13 +34,19 @@ impl RpoDigest { ::as_bytes(self) } - pub fn digests_as_elements<'a, I>(digests: I) -> impl Iterator + pub fn digests_as_elements_iter<'a, I>(digests: I) -> impl Iterator where I: Iterator, { digests.flat_map(|d| d.0.iter()) } + pub fn digests_as_elements(digests: &[Self]) -> &[Felt] { + let p = digests.as_ptr(); + let len = digests.len() * DIGEST_SIZE; + unsafe { slice::from_raw_parts(p as *const Felt, len) } + } + /// Returns hexadecimal representation of this digest prefixed with `0x`. pub fn to_hex(&self) -> String { bytes_to_hex_string(self.as_bytes()) diff --git a/src/hash/rescue/rpo/mod.rs b/src/hash/rescue/rpo/mod.rs index 31ca6f35..ede50538 100644 --- a/src/hash/rescue/rpo/mod.rs +++ b/src/hash/rescue/rpo/mod.rs @@ -1,4 +1,3 @@ -use alloc::vec::Vec; use core::ops::Range; use super::{ @@ -155,7 +154,7 @@ impl Hasher for Rpo256 { // initialize the state by copying the digest elements into the rate portion of the state // (8 total elements), and set the capacity elements to 0. let mut state = [ZERO; STATE_WIDTH]; - let it = Self::Digest::digests_as_elements(values.iter()); + let it = Self::Digest::digests_as_elements_iter(values.iter()); for (i, v) in it.enumerate() { state[RATE_RANGE.start + i] = *v; } @@ -166,9 +165,7 @@ impl Hasher for Rpo256 { } fn merge_many(values: &[Self::Digest]) -> Self::Digest { - let elements: Vec = - Self::Digest::digests_as_elements(values.iter()).copied().collect(); - Self::hash_elements(&elements) + Self::hash_elements(Self::Digest::digests_as_elements(values)) } fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { @@ -297,7 +294,7 @@ impl Rpo256 { // initialize the state by copying the digest elements into the rate portion of the state // (8 total elements), and set the capacity elements to 0. let mut state = [ZERO; STATE_WIDTH]; - let it = RpoDigest::digests_as_elements(values.iter()); + let it = RpoDigest::digests_as_elements_iter(values.iter()); for (i, v) in it.enumerate() { state[RATE_RANGE.start + i] = *v; } diff --git a/src/hash/rescue/rpx/digest.rs b/src/hash/rescue/rpx/digest.rs index c67d5bdf..8f953fc6 100644 --- a/src/hash/rescue/rpx/digest.rs +++ b/src/hash/rescue/rpx/digest.rs @@ -1,5 +1,5 @@ use alloc::string::String; -use core::{cmp::Ordering, fmt::Display, ops::Deref}; +use core::{cmp::Ordering, fmt::Display, ops::Deref, slice}; use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO}; use crate::{ @@ -34,13 +34,19 @@ impl RpxDigest { ::as_bytes(self) } - pub fn digests_as_elements<'a, I>(digests: I) -> impl Iterator + pub fn digests_as_elements_iter<'a, I>(digests: I) -> impl Iterator where I: Iterator, { digests.flat_map(|d| d.0.iter()) } + pub fn digests_as_elements(digests: &[Self]) -> &[Felt] { + let p = digests.as_ptr(); + let len = digests.len() * DIGEST_SIZE; + unsafe { slice::from_raw_parts(p as *const Felt, len) } + } + /// Returns hexadecimal representation of this digest prefixed with `0x`. pub fn to_hex(&self) -> String { bytes_to_hex_string(self.as_bytes()) diff --git a/src/hash/rescue/rpx/mod.rs b/src/hash/rescue/rpx/mod.rs index 36e381b5..0435cf69 100644 --- a/src/hash/rescue/rpx/mod.rs +++ b/src/hash/rescue/rpx/mod.rs @@ -1,4 +1,3 @@ -use alloc::vec::Vec; use core::ops::Range; use super::{ @@ -161,7 +160,7 @@ impl Hasher for Rpx256 { // initialize the state by copying the digest elements into the rate portion of the state // (8 total elements), and set the capacity elements to 0. let mut state = [ZERO; STATE_WIDTH]; - let it = Self::Digest::digests_as_elements(values.iter()); + let it = Self::Digest::digests_as_elements_iter(values.iter()); for (i, v) in it.enumerate() { state[RATE_RANGE.start + i] = *v; } @@ -172,9 +171,7 @@ impl Hasher for Rpx256 { } fn merge_many(values: &[Self::Digest]) -> Self::Digest { - let elements: Vec = - Self::Digest::digests_as_elements(values.iter()).copied().collect(); - Self::hash_elements(&elements) + Self::hash_elements(Self::Digest::digests_as_elements(values)) } fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest { @@ -300,7 +297,7 @@ impl Rpx256 { // initialize the state by copying the digest elements into the rate portion of the state // (8 total elements), and set the capacity elements to 0. let mut state = [ZERO; STATE_WIDTH]; - let it = RpxDigest::digests_as_elements(values.iter()); + let it = RpxDigest::digests_as_elements_iter(values.iter()); for (i, v) in it.enumerate() { state[RATE_RANGE.start + i] = *v; }