Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace bn.js with bigint wrapper #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions lib/crypto/bn.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,78 @@
'use strict';

var BN = require('bn.js');
var $ = require('../util/preconditions');
var _ = require('lodash');

class BN {
#value

constructor(number, base, endian) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have this wrapper somewhere else? Maybe in currency? To unify I mean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no
a fully compatible bn.js replacement is not this clean
here, bn.js usage is very limited so it can be replaced

also, this was still using bn.js@2

if (number === undefined) {
this.#value = BigInt(0)
} else if (typeof number === 'number') {
this.#value = BigInt(number)
} else if (typeof number === 'string') {
if (base === 'hex') base = 16
if (base === undefined) base = /[a-f]/.test(number) ? 16 : 10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not actually what bn does, recheck

if (base === 10) {
this.#value = BigInt(number)
} else if (base === 16) {
this.#value = BigInt('0x' + number)
} else {
throw new Error('Unexpected base')
}
} else if (Buffer.isBuffer(number)) {
if (endian !== undefined && endian !== 'be' && endian !== 'le') {
throw new Error('Unexpected endian')
}
if (endian === 'le') number = Buffer.from(number).reverse()
this.#value = BigInt('0x' + number.toString('hex'))
} else if (Array.isArray(number)) {
const buf = Buffer.from(number)
if (!buf.every((v, i) => number[i] === v)) throw new Error('Unexpected input')
return new BN(buf, base, endian)
} else if (number instanceof BN) {
this.#value = number.#value
} else {
throw new Error('Unexpected input type')
}
}

cmp(b) {
if (this.#value === b.#value) return 0
return (this.#value < b.#value) ? -1 : 1
}

add(b) {
const res = new BN()
res.#value = this.#value + b.#value
return res
}

sub(b) {
const res = new BN()
res.#value = this.#value - b.#value
return res
}

neg() {
const res = new BN()
res.#value = BigInt(0) - this.#value
return res
}

toString(base = 10, padding) {
if (base === 'hex') base = 16
if (base === 10 || base === 16) {
const str = this.#value.toString(base)
if (!padding) return str
const pad = str.length - 1 - (str.length - 1) % padding + padding
return str.padStart(pad, '0')
}
throw new Error('Unexpected base')
}
}

var reversebuf = function(buf) {
return Buffer.from(buf).reverse()
};
Expand Down Expand Up @@ -53,7 +122,7 @@ function fromSM(buf, opts) {
if (buf[0] & 0x80) {
buf[0] = buf[0] & 0x7f;
ret = fromBuffer(buf);
ret.neg().copy(ret);
ret = ret.neg();
} else {
ret = fromBuffer(buf);
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"@noble/hashes": "^1.3.3",
"@noble/secp256k1": "^1.7.1",
"blake-hash": "1.1.1",
"bn.js": "2.0.4",
"bs58": "2.0.1",
"inherits": "2.0.1",
"lodash": "^4.17.19"
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ [email protected]:
inherits "^2.0.3"
nan "^2.2.1"

[email protected]:
version "2.0.4"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-2.0.4.tgz#220a7cd677f7f1bfa93627ff4193776fe7819480"
integrity sha1-Igp81nf38b+pNif/QZN3b+eBlIA=

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down