Skip to content

Commit

Permalink
add hash_public_values
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed May 14, 2024
1 parent d20334f commit 6f9eb17
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
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 = "0.4.5"

[dev-dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
Expand Down
14 changes: 10 additions & 4 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -129,18 +130,23 @@ 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));

Check failure on line 137 in core/src/io.rs

View workflow job for this annotation

GitHub Actions / Test (x86-64)

cannot find value `publicValues` in this scope

Check failure on line 137 in core/src/io.rs

View workflow job for this annotation

GitHub Actions / Test (x86-64)

cannot find function `sha256` in this scope

Check failure on line 137 in core/src/io.rs

View workflow job for this annotation

GitHub Actions / Test (x86-64)

cannot find function `uint256` in this scope

Check failure on line 137 in core/src/io.rs

View workflow job for this annotation

GitHub Actions / Test (x86-64)

cannot find function `bytes32` in this scope
/// ```
pub fn hash_public_values(&self) -> BigUint {
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;
hash[0] &= 0b00011111;

BabyBearPoseidon2::from_bytes(hasher.finalize().as_slice()).unwrap()
BigUint::from_bytes_be(&hash)
}
}

Expand Down

0 comments on commit 6f9eb17

Please sign in to comment.