Skip to main content
Version: v0.30.0

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.

sha256
pub fn sha256<N>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash.nr#L10-L12

example:

sha256_var
let digest = std::hash::sha256_var([x as u8], 1);
Source code: test_programs/execution_success/sha256/src/main.nr#L17-L19
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::sha256::sha256_var(x, 4);
}
info

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

blake2s
pub fn blake2s<N>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash.nr#L16-L18

example:

fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::blake2s(x);
}
info

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

blake3
pub fn blake3<N>(input: [u8; N]) -> [u8; 32]
Source code: noir_stdlib/src/hash.nr#L22-L24

example:

fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::blake3(x);
}
info

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.

pedersen_hash
pub fn pedersen_hash<N>(input: [Field; N]) -> Field
Source code: noir_stdlib/src/hash.nr#L46-L48

example:

pedersen-hash
use dep::std;

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-L8
info

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.

pedersen_commitment
struct PedersenPoint {
x : Field,
y : Field,
}

pub fn pedersen_commitment<N>(input: [Field; N]) -> PedersenPoint {
Source code: noir_stdlib/src/hash.nr#L27-L34

example:

pedersen-commitment
use dep::std;

fn main(x: Field, y: Field, expected_commitment: std::hash::PedersenPoint) {
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-L9
info

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.

keccak256
pub fn keccak256<N>(input: [u8; N], message_size: u32) -> [u8; 32]
Source code: noir_stdlib/src/hash.nr#L68-L70

example:

keccak256
use dep::std;

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-L22
info

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:

poseidon
use dep::std::hash::poseidon;
use dep::std::hash::poseidon2;

fn main(x1: [Field; 2], y1: pub Field, x2: [Field; 4], y2: pub Field, x3: [Field; 4], y3: Field) {
let hash1 = poseidon::bn254::hash_2(x1);
assert(hash1 == y1);

let hash2 = poseidon::bn254::hash_4(x2);
assert(hash2 == y2);

let hash3 = poseidon2::Poseidon2::hash(x3, x3.len());
assert(hash3 == y3);
}
Source code: test_programs/execution_success/poseidon_bn254_hash/src/main.nr#L1-L15

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);

The above example for Poseidon also includes Poseidon2.

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<N>(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.