SHA-2
The OpenVM SHA-2 guest library provides access to a set of accelerated SHA-2 family hash functions. Currently, it supports the following:
- SHA-256
SHA-256
Refer here for more details on SHA-256.
For SHA-256, the SHA2 guest library provides two functions for use in your guest code:
sha256(input: &[u8]) -> [u8; 32]
: Computes the SHA-256 hash of the input data and returns it as an array of 32 bytes.set_sha256(input: &[u8], output: &mut [u8; 32])
: Sets the output to the SHA-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_sha2::sha256;
openvm::entry!(main);
pub fn main() {
let test_vectors = [(
"",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
)];
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 = sha256(&black_box(input));
if output != *expected_output {
panic!();
}
}
}
To be able to import the sha256
function, add the following to your Cargo.toml
file:
openvm-sha2 = { 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.sha256]