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
Unconstrainedcan be empty when compiling, but never empty when running as the prover. (there is no way to create an emptyUnconstrainedin 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
| Name | Type |
|---|---|
isSome | boolean |
value? | T |
Defined in
Properties
option
• Private option: { isSome: true ; value: T } | { isSome: false ; value: undefined }
Defined in
provable
▪ Static provable: Provable\<Unconstrained\<any>> & { toInput: (x: Unconstrained\<any>) => { fields?: Field[] ; packed?: [Field, number][] } }
Defined in
Methods
get
▸ get(): T
Read an unconstrained value.
Note: Can only be called outside provable code.
Returns
T
Defined in
set
▸ set(value): void
Modify the unconstrained value.
Parameters
| Name | Type |
|---|---|
value | T |
Returns
void
Defined in
setTo
▸ setTo(value): void
Set the unconstrained value to the same as another Unconstrained.
Parameters
| Name | Type |
|---|---|
value | Unconstrained\<T> |
Returns
void
Defined in
updateAsProver
▸ updateAsProver(compute): void
Update an Unconstrained by a witness computation.
Parameters
| Name | Type |
|---|---|
compute | (value: T) => T |
Returns
void
Defined in
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
| Name | Type |
|---|---|
value | T |
Returns
Unconstrained\<T>
Defined in
witness
▸ Static witness\<T>(compute): Unconstrained\<T>
Create an Unconstrained from a witness computation.
Type parameters
| Name |
|---|
T |
Parameters
| Name | Type |
|---|---|
compute | () => T |
Returns
Unconstrained\<T>