Skip to main content

Hashed

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

Warning: When wrapping a type with Hashed, make sure that that type is safe to automatically pack and unpack in provable code. In particular, do not use Hashed with types that define a custom toInput() (specifying a certain bit packing) but no corresponding check() method (that constrains the bit lengths of the packed parts).

Type parameters

T

Constructors

new Hashed()

new Hashed<T>(hash: Field, value: Unconstrained<T>): Hashed<T>

Parameters

hash: Field

value: Unconstrained\<T>

Returns

Hashed\<T>

Source

lib/provable/packed.ts:241

Properties

hash

hash: Field;

Source

lib/provable/packed.ts:204


value

value: Unconstrained<T>;

Source

lib/provable/packed.ts:205


_innerProvable

static _innerProvable: undefined | ProvableHashable<any>;

Source

lib/provable/packed.ts:288


_provable

static _provable: undefined | ProvableHashable<Hashed<any>>;

Source

lib/provable/packed.ts:287

Accessors

Constructor

get Constructor(): typeof Hashed

Returns

typeof Hashed

Source

lib/provable/packed.ts:290


innerProvable

get static innerProvable(): ProvableHashable<any>

Returns

ProvableHashable\<any>

Source

lib/provable/packed.ts:294

Methods

toFields()

toFields(): Field[]

Returns

Field[]

Source

lib/provable/packed.ts:282


unhash()

unhash(): T

Unwrap a value from its hashed variant.

Returns

T

Source

lib/provable/packed.ts:270


_hash()

static _hash(_: any): Field

Parameters

_: any

Returns

Field

Source

lib/provable/packed.ts:246


create()

static create<T>(type: WithProvable<ProvableHashable<T>>, hash?: (t: T) => Field): typeof Hashed & {
"provable": ProvableHashable<Hashed<T>>;
"empty": Hashed<T>;
}

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

Type parameters

T

Parameters

type: WithProvable\<ProvableHashable\<T>>

hash?

Returns

typeof Hashed & { "provable": ProvableHashable\<Hashed\<T>>; "empty": Hashed\<T>; }

Source

lib/provable/packed.ts:210


hash()

static hash<T>(value: T, hash?: Field): Hashed<T>

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

let hashed = HashedType.hash(value);

Optionally, if you already have the hash, you can pass it in and avoid recomputing it.

Type parameters

T

Parameters

value: T

hash?: Field

Returns

Hashed\<T>

Source

lib/provable/packed.ts:259