fn Modulus(comptime max_bits: comptime_int) type

A modulus, defining a finite field. All operations within the field are performed modulo this modulus, without heap allocations. max_bits represents the number of bits in the maximum value the modulus can be set to.

Parameters

max_bits: comptime_int,

Fields

zero: Fe,

The neutral element.

v: FeUint,

The modulus value.

rr: Fe,

R^2 for the Montgomery representation.

m0inv: Limb,

Inverse of the first limb

leading: usize,

Number of leading zero bits in the modulus.

Functions

fn add(self: Self, x: Fe, y: Fe) Fe

Adds two field elements (mod m).

fn bits(self: Self) usize

Actual size of the modulus, in bits.

fn fromBytes(bytes: []const u8, comptime endian: builtin.Endian) InvalidModulusError || OverflowError!Self

Creates a new modulus from a byte string.

fn fromMontgomery(self: Self, x: *Fe) RepresentationError!void

Takes a field element out of the Montgomery form.

fn fromPrimitive(comptime T: type, x: T) InvalidModulusError || OverflowError!Self

Creates a new modulus from a primitive value. The modulus must be odd and large…

Creates a new modulus from a primitive value. The modulus must be odd and larger than 2.

fn fromUint(v_: FeUint) InvalidModulusError!Self

Creates a new modulus from a Uint value. The modulus must be odd and larger t…

Creates a new modulus from a Uint value. The modulus must be odd and larger than 2.

fn mul(self: Self, x: Fe, y: Fe) Fe

Multiplies two field elements.

fn one(self: Self) Fe

Returns the element 1.

fn pow(self: Self, x: Fe, e: Fe) NullExponentError!Fe

Returns x^e (mod m) in constant time.

fn powPublic(self: Self, x: Fe, e: Fe) NullExponentError!Fe

Returns x^e (mod m), assuming that the exponent is public. The function remains…

Returns x^e (mod m), assuming that the exponent is public. The function remains constant time with respect to x.

fn powWithEncodedExponent(self: Self, x: Fe, e: []const u8, endian: builtin.Endian) NullExponentError!Fe

Returns x^e (mod m), assuming that the exponent is public, and provided as a byt…

Returns x^e (mod m), assuming that the exponent is public, and provided as a byte string. Exponents are usually small, so this function is faster than powPublic as a field element doesn’t have to be created if a serialized representation is already available.

fn reduce(self: Self, x: anytype) Fe

Reduces an arbitrary Uint, converting it to a field element.

fn rejectNonCanonical(self: Self, fe: Fe) error{NonCanonical}!void

Rejects field elements that are not in the canonical form.

fn sq(self: Self, x: Fe) Fe

Squares a field element.

fn sub(self: Self, x: Fe, y: Fe) Fe

Subtracts two field elements (mod m).

fn toBytes(self: Self, bytes: []u8, comptime endian: builtin.Endian) OverflowError!void

Serializes the modulus to a byte string.

fn toMontgomery(self: Self, x: *Fe) RepresentationError!void

Converts a field element to the Montgomery form.

Values

Fe
undefined

A field element, representing a value within the field defined by this modulus.