Skip to content

Commit

Permalink
docs: hashablekey trait (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 authored Dec 3, 2024
1 parent 15be73d commit a3de183
Showing 1 changed file with 16 additions and 1 deletion.
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

0 comments on commit a3de183

Please sign in to comment.