* 5.6

* bnSqrt opt

* Additional sqrt tests

* Adjust bnSqrt input types

* test-case attributions
This commit is contained in:
Jaco Greeff
2021-02-07 12:14:55 +01:00
committed by GitHub
parent 476689f615
commit 654e44aa99
19 changed files with 1081 additions and 909 deletions
+15
View File
@@ -1,5 +1,20 @@
# CHANGELOG
## 5.6.1 Feb 7, 2021
Contributed:
- Fix installation instructions in `@polkadot/x-global` (Thanks to https://github.com/raoulmillais)
Changes:
- Conform to new ss58 encoding with ids up to 16,383 (>= 64 as 2 bytes)
- Use JS sqrt inside `bnSqrt` when value <= MAX_SAFE_INTEGER
- Adjust all `Buffer.from` usages, prefer `Uint8Array` where possible
- Expand tests for keyring with `vrf{Sign, Verify}`
- Align `@polkadot/networks` with latest ss58 registry
## 5.5.2 Feb 2, 2021
Changes:
+3 -3
View File
@@ -20,10 +20,10 @@
"test:one": "polkadot-dev-run-test"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@polkadot/dev": "^0.61.24",
"@babel/core": "^7.12.13",
"@polkadot/dev": "^0.61.25",
"@polkadot/ts": "^0.3.59",
"@types/jest": "^26.0.20"
},
"version": "5.5.3-4"
"version": "5.6.0"
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/hw-ledger-transports",
"version": "5.5.3-4",
"version": "5.6.0",
"repository": "github:polkadot-js/common",
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
@@ -11,7 +11,7 @@
"react-native": "react-native.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.12.5",
"@babel/runtime": "^7.12.13",
"@ledgerhq/hw-transport-webusb": "^5.41.0"
},
"devDependencies": {
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/hw-ledger",
"version": "5.5.3-4",
"version": "5.6.0",
"repository": "github:polkadot-js/common",
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
@@ -9,9 +9,9 @@
"main": "index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/hw-ledger-transports": "5.5.3-4",
"@polkadot/util": "^5.5.3-4",
"@babel/runtime": "^7.12.13",
"@polkadot/hw-ledger-transports": "5.6.0",
"@polkadot/util": "^5.6.0",
"@zondax/ledger-polkadot": "^0.13.0"
}
}
+6 -6
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/keyring",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "Keyring management",
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
@@ -15,12 +15,12 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/util": "5.5.3-4",
"@polkadot/util-crypto": "5.5.3-4"
"@babel/runtime": "^7.12.13",
"@polkadot/util": "5.6.0",
"@polkadot/util-crypto": "5.6.0"
},
"peerDependencies": {
"@polkadot/util": "5.5.3-4",
"@polkadot/util-crypto": "5.5.3-4"
"@polkadot/util": "5.6.0",
"@polkadot/util-crypto": "5.6.0"
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/networks",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A list of all available Substrate networks and their applicable prefixes",
"main": "index.js",
"sideEffects": false,
@@ -12,10 +12,10 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/networks#readme",
"dependencies": {
"@babel/runtime": "^7.12.5"
"@babel/runtime": "^7.12.13"
},
"devDependencies": {
"@polkadot/util": "5.5.3-4",
"@polkadot/x-fetch": "5.5.3-4"
"@polkadot/util": "5.6.0",
"@polkadot/x-fetch": "5.6.0"
}
}
+7 -7
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"sideEffects": [
@@ -15,16 +15,16 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/networks": "5.5.3-4",
"@polkadot/util": "5.5.3-4",
"@babel/runtime": "^7.12.13",
"@polkadot/networks": "5.6.0",
"@polkadot/util": "5.6.0",
"@polkadot/wasm-crypto": "^3.2.2",
"@polkadot/x-randomvalues": "5.5.3-4",
"@polkadot/x-randomvalues": "5.6.0",
"base-x": "^3.0.8",
"blakejs": "^1.1.0",
"bn.js": "^4.11.9",
"create-hash": "^1.2.0",
"elliptic": "^6.5.3",
"elliptic": "^6.5.4",
"hash.js": "^1.1.7",
"js-sha3": "^0.8.0",
"scryptsy": "^2.1.0",
@@ -39,6 +39,6 @@
"@types/xxhashjs": "^0.2.2"
},
"peerDependencies": {
"@polkadot/util": "5.5.3-4"
"@polkadot/util": "5.6.0"
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"sideEffects": false,
@@ -12,9 +12,9 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-textdecoder": "5.5.3-4",
"@polkadot/x-textencoder": "5.5.3-4",
"@babel/runtime": "^7.12.13",
"@polkadot/x-textdecoder": "5.6.0",
"@polkadot/x-textencoder": "5.6.0",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.11.9",
"camelcase": "^5.3.1",
+12
View File
@@ -92,3 +92,15 @@ export const BN_MILLION: BN = new BN(1_000_000);
* @summary BN constant for 1,000,000,000.
*/
export const BN_BILLION: BN = new BN(1_000_000_000);
/**
* @name BN_QUINTILL
* @summary BN constant for 1,000,000,000,000,000,000.
*/
export const BN_QUINTILL: BN = BN_BILLION.mul(BN_BILLION);
/**
* @name BN_MAX_INTEGER
* @summary BN constant for MAX_SAFE_INTEGER
*/
export const BN_MAX_INTEGER: BN = new BN(Number.MAX_SAFE_INTEGER);
+27 -16
View File
@@ -5,6 +5,29 @@ import BN from 'bn.js';
import { bnSqrt } from '.';
const TESTS: [string | number | BN | BigInt, string | number][] = [
[0, 0],
[1, 1],
[4, 2],
[256 * 256, 256],
[Number.MAX_SAFE_INTEGER, 94906265],
[new BN(Number.MAX_SAFE_INTEGER).iaddn(256), 94906265],
[12345678n * 12345679n, 12345678],
// test cases below from https://github.com/Aisse-258/bigint-isqrt/blob/f5254b9750841959022461c1353437a07a08f501/test/sqrt-test.js
[
'54866395443885995655625',
'234235768925'
],
[
'82120471531550314555681345949499512621827274120673745141541602816614526075010755373654280259022317599142038423759320355177481886719814621305828811322920076213800348341464996337890625',
'9062034624274524065844376014975805577107171799890766992670739972241112960081909332275390625'
],
[
'2068290204957779940494571454815902632050207752627007664813877111611972735314294907791993008455381265730624848057015476004143868408297250386512042711005577373327434315310235583583151773358353534031397385969275475421659036494173765903706963544628565027679444680637920371210258368358706248446698622704138943704561842220533563976838506992543814273403403362954987589637283890281226038798503294069403370486523697009383229578343444337394508531937972775639593763364402389410142290440075517598007930025487176173228232153685095699851764630884360483051570513804913157899314303269924424908606245851172031453725690942288677622973423417902662711606364188470325930406288190754356367655606430407552275285889968860367977288564364154999338284288438279464648663835830140826171534470717958048887869721791862539529444820484648908879364324577951008050851801647916673254009490596379549192296404841454780884358889449367135382779230574781333008561195244620152128505091883408484094800737142399855337799331037607433934520934135669364954550499058123797911762228642423567268540444910163468496805547450495819213412636594319148417857753712526066272727725420886898680432958409892237191284583904066400736417812258327303739879128916456683685232170600928808798462776174857486589598776587550843543720784829419478239563366491666155716208452087857080883906200829769222868347286071365156305396955420561718593130305703074908891135467193627721682044757187980688062367562451981939375400543212890625',
'45478458691536369397924851088318871329614555665525810477032568598360481742967756704524034986984134271202405190906906195123751965012732122548540879928668891416468847696038707003466943811913626146981446753434347552196868717612248915137033581428310504778371173112125038060138252719589122657221314190626986951627417768579641354255445988772645883238873672997347648890137913979299598785628581961589739331483895853398160785485300758458294999124984266041998145956341386549681176624661615465976970855592467853824812945820721933491609237311130118125761849911376495970746282571994312288380577879004620824004760109618074670810797544446334767103935754106103208789391060008291027361311679876439695844637933763721093782805837690830230712890625'
]
];
describe('bnSqrt', (): void => {
it('fails on < 0 roots', (): void => {
expect(
@@ -12,21 +35,9 @@ describe('bnSqrt', (): void => {
).toThrow(/negative numbers is not supported/);
});
it('finds the sqrt (exact)', (): void => {
expect(
bnSqrt(new BN(256).mul(new BN(256)))
).toEqual(new BN(256));
});
it('finds the sqrt (round)', (): void => {
expect(
bnSqrt(new BN(266))
).toEqual(new BN(16));
});
it('find the sqrt for 1', (): void => {
expect(
bnSqrt(new BN(1))
).toEqual(new BN(1));
TESTS.forEach(([value, expected], index): void => {
it(`calcs for test #${index}`, (): void => {
expect(bnSqrt(value).eq(new BN(expected))).toEqual(true);
});
});
});
+27 -15
View File
@@ -1,19 +1,13 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ToBn } from '../types';
import BN from 'bn.js';
import { assert } from '../assert';
import { BN_ONE, BN_TWO, BN_ZERO } from '../bn';
// https://golb.hplar.ch/2018/09/javascript-bigint.html
function newtonIteration (n: BN, x0: BN): BN {
const x1 = n.div(x0).iadd(x0).ishrn(1);
return (x0.eq(x1) || x0.eq(x1.sub(BN_ONE)))
? x0
: newtonIteration(n, x1);
}
import { BN_MAX_INTEGER, BN_ONE, BN_ZERO } from '../bn';
import { bnToBn } from './toBn';
/**
* @name bnSqrt
@@ -28,10 +22,28 @@ function newtonIteration (n: BN, x0: BN): BN {
* bnSqrt(new BN(16)).toString(); // => '4'
* ```
*/
export function bnSqrt (value: BN): BN {
assert(value.gte(BN_ZERO), 'square root of negative numbers is not supported');
export function bnSqrt <ExtToBn extends ToBn> (value: ExtToBn | BN | BigInt | string | number | null): BN {
const n = bnToBn(value);
return value.lt(BN_TWO)
? value
: newtonIteration(value, new BN(1));
assert(n.gte(BN_ZERO), 'square root of negative numbers is not supported');
// https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
// shortcut <= 2^53 - 1 to use the JS utils
if (n.lte(BN_MAX_INTEGER)) {
return new BN(Math.floor(Math.sqrt(n.toNumber())));
}
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
// output will be larger than this, we expect this to be a safe start
let x0 = new BN(94906265);
while (true) {
const x1 = n.div(x0).iadd(x0).ishrn(1);
if (x0.eq(x1) || x0.eq(x1.sub(BN_ONE))) {
return x0;
}
x0 = x1;
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-fetch",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A cross-environment fetch replacement",
"browser": "browser.js",
"main": "node.js",
@@ -14,8 +14,8 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-fetch#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-global": "5.5.3-4",
"@babel/runtime": "^7.12.13",
"@polkadot/x-global": "5.6.0",
"@types/node-fetch": "^2.5.8",
"node-fetch": "^2.6.1"
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-global",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A cross-environment global replacement",
"main": "index.js",
"sideEffects": false,
@@ -12,7 +12,7 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-global#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@babel/runtime": "^7.12.13",
"@types/node-fetch": "^2.5.8",
"node-fetch": "^2.6.1"
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-randomvalues",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A cross-environment window.crypto.getRandomValues replacement",
"browser": "browser.js",
"main": "node.js",
@@ -14,10 +14,10 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-randomvalues#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-global": "5.5.3-4"
"@babel/runtime": "^7.12.13",
"@polkadot/x-global": "5.6.0"
},
"devDependencies": {
"@types/react-native": "^0.63.46"
"@types/react-native": "^0.63.48"
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-rxjs",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A pass-through interface for RxJs (allowing node + mjs usage)",
"main": "index.js",
"sideEffects": false,
@@ -12,7 +12,7 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-rxjs#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@babel/runtime": "^7.12.13",
"rxjs": "^6.6.3"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-textdecoder",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A TextDecoder replacement",
"browser": "browser.js",
"main": "node.js",
@@ -14,7 +14,7 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-textdecoder#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-global": "5.5.3-4"
"@babel/runtime": "^7.12.13",
"@polkadot/x-global": "5.6.0"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-textencoder",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A TextDecoder replacement",
"browser": "browser.js",
"main": "node.js",
@@ -14,7 +14,7 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-textencoder#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-global": "5.5.3-4"
"@babel/runtime": "^7.12.13",
"@polkadot/x-global": "5.6.0"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/x-ws",
"version": "5.5.3-4",
"version": "5.6.0",
"description": "A cross-environment WebSocket replacement",
"browser": "browser.js",
"main": "node.js",
@@ -14,8 +14,8 @@
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-ws#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-global": "5.5.3-4",
"@babel/runtime": "^7.12.13",
"@polkadot/x-global": "5.6.0",
"@types/websocket": "^1.0.1",
"websocket": "^1.0.33"
}
+950 -828
View File
File diff suppressed because it is too large Load Diff