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

Benchmark for BigInt #226

Merged
merged 5 commits into from
Dec 22, 2019
Merged
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
133 changes: 84 additions & 49 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable new-cap, no-new */
/* global BigInt */
/* eslint-disable new-cap, no-new, no-unused-expressions */

var benchmark = require('benchmark');
var crypto = require('crypto');
Expand Down Expand Up @@ -36,6 +37,10 @@ function add (op, obj) {
console.log('Benchmarking: ' + op);

Object.keys(obj).forEach(function (name) {
if (name === 'BigInt' && typeof BigInt === 'undefined') {
return;
}

if (name === 'bignum' && bignum === undefined) {
return;
}
Expand Down Expand Up @@ -101,70 +106,73 @@ while (fixtures.length < 25) {
var aj = prng.randomBytes(768).toString('hex');
var bj = prng.randomBytes(768).toString('hex');

fixture.a10base = new bn(a, 16).toString(10);
fixture.a16base = new bn(a, 16).toString(16);

// BN
fixture.a1 = new bn(a, 16);
fixture.b1 = new bn(b, 16);
fixture.a1j = new bn(aj, 16);
fixture.b1j = new bn(bj, 16);
fixture.as1 = fixture.a1.mul(fixture.a1).iaddn(0x2adbeef);
fixture.am1 = fixture.a1.toRed(bn.red('k256'));
fixture.pow1 = fixture.am1.fromRed();

// BigInt
fixture.a2 = BigInt(fixture.a1.toString(10));
fixture.b2 = BigInt(fixture.b1.toString(10));
fixture.a2j = BigInt(fixture.a1j.toString(10));
fixture.b2j = BigInt(fixture.b1j.toString(10));
fixture.as2 = fixture.a2 * fixture.a2 + 0x2adbeefn;

// bignum
if (bignum) {
fixture.a2 = new bignum(a, 16);
fixture.b2 = new bignum(b, 16);
fixture.a2j = new bignum(aj, 16);
fixture.b2j = new bignum(bj, 16);
fixture.a3 = new bignum(a, 16);
fixture.b3 = new bignum(b, 16);
fixture.a3j = new bignum(aj, 16);
fixture.b3j = new bignum(bj, 16);
fixture.as3 = fixture.a3.mul(fixture.a3).add(0x2adbeef);
}

// bigi
fixture.a4 = new bigi(a, 16);
fixture.b4 = new bigi(b, 16);
fixture.a4j = new bigi(aj, 16);
fixture.b4j = new bigi(bj, 16);
fixture.as4 = fixture.a4.multiply(fixture.a4).add(bigi.valueOf(0x2adbeef));

// sjcl
fixture.a5 = new sjcl(a, 16);
fixture.b5 = new sjcl(b, 16);
fixture.a5j = new sjcl(aj, 16);
fixture.b5j = new sjcl(bj, 16);
// fixture.as5 = fixture.a5.mul(fixture.a5).add(0x2adbeef);
fixture.am5 = new sjcl.prime.p256k(fixture.a5);

// BigInteger
fixture.a6 = new BigInteger(a, 16);
fixture.b6 = new BigInteger(b, 16);
fixture.a6j = new BigInteger(aj, 16);
fixture.b6j = new BigInteger(bj, 16);

// SilentMattBigInteger
fixture.a8 = SilentMattBigInteger.parse(a, 16);
fixture.b8 = SilentMattBigInteger.parse(b, 16);

fixture.a8j = SilentMattBigInteger.parse(aj, 16);
fixture.b8j = SilentMattBigInteger.parse(aj, 16);

//
fixture.as1 = fixture.a1.mul(fixture.a1).iaddn(0x2adbeef);
if (bignum) {
fixture.as2 = fixture.a2.mul(fixture.a2).add(0x2adbeef);
}
fixture.as4 = fixture.a4.multiply(fixture.a4).add(bigi.valueOf(0x2adbeef));
// fixture.as5 = fixture.a5.mul(fixture.a5).add(0x2adbeef);
fixture.as6 = fixture.a6.multiply(fixture.a6).add(
new BigInteger('2adbeef', 16));
fixture.as8 = fixture.a8.multiply(fixture.a8).add(
SilentMattBigInteger.parse('2adbeef', 16));

fixture.am1 = fixture.a1.toRed(bn.red('k256'));
fixture.am5 = new sjcl.prime.p256k(fixture.a5);

fixture.pow1 = fixture.am1.fromRed();

fixture.a10base = fixture.a1.toString(10);
fixture.a16base = a;
// SilentMattBigInteger
fixture.a7 = SilentMattBigInteger.parse(a, 16);
fixture.b7 = SilentMattBigInteger.parse(b, 16);
fixture.a7j = SilentMattBigInteger.parse(aj, 16);
fixture.b7j = SilentMattBigInteger.parse(aj, 16);
fixture.as7 = fixture.a7.multiply(fixture.a7).add(
SilentMattBigInteger.parse('2adbeef', 16));
}

add('create-10', {
'bn.js': function (fixture) {
new bn(fixture.a10base, 10);
},
BigInt: function (fixture) {
BigInt(fixture.a10base);
},
bignum: function (fixture) {
new bignum(fixture.a10base, 10);
},
Expand Down Expand Up @@ -204,27 +212,33 @@ add('toString-10', {
'bn.js': function (fixture) {
fixture.a1.toString(10);
},
bignum: function (fixture) {
BigInt: function (fixture) {
fixture.a2.toString(10);
},
bignum: function (fixture) {
fixture.a3.toString(10);
},
bigi: function (fixture) {
fixture.a4.toString(10);
},
yaffle: function (fixture) {
fixture.a6.toString(10);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8.toString(10);
fixture.a7.toString(10);
}
});

add('toString-hex', {
'bn.js': function (fixture) {
fixture.a1.toString(16);
},
bignum: function (fixture) {
BigInt: function (fixture) {
fixture.a2.toString(16);
},
bignum: function (fixture) {
fixture.a3.toString(16);
},
bigi: function (fixture) {
fixture.a4.toString(16);
},
Expand All @@ -235,16 +249,19 @@ add('toString-hex', {
fixture.a6.toString(16);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8.toString(16);
fixture.a7.toString(16);
}
});

add('add', {
'bn.js': function (fixture) {
fixture.a1.add(fixture.b1);
},
BigInt: function (fixture) {
fixture.a2 + fixture.b2;
},
bignum: function (fixture) {
fixture.a2.add(fixture.b2);
fixture.a3.add(fixture.b3);
},
bigi: function (fixture) {
fixture.a4.add(fixture.b4);
Expand All @@ -256,16 +273,19 @@ add('add', {
fixture.a6.add(fixture.b6);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8.add(fixture.a8);
fixture.a7.add(fixture.a7);
}
});

add('sub', {
'bn.js': function (fixture) {
fixture.b1.sub(fixture.a1);
},
BigInt: function (fixture) {
fixture.a2 - fixture.b2;
},
bignum: function (fixture) {
fixture.b2.sub(fixture.a2);
fixture.b3.sub(fixture.a3);
},
bigi: function (fixture) {
fixture.b4.subtract(fixture.a4);
Expand All @@ -277,7 +297,7 @@ add('sub', {
fixture.b6.subtract(fixture.a6);
},
'silentmatt-biginteger': function (fixture) {
fixture.b8.subtract(fixture.a8);
fixture.b7.subtract(fixture.a7);
}
});

Expand All @@ -288,8 +308,11 @@ add('mul', {
'bn.js[FFT]': function (fixture) {
fixture.a1.mulf(fixture.b1);
},
BigInt: function (fixture) {
fixture.a2 * fixture.b2;
},
bignum: function (fixture) {
fixture.a2.mul(fixture.b2);
fixture.a3.mul(fixture.b3);
},
bigi: function (fixture) {
fixture.a4.multiply(fixture.b4);
Expand All @@ -301,7 +324,7 @@ add('mul', {
fixture.a6.multiply(fixture.b6);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8.multiply(fixture.b8);
fixture.a7.multiply(fixture.b7);
}
});

Expand All @@ -312,8 +335,11 @@ add('mul-jumbo', {
'bn.js[FFT]': function (fixture) {
fixture.a1j.mulf(fixture.b1j);
},
BigInt: function (fixture) {
fixture.a2j * fixture.b2j;
},
bignum: function (fixture) {
fixture.a2j.mul(fixture.b2j);
fixture.a3j.mul(fixture.b3j);
},
bigi: function (fixture) {
fixture.a4j.multiply(fixture.b4j);
Expand All @@ -325,16 +351,19 @@ add('mul-jumbo', {
fixture.a6j.multiply(fixture.b6j);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8j.multiply(fixture.b8j);
fixture.a7j.multiply(fixture.b7j);
}
});

add('sqr', {
'bn.js': function (fixture) {
fixture.a1.mul(fixture.a1);
},
BigInt: function (fixture) {
fixture.a2 * fixture.a2;
},
bignum: function (fixture) {
fixture.a2.mul(fixture.a2);
fixture.a3.mul(fixture.a3);
},
bigi: function (fixture) {
fixture.a4.square();
Expand All @@ -346,16 +375,19 @@ add('sqr', {
fixture.a6.multiply(fixture.a6);
},
'silentmatt-biginteger': function (fixture) {
fixture.a8.multiply(fixture.a8);
fixture.a7.multiply(fixture.a7);
}
});

add('div', {
'bn.js': function (fixture) {
fixture.as1.div(fixture.a1);
},
BigInt: function (fixture) {
fixture.as2 / fixture.a2;
},
bignum: function (fixture) {
fixture.as2.div(fixture.a2);
fixture.as3.div(fixture.a3);
},
bigi: function (fixture) {
fixture.as4.divide(fixture.a4);
Expand All @@ -364,16 +396,19 @@ add('div', {
fixture.as6.divide(fixture.a6);
},
'silentmatt-biginteger': function (fixture) {
fixture.as8.divide(fixture.a8);
fixture.as7.divide(fixture.a7);
}
});

add('mod', {
'bn.js': function (fixture) {
fixture.as1.mod(fixture.a1);
},
BigInt: function (fixture) {
fixture.as2 / fixture.a2;
},
bignum: function (fixture) {
fixture.as2.mod(fixture.a2);
fixture.as3.mod(fixture.a3);
},
bigi: function (fixture) {
fixture.as4.mod(fixture.a4);
Expand All @@ -385,9 +420,9 @@ add('mod', {
: remainder;
},
'silentmatt-biginteger': function (fixture) {
var remainder = fixture.as8.remainder(fixture.a8);
var remainder = fixture.as7.remainder(fixture.a7);
return remainder.compare(BigInteger.ZERO) < 0
? remainder.add(fixture.a8)
? remainder.add(fixture.a7)
: remainder;
}
});
Expand Down Expand Up @@ -418,7 +453,7 @@ add('pow k256', {
fixture.am1.redPow(fixture.pow1);
},
bignum: function (fixture) {
fixture.a2.powm(fixture.a2, prime1);
fixture.a3.powm(fixture.a3, prime1);
}
});

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
"buffer": false
},
"devDependencies": {
"babel-eslint": "^10.0.2",
"mocha": "^6.1.4",
"standardx": "^4.0.0"
},
"standardx": {
"parser": "babel-eslint"
},
"eslintConfig": {
"rules": {
"semi": [
Expand Down
Loading