-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
45 lines (38 loc) · 1.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// IMPORTS
// ================================================================================================
import { WasmOptions, FiniteField } from '@guildofweavers/galois';
import { PrimeField } from './lib/PrimeField';
import { WasmPrimeField128 } from './lib/subfields';
// CONSTANTS
// ================================================================================================
const P128 = 2n**128n;
const P64 = 2n**64n;
// PUBLIC FUNCTIONS
// ================================================================================================
export function createPrimeField(modulus: bigint, useWasm?: boolean): FiniteField
export function createPrimeField(modulus: bigint, options?: Partial<WasmOptions>): FiniteField
export function createPrimeField(modulus: bigint, useWasmOrOptions?: boolean | Partial<WasmOptions>): FiniteField {
if (!useWasmOrOptions) {
return new PrimeField(modulus);
}
const Subfield = getPrimeSubfieldConstructor(modulus);
if (!Subfield) {
return new PrimeField(modulus);
}
const wasmOptions = normalizeWasmOptions(useWasmOrOptions);
return new Subfield(modulus, wasmOptions);
}
// HELPER FUNCTIONS
// ================================================================================================
function getPrimeSubfieldConstructor(modulus: bigint) {
if (modulus < P128 && modulus > (P128 - P64)) {
return WasmPrimeField128;
}
}
function normalizeWasmOptions(useWasmOrOptions: boolean | Partial<WasmOptions>): WasmOptions {
if (typeof useWasmOrOptions === 'boolean') {
return { memory: new WebAssembly.Memory({ initial: 10 }) };
}
const memory = useWasmOrOptions.memory || new WebAssembly.Memory({ initial: 10 });
return { memory };
}