Skip to main content

Hash

const Hash: {
BLAKE2B: {
hash: Bytes;
};
hash: (input: Field[]) => Field;
Keccak256: {
hash: Bytes;
};
Keccak384: {
hash: Bytes;
};
Keccak512: {
hash: Bytes;
};
Poseidon: {
Sponge: typeof Sponge;
Unsafe: {
hashToGroup: Group;
};
hash: Field;
hashAnyLength: Field;
hashPacked: Field;
hashToGroup: Group;
hashWithPrefix: Field;
initialState: [Field, Field, Field];
update: [Field, Field, Field];
};
SHA2: {
hash: <T>(length: T, data: FlexibleBytes) => Bytes;
};
SHA2_256: {
hash: (data: FlexibleBytes) => Bytes;
};
SHA3_256: {
hash: Bytes;
};
SHA3_384: {
hash: Bytes;
};
SHA3_512: {
hash: Bytes;
};
};

Defined in: lib/provable/crypto/hash.ts:11

A collection of hash functions which can be used in provable code.

Type declaration​

BLAKE2B​

BLAKE2B: {
hash: Bytes;
};

BLAKE2B.hash()​

hash(bytes: Bytes): Bytes;
Parameters​
bytes​

Bytes

Returns​

Bytes

hash()​

hash: (input: Field[]) => Field = Poseidon.hash;

Hashes the given field elements using Poseidon. Alias for Poseidon.hash().

let hash = Hash.hash([a, b, c]);

Important: This is by far the most efficient hash function o1js has available in provable code. Use it by default, if no compatibility concerns require you to use a different hash function.

The Poseidon implementation operates over the native Pallas base field and uses parameters generated specifically for the Mina blockchain.

We use a rate of 2, which means that 2 field elements are hashed per permutation. A permutation causes 11 rows in the constraint system.

You can find the full set of Poseidon parameters here.

Parameters​

input​

Field[]

Returns​

Field

Keccak256​

Keccak256: {
hash: Bytes;
};

The pre-NIST Keccak hash function with an output length of 256 bits.

Keccak256.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using Keccak-256.

This is an alias for Keccak.preNist(256, bytes).
See Keccak.preNist for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes

Keccak384​

Keccak384: {
hash: Bytes;
};

The pre-NIST Keccak hash function with an output length of 384 bits.

Keccak384.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using Keccak-384.

This is an alias for Keccak.preNist(384, bytes).
See Keccak.preNist for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes

Keccak512​

Keccak512: {
hash: Bytes;
};

The pre-NIST Keccak hash function with an output length of 512 bits.

Keccak512.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using Keccak-512.

This is an alias for Keccak.preNist(512, bytes).
See Keccak.preNist for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes

Poseidon​

Poseidon: {
Sponge: typeof Sponge;
Unsafe: {
hashToGroup: Group;
};
hash: Field;
hashAnyLength: Field;
hashPacked: Field;
hashToGroup: Group;
hashWithPrefix: Field;
initialState: [Field, Field, Field];
update: [Field, Field, Field];
};

The Poseidon hash function.

See Hash.hash for details and usage examples.

Poseidon.Sponge​

Sponge: typeof Sponge;

Poseidon.Unsafe​

Unsafe: {
hashToGroup: Group;
};

Poseidon.Unsafe.hashToGroup()​

hashToGroup(input: Field[]): Group;

Low-level version of Poseidon.hashToGroup().

Warning: This function is marked unsafe because its output is not deterministic. It returns the square root of a value without constraining which of the two possible square roots is chosen. This allows the prover to choose between two different hashes, which can be a vulnerability if consuming code treats the output as unique.

Parameters​
input​

Field[]

Returns​

Group

Poseidon.hash()​

hash(input: Field[]): Field;
Parameters​
input​

Field[]

Returns​

Field

Poseidon.hashAnyLength()​

hashAnyLength(input: Field[]): Field;
Parameters​
input​

Field[]

Returns​

Field

Poseidon.hashPacked()​

