aiken/crypto/bls12_381/scalar

This module implements arithmetic operations in the scalar field associated with the BLS12-381 elliptic curve. The scalar field, defined over a prime number q, is derived from the order of the subgroup G1.

More explicitly, we have the identity:

builtin.bls12_381_g1_scalar_mul(q, bls12_381_g1_generator) == 1

where,

q = 52435875175126190479447740508185965837690552500527637822603658699938581184513

This module provides functionality for basic arithmetic operations (addition, subtraction, multiplication, division) within this scalar field. Additionally, it includes advanced operations such as exponentiation and calculation of multiplicative inverses, tailored for cryptographic applications.

Types

Opaque type representing an element of the finite field Scalar.

Constants

field_prime: Int = 52435875175126190479447740508185965837690552500527637822603658699938581184513

The prime number defining the scalar field of the BLS12-381 curve.

one: Scalar

Represents the multiplicative identity (one) in the Scalar field.

zero: Scalar

Represents the additive identity (zero) in the Scalar field.

Functions

Constructing

new(n: Int) -> Option<Scalar>

Constructs a new Scalar element from an integer, ensuring it’s within the valid range of the field. Returns None if the integer is negative or greater than the prime number defining the field.

from_bytearray_big_endian(bytes: ByteArray) -> Option<Scalar>

Constructs a new Scalar element from a Big-Endian (most-significant bits first) ByteArray.

from_bytearray_little_endian(bytes: ByteArray) -> Option<Scalar>

Constructs a new Scalar element from a Little-Endian (least-significant bits first) ByteArray.

Modifying

scale(self: Scalar, e: Int) -> Scalar

Exponentiates an Scalar element by a non-negative integer exponent, using repeated squaring. Note that this function returns scalar.zero for negative exponents. A dedicated builtin function for this is in the making, see CIP 109.

scale2(self: Scalar, k: Int) -> Scalar

A faster version of scale for the case where the exponent is a power of two. That is, the exponent e = 2^k for some non-negative integer k. Which is used alot in zk-SNARKs.

Combining

add(left: Scalar, right: Scalar) -> Scalar

Adds two Scalar elements, ensuring the result stays within the finite field range.

div(left: Scalar, right: Scalar) -> Option<Scalar>

Divides one Scalar element by another, returning None if the divisor is zero.

mul(left: Scalar, right: Scalar) -> Scalar

Multiplies two Scalar elements, with the result constrained within the finite field.

neg(self: Scalar) -> Scalar

Calculates the additive inverse of a Scalar element.

recip(self: Scalar) -> Option<Scalar>

Calculates the multiplicative inverse of an Scalar element, returning None if the element is zero.

sub(left: Scalar, right: Scalar) -> Scalar

Subtracts one Scalar element from another, with the result wrapped within the finite field range.

Transforming

to_int(self: Scalar) -> Int

Converts a Scalar element back to its integer representation.

to_bytearray_big_endian(self: Scalar, size: Int) -> ByteArray

Converts a Scalar element to a Big-Endian (most-significant bits first) ByteArray.

to_bytearray_little_endian(self: Scalar, size: Int) -> ByteArray

Converts a Scalar element to a Little-Endian (least-significant bits first) ByteArray.

Search Document