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

docs: hashablekey trait #1653

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all 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
17 changes: 16 additions & 1 deletion crates/prover/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,33 @@ pub trait HashableKey {
/// Hash the key into a digest of BabyBear elements.
fn hash_babybear(&self) -> [BabyBear; DIGEST_SIZE];

/// Hash the key into a digest of u32 elements.
/// Hash the key into a digest of u32 elements.
fn hash_u32(&self) -> [u32; DIGEST_SIZE];

/// Hash the key into a Bn254Fr element.
fn hash_bn254(&self) -> Bn254Fr {
babybears_to_bn254(&self.hash_babybear())
}

/// Hash the key into a 32 byte hex string, prefixed with "0x".
///
/// This is ideal for generating a vkey hash for onchain verification.
fn bytes32(&self) -> String {
let vkey_digest_bn254 = self.hash_bn254();
format!("0x{:0>64}", vkey_digest_bn254.as_canonical_biguint().to_str_radix(16))
}

/// Hash the key into a 32 byte array.
///
/// This has the same value as `bytes32`, but as a raw byte array.
fn bytes32_raw(&self) -> [u8; 32] {
let vkey_digest_bn254 = self.hash_bn254();
let vkey_bytes = vkey_digest_bn254.as_canonical_biguint().to_bytes_be();
let mut result = [0u8; 32];
result[1..].copy_from_slice(&vkey_bytes);
result
}

/// Hash the key into a digest of bytes elements.
fn hash_bytes(&self) -> [u8; DIGEST_SIZE * 4] {
words_to_bytes_be(&self.hash_u32())
Expand Down
Loading