@zytekaron/galois
Version:
A general-purpose Galois field implementation.
154 lines (151 loc) • 5.18 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
/**
* Common irreducible polynomials used in GF(2^8) fields.
*/
export declare const enum Polynomials {
AES = 283,
REED_SOLOMON = 285
}
/**
* Common generator values used with GF(2^8) fields.
*/
export declare const enum Generators {
AES = 3,
REED_SOLOMON = 229,
FAST = 2
}
/**
* Represents a point with x and y coordinates in the Galois Field.
*/
export interface Point {
x: number;
y: number;
}
/**
* GF256 defines a Galois Field GF(2^8) with a given irreducible polynomial and generator.
*/
export declare class GF256 {
private readonly exp;
private readonly log;
readonly poly: number;
readonly gen: number;
/**
* Creates a new GF(2^8) field using the given polynomial and generator.
* @param poly - The irreducible polynomial to define the field (default: Polynomials.AES).
* @param gen - The generator for the field (default: Generators.AES).
*/
constructor(poly?: number, gen?: number);
/**
* Performs multiplication in the Galois Field.
* @param a - First operand (byte).
* @param b - Second operand (byte).
* @returns The result of a * b in GF(2^8).
*/
mul(a: number, b: number): number;
/**
* Performs division in the Galois Field.
* @param a - Numerator (byte).
* @param b - Denominator (byte, must not be zero).
* @returns The result of a / b in GF(2^8).
* @throws Error if b is zero or inputs are invalid.
*/
div(a: number, b: number): number;
/**
* Returns the multiplicative inverse in the Galois Field.
* @param a - Value to invert (must not be zero).
* @returns The multiplicative inverse of a in GF(2^8).
* @throws Error if a is zero or not a valid byte.
*/
inv(a: number): number;
/**
* Performs addition in GF(2^8).
* @param a - First operand (byte).
* @param b - Second operand (byte).
* @returns The result of a + b in GF(2^8).
*/
add(a: number, b: number): number;
/**
* Performs subtraction in GF(2^8).
* @param a - First operand (byte).
* @param b - Second operand (byte).
* @returns The result of a - b in GF(2^8).
*/
sub(a: number, b: number): number;
/**
* Debug utility to print the tables.
*/
printTables(): void;
/**
* Evaluates a polynomial using Horner's method for x.
* @param coefficients - Polynomial coefficients in increasing order.
* @param x - The x-value to evaluate the polynomial at (must not be zero).
* @returns The evaluated result.
* @throws Error if x is zero or invalid.
*/
evaluate(coefficients: Uint8Array, x: number): number;
/**
* Takes n sample points and uses Lagrange Interpolation to determine the value at x.
* @param samples - Array of sample points (x, y) to interpolate.
* @param x - The x-value at which to interpolate.
* @returns The interpolated result at x.
*/
interpolate(samples: Point[], x: number): number;
/**
* Adds two polynomials in the field.
* @param a - First polynomial
* @param b - Second polynomial
* @returns The sum polynomial
*/
addPolynomials(a: Uint8Array, b: Uint8Array): Uint8Array;
/**
* Multiplies two polynomials in the field.
* @param a - First polynomial
* @param b - Second polynomial
* @returns The product polynomial
*/
mulPolynomials(a: Uint8Array, b: Uint8Array): Uint8Array;
/**
* Returns a list of all valid generator elements for the field defined by the provided polynomial.
* @param poly - The irreducible polynomial to test generators against.
* @returns An array of all valid generator values for this polynomial.
*/
static findAllGenerators(poly: number): number[];
/**
* Checks whether gen is a generator for the field defined by the provided polynomial.
* @param poly - The irreducible polynomial.
* @param gen - The candidate generator to test.
* @returns Whether gen is is a valid generator in the field defined by poly.
*/
static isGenerator(poly: number, gen: number): boolean;
/**
* Multiplies two bytes in GF(2^8) without log/exp (used during table generation).
* @param a - First byte operand.
* @param b - Second byte operand.
* @param poly - The irreducible polynomial used for reduction.
* @returns The result of multiplication in GF(2^8).
*/
static mulByte(a: number, b: number, poly: number): number;
/**
* Generates a random polynomial of the given degree over GF(2^8),
* with the constant term (intercept) set explicitly.
*
* This is useful for Shamir Secret Sharing and other schemes that require
* randomly constructed polynomials over a finite field.
*
* @param intercept - The constant term (f(0)) of the polynomial.
* @param degree - The degree of the polynomial (number of random coefficients).
* @returns A Uint8Array of length (degree + 1), where index 0 is the intercept.
*/
static makePolynomial(intercept: number, degree: number): Uint8Array;
/**
* Validates that a number is within the valid byte range [0, 256).
* @throws Error if the number is outside the valid range.
*/
private static validateByte;
/**
* Validates that a number is within the valid uint16 range [0, 65536).
* @throws Error if the number is outside the valid range.
*/
private static validateUInt16;
}
export {};