primitives.field#
BabyBear field GF(p) and quartic extension GF(p^4).
Uses galois library for base field GF(p) arithmetic. Uses custom FF4 class for fast quartic extension field arithmetic.
Type Discipline#
- FF (galois GF(p)):
Base field columns and scalars. Fast jit-compiled numpy operations.
- FF4:
Extension field GF(p^4) scalars and columns. Stores coefficients as int64 arrays. Supports operator overloads: +, -, , /, *, negation. Scalars: shape (4,). Columns: shape (N, 4). Coefficients in ascending degree: c0 + c1*x + c2*x^2 + c3*x^3 as [c0, c1, c2, c3].
Attributes#
Base field GF(p) - BabyBear prime field. |
|
Classes#
Extension field GF(p^4) = GF(p)[x]/(x^4 - 11) element or column. |
Functions#
|
Extract ascending-order coefficients [a0, a1, a2, a3] from FF4 element. |
|
Construct galois FF4 scalar from ascending-order coefficients [a0, a1, a2, a3]. |
|
Embed base field element into galois FF4 as (val, 0, 0, 0). |
|
Construct galois FF4 array from parallel coefficient lists. |
|
Parse JSON [[c0,c1,c2,c3],...] to galois FF4 array. |
|
Convert galois FF4 array to JSON [[c0,c1,c2,c3],...] format. |
|
Parse JSON [[c0,c1,c2,c3],...] to FF4 column. |
|
Convert FF4 column/scalar to JSON [[c0,c1,c2,c3],...] format. |
|
Embed base field element into extension field as (x, 0, 0, 0). |
|
Multiply two extension field elements. |
|
Multiply extension field element by a base field element. |
|
Add two extension field elements. |
|
Subtract two extension field elements. |
|
Negate an extension field element. |
|
Multiplicative inverse in extension field. |
|
Division in extension field: a / b. |
|
Exponentiation in extension field. |
|
Compute x^(2^log_power) by repeated squaring. |
|
DEPRECATED: Use a + b. |
|
DEPRECATED: Use a - b. |
|
DEPRECATED: Use -a. |
|
DEPRECATED: Use a * b. |
|
DEPRECATED: Use a.mul_base(b). |
|
DEPRECATED: Use FF4.from_base(b). |
|
DEPRECATED: Use FF4.broadcast(scalar, n). |
|
DEPRECATED: Use a.inv() or a ** -1. |
|
DEPRECATED: Use a * s. |
|
DEPRECATED: Use FF4.zeros(n). |
|
DEPRECATED: Use FF4.from_rows(rows). |
|
DEPRECATED: Use v.to_rows(). |
|
DEPRECATED: Use v.roll(shift). |
|
DEPRECATED: Use v.cumsum(). |
|
DEPRECATED: Use FF(data) directly. |
|
DEPRECATED: Use FF.Zeros(n) directly. |
|
DEPRECATED: Use FF(np.full(n, val % BABYBEAR_PRIME)) directly. |
|
DEPRECATED: Use np.roll(arr, shift) directly. |
|
Return primitive 2^n_bits-th root of unity. |
|
Return inverse of primitive 2^n_bits-th root of unity. |
|
Multiplicative inverse of x modulo BABYBEAR_PRIME. |
|
Convert a canonical integer to BabyBear Montgomery form. |
|
Convert a BabyBear Montgomery-form value to canonical form. |
|
Reverse the lowest bit_len bits of x. |
|
Reorder list elements by bit-reversing their indices. |
|
Montgomery batch inversion for any galois array. |
|
Montgomery batch inversion for FF4 elements. |
|
Montgomery batch inversion in the base field. |
|
Evaluate multiple polynomials at a single FF4 point using BSGS. |
Module Contents#
- class primitives.field.FF4(data=None)[source]#
Extension field GF(p^4) = GF(p)[x]/(x^4 - 11) element or column.
Internal storage: int64 ndarray of shape (4,) for scalar or (N, 4) for column. Coefficients in ascending degree: c0 + c1*x + c2*x^2 + c3*x^3 stored as [c0, c1, c2, c3].
- Supports operator overloads for clean mathematical notation:
c = a + b # addition c = a - b # subtraction c = a * b # polynomial multiplication c = a / b # division (multiply by inverse) c = a ** n # exponentiation (n can be -1) c = -a # negation
- classmethod from_base(base) FF4[source]#
Lift base field values to FF4 as (val, 0, 0, 0).
- Args:
base: int, FF array, list[int], or ndarray of base field elements.
- classmethod from_rows(rows: list) FF4[source]#
Create from list of [c0, c1, c2, c3] coefficient lists.
- classmethod broadcast(scalar: FF4, n: int) FF4[source]#
Broadcast a scalar FF4 to a column of length n.
- primitives.field.ff4_coeffs(elem) list[int][source]#
Extract ascending-order coefficients [a0, a1, a2, a3] from FF4 element.
- primitives.field.ff4(coeffs) GaloisFF4[source]#
Construct galois FF4 scalar from ascending-order coefficients [a0, a1, a2, a3].
Also accepts FF4 objects for backward compat.
- primitives.field.ff4_from_base(val: int) GaloisFF4[source]#
Embed base field element into galois FF4 as (val, 0, 0, 0).
- primitives.field.ff4_array(c0: list[int], c1: list[int], c2: list[int], c3: list[int]) GaloisFF4[source]#
Construct galois FF4 array from parallel coefficient lists.
- primitives.field.ff4_from_json(json_arr: list[list[int]]) GaloisFF4[source]#
Parse JSON [[c0,c1,c2,c3],…] to galois FF4 array.
- primitives.field.ff4_to_json(arr) list[list[int]][source]#
Convert galois FF4 array to JSON [[c0,c1,c2,c3],…] format.
- primitives.field.ef4_from_json(json_arr: list[list[int]]) FF4[source]#
Parse JSON [[c0,c1,c2,c3],…] to FF4 column.
- primitives.field.ef4_to_json(ef4_col: FF4) list[list[int]][source]#
Convert FF4 column/scalar to JSON [[c0,c1,c2,c3],…] format.
- primitives.field.ef4_from_base(x: int) FF4[source]#
Embed base field element into extension field as (x, 0, 0, 0).
DEPRECATED: Use FF4(x) or FF4.from_base(x) instead.
- primitives.field.ef4_mul(a, b) FF4[source]#
Multiply two extension field elements.
DEPRECATED: Use a * b instead.
- primitives.field.ef4_mul_base(a, b: int) FF4[source]#
Multiply extension field element by a base field element.
DEPRECATED: Use a.mul_base(b) instead.
- primitives.field.ef4_add(a, b) FF4[source]#
Add two extension field elements.
DEPRECATED: Use a + b instead.
- primitives.field.ef4_sub(a, b) FF4[source]#
Subtract two extension field elements.
DEPRECATED: Use a - b instead.
- primitives.field.ef4_neg(a) FF4[source]#
Negate an extension field element.
DEPRECATED: Use -a instead.
- primitives.field.ef4_inv(x) FF4[source]#
Multiplicative inverse in extension field.
DEPRECATED: Use x ** -1 instead.
- primitives.field.ef4_div(a, b) FF4[source]#
Division in extension field: a / b.
DEPRECATED: Use a / b instead.
- primitives.field.ef4_pow(x, n: int) FF4[source]#
Exponentiation in extension field.
DEPRECATED: Use x ** n instead.
- primitives.field.ef4_exp_power_of_2(x, log_power: int) FF4[source]#
Compute x^(2^log_power) by repeated squaring.
DEPRECATED: Use x ** (2 ** log_power) instead.
- primitives.field.ef4v_from_scalar(coeffs, n: int) FF4[source]#
DEPRECATED: Use FF4.broadcast(scalar, n).
- primitives.field.ff_constant(val: int, n: int) FF[source]#
DEPRECATED: Use FF(np.full(n, val % BABYBEAR_PRIME)) directly.
- primitives.field.W: list[int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][source]#
- primitives.field.get_omega_inv(n_bits: int) int[source]#
Return inverse of primitive 2^n_bits-th root of unity.
- primitives.field.to_monty(x: int) int[source]#
Convert a canonical integer to BabyBear Montgomery form.
- primitives.field.from_monty(x: int) int[source]#
Convert a BabyBear Montgomery-form value to canonical form.
- primitives.field.reverse_bits_len(x: int, bit_len: int) int[source]#
Reverse the lowest bit_len bits of x.
- primitives.field.bit_reverse_list(lst: list) list[source]#
Reorder list elements by bit-reversing their indices.
- primitives.field.batch_inverse(values)[source]#
Montgomery batch inversion for any galois array.
Converts N field inversions into 3N-3 multiplications + 1 inversion.
- primitives.field.ef4_batch_inverse(values: list) list[source]#
Montgomery batch inversion for FF4 elements.
Converts N extension field inversions into 3N-3 multiplications + 1 inversion. Accepts list of FF4 scalars or list of FF4Coeffs (list[int]).
- primitives.field.batch_inverse_base(values: list) list[source]#
Montgomery batch inversion in the base field.
- primitives.field.eval_poly_ef4_batch(coeffs_per_col: list[list[int]], eval_point) list[source]#
Evaluate multiple polynomials at a single FF4 point using BSGS.
- Args:
coeffs_per_col: Polynomial coefficient vectors, all same degree. eval_point: FF4 scalar or [c0,c1,c2,c3] list.
- Returns:
List of FF4 scalars, one per polynomial.