Hash methods
Many of the common hash methods have been moved outside of the Noir standard library and exist as independent libraries. You can find the complete list of libraries in the Hashes section of the awesome-noir repo, including:
- keccak256
- MiMC
- Poseidon
- RIPEMD160
- sha256
- sha512
sha256 compression
Performs a sha256 compression on an input and initial state, returning the resulting state.
This is a different function than sha256. See this library for sha256 hashing.
pub fn sha256_compression(input: [u32; 16], state: [u32; 8]) -> [u32; 8] {}
Source code: noir_stdlib/src/hash/mod.nr#L11-L13
This is a black box function. Read this section to learn more about black box functions in Noir.
blake2s
Given an array of bytes, returns an array with the Blake2 hash
pub fn blake2s<let N: u32>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash/mod.nr#L28-L30
example:
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::blake2s(x);
}
This is a black box function. Read this section to learn more about black box functions in Noir.
blake3
Given an array of bytes, returns an array with the Blake3 hash
pub fn blake3<let N: u32>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash/mod.nr#L34-L36
example:
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::blake3(x);
}
This is a black box function. Read this section to learn more about black box functions in Noir.
pedersen_hash
Given an array of Fields, returns the Pedersen hash.
pub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field
Source code: noir_stdlib/src/hash/mod.nr#L59-L61
example:
fn main(x: Field, y: Field, expected_hash: Field) {
let hash = std::hash::pedersen_hash([x, y]);
assert_eq(hash, expected_hash);
}
Source code: test_programs/execution_success/pedersen_hash/src/main.nr#L1-L6
This is a black box function. Read this section to learn more about black box functions in Noir.
pedersen_commitment
Given an array of Fields, returns the Pedersen commitment.
pub fn pedersen_commitment<let N: u32>(input: [Field; N]) -> EmbeddedCurvePoint {
Source code: noir_stdlib/src/hash/mod.nr#L39-L41
example:
fn main(x: Field, y: Field, expected_commitment: std::embedded_curve_ops::EmbeddedCurvePoint) {
let commitment = std::hash::pedersen_commitment([x, y]);
assert_eq(commitment.x, expected_commitment.x);
assert_eq(commitment.y, expected_commitment.y);
}
Source code: test_programs/execution_success/pedersen_commitment/src/main.nr#L1-L7
This is a black box function. Read this section to learn more about black box functions in Noir.
keccakf1600
Given an initial [u64; 25]
state, returns the state resulting from applying a keccakf1600 permutation ([u64; 25]
).
pub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {}
Source code: noir_stdlib/src/hash/mod.nr#L16-L18
This is a black box function. Read this section to learn more about black box functions in Noir.
hash_to_field
fn hash_to_field(_input : [Field]) -> Field {}
Calculates the blake2s
hash of the inputs and returns the hash modulo the field modulus to return
a value which can be represented as a Field
.