-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40f62d4
commit 3bf0cd2
Showing
29 changed files
with
253 additions
and
240 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::types::Buffer; | ||
use num_bigint::BigUint; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
use sha2::{Digest, Sha256}; | ||
|
||
/// Public values for the prover. | ||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct SP1PublicValues { | ||
buffer: Buffer, | ||
} | ||
|
||
impl SP1PublicValues { | ||
/// Create a new `SP1PublicValues`. | ||
pub const fn new() -> Self { | ||
Self { buffer: Buffer::new() } | ||
} | ||
|
||
pub fn raw(&self) -> String { | ||
format!("0x{}", hex::encode(self.buffer.data.clone())) | ||
} | ||
|
||
/// Create a `SP1PublicValues` from a slice of bytes. | ||
pub fn from(data: &[u8]) -> Self { | ||
Self { buffer: Buffer::from(data) } | ||
} | ||
|
||
pub fn as_slice(&self) -> &[u8] { | ||
self.buffer.data.as_slice() | ||
} | ||
|
||
pub fn to_vec(&self) -> Vec<u8> { | ||
self.buffer.data.clone() | ||
} | ||
|
||
/// Read a value from the buffer. | ||
pub fn read<T: Serialize + DeserializeOwned>(&mut self) -> T { | ||
self.buffer.read() | ||
} | ||
|
||
/// Read a slice of bytes from the buffer. | ||
pub fn read_slice(&mut self, slice: &mut [u8]) { | ||
self.buffer.read_slice(slice); | ||
} | ||
|
||
/// Write a value to the buffer. | ||
pub fn write<T: Serialize>(&mut self, data: &T) { | ||
self.buffer.write(data); | ||
} | ||
|
||
/// Write a slice of bytes to the buffer. | ||
pub fn write_slice(&mut self, slice: &[u8]) { | ||
self.buffer.write_slice(slice); | ||
} | ||
|
||
/// Hash the public values. | ||
pub fn hash(&self) -> Vec<u8> { | ||
let mut hasher = Sha256::new(); | ||
hasher.update(self.buffer.data.as_slice()); | ||
hasher.finalize().to_vec() | ||
} | ||
|
||
/// 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)); | ||
/// ``` | ||
pub fn hash_bn254(&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 { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.buffer.data | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_hash_public_values() { | ||
let test_hex = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; | ||
let test_bytes = hex::decode(test_hex).unwrap(); | ||
|
||
let mut public_values = SP1PublicValues::new(); | ||
public_values.write_slice(&test_bytes); | ||
let hash = public_values.hash_bn254(); | ||
|
||
let expected_hash = "1ce987d0a7fcc2636fe87e69295ba12b1cc46c256b369ae7401c51b805ee91bd"; | ||
let expected_hash_biguint = BigUint::from_bytes_be(&hex::decode(expected_hash).unwrap()); | ||
|
||
assert_eq!(hash, expected_hash_biguint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.