From d20334ff01cf8d2c6ef6150e853cf8cb3b7f5ab3 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:04:53 -0700 Subject: [PATCH 1/9] fix --- core/src/io.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/io.rs b/core/src/io.rs index 28698d1e69..b946b56f74 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -2,6 +2,7 @@ use crate::{ stark::{ShardProof, StarkVerifyingKey}, utils::{BabyBearPoseidon2, Buffer}, }; +use k256::sha2::{Digest, Sha256}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Standard input for the prover. @@ -127,6 +128,20 @@ impl SP1PublicValues { pub fn write_slice(&mut self, slice: &[u8]) { self.buffer.write_slice(slice); } + + /// Hash the public values to get a Bn254 value. + pub fn hash_public_values(&self) -> Bn254Fr { + let mut hasher = Sha256::new(); + hasher.update(self.buffer.data.as_slice()); + let hash_result = hasher.finalize(); + + let mut hash = hash_result.to_vec(); + + // Mask the top 3 bits. + hash[0] &= 0b00111111; + + BabyBearPoseidon2::from_bytes(hasher.finalize().as_slice()).unwrap() + } } impl AsRef<[u8]> for SP1PublicValues { From 6f9eb1752d20dbdba1e3e8a061708debc13a68b1 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:17:32 -0700 Subject: [PATCH 2/9] add hash_public_values --- Cargo.lock | 1 + core/Cargo.toml | 1 + core/src/io.rs | 14 ++++++++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a0e9cb918..49ea026d93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4305,6 +4305,7 @@ dependencies = [ "log", "nohash-hasher", "num", + "num-bigint 0.4.5", "num_cpus", "p3-air", "p3-baby-bear", diff --git a/core/Cargo.toml b/core/Cargo.toml index 4758fc939b..09b34f352d 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -59,6 +59,7 @@ web-time = "1.1.0" rayon-scan = "0.1.1" serial_test = "3.1.1" thiserror = "1.0.60" +num-bigint = "0.4.5" [dev-dependencies] tiny-keccak = { version = "2.0.2", features = ["keccak"] } diff --git a/core/src/io.rs b/core/src/io.rs index b946b56f74..f18be98fe0 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -3,6 +3,7 @@ use crate::{ utils::{BabyBearPoseidon2, Buffer}, }; use k256::sha2::{Digest, Sha256}; +use num_bigint::BigUint; use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Standard input for the prover. @@ -129,8 +130,13 @@ impl SP1PublicValues { self.buffer.write_slice(slice); } - /// Hash the public values to get a Bn254 value. - pub fn hash_public_values(&self) -> Bn254Fr { + /// Hash the public values, mask the top 3 bits and return a BigUint. + /// Matches the implementation of hashPublicValues in the Solidity verifier. + /// + /// ``` + /// return sha256(publicValues) & bytes32(uint256((1 << 253) - 1)); + /// ``` + pub fn hash_public_values(&self) -> BigUint { let mut hasher = Sha256::new(); hasher.update(self.buffer.data.as_slice()); let hash_result = hasher.finalize(); @@ -138,9 +144,9 @@ impl SP1PublicValues { let mut hash = hash_result.to_vec(); // Mask the top 3 bits. - hash[0] &= 0b00111111; + hash[0] &= 0b00011111; - BabyBearPoseidon2::from_bytes(hasher.finalize().as_slice()).unwrap() + BigUint::from_bytes_be(&hash) } } From ec74885b410b44751a068f694456fb92a5db8eeb Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:31:32 -0700 Subject: [PATCH 3/9] test_hash_public_values --- core/src/io.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/src/io.rs b/core/src/io.rs index f18be98fe0..9253d9a31f 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -146,6 +146,8 @@ impl SP1PublicValues { // Mask the top 3 bits. hash[0] &= 0b00011111; + println!("{:?}", hex::encode(&hash)); + BigUint::from_bytes_be(&hash) } } @@ -193,3 +195,23 @@ pub mod proof_serde { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hash_public_values() { + let random_hex = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; + let random_bytes = hex::decode(random_hex).unwrap(); + + let expected_hash = "1ce987d0a7fcc2636fe87e69295ba12b1cc46c256b369ae7401c51b805ee91bd"; + let expected_hash_biguint = BigUint::from_bytes_be(&hex::decode(expected_hash).unwrap()); + + let mut public_values = SP1PublicValues::new(); + public_values.write_slice(&random_bytes); + let hash = public_values.hash_public_values(); + + assert_eq!(hash, expected_hash_biguint); + } +} From d130a87ba2cb144fa34d849b7f00a080a1981ebd Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:34:03 -0700 Subject: [PATCH 4/9] fix --- core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 09b34f352d..60f98dc7cb 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -59,7 +59,7 @@ web-time = "1.1.0" rayon-scan = "0.1.1" serial_test = "3.1.1" thiserror = "1.0.60" -num-bigint = "0.4.5" +num-bigint = { version = "0.4.3", default-features = false } [dev-dependencies] tiny-keccak = { version = "2.0.2", features = ["keccak"] } From 0c293bc4f9891f1f4ce288d0b27a56a77356e568 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:37:49 -0700 Subject: [PATCH 5/9] fix --- core/src/io.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/src/io.rs b/core/src/io.rs index 9253d9a31f..c44b7f0df5 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -146,8 +146,6 @@ impl SP1PublicValues { // Mask the top 3 bits. hash[0] &= 0b00011111; - println!("{:?}", hex::encode(&hash)); - BigUint::from_bytes_be(&hash) } } From 61b7df173201c95d5725b4d6bca16ab19ab85c06 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:38:18 -0700 Subject: [PATCH 6/9] fix --- core/src/io.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/io.rs b/core/src/io.rs index c44b7f0df5..09c6774243 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -137,15 +137,16 @@ impl SP1PublicValues { /// return sha256(publicValues) & bytes32(uint256((1 << 253) - 1)); /// ``` pub fn hash_public_values(&self) -> BigUint { + // Hash the public values. let mut hasher = Sha256::new(); hasher.update(self.buffer.data.as_slice()); let hash_result = hasher.finalize(); - let mut hash = hash_result.to_vec(); // Mask the top 3 bits. hash[0] &= 0b00011111; + // Return the masked hash as a BigUint. BigUint::from_bytes_be(&hash) } } From 9d899f443298eab53d5489c584d859b11034af7b Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 18:43:29 -0700 Subject: [PATCH 7/9] fix --- core/src/io.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/io.rs b/core/src/io.rs index 09c6774243..db9197853a 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -131,10 +131,10 @@ impl SP1PublicValues { } /// Hash the public values, mask the top 3 bits and return a BigUint. - /// Matches the implementation of hashPublicValues in the Solidity verifier. + /// Matches the implementation of `hashPublicValues` in the Solidity verifier. /// - /// ``` - /// return sha256(publicValues) & bytes32(uint256((1 << 253) - 1)); + /// ```solidity + /// sha256(publicValues) & bytes32(uint256((1 << 253) - 1)); /// ``` pub fn hash_public_values(&self) -> BigUint { // Hash the public values. From 6bd5b202128a3958e5dc0f6472bfaf2e5f3e6583 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 19:05:08 -0700 Subject: [PATCH 8/9] reorder --- core/src/io.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/io.rs b/core/src/io.rs index db9197853a..3a4c99ae87 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -201,16 +201,16 @@ mod tests { #[test] fn test_hash_public_values() { - let random_hex = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; - let random_bytes = hex::decode(random_hex).unwrap(); - - let expected_hash = "1ce987d0a7fcc2636fe87e69295ba12b1cc46c256b369ae7401c51b805ee91bd"; - let expected_hash_biguint = BigUint::from_bytes_be(&hex::decode(expected_hash).unwrap()); + let test_hex = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; + let test_bytes = hex::decode(test_hex).unwrap(); let mut public_values = SP1PublicValues::new(); - public_values.write_slice(&random_bytes); + public_values.write_slice(&test_bytes); let hash = public_values.hash_public_values(); + let expected_hash = "1ce987d0a7fcc2636fe87e69295ba12b1cc46c256b369ae7401c51b805ee91bd"; + let expected_hash_biguint = BigUint::from_bytes_be(&hex::decode(expected_hash).unwrap()); + assert_eq!(hash, expected_hash_biguint); } } From 524b20e4fb473a5e80e7f53b7d13275bce46fb6b Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Mon, 13 May 2024 19:16:07 -0700 Subject: [PATCH 9/9] nit: comment --- core/src/io.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/io.rs b/core/src/io.rs index 3a4c99ae87..a295a8ead2 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -130,8 +130,8 @@ impl SP1PublicValues { self.buffer.write_slice(slice); } - /// Hash the public values, mask the top 3 bits and return a BigUint. - /// Matches the implementation of `hashPublicValues` in the Solidity verifier. + /// Hash the public values, mask the top 3 bits and return a BigUint. Matches the implementation + /// of `hashPublicValues` in the Solidity verifier. /// /// ```solidity /// sha256(publicValues) & bytes32(uint256((1 << 253) - 1));