In this document, we explain how the field operations related to Circle STARK are done in the Bitcoin Wildlife Sanctuary's implementation in the Bitcoin script. This implementation was a fork from the BitVM implementation of M31 and BabyBear that focuses more on M31-specific optimization (such as two-layer Karatsuba for QM31).
There are three fields being implemented:
As follows, we explain the algorithm that we use to perform field operations (focusing on addition and multiplication). For readibility, we would not present the Bitcoin script. Readers can find the corresponding scripts in the GitHub repository. Instead, we present pseudocode. All the pseudocode is running over the Bitcoin integers.
In the following, we use
To add two numbers
The reason for subtracting
To multiply two numbers
The lookup table enables the algorithm to perform fewer than 31 additions. Note that the lookup table includes elements that have already been subtracted by
We now move our attention into CM31. Addition is trivial as it is just adding the real part and the imaginary part separately. So, we will now focus on multiplication.
We can represent the two CM31 elements as
The naive way is to compute the four products
One can see that this algorithm only uses three M31 multiplications.
Similarly, addition in QM31 is trivial as it is adding the first element and the second element, both of which are CM31, separately. Here, we describe the multiplication algorithm, which is slightly more complicated as we need to multiply the square of the nonresidue.
Let us represent a QM31 element as
Here
The algorithm is as follows.
One can see that it uses three CM31 multiplications, which also means that QM31 multiplication in total takes 9 M31 multiplications.
It is useful to note that since