notes
In this note we assume that all values are elements in a 64-bit field defined by modulus . One of the nice properties of this field is that multiplying two elements in this field with values less than does not overflow field modulus.
In the sections below, we describe how we can implement common operations on u32 values (i.e., 32-bit unsigned integers) in this field very efficiently. This includes arithmetic operations, comparison operations, and bitwise operations.
Vast majority of operations rely only on 16-bit range checks which can be implemented efficiently using a methodology described in this note. Some operations, such as bitwise AND
, OR
, and XOR
require lookups in additional auxiliary tables, which are described here.
Another nice property of this field is that checking whether four 16-bit values form a valid field element can be done relatively cheaply. Assume , , , and are known to be 16-bit values (e.g., less than ), and we want to verify that is a valid field element.
For simplicity, let's denote:
We can then impose the following constraint to verify element validity:
The above constraint holds only if either of the following hold:
To satisfy the latter constraint, the prover would need to set , which is possible only when .
This constraint is sufficient because modulus in binary representation is 32 ones, followed by 31 zeros, followed by a single one:
This implies that the largest possible 64-bit value encoding a valid field element would be 32 ones, followed by 32 zeros:
Thus, for a 64-bit value to encode a valid field element, either the lower 32 bits must be all zeros, or the upper 32 bits must not be all ones (which is ).
Converting field elements into u32 values may be accomplished via two operations U32SPLIT
and U32CAST
. Supporting these operations requires 5 helper registers.
Assume is a field element. We define U32SPLIT
operation as , where contains lower 32-bits of , and contains the upper 32 bits of . The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide five non-deterministic 'hint' values , , , , and such that:
The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume is a field element. We define U32CAST
operation as , where contains lower 32-bits of . The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide five non-deterministic 'hint' values , , , , and such that:
The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume and are known to be 32-bit values. U32ADD
operation computes , where contains the low 32-bits of the result and is the carry bit. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide two non-deterministic 'hint' values and such that:
We also need to enforce constraints against and to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
In implementing efficient big integer arithmetic, it may be convenient to have an "addition with carry" operation which has the following semantics. Assume and are known to be 32-bit values, and is known to be a binary value (either or ). U32ADDC
operation computes , where contains the low 32-bits of the result and is the carry bit. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide two non-deterministic 'hint' values and such that:
We also need to enforce constraints against and to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume and are known to be 32-bit values. U32SUB
operation computes , where contains the 32-bit result in two's complement and is the borrow bit. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide two non-deterministic 'hint' values and such that:
We also need to enforce constraints against and to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume and are known to be 32-bit values. U32MUL
operation computes , where contains the low 32 bits of the result and contains the upper 32 bits of the result. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide five non-deterministic 'hint' values , , , , and such that:
The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
In implementing efficient big integer arithmetic, it may be convenient to have an "multiply-add" operation which has the following semantics. Assume , , and are known to be 32-bit values. U32MADD
operation computes , where contains the low 32 bits of the result and contains the upper 32 bits of the result. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide five non-deterministic 'hint' values , , , , and such that:
The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Note: that the above constraints guarantee the correctness of the operation iff cannot overflow field modules (which is the case for the field with modulus ).
Assume and are known to be 32-bit values. U32DIV
operation computes , where contains the quotient and contains the remainder. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide four non-deterministic 'hint' values , , , such that:
The second constraint enforces that , while the third constraint enforces that . We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range-checks.
Assume and are known to be 32-bit values. We define U32LT
and U32GT
operations as follows:
Both of these operations can be emulated using U32SUB
operation. For example, U32SUB SWAP DROP
would be equivalent to U32LT
, and U32SUB SWAP DROP NOT
would be equivalent to U32GT
. But, if we wanted to have these operations execute in just a single VM cycle, we could do it as described below.
We do not cover equality comparisons in this note because equality comparisons for field elements and u32 values are identical.
Stack transition for U32LT
operation would look as follows:
To facilitate this operation, the prover needs to provide four non-deterministic 'hint' values and such that:
Here, and hold the lower and upper 16 bits of in two's complement. Thus, we also need to enforce constraints against and to make sure that they are 16-bit values, and this can be done via permutation-based range-checks.
Stack transition for U32GT
operation looks identical to the stack transition of the U32LT
operation, but the constraint we need to enforce are slightly different. Specifically:
Here, and hold the lower and upper 16 bits of in two's complement (rather than ). And we also need to enforce additional constraints to make sure that and are 16-bit values.
To perform u32 bit shift operations in a single VM cycle, we need to introduce an additional (6th) helper register. We also need to rely on a lookup table which maps integers in the range between and to the corresponding powers of two (i.e., , , etc.). AIR for such a table can be defined relatively easily.
Assume is known to be a 32-bit value and is an integer in the range . U32SHL
operation computes , where is the result of performing a bitwise shift of by bits to the left. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide six non-deterministic 'hint' values , , , , , and such that:
The first constraint needs to be enforced via a permutation-based lookup table. The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume is known to be a 32-bit value and is an integer in the range . U32SHR
operation computes , where is the result of performing a bitwise shift of by bits to the right. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide six non-deterministic 'hint' values , , , , , and such that:
The first constraint needs to be enforced via a permutation-based lookup table. The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Similarly to bit shift operations, performing bit rotation operations in a single VM cycle requires a 6th helper register and also relies on a lookup table which maps integers in the range between and to the corresponding powers of two.
Assume is known to be a 32-bit value and is an integer in the range . U32ROTL
operation computes , where is the result of performing a bitwise rotation of by bits to the left. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide six non-deterministic 'hint' values , , , , , and such that:
The first constraint needs to be enforced via a permutation-based lookup table. The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume is known to be a 32-bit value and is an integer in the range . U32ROTR
operation computes , where is the result of performing a bitwise rotation of by bits to the right. The diagram below illustrates this graphically.
To facilitate this operation, the prover needs to provide six non-deterministic 'hint' values , , , , and such that:
The first constraint needs to be enforced via a permutation-based lookup table. The last constraint ensures that the decomposition of into four 16-bit values is done in such a way that these values encode a valid field element (as described here). We also need to enforce constraints against , , , to make sure that they are 16-bit values, and this can be done via permutation-based range checks.
Assume and are known to be a 32-bit values. U32AND
operation computes , where is the result of performing a bitwise AND of and . The diagram below illustrates this graphically.
To facilitate this operation, we will need to perform a lookup in a table described here. The prover will also need to provide a non-deterministic 'hint' for the lookup (row address ). The lookup in the table can be accomplished by including the following values into the lookup product:
Bitwise OR operation U32OR
can be performed in the same way as the bitwise AND operation, but we'll need to use a different lookup table - the one specialized for computing bitwise OR operations.
Bitwise XOR operation U32XOR
can be performed in the same way as the bitwise AND operation, but we'll need to use a different lookup table - the one specialized for computing bitwise XOR operations.
Assume is known to be a 32-bit value. U32NOT
operation computes , where is a bitwise negation of . The diagram below illustrates this graphically.
To facilitate this operation we need to enforce the following constraints: