Skip to main content

Unconstrained

o1js / Modules / Unconstrained

Class: Unconstrained\<T>

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

Name
T

Table of contents

Constructors

Properties

Methods

Constructors

constructor

Private new Unconstrained\<T>(isSome, value?)

Type parameters

Name
T

Parameters

NameType
isSomeboolean
value?T

Defined in

lib/circuit-value.ts:533

Properties

option

Private option: { isSome: true ; value: T } | { isSome: false ; value: undefined }

Defined in

lib/circuit-value.ts:529


provable

Static provable: Provable\<Unconstrained\<any>> & { toInput: (x: Unconstrained\<any>) => { fields?: Field[] ; packed?: [Field, number][] } }

Defined in

lib/circuit-value.ts:602

Methods

get

get(): T

Read an unconstrained value.

Note: Can only be called outside provable code.

Returns

T

Defined in

lib/circuit-value.ts:542


set

set(value): void

Modify the unconstrained value.

Parameters

NameType
valueT

Returns

void

Defined in

lib/circuit-value.ts:556


setTo

setTo(value): void

Set the unconstrained value to the same as another Unconstrained.

Parameters

NameType
valueUnconstrained\<T>

Returns

void

Defined in

lib/circuit-value.ts:563


updateAsProver

updateAsProver(compute): void

Update an Unconstrained by a witness computation.

Parameters

NameType
compute(value: T) => T

Returns

void

Defined in

lib/circuit-value.ts:595


from

Static from\<T>(value): Unconstrained\<T>

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

Name
T

Parameters

NameType
valueT

Returns

Unconstrained\<T>

Defined in

lib/circuit-value.ts:578


witness

Static witness\<T>(compute): Unconstrained\<T>

Create an Unconstrained from a witness computation.

Type parameters

Name
T

Parameters

NameType
compute() => T

Returns

Unconstrained\<T>

Defined in

lib/circuit-value.ts:585