From 11f1626ecc9b0761910b8dfb5465113d834153d5 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Mon, 27 Feb 2023 18:41:23 +0000 Subject: [PATCH] modular: Add comment. Add benchmark --- benchmark/modular.js | 10 ++++++++++ src/abstract/modular.ts | 1 + 2 files changed, 11 insertions(+) create mode 100644 benchmark/modular.js diff --git a/benchmark/modular.js b/benchmark/modular.js new file mode 100644 index 0000000..5f4234b --- /dev/null +++ b/benchmark/modular.js @@ -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)); +}); diff --git a/src/abstract/modular.ts b/src/abstract/modular.ts index 0ded40b..5b50a3f 100644 --- a/src/abstract/modular.ts +++ b/src/abstract/modular.ts @@ -56,6 +56,7 @@ export function invert(number: bigint, modulo: bigint): bigint { throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`); } // 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 b = modulo; // prettier-ignore