Constant for secp256k1 curve.n (#1192)

* Constant for secp256k1 curve.n

* Link to constant definition

* global space replace
This commit is contained in:
Jaco
2021-11-16 06:35:25 +02:00
committed by GitHub
parent 184dbccf39
commit 23f68c63f4
@@ -4,9 +4,13 @@
import { assert, BN, bnToU8a, isU8a } from '@polkadot/util';
import { BN_BE_256_OPTS } from '../bn';
import { secp256k1 } from './secp256k1';
const ecparams = secp256k1.curve as { n: BN };
// pre-defined curve param as lifted form elliptic
// https://github.com/indutny/elliptic/blob/e71b2d9359c5fe9437fbf46f1f05096de447de57/lib/elliptic/curves.js#L182
const N = new BN(
'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141'.replace(/ /g, ''),
'hex'
);
export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Array): Uint8Array {
assert(isU8a(seckey) && seckey.length === 32, 'Expected seckey to be an Uint8Array with length 32');
@@ -14,12 +18,12 @@ export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Arr
const bn = new BN(tweak);
assert(bn.cmp(ecparams.n) < 0, 'Tweak parameter is out of range');
assert(bn.cmp(N) < 0, 'Tweak parameter is out of range');
bn.iadd(new BN(seckey));
if (bn.cmp(ecparams.n) >= 0) {
bn.isub(ecparams.n);
if (bn.cmp(N) >= 0) {
bn.isub(N);
}
assert(!bn.isZero(), 'Invalid resulting private key');