Skip to main content

Packed

o1js / Modules / Packed

Class: Packed\<T>

Packed<T> is a "packed" representation of any type T.

"Packed" means that field elements which take up fewer than 254 bits are packed together into as few field elements as possible.

For example, you can pack several Bools (1 bit) or UInt32s (32 bits) into a single field element.

Using a packed representation can make sense 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.

Usage:

// define a packed type from a type
let PackedType = Packed.create(MyType);

// pack a value
let packed = PackedType.pack(value);

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

// unpack a value
let value = packed.unpack();

Warning: Packing only makes sense where packing actually reduces the number of field elements. For example, it doesn't make sense to pack a single Bool, because it will be 1 field element before and after packing. On the other hand, it does makes sense to pack a type that holds 10 or 20 Bools.

Type parameters

Name
T

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Packed\<T>(packed, value)

Type parameters

Name
T

Parameters

NameType
packedField[]
valueUnconstrained\<T>

Defined in

lib/provable-types/packed.ts:73

Properties

packed

packed: Field[]

Defined in

lib/provable-types/packed.ts:49


value

value: Unconstrained\<T>

Defined in

lib/provable-types/packed.ts:50


_innerProvable

Static _innerProvable: undefined | ProvableExtended\<any>

Defined in

lib/provable-types/packed.ts:111


_provable

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

Defined in

lib/provable-types/packed.ts:110

Accessors

Constructor

get Constructor(): typeof Packed

Returns

typeof Packed

Defined in

lib/provable-types/packed.ts:113


innerProvable

Static get innerProvable(): ProvableExtended\<any>

Returns

ProvableExtended\<any>

Defined in

lib/provable-types/packed.ts:121


provable

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

Returns

ProvableHashable\<Packed\<any>>

Defined in

lib/provable-types/packed.ts:117

Methods

toFields

toFields(): Field[]

Returns

Field[]

Defined in

lib/provable-types/packed.ts:105


unpack

unpack(): T

Unpack a value.

Returns

T

Defined in

lib/provable-types/packed.ts:90


create

Static create\<T>(type): typeof Packed

Create a packed representation of type. You can then use PackedType.pack(x) to pack a value.

Type parameters

Name
T

Parameters

NameType
typeProvableExtended\<T>

Returns

typeof Packed

Defined in

lib/provable-types/packed.ts:55


pack

Static pack\<T>(x): Packed\<T>

Pack a value.

Type parameters

Name
T

Parameters

NameType
xT

Returns

Packed\<T>

Defined in

lib/provable-types/packed.ts:81