Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hash_public_values #730

Merged
merged 9 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { version = "0.4.3", default-features = false }

[dev-dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
Expand Down
42 changes: 42 additions & 0 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::{
stark::{ShardProof, StarkVerifyingKey},
utils::{BabyBearPoseidon2, Buffer},
};
use k256::sha2::{Digest, Sha256};
use num_bigint::BigUint;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Standard input for the prover.
Expand Down Expand Up @@ -127,6 +129,26 @@ impl SP1PublicValues {
pub fn write_slice(&mut self, slice: &[u8]) {
self.buffer.write_slice(slice);
}

/// Hash the public values, mask the top 3 bits and return a BigUint.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment doens't seem to span 100 columns?

/// Matches the implementation of hashPublicValues in the Solidity verifier.
///
/// ```
/// return sha256(publicValues) & bytes32(uint256((1 << 253) - 1));
ratankaliani marked this conversation as resolved.
Show resolved Hide resolved
/// ```
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)
}
}

impl AsRef<[u8]> for SP1PublicValues {
Expand Down Expand Up @@ -172,3 +194,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);
}
}
Loading