Hashed
Defined in: lib/provable/packed.ts:195
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>
Defined in: lib/provable/packed.ts:233
Parameters
hash
value
Returns
Hashed
<T
>
Properties
hash
hash: Field;
Defined in: lib/provable/packed.ts:196
value
value: Unconstrained<T>;
Defined in: lib/provable/packed.ts:197
_innerProvable
static _innerProvable:
| undefined
| ProvableHashable<any>;
Defined in: lib/provable/packed.ts:276
_provable
static _provable:
| undefined
| ProvableHashable<Hashed<any>>;
Defined in: lib/provable/packed.ts:275
Accessors
Constructor
Get Signature
get Constructor(): typeof Hashed
Defined in: lib/provable/packed.ts:278
Returns
typeof Hashed
innerProvable
Get Signature
get static innerProvable(): ProvableHashable<any>
Defined in: lib/provable/packed.ts:282
Returns
ProvableHashable
<any
>
Methods
toFields()
toFields(): Field[]
Defined in: lib/provable/packed.ts:270
Returns
Field
[]
unhash()
unhash(): T
Defined in: lib/provable/packed.ts:260
Unwrap a value from its hashed variant.
Returns
T
_hash()
static _hash(_: any): Field
Defined in: lib/provable/packed.ts:238
Parameters
_
any
Returns
create()
static create<T>(type: WithProvable<ProvableHashable<T>>, hash?: (t: T) => Field): typeof Hashed & {
provable: ProvableHashable<Hashed<T>>;
empty: Hashed<T>;
}
Defined in: lib/provable/packed.ts:202
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?
(t
: T
) => Field
Returns
typeof Hashed
& {
provable
: ProvableHashable
<Hashed
<T
>>;
empty
: Hashed
<T
>;
}
hash()
static hash<T>(value: T, hash?: Field): Hashed<T>
Defined in: lib/provable/packed.ts:251
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?
Returns
Hashed
<T
>