Try โ€‚โ€‰HackMD

How to do Circle STARK math in Bitcoin?

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:

  • M31: The prime field under the prime modulus
    231โˆ’1
    . This is where the FFT in Circle STARK is defined over. The trace in STARK is also encoded as M31 elements.
  • CM31: A degree-2 extension field of M31, under the irreducible polynomial
    x2+1
    . This is in essence the complex field
    (a+ib)
    where
    i
    is the imaginary unit.
  • QM31: A degree-2 extension field of CM31, under another irreducible polynomial
    y2โˆ’2โˆ’i
    . One can write an QM31 element as
    (a+ib)+j(c+id)
    , where
    a,b,c,d
    are M31 elements. Probabilistic tests in Circle STARK are done in this field because it is big enough to offer a sufficient security level for these tests.

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.

M31

In the following, we use

q=231โˆ’1 to represent the prime.

To add two numbers

a and
b
, the script works as follows:

  • Compute
    bโ€ฒโ†bโˆ’q
    . This makes
    bโ€ฒ
    a negative number.
  • Compute
    cโ€ฒโ†a+bโ€ฒ
    .
  • If
    cโ€ฒ<0
    ,
    cโ†cโ€ฒ+q
    . Otherwise,
    cโ†cโ€ฒ
    .
  • Return
    c
    as the sum.

The reason for subtracting

q from
b
before adding it with
a
is to make sure
cโ€ฒ
stays within the Bitcoin integer limit.

To multiply two numbers

a and
b
, we use an algorithm that incorporates a small lookup table. Here are the concrete algorithm:

  • Bit-decompose
    b
    into bits. Let
    d1,d2,...,d31
    represent the 31 bits of
    b
    , with
    d1
    be the most significant bit.
  • Compute a three-element lookup table:
    • (amodq)โˆ’q
    • (2amodq)โˆ’q
    • (3amodq)โˆ’q
  • Initialize an aggregator variable
    sโ†0
    .
  • If
    d1=1
    :
    • sโ€ฒโ†s+((amodq)โˆ’q)
      .
    • If
      sโ€ฒ<0
      ,
      sโ†sโ€ฒ+q
      . Otherwise,
      sโ†sโ€ฒ
      .
  • For each pair
    (d2,d3),(d4,d5),...,(d30,d31)
    :
    • Double
      s
      twice using the addition algorithm.
    • Let the first bit in a pair be
      dl
      and the second bit be
      dr
      .
    • If
      dl=1
      and
      dr=1
      :
      • sโ€ฒโ†s+((3amodq)โˆ’q)
        .
      • If
        sโ€ฒ<0
        ,
        sโ†sโ€ฒ+q
        . Otherwise,
        sโ†sโ€ฒ
        .
    • Else, if
      dl=1
      and
      dr=0
      :
      • sโ€ฒโ†s+((2amodq)โˆ’q)
        .
      • If
        sโ€ฒ<0
        ,
        sโ†sโ€ฒ+q
        . Otherwise,
        sโ†sโ€ฒ
        .
    • Else, if
      dl=0
      and
      dr=1
      :
      • sโ€ฒโ†s+((amodq)โˆ’q)
        .
      • If
        sโ€ฒ<0
        ,
        sโ†sโ€ฒ+q
        . Otherwise,
        sโ†sโ€ฒ
        .
  • Return
    s
    as the product.

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

q. This is to save the step
bโ€ฒโ†bโˆ’q
in the addition algorithm.

CM31

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

a1+ib1 and
a2+ib2
. The product is:

(a1a2โˆ’b1b2)+i(a1b2+a2b1)

The naive way is to compute the four products

a1a2,
b1b2
,
a1b2
,
a2b1
individually, but one can do so more efficiently through Karatsuba, the algorithm of which is as follows:

  • Compute
    p1=a1a2
    and
    p2=b1b2
    .
  • Compute
    t1=a1+b1
    and
    t2=a2+b2
    .
  • Compute
    p3=t1t2=a1a2+a1b2+a2b1+b1b2
    .
  • Compute
    a3=p1โˆ’p2
    .
  • Compute
    b3=p3โˆ’p1โˆ’p2
    .

One can see that this algorithm only uses three M31 multiplications.

QM31

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

c1+jd1 and
c2+jd2
where
c1
,
d1
,
c2
,
d2
are all CM31 elements and
j
is the unit for the second element. Note that the product would look as follows:

(c1c2+j2d1d2)+j(c1d2+c2d1)

Here

j2=2+i.

The algorithm is as follows.

  • Compute
    p1=c1c2
    and
    p2=d1d2
    .
  • Compute
    t1=c1+d1
    and
    t2=c2+d2
    .
  • Compute
    p3=t1t2=c1c2+c1d2+c2d1+d1d2
    .
  • Compute
    c3=c1c2+(2+i)d1d2
    .
  • Compute
    d3=p3โˆ’p1โˆ’p2
    .

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

c1c2 and
d1d2
are in CM31, so they already have an imaginary part. Let
c1c2=Re(c1c2)+iโ‹…Im(c1c2)
and
d1d2=Re(d1d2)+iโ‹…Im(d1d2)
.