Unconstrained
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 emptyUnconstrained
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][];
};
};
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][];
Source
lib/provable/types/unconstrained.ts:111
Methods
get()
get(): T
Read an unconstrained value.
Note: Can only be called outside provable code.
Returns
T
Source
lib/provable/types/unconstrained.ts:53
set()
set(value: T): void
Modify the unconstrained value.
Parameters
• value: T
Returns
void
Source
lib/provable/types/unconstrained.ts:67
setTo()
setTo(value: Unconstrained<T>): void
Set the unconstrained value to the same as another Unconstrained
.
Parameters
• value: Unconstrained
\<T
>
Returns
void
Source
lib/provable/types/unconstrained.ts:74
updateAsProver()
updateAsProver(compute: (value: T) => T): void
Update an Unconstrained
by a witness computation.
Parameters
• compute
Returns
void
Source
lib/provable/types/unconstrained.ts:104
from()
static from<T>(value: T | Unconstrained<T>): 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
• T
Parameters
• value: T
| Unconstrained
\<T
>
Returns
Unconstrained
\<T
>
Source
lib/provable/types/unconstrained.ts:89
withEmpty()
static withEmpty<T>(empty: T): Provable<Unconstrained<T>, T> & {
"empty": () => Unconstrained<T>;
"toInput": (x: Unconstrained<T>) => {
"fields": Field[];
"packed": [Field, number][];
};
}
Type parameters
• T
Parameters
• empty: T
Returns
Provable
\<Unconstrained
\<T
>, T
> & {
"empty"
: () => Unconstrained
\<T
>;
"toInput"
: (x
: Unconstrained
\<T
>) => {
"fields"
: Field
[];
"packed"
: [Field
, number
][];
};
}
Source
lib/provable/types/unconstrained.ts:131
witness()
static witness<T>(compute: () => T): Unconstrained<T>
Create an Unconstrained
from a witness computation.
Type parameters
• T
Parameters
• compute
Returns
Unconstrained
\<T
>