modular: Add comment. Add benchmark

This commit is contained in:
Paul Miller 2023-02-27 18:41:23 +00:00
parent 53ff287bf7
commit 11f1626ecc
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B
2 changed files with 11 additions and 0 deletions

10
benchmark/modular.js Normal file

@ -0,0 +1,10 @@
import { run, mark } from 'micro-bmark';
import { secp256k1 } from '../secp256k1.js';
run(async () => {
console.log(`\x1b[36mmodular, secp256k1 field\x1b[0m`);
const { Fp } = secp256k1.CURVE;
await mark('invert a', 30000, () => Fp.inv(2n ** 232n - 5910n));
await mark('invert b', 30000, () => Fp.inv(2n ** 231n - 5910n));
await mark('sqrt', 15000, () => Fp.sqrt(2n ** 231n - 5910n));
});

@ -56,6 +56,7 @@ export function invert(number: bigint, modulo: bigint): bigint {
throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`); throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`);
} }
// Eucledian GCD https://brilliant.org/wiki/extended-euclidean-algorithm/ // Eucledian GCD https://brilliant.org/wiki/extended-euclidean-algorithm/
// Fermat's little theorem "CT-like" version inv(n) = n^(m-2) mod m is 30x slower.
let a = mod(number, modulo); let a = mod(number, modulo);
let b = modulo; let b = modulo;
// prettier-ignore // prettier-ignore