Mersenne Primes

Proof systems typically rely on finite field operations, where efficient field arithmetic is crucial for optimizing proof generation. In STARK protocols, there is no direct dependency between the security level of the proof system and the field size. This allows the use of small fields with highly efficient arithmetic.

Stwo uses a particular family of primes called Mersenne primes. A Mersenne prime is defined as a prime number that is one less than a power of two, expressed as . Stwo uses the Mersenne prime of size , which is implemented as follows:

pub struct M31(pub u32);

Why ?

The key advantage is extremely cheap modular reduction after a 31-bit multiplication. Consider computing , where . This operation involves a 31-bit integer multiplication, producing a 62-bit intermediate result, which is then reduced modulo .

Suppose then we can decompose into two 31-bit values and , such that , as shown in the following figure.

Figure 1: Product decomposition

To perform modular reduction, we start with: Substituting gives:

Since and are both 31-bit values, they can be directly represented as field elements. Consequently, modular reduction is performed with a single field addition. This makes arithmetic over Mersenne primes exceptionally fast, making them an ideal choice for our STARK protocol. The above field multiplication is implemented as:

    fn mul(self, rhs: Self) -> Self::Output {
        Self::reduce((self.0 as u64) * (rhs.0 as u64))
    }

where reduce is a function which performs efficient reduction of the resulting number modulo .

Why We Need Extensions ?

We cannot instantiate our STARK protocols using since it is not FFT-friendly field, meaning it does not contain a multiplicative subgroup of order that is a large power of two (commonly referred to as a smooth subgroup). The multiplicative group of has the following order:

As shown above, the multiplicative group of lacks a smooth subgroup of size that is a large power of two because there is no large power of two that divides . In other words, there does not exist a sufficiently large such that . To make compatible with STARKs, we will work over extensions of it.

Field Operations

Stwo avoids code duplication by providing two Rust macros, impl_field! and impl_extension_field!, for implementing field and extension field operations.

For example, field operations for are implemented using the Rust macro impl_field!, which takes as argument the field and the size of the field , as follows:

impl_field!(M31, P);

Since we work over extensions of , it has the type alias BaseField, as follows:

pub type BaseField = M31;

Extensions of Mersenne Prime Field

This section describes two extensions of : complex extension and quartic extension.

Complex Extension

We construct the degree-2 extension of denoted by using the polynomial which is irreducible over .

This extension forms a field of size , where elements can be represented as or where and is the root of the polynomial i.e. . This is implemented as follows:

pub struct CM31(pub M31, pub M31);

The order of the multiplicative group of is calculated as follows:

As shown above, i.e. the multiplicative group of contains a subgroup of size that is a large power of two. This makes it suitable for instantiating STARKs. This subgroup is what we refer to as the Circle group (explored further in the next section).

Similar to , the operations of are defined using the macros impl_field! and impl_extension_field! (which takes as argument the field and the field to be extended i.e. ). These are implemented as follows:

impl_field!(CM31, P2);
impl_extension_field!(CM31, M31);

where P2 is the size of i.e. .

Quartic Extension

For the soundness of the protocol, it is crucial that the verifier samples random challenges from a sufficiently large field to ensure that an adversary cannot guess or brute-force the challenges and generate a proof that passes verification without knowledge of the witness.

If we use , then 31-bit random challenges are not sufficient to maintain the security of the protocol. To address this, the verifier draws random challenges from a degree-4 extension of denoted by , which is equivalent to degree-2 extension of , denoted as

Here the polynomial is irreducible over .

The elements of can be represented as or where and is the root of the polynomial i.e. . This is implemented as follows:

pub struct QM31(pub CM31, pub CM31);
pub type SecureField = QM31;

Since the verifier uses the field to sample random challenges it is given the type alias SecureField.

Alternatively, the elements of can also be represented as four elements of i.e. or

where . With four elements from , the challenge space consists of 124-bit values, offering a sufficiently large possibilities to sample a random challenge.

Similar to , the operations of are defined using the macros impl_field! and impl_extension_field!, implemented as follows:

impl_field!(QM31, P4);
impl_extension_field!(QM31, CM31);

where P4 is the size of i.e. .

In the next section, we will explore the circle group, which is used to instantiate the STARK protocol.