Re-use pre-define BN_{BE, LE}_* constants (#1175)

* Re-use pre-define BN_{BE, LE}_* constants

* ordering update

* bnMax/bnMin opt

* u8aToHex flatten
This commit is contained in:
Jaco
2021-11-12 09:19:49 +02:00
committed by GitHub
parent 46beaa1151
commit ba0a183bd9
21 changed files with 94 additions and 72 deletions
@@ -7,6 +7,7 @@ import type { HexString } from '@polkadot/util/types';
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
import { blake2AsU8a } from '../blake2/asU8a';
import { BN_LE_16_OPTS } from '../bn';
import { decodeAddress } from './decode';
const PREFIX = stringToU8a('modlpy/utilisuba');
@@ -16,7 +17,7 @@ export function createKeyDerived (who: HexString | Uint8Array | string, index: b
u8aConcat(
PREFIX,
decodeAddress(who),
bnToU8a(index, { bitLength: 16, isLe: true })
bnToU8a(index, BN_LE_16_OPTS)
)
);
}
+2 -1
View File
@@ -7,6 +7,7 @@ import type { HexString } from '@polkadot/util/types';
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
import { blake2AsU8a } from '../blake2/asU8a';
import { BN_LE_16_OPTS } from '../bn';
import { addressToU8a } from './util';
const PREFIX = stringToU8a('modlpy/utilisuba');
@@ -17,7 +18,7 @@ export function createKeyMulti (who: (HexString | Uint8Array | string)[], thresh
PREFIX,
compactToU8a(who.length),
...u8aSorted(who.map(addressToU8a)),
bnToU8a(threshold, { bitLength: 16, isLe: true })
bnToU8a(threshold, BN_LE_16_OPTS)
)
);
}
+14
View File
@@ -0,0 +1,14 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const BN_LE_OPTS = { isLe: true };
export const BN_LE_16_OPTS = { bitLength: 16, isLe: true };
export const BN_BE_32_OPTS = { bitLength: 32, isLe: false };
export const BN_LE_32_OPTS = { bitLength: 32, isLe: true };
export const BN_BE_256_OPTS = { bitLength: 256, isLe: false };
export const BN_LE_256_OPTS = { bitLength: 256, isLe: true };
export const BN_LE_512_OPTS = { bitLength: 512, isLe: true };
@@ -5,6 +5,7 @@ import type { Keypair } from '../../types';
import { assert, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
import { BN_BE_32_OPTS } from '../../bn';
import { hmacSha512 } from '../../hmac';
import { secp256k1KeypairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
import { HARDENED, hdValidatePath } from '../validatePath';
@@ -24,7 +25,7 @@ function createCoded (secretKey: Uint8Array, chainCode: Uint8Array): CodedKeypai
}
function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
const indexBuffer = bnToU8a(index, { bitLength: 32, isLe: false });
const indexBuffer = bnToU8a(index, BN_BE_32_OPTS);
const data = index >= HARDENED
? u8aConcat(new Uint8Array(1), hd.secretKey, indexBuffer)
: u8aConcat(hd.publicKey, indexBuffer);
@@ -3,6 +3,7 @@
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@polkadot/util';
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn';
import { hmacSha512 } from '../../hmac';
// performs hard-only derivation on the xprv
@@ -10,19 +11,19 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra
const kl = xprv.subarray(0, 32);
const kr = xprv.subarray(32, 64);
const cc = xprv.subarray(64, 96);
const data = u8aConcat([0], kl, kr, bnToU8a(index, { bitLength: 32, isLe: true }));
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
const z = hmacSha512(cc, data);
data[0] = 0x01;
return u8aConcat(
bnToU8a(
u8aToBn(kl, { isLe: true }).iadd(u8aToBn(z.subarray(0, 28), { isLe: true }).imul(BN_EIGHT)),
{ bitLength: 512, isLe: true }
u8aToBn(kl, BN_LE_OPTS).iadd(u8aToBn(z.subarray(0, 28), BN_LE_OPTS).imul(BN_EIGHT)),
BN_LE_512_OPTS
).subarray(0, 32),
bnToU8a(
u8aToBn(kr, { isLe: true }).iadd(u8aToBn(z.subarray(32, 64), { isLe: true })),
{ bitLength: 512, isLe: true }
u8aToBn(kr, BN_LE_OPTS).iadd(u8aToBn(z.subarray(32, 64), BN_LE_OPTS)),
BN_LE_512_OPTS
).subarray(0, 32),
hmacSha512(cc, data).subarray(32, 64)
);
@@ -6,14 +6,11 @@ import type { HexString } from '@polkadot/util/types';
import { BN, bnToU8a, compactAddLength, hexToU8a, isBigInt, isBn, isHex, isNumber, isString, stringToU8a } from '@polkadot/util';
import { blake2AsU8a } from '../blake2/asU8a';
import { BN_LE_256_OPTS } from '../bn';
const RE_NUMBER = /^\d+$/;
const JUNCTION_ID_LEN = 32;
const BN_OPTIONS = {
bitLength: 256,
isLe: true
};
export class DeriveJunction {
readonly #chainCode: Uint8Array = new Uint8Array(32);
@@ -61,7 +58,7 @@ export class DeriveJunction {
public soft (value: HexString | number | string | bigint | BN | Uint8Array): DeriveJunction {
if (isNumber(value) || isBn(value) || isBigInt(value)) {
return this.soft(bnToU8a(value, BN_OPTIONS));
return this.soft(bnToU8a(value, BN_LE_256_OPTS));
} else if (isHex(value)) {
return this.soft(hexToU8a(value));
} else if (isString(value)) {
+2 -1
View File
@@ -5,6 +5,7 @@
import { bnToU8a, u8aConcat } from '@polkadot/util';
import { BN_BE_32_OPTS } from '../bn';
import { hmacSha512 } from '../hmac';
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
@@ -14,7 +15,7 @@ export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: numb
while (out.length < len) {
num++;
block.set(bnToU8a(num, { bitLength: 32, isLe: false }), salt.length);
block.set(bnToU8a(num, BN_BE_32_OPTS), salt.length);
let prev = hmacSha512(password, block);
const md = prev;
+4 -3
View File
@@ -5,6 +5,7 @@ import type { Params } from './types';
import { assert, u8aToBn } from '@polkadot/util';
import { BN_LE_OPTS } from '../bn';
import { DEFAULT_PARAMS } from './defaults';
interface Result {
@@ -14,9 +15,9 @@ interface Result {
export function scryptFromU8a (data: Uint8Array): Result {
const salt = data.subarray(0, 32);
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), { isLe: true }).toNumber();
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), { isLe: true }).toNumber();
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), { isLe: true }).toNumber();
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), BN_LE_OPTS).toNumber();
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), BN_LE_OPTS).toNumber();
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), BN_LE_OPTS).toNumber();
// FIXME At this moment we assume these to be fixed params, this is not a great idea since we lose flexibility
// and updates for greater security. However we need some protection against carefully-crafted params that can
+5 -3
View File
@@ -5,11 +5,13 @@ import type { Params } from './types';
import { bnToU8a, u8aConcat } from '@polkadot/util';
import { BN_LE_32_OPTS } from '../bn';
export function scryptToU8a (salt: Uint8Array, { N, p, r }: Params): Uint8Array {
return u8aConcat(
salt,
bnToU8a(N, { bitLength: 32, isLe: true }),
bnToU8a(p, { bitLength: 32, isLe: true }),
bnToU8a(r, { bitLength: 32, isLe: true })
bnToU8a(N, BN_LE_32_OPTS),
bnToU8a(p, BN_LE_32_OPTS),
bnToU8a(r, BN_LE_32_OPTS)
);
}
+4 -3
View File
@@ -3,7 +3,8 @@
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
import { EXPAND_OPT, secp256k1 } from './secp256k1';
import { BN_BE_256_OPTS } from '../bn';
import { secp256k1 } from './secp256k1';
export function secp256k1Expand (publicKey: Uint8Array): Uint8Array {
assert([33, 65].includes(publicKey.length), 'Invalid publicKey provided');
@@ -13,7 +14,7 @@ export function secp256k1Expand (publicKey: Uint8Array): Uint8Array {
.getPublic();
return u8aConcat(
bnToU8a(expanded.getX(), EXPAND_OPT),
bnToU8a(expanded.getY(), EXPAND_OPT)
bnToU8a(expanded.getX(), BN_BE_256_OPTS),
bnToU8a(expanded.getY(), BN_BE_256_OPTS)
);
}
@@ -5,7 +5,8 @@ import type { Keypair } from '../../types';
import { assert, bnToU8a } from '@polkadot/util';
import { EXPAND_OPT, secp256k1 } from '../secp256k1';
import { BN_BE_256_OPTS } from '../../bn';
import { secp256k1 } from '../secp256k1';
/**
* @name secp256k1KeypairFromSeed
@@ -18,6 +19,6 @@ export function secp256k1KeypairFromSeed (seed: Uint8Array): Keypair {
return {
publicKey: new Uint8Array(key.getPublic().encodeCompressed()),
secretKey: bnToU8a(key.getPrivate(), EXPAND_OPT)
secretKey: bnToU8a(key.getPrivate(), BN_BE_256_OPTS)
};
}
@@ -3,8 +3,5 @@
import elliptic from 'elliptic';
const EC = elliptic.ec;
export const secp256k1 = new EC('secp256k1');
export const EXPAND_OPT = { bitLength: 256, isLe: false };
// eslint-disable-next-line new-cap
export const secp256k1 = new elliptic.ec('secp256k1');
+4 -3
View File
@@ -6,8 +6,9 @@ import type { HashType } from './types';
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
import { BN_BE_256_OPTS } from '../bn';
import { secp256k1Hasher } from './hasher';
import { EXPAND_OPT, secp256k1 } from './secp256k1';
import { secp256k1 } from './secp256k1';
/**
* @name secp256k1Sign
@@ -20,8 +21,8 @@ export function secp256k1Sign (message: Uint8Array | string, { secretKey }: Part
const ecsig = key.sign(secp256k1Hasher(hashType, message));
return u8aConcat(
bnToU8a(ecsig.r, EXPAND_OPT),
bnToU8a(ecsig.s, EXPAND_OPT),
bnToU8a(ecsig.r, BN_BE_256_OPTS),
bnToU8a(ecsig.s, BN_BE_256_OPTS),
new Uint8Array([ecsig.recoveryParam || 0])
);
}
@@ -3,6 +3,7 @@
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 };
@@ -23,5 +24,5 @@ export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Arr
assert(!bn.isZero(), 'Invalid resulting private key');
return bnToU8a(bn, { bitLength: 256, isLe: false });
return bnToU8a(bn, BN_BE_256_OPTS);
}