Hash methods
sha256
Given an array of bytes, returns the resulting sha256 hash.
Specify a message_size to hash only the first message_size
bytes of the input.
pub fn sha256<let N: u32>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash/sha256.nr#L7-L9
example:
let digest = std::hash::sha256_var([x as u8], 1);
Source code: test_programs/execution_success/sha256/src/main.nr#L15-L17
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::sha256::sha256_var(x, 4);
}
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#L20-L22
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#L26-L28
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#L51-L53
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#L31-L33
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.
keccak256
Given an array of bytes (u8
), returns the resulting keccak hash as an array of
32 bytes ([u8; 32]
). Specify a message_size to hash only the first
message_size
bytes of the input.
pub fn keccak256<let N: u32>(input: [u8; N], message_size: u32) -> [u8; 32]
Source code: noir_stdlib/src/hash/mod.nr#L117-L119
example:
fn main(x: Field, result: [u8; 32]) {
// We use the `as` keyword here to denote the fact that we want to take just the first byte from the x Field
// The padding is taken care of by the program
let digest = std::hash::keccak256([x as u8], 1);
assert(digest == result);
//#1399: variable message size
let message_size = 4;
let hash_a = std::hash::keccak256([1, 2, 3, 4], message_size);
let hash_b = std::hash::keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size);
assert(hash_a == hash_b);
let message_size_big = 8;
let hash_c = std::hash::keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size_big);
assert(hash_a != hash_c);
}
Source code: test_programs/execution_success/keccak256/src/main.nr#L1-L20
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
mimc_bn254 and mimc
mimc_bn254
is mimc
, but with hardcoded parameters for the BN254 curve. You can use it by
providing an array of Fields, and it returns a Field with the hash. You can use the mimc
method if
you're willing to input your own constants:
fn mimc<N>(x: Field, k: Field, constants: [Field; N], exp : Field) -> Field
otherwise, use the mimc_bn254
method:
fn mimc_bn254<let N: u32>(array: [Field; N]) -> Field
example:
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::mimc::mimc_bn254(x);
}
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
.