Skip to main content

Unconstrained

Defined in: lib/provable/types/unconstrained.ts:39

Container which holds an unconstrained value. This can be used to pass values between the out-of-circuit blocks in provable code.

Invariants:

  • An Unconstrained's value can only be accessed in auxiliary contexts.
  • An Unconstrained can be empty when compiling, but never empty when running as the prover. (there is no way to create an empty Unconstrained in the prover)

Example

let x = Unconstrained.from(0n);

class MyContract extends SmartContract {
`@method` myMethod(x: Unconstrained<bigint>) {

Provable.witness(Field, () => {
// we can access and modify `x` here
let newValue = x.get() + otherField.toBigInt();
x.set(newValue);

// ...
});

// throws an error!
x.get();
}

Type Parameters

T

Properties

provable

static provable: UnconstrainedProvable<any> & {
empty: () => Unconstrained<any>;
toInput: (x: Unconstrained<any>) => {
fields: Field[];
packed: [Field, number][];
};
};

Defined in: lib/provable/types/unconstrained.ts:109

Type declaration

empty()
empty: () => Unconstrained<any>;
Returns

Unconstrained<any>

toInput()
toInput: (x: Unconstrained<any>) => {
fields: Field[];
packed: [Field, number][];
};
Parameters
x

Unconstrained<any>

Returns
{
fields: Field[];
packed: [Field, number][];
}
fields?
optional fields: Field[];
packed?
optional packed: [Field, number][];

Methods

get()

get(): T

Defined in: lib/provable/types/unconstrained.ts:51

Read an unconstrained value.

Note: Can only be called outside provable code.

Returns

T


set()

set(value: T): void

Defined in: lib/provable/types/unconstrained.ts:65

Modify the unconstrained value.

Parameters

value

T

Returns

void


setTo()

setTo(value: Unconstrained<T>): void

Defined in: lib/provable/types/unconstrained.ts:72

Set the unconstrained value to the same as another Unconstrained.

Parameters

value

Unconstrained<T>

Returns

void


updateAsProver()

updateAsProver(compute: (value: T) => T): void

Defined in: lib/provable/types/unconstrained.ts:102

Update an Unconstrained by a witness computation.

Parameters

compute

(value: T) => T

Returns

void


from()

static from<T>(value: T | Unconstrained<T>): Unconstrained<T>

Defined in: lib/provable/types/unconstrained.ts:87

Create an Unconstrained with the given value.

Note: If T contains provable types, Unconstrained.from is an anti-pattern, because it stores witnesses in a space that's intended to be used outside the proof. Something like the following should be used instead:

let xWrapped = Unconstrained.witness(() => Provable.toConstant(type, x));

Type Parameters

T

Parameters

value

T | Unconstrained<T>

Returns

Unconstrained<T>


withEmpty()

static withEmpty<T>(empty: T): Provable<Unconstrained<T>, T> & {
empty: () => Unconstrained<T>;
toInput: (x: Unconstrained<T>) => {
fields: Field[];
packed: [Field, number][];
};
}

Defined in: lib/provable/types/unconstrained.ts:129

Type Parameters

T

Parameters

empty

T

Returns

Provable<Unconstrained<T>, T> & { empty: () => Unconstrained<T>; toInput: (x: Unconstrained<T>) => { fields: Field[]; packed: [Field, number][]; }; }


witness()

static witness<T>(compute: () => T): Unconstrained<T>

Defined in: lib/provable/types/unconstrained.ts:95

Create an Unconstrained from a witness computation.

Type Parameters

T

Parameters

compute

() => T

Returns

Unconstrained<T>