hashPacked<T>(type: WithProvable<Hashable<T>>, value: T): Field;

Hashes a provable type efficiently.

let skHash = Poseidon.hashPacked(PrivateKey, secretKey);

Note: Instead of just doing Poseidon.hash(value.toFields()), this uses the toInput() method on the provable type to pack the input into as few field elements as possible. This saves constraints because packing has a much lower per-field element cost than hashing.

Type Parameters​
T​

T

Parameters​
type​

WithProvable<Hashable<T>>

value​

T

Returns​

Field

Poseidon.hashToGroup()​

hashToGroup(input: Field[]): Group;

Hashes a list of field elements to a point on the Pallas curve.

The output point is deterministic and its discrete log is not efficiently computable.

Parameters​
input​

Field[]

Returns​

Group

Poseidon.hashWithPrefix()​

hashWithPrefix(prefix: string, input: Field[]): Field;
Parameters​
prefix​

string

input​

Field[]

Returns​

Field

Poseidon.initialState()​

initialState(): [Field, Field, Field];
Returns​

[Field, Field, Field]

Poseidon.update()​

update(state: [Field, Field, Field], input: Field[]): [Field, Field, Field];
Parameters​
state​

[Field, Field, Field]

input​

Field[]

Returns​

[Field, Field, Field]

SHA2​

SHA2: {
hash: <T>(length: T, data: FlexibleBytes) => Bytes;
};

The SHA2 hash function with an output length of 224 | 256 | 384 | 512 bits.

SHA2.hash()​

hash: <T>(length: T, data: FlexibleBytes) => Bytes = Gadgets.SHA2.hash;

Hashes the given bytes using SHA2.

This is an alias for Gadgets.SHA2.hash(length,bytes).
See Gadgets.SHA2.hash for details and usage examples.

Implementation of NIST SHA-2 hash Function. Supports output lengths of 224, 256, 384, or 512 bits.

Applies the SHA-2 hash function to a list of big-endian byte-sized Field elements, flexible to handle varying output lengths (224, 256, 384, 512 bits) as specified.

The function accepts Bytes as the input message, which is a type that represents a static-length list of byte-sized field elements (range-checked using Gadgets.rangeCheck8). Alternatively, you can pass plain number[] of Uint8Array to perform a hash outside provable code.

Produces an output of Bytes that conforms to the chosen bit length. Both input and output bytes are big-endian.

Type Parameters​
T​

T extends Length

Parameters​
length​

T

data​

FlexibleBytes

Returns​

Bytes

SHA2_256​

SHA2_256: {
hash: (data: FlexibleBytes) => Bytes;
};

The SHA2 hash function with an output length of 256 bits.

SHA2_256.hash()​

hash: (data: FlexibleBytes) => Bytes = Gadgets.SHA256.hash;

Hashes the given bytes using SHA2-256.

This is an alias for Gadgets.SHA256.hash(bytes).
See Gadgets.SHA256.hash for details and usage examples.

Parameters​
data​

FlexibleBytes

Returns​

Bytes

SHA3_256​

SHA3_256: {
hash: Bytes;
};

The SHA3 hash function with an output length of 256 bits.

SHA3_256.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using SHA3-256.

This is an alias for Keccak.nistSha3(256, bytes).
See Keccak.nistSha3 for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes

SHA3_384​

SHA3_384: {
hash: Bytes;
};

The SHA3 hash function with an output length of 384 bits.

SHA3_384.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using SHA3-384.

This is an alias for Keccak.nistSha3(384, bytes).
See Keccak.nistSha3 for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes

SHA3_512​

SHA3_512: {
hash: Bytes;
};

The SHA3 hash function with an output length of 512 bits.

SHA3_512.hash()​

hash(bytes: Bytes): Bytes;

Hashes the given bytes using SHA3-512.

This is an alias for Keccak.nistSha3(512, bytes).
See Keccak.nistSha3 for details and usage examples.

Parameters​
bytes​

Bytes

Returns​

Bytes