Hash methods
sha256 compression
Performs a sha256 compression on an input and initial state, returning the resulting state.
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.
poseidon
Given an array of Fields, returns a new Field with the Poseidon Hash. Mind that you need to specify how many inputs are there to your Poseidon function.
// example for hash_1, hash_2 accepts an array of length 2, etc
fn hash_1(input: [Field; 1]) -> Field
example:
use std::hash::poseidon;
fn main(x1: [Field; 2], y1: pub Field, x2: [Field; 4], y2: pub Field) {
let hash1 = poseidon::bn254::hash_2(x1);
assert(hash1 == y1);
let hash2 = poseidon::bn254::hash_4(x2);
assert(hash2 == y2);
}
Source code: test_programs/execution_success/poseidon_bn254_hash/src/main.nr#L1-L11
poseidon 2
Given an array of Fields, returns a new Field with the Poseidon2 Hash. Contrary to the Poseidon
function, there is only one hash and you can specify a message_size to hash only the first
message_size
bytes of the input,
// example for hashing the first three elements of the input
Poseidon2::hash(input, 3);
example:
use std::hash::poseidon2;
fn main(inputs: [Field; 4], expected_hash: Field) {
let hash = poseidon2::Poseidon2::hash(inputs, inputs.len());
assert_eq(hash, expected_hash);
}
Source code: test_programs/execution_success/poseidon2/src/main.nr#L1-L8
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
.