Skip to main content

Hashed

o1js / Modules / Hashed

Class: Hashed\<T>

Hashed<T> represents a type T by its hash.

Since a hash is only a single field element, this can be more efficient in provable code where the number of constraints depends on the number of field elements per value.

For example, Provable.if(bool, x, y) takes O(n) constraints, where n is the number of field elements in x and y. With Hashed, this is reduced to O(1).

The downside is that you will pay the overhead of hashing your values, so it helps to experiment in which parts of your code a hashed representation is beneficial.

Usage:

// define a hashed type from a type
let HashedType = Hashed.create(MyType);

// hash a value
let hashed = HashedType.hash(value);

// ... operations on hashes, more efficient than on plain values ...

// unhash to get the original value
let value = hashed.unhash();

Type parameters

Name
T

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Hashed\<T>(hash, value)

Type parameters

Name
T

Parameters

NameType
hashField
valueUnconstrained\<T>

Defined in

lib/provable-types/packed.ts:200

Properties

hash

hash: Field

Defined in

lib/provable-types/packed.ts:171


value

value: Unconstrained\<T>

Defined in

lib/provable-types/packed.ts:172


_innerProvable

Static _innerProvable: undefined | ProvableHashable\<any>

Defined in

lib/provable-types/packed.ts:238


_provable

Static _provable: undefined | ProvableHashable\<Hashed\<any>>

Defined in

lib/provable-types/packed.ts:237

Accessors

Constructor

get Constructor(): typeof Hashed

Returns

typeof Hashed

Defined in

lib/provable-types/packed.ts:240


innerProvable

Static get innerProvable(): ProvableHashable\<any>

Returns

ProvableHashable\<any>

Defined in

lib/provable-types/packed.ts:248


provable

Static get provable(): ProvableHashable\<Hashed\<any>>

Returns

ProvableHashable\<Hashed\<any>>

Defined in

lib/provable-types/packed.ts:244

Methods

toFields

toFields(): Field[]

Returns

Field[]

Defined in

lib/provable-types/packed.ts:232


unhash

unhash(): T

Unwrap a value from its hashed variant.

Returns

T

Defined in

lib/provable-types/packed.ts:220


_hash

Static _hash(_): Field

Parameters

NameType
_any

Returns

Field

Defined in

lib/provable-types/packed.ts:205


create

Static create\<T>(type, hash?): typeof Hashed

Create a hashed representation of type. You can then use HashedType.hash(x) to wrap a value in a Hashed.

Type parameters

Name
T

Parameters

NameType
typeProvableHashable\<T>
hash?(t: T) => Field

Returns

typeof Hashed

Defined in

lib/provable-types/packed.ts:177


hash

Static hash\<T>(value): Hashed\<T>

Wrap a value, and represent it by its hash in provable code.

Type parameters

Name
T

Parameters

NameType
valueT

Returns

Hashed\<T>

Defined in

lib/provable-types/packed.ts:212