Keccak256
The OpenVM Keccak256 guest library provides two functions for use in your guest code:
keccak256(input: &[u8]) -> [u8; 32]
: Computes the Keccak-256 hash of the input data and returns it as an array of 32 bytes.set_keccak256(input: &[u8], output: &mut [u8; 32])
: Sets the output to the Keccak-256 hash of the input data into the provided output buffer.
See the full example here.
Example
use core::hint::black_box;
use hex::FromHex;
use openvm_keccak256::keccak256;
openvm::entry!(main);
pub fn main() {
let test_vectors = [
(
"",
"C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
),
(
"CC",
"EEAD6DBFC7340A56CAEDC044696A168870549A6A7F6F56961E84A54BD9970B8A",
),
];
for (input, expected_output) in test_vectors.iter() {
let input = Vec::from_hex(input).unwrap();
let expected_output = Vec::from_hex(expected_output).unwrap();
let output = keccak256(&black_box(input));
if output != *expected_output {
panic!();
}
}
}
To be able to import the keccak256
function, add the following to your Cargo.toml
file:
openvm-keccak256 = { git = "https://github.com/openvm-org/openvm.git" }
hex = { version = "0.4.3" }
Config parameters
For the guest program to build successfully add the following to your .toml
file:
[app_vm_config.keccak]