Hashing functions
Garaga provides implementations of common hash functions optimized for Cairo.
SHA-512
A pure Cairo implementation of SHA-512 following RFC-6234. This is used internally for EdDSA signature verification but is also available for general use.
Usage
use garaga::hashes::sha_512::sha512;
fn hash_message() {
// Hash a single byte
let input: Array<u8> = array![49]; // ASCII '1'
let hash: Array<u8> = sha512(input);
// hash is a 64-byte (512-bit) array
assert!(hash.len() == 64);
}
fn hash_empty() {
// Hash empty input
let empty: Array<u8> = array![];
let hash = sha512(empty);
// SHA-512 of empty string
assert!(
hash.span() == array![
0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
].span()
);
}
fn hash_text() {
// Hash arbitrary text
let msg: Array<u8> = array![
0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70,
0x73, 0x75, 0x6D // "Lorem ipsum"
];
let hash = sha512(msg);
// Returns 64-byte hash
}Function Signature
Input:
Array<u8>- The message bytes to hashOutput:
Array<u8>- 64-byte (512-bit) hash result
Poseidon (BN254 Scalar Field)
A ZK-friendly hash function operating on the BN254 scalar field. This implementation is compatible with:
Circom Poseidon (
nInputs=2)Noir Poseidon (
std::hash::poseidon::hash::hash_2)
Cross-Language Example
The same hash computed in Noir and Cairo:
Function Signature
Input: Two
u384field elementsOutput:
u384- The Poseidon hash result
Last updated
Was this helpful?