Skip to main content

Packed

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

T

Constructors

new Packed(packed, value)

new Packed<T>(packed: Field[], value: Unconstrained<T>): Packed<T>

Parameters

packed: Field[]

value: Unconstrained\<T>

Returns

Packed\<T>

Source

lib/provable/packed.ts:77

Properties

packed

packed: Field[];

Source

lib/provable/packed.ts:46


value

value: Unconstrained<T>;

Source

lib/provable/packed.ts:47


_innerProvable

static _innerProvable: undefined | ProvableExtended<any>;

Source

lib/provable/packed.ts:119


_provable

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

Source

lib/provable/packed.ts:118

Accessors

Constructor

get Constructor(): typeof Packed

Returns

typeof Packed

Source

lib/provable/packed.ts:121


innerProvable

get static innerProvable(): ProvableExtended<any>

Returns

ProvableExtended\<any>

Source

lib/provable/packed.ts:125

Methods

toFields()

toFields(): Field[]

Returns

Field[]

Source

lib/provable/packed.ts:113


unpack()

unpack(): T

Unpack a value.

Returns

T

Source

lib/provable/packed.ts:98


create()

static create<T>(type: ProvableExtended<T>): typeof Packed & {
provable: ProvableHashable<Packed<T>>;
}

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

Type parameters

T

Parameters

type: ProvableExtended\<T>

Returns

typeof Packed & { provable: ProvableHashable\<Packed\<T>>; }

Source

lib/provable/packed.ts:52


pack()

static pack<T>(x: T): Packed<T>

Pack a value.

Type parameters

T

Parameters

x: T

Returns

Packed\<T>

Source

lib/provable/packed.ts:85