bn.js 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jshintrc +89 -0
- package/README.md +1 -1
- package/lib/bn.js +154 -100
- package/package.json +4 -3
- package/test/bn-test.js +58 -0
- package/1.js +0 -8
package/.jshintrc
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
// JSHint Default Configuration File (as on JSHint website)
|
|
3
|
+
// See http://jshint.com/docs/ for more details
|
|
4
|
+
|
|
5
|
+
"maxerr" : 50, // {int} Maximum error before stopping
|
|
6
|
+
|
|
7
|
+
// Enforcing
|
|
8
|
+
"bitwise" : false, // true: Prohibit bitwise operators (&, |, ^, etc.)
|
|
9
|
+
"camelcase" : true, // true: Identifiers must be in camelCase
|
|
10
|
+
"curly" : false, // true: Require {} for every new block or scope
|
|
11
|
+
"eqeqeq" : true, // true: Require triple equals (===) for comparison
|
|
12
|
+
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
|
|
13
|
+
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
|
|
14
|
+
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
|
|
15
|
+
"indent" : 2, // {int} Number of spaces to use for indentation
|
|
16
|
+
"latedef" : false, // true: Require variables/functions to be defined before being used
|
|
17
|
+
"newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()`
|
|
18
|
+
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
|
|
19
|
+
"noempty" : false, // true: Prohibit use of empty blocks
|
|
20
|
+
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
|
|
21
|
+
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
|
|
22
|
+
"plusplus" : false, // true: Prohibit use of `++` & `--`
|
|
23
|
+
"quotmark" : "single", // Quotation mark consistency:
|
|
24
|
+
// false : do nothing (default)
|
|
25
|
+
// true : ensure whatever is used is consistent
|
|
26
|
+
// "single" : require single quotes
|
|
27
|
+
// "double" : require double quotes
|
|
28
|
+
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
|
|
29
|
+
"unused" : true, // true: Require all defined variables be used
|
|
30
|
+
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
|
|
31
|
+
"maxparams" : false, // {int} Max number of formal params allowed per function
|
|
32
|
+
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
|
|
33
|
+
"maxstatements" : false, // {int} Max number statements per function
|
|
34
|
+
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
|
|
35
|
+
"maxlen" : false, // {int} Max number of characters per line
|
|
36
|
+
|
|
37
|
+
// Relaxing
|
|
38
|
+
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
|
|
39
|
+
"boss" : false, // true: Tolerate assignments where comparisons would be expected
|
|
40
|
+
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
|
|
41
|
+
"eqnull" : false, // true: Tolerate use of `== null`
|
|
42
|
+
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
|
|
43
|
+
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
|
|
44
|
+
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
|
|
45
|
+
// (ex: `for each`, multiple try/catch, function expression…)
|
|
46
|
+
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
|
|
47
|
+
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
|
|
48
|
+
"funcscope" : false, // true: Tolerate defining variables inside control statements
|
|
49
|
+
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
|
|
50
|
+
"iterator" : false, // true: Tolerate using the `__iterator__` property
|
|
51
|
+
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
|
|
52
|
+
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
|
|
53
|
+
"laxcomma" : false, // true: Tolerate comma-first style coding
|
|
54
|
+
"loopfunc" : false, // true: Tolerate functions being defined in loops
|
|
55
|
+
"multistr" : false, // true: Tolerate multi-line strings
|
|
56
|
+
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
|
|
57
|
+
"notypeof" : false, // true: Tolerate invalid typeof operator values
|
|
58
|
+
"proto" : false, // true: Tolerate using the `__proto__` property
|
|
59
|
+
"scripturl" : false, // true: Tolerate script-targeted URLs
|
|
60
|
+
"shadow" : true, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
|
|
61
|
+
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
|
|
62
|
+
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
|
|
63
|
+
"validthis" : false, // true: Tolerate using this in a non-constructor function
|
|
64
|
+
|
|
65
|
+
// Environments
|
|
66
|
+
"browser" : true, // Web Browser (window, document, etc)
|
|
67
|
+
"browserify" : false, // Browserify (node.js code in the browser)
|
|
68
|
+
"couch" : false, // CouchDB
|
|
69
|
+
"devel" : true, // Development/debugging (alert, confirm, etc)
|
|
70
|
+
"dojo" : false, // Dojo Toolkit
|
|
71
|
+
"jasmine" : false, // Jasmine
|
|
72
|
+
"jquery" : false, // jQuery
|
|
73
|
+
"mocha" : true, // Mocha
|
|
74
|
+
"mootools" : false, // MooTools
|
|
75
|
+
"node" : false, // Node.js
|
|
76
|
+
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
|
|
77
|
+
"prototypejs" : false, // Prototype and Scriptaculous
|
|
78
|
+
"qunit" : false, // QUnit
|
|
79
|
+
"rhino" : false, // Rhino
|
|
80
|
+
"shelljs" : false, // ShellJS
|
|
81
|
+
"worker" : false, // Web Workers
|
|
82
|
+
"wsh" : false, // Windows Scripting Host
|
|
83
|
+
"yui" : false, // Yahoo User Interface
|
|
84
|
+
|
|
85
|
+
// Custom Globals
|
|
86
|
+
"globals" : {
|
|
87
|
+
"module": true
|
|
88
|
+
} // additional predefined global variables
|
|
89
|
+
}
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Just a bike-shed.
|
|
|
6
6
|
|
|
7
7
|
This software is licensed under the MIT License.
|
|
8
8
|
|
|
9
|
-
Copyright Fedor Indutny,
|
|
9
|
+
Copyright Fedor Indutny, 2015.
|
|
10
10
|
|
|
11
11
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
12
12
|
copy of this software and associated documentation files (the
|
package/lib/bn.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
(function(module, exports) {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
1
5
|
// Utils
|
|
2
6
|
|
|
3
7
|
function assert(val, msg) {
|
|
@@ -5,19 +9,14 @@ function assert(val, msg) {
|
|
|
5
9
|
throw new Error(msg || 'Assertion failed');
|
|
6
10
|
}
|
|
7
11
|
|
|
8
|
-
function assertEqual(l, r, msg) {
|
|
9
|
-
if (l != r)
|
|
10
|
-
throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
|
|
11
|
-
}
|
|
12
|
-
|
|
13
12
|
// Could use `inherits` module, but don't want to move from single file
|
|
14
13
|
// architecture yet.
|
|
15
14
|
function inherits(ctor, superCtor) {
|
|
16
|
-
ctor.super_ = superCtor
|
|
17
|
-
var TempCtor = function () {}
|
|
18
|
-
TempCtor.prototype = superCtor.prototype
|
|
19
|
-
ctor.prototype = new TempCtor()
|
|
20
|
-
ctor.prototype.constructor = ctor
|
|
15
|
+
ctor.super_ = superCtor;
|
|
16
|
+
var TempCtor = function () {};
|
|
17
|
+
TempCtor.prototype = superCtor.prototype;
|
|
18
|
+
ctor.prototype = new TempCtor();
|
|
19
|
+
ctor.prototype.constructor = ctor;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
// BN
|
|
@@ -47,6 +46,8 @@ function BN(number, base, endian) {
|
|
|
47
46
|
}
|
|
48
47
|
if (typeof module === 'object')
|
|
49
48
|
module.exports = BN;
|
|
49
|
+
else
|
|
50
|
+
exports.BN = BN;
|
|
50
51
|
|
|
51
52
|
BN.BN = BN;
|
|
52
53
|
BN.wordSize = 26;
|
|
@@ -292,7 +293,6 @@ var base = 2 - 1;
|
|
|
292
293
|
while (++base < 36 + 1) {
|
|
293
294
|
var groupSize = 0;
|
|
294
295
|
var groupBase = 1;
|
|
295
|
-
// TODO: <=
|
|
296
296
|
while (groupBase < (1 << BN.wordSize) / base) {
|
|
297
297
|
groupBase *= base;
|
|
298
298
|
groupSize += 1;
|
|
@@ -481,8 +481,6 @@ BN.prototype.bitLength = function bitLength() {
|
|
|
481
481
|
};
|
|
482
482
|
|
|
483
483
|
BN.prototype.byteLength = function byteLength() {
|
|
484
|
-
var hi = 0;
|
|
485
|
-
var w = this.words[this.length - 1];
|
|
486
484
|
return Math.ceil(this.bitLength() / 8);
|
|
487
485
|
};
|
|
488
486
|
|
|
@@ -496,6 +494,115 @@ BN.prototype.neg = function neg() {
|
|
|
496
494
|
return r;
|
|
497
495
|
};
|
|
498
496
|
|
|
497
|
+
|
|
498
|
+
// Or `num` with `this` in-place
|
|
499
|
+
BN.prototype.ior = function ior(num) {
|
|
500
|
+
this.sign = this.sign || num.sign;
|
|
501
|
+
|
|
502
|
+
while (this.length < num.length)
|
|
503
|
+
this.words[this.length++] = 0;
|
|
504
|
+
|
|
505
|
+
for (var i = 0; i < num.length; i++)
|
|
506
|
+
this.words[i] = this.words[i] | num.words[i];
|
|
507
|
+
|
|
508
|
+
return this.strip();
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
// Or `num` with `this`
|
|
513
|
+
BN.prototype.or = function or(num) {
|
|
514
|
+
if (this.length > num.length)
|
|
515
|
+
return this.clone().ior(num);
|
|
516
|
+
else
|
|
517
|
+
return num.clone().ior(this);
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
// And `num` with `this` in-place
|
|
522
|
+
BN.prototype.iand = function iand(num) {
|
|
523
|
+
this.sign = this.sign && num.sign;
|
|
524
|
+
|
|
525
|
+
// b = min-length(num, this)
|
|
526
|
+
var b;
|
|
527
|
+
if (this.length > num.length)
|
|
528
|
+
b = num;
|
|
529
|
+
else
|
|
530
|
+
b = this;
|
|
531
|
+
|
|
532
|
+
for (var i = 0; i < b.length; i++)
|
|
533
|
+
this.words[i] = this.words[i] & num.words[i];
|
|
534
|
+
|
|
535
|
+
this.length = b.length;
|
|
536
|
+
|
|
537
|
+
return this.strip();
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
// And `num` with `this`
|
|
542
|
+
BN.prototype.and = function and(num) {
|
|
543
|
+
if (this.length > num.length)
|
|
544
|
+
return this.clone().iand(num);
|
|
545
|
+
else
|
|
546
|
+
return num.clone().iand(this);
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
// Xor `num` with `this` in-place
|
|
551
|
+
BN.prototype.ixor = function ixor(num) {
|
|
552
|
+
this.sign = this.sign || num.sign;
|
|
553
|
+
|
|
554
|
+
// a.length > b.length
|
|
555
|
+
var a;
|
|
556
|
+
var b;
|
|
557
|
+
if (this.length > num.length) {
|
|
558
|
+
a = this;
|
|
559
|
+
b = num;
|
|
560
|
+
} else {
|
|
561
|
+
a = num;
|
|
562
|
+
b = this;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
for (var i = 0; i < b.length; i++)
|
|
566
|
+
this.words[i] = a.words[i] ^ b.words[i];
|
|
567
|
+
|
|
568
|
+
if (this !== a)
|
|
569
|
+
for (; i < a.length; i++)
|
|
570
|
+
this.words[i] = a.words[i];
|
|
571
|
+
|
|
572
|
+
this.length = a.length;
|
|
573
|
+
|
|
574
|
+
return this.strip();
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
// Xor `num` with `this`
|
|
579
|
+
BN.prototype.xor = function xor(num) {
|
|
580
|
+
if (this.length > num.length)
|
|
581
|
+
return this.clone().ixor(num);
|
|
582
|
+
else
|
|
583
|
+
return num.clone().ixor(this);
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
// Set `bit` of `this`
|
|
588
|
+
BN.prototype.setn = function setn(bit, val) {
|
|
589
|
+
assert(typeof bit === 'number' && bit >= 0);
|
|
590
|
+
|
|
591
|
+
var off = (bit / 26) | 0;
|
|
592
|
+
var wbit = bit % 26;
|
|
593
|
+
|
|
594
|
+
while (this.length <= off)
|
|
595
|
+
this.words[this.length++] = 0;
|
|
596
|
+
|
|
597
|
+
if (val)
|
|
598
|
+
this.words[off] = this.words[off] | (1 << wbit);
|
|
599
|
+
else
|
|
600
|
+
this.words[off] = this.words[off] & ~(1 << wbit);
|
|
601
|
+
|
|
602
|
+
return this.strip();
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
|
|
499
606
|
// Add `num` to `this` in-place
|
|
500
607
|
BN.prototype.iadd = function iadd(num) {
|
|
501
608
|
// negative + positive
|
|
@@ -598,12 +705,14 @@ BN.prototype.isub = function isub(num) {
|
|
|
598
705
|
}
|
|
599
706
|
|
|
600
707
|
// a > b
|
|
708
|
+
var a;
|
|
709
|
+
var b;
|
|
601
710
|
if (cmp > 0) {
|
|
602
|
-
|
|
603
|
-
|
|
711
|
+
a = this;
|
|
712
|
+
b = num;
|
|
604
713
|
} else {
|
|
605
|
-
|
|
606
|
-
|
|
714
|
+
a = num;
|
|
715
|
+
b = this;
|
|
607
716
|
}
|
|
608
717
|
|
|
609
718
|
var carry = 0;
|
|
@@ -780,7 +889,6 @@ BN.prototype.imul = function imul(num) {
|
|
|
780
889
|
this.length = this.length + num.length;
|
|
781
890
|
this.words[this.length - 1] = 0;
|
|
782
891
|
|
|
783
|
-
var lastCarry = 0;
|
|
784
892
|
for (var k = this.length - 2; k >= 0; k--) {
|
|
785
893
|
// Sum all words with the same `i + j = k` and accumulate `carry`,
|
|
786
894
|
// note that carry could be >= 0x3ffffff
|
|
@@ -855,7 +963,6 @@ BN.prototype.ishln = function ishln(bits) {
|
|
|
855
963
|
var s = (bits - r) / 26;
|
|
856
964
|
var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
|
|
857
965
|
|
|
858
|
-
var o = this.clone();
|
|
859
966
|
if (r !== 0) {
|
|
860
967
|
var carry = 0;
|
|
861
968
|
for (var i = 0; i < this.length; i++) {
|
|
@@ -1068,64 +1175,17 @@ BN.prototype.subn = function subn(num) {
|
|
|
1068
1175
|
BN.prototype.iabs = function iabs() {
|
|
1069
1176
|
this.sign = false;
|
|
1070
1177
|
|
|
1071
|
-
return this
|
|
1178
|
+
return this;
|
|
1072
1179
|
};
|
|
1073
1180
|
|
|
1074
1181
|
BN.prototype.abs = function abs() {
|
|
1075
1182
|
return this.clone().iabs();
|
|
1076
1183
|
};
|
|
1077
1184
|
|
|
1078
|
-
BN.prototype.
|
|
1079
|
-
assert(!num.sign);
|
|
1080
|
-
if (this.sign) {
|
|
1081
|
-
this.sign = false;
|
|
1082
|
-
this._ishlnsub(num, shift);
|
|
1083
|
-
this.sign = !this.sign;
|
|
1084
|
-
return this;
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
// Bigger storage is needed
|
|
1088
|
-
var len = num.length + shift + 1;
|
|
1089
|
-
if (this.words.length < len) {
|
|
1090
|
-
var t = new Array(len);
|
|
1091
|
-
for (var i = 0; i < this.length; i++)
|
|
1092
|
-
t[i] = this.words[i];
|
|
1093
|
-
this.words = t;
|
|
1094
|
-
} else {
|
|
1095
|
-
i = this.length;
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
// Zeroify rest
|
|
1099
|
-
this.length = Math.max(this.length, len);
|
|
1100
|
-
for (; i < this.length; i++)
|
|
1101
|
-
this.words[i] = 0;
|
|
1102
|
-
|
|
1103
|
-
var carry = 0;
|
|
1104
|
-
for (var i = 0; i < num.length; i++) {
|
|
1105
|
-
var w = this.words[i + shift] + num.words[i] + carry;
|
|
1106
|
-
carry = w >> 26;
|
|
1107
|
-
this.words[i + shift] = w & 0x3ffffff;
|
|
1108
|
-
}
|
|
1109
|
-
for (; i < this.length - shift; i++) {
|
|
1110
|
-
var w = this.words[i + shift] + carry;
|
|
1111
|
-
carry = w >> 26;
|
|
1112
|
-
this.words[i + shift] = w & 0x3ffffff;
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
|
-
return this.strip();
|
|
1116
|
-
};
|
|
1117
|
-
|
|
1118
|
-
BN.prototype._ishlnsub = function _ishlnsub(num, shift) {
|
|
1119
|
-
assert(!num.sign);
|
|
1120
|
-
if (this.sign) {
|
|
1121
|
-
this.sign = false;
|
|
1122
|
-
this._ishlnadd(num, shift);
|
|
1123
|
-
this.sign = !this.sign;
|
|
1124
|
-
return this;
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1185
|
+
BN.prototype._ishlnsubmul = function _ishlnsubmul(num, mul, shift) {
|
|
1127
1186
|
// Bigger storage is needed
|
|
1128
1187
|
var len = num.length + shift;
|
|
1188
|
+
var i;
|
|
1129
1189
|
if (this.words.length < len) {
|
|
1130
1190
|
var t = new Array(len);
|
|
1131
1191
|
for (var i = 0; i < this.length; i++)
|
|
@@ -1142,8 +1202,10 @@ BN.prototype._ishlnsub = function _ishlnsub(num, shift) {
|
|
|
1142
1202
|
|
|
1143
1203
|
var carry = 0;
|
|
1144
1204
|
for (var i = 0; i < num.length; i++) {
|
|
1145
|
-
var w = this.words[i + shift]
|
|
1146
|
-
|
|
1205
|
+
var w = this.words[i + shift] + carry;
|
|
1206
|
+
var right = num.words[i] * mul;
|
|
1207
|
+
w -= right & 0x3ffffff;
|
|
1208
|
+
carry = (w >> 26) - ((right / 0x4000000) | 0);
|
|
1147
1209
|
this.words[i + shift] = w & 0x3ffffff;
|
|
1148
1210
|
}
|
|
1149
1211
|
for (; i < this.length - shift; i++) {
|
|
@@ -1196,16 +1258,13 @@ BN.prototype._wordDiv = function _wordDiv(num, mode) {
|
|
|
1196
1258
|
q.words[i] = 0;
|
|
1197
1259
|
}
|
|
1198
1260
|
|
|
1199
|
-
var diff = a.clone().
|
|
1261
|
+
var diff = a.clone()._ishlnsubmul(b, 1, m);
|
|
1200
1262
|
if (!diff.sign) {
|
|
1201
1263
|
a = diff;
|
|
1202
1264
|
if (q)
|
|
1203
1265
|
q.words[m] = 1;
|
|
1204
1266
|
}
|
|
1205
1267
|
|
|
1206
|
-
var st = new BN(null);
|
|
1207
|
-
st.words = new Array(b.length + 1);
|
|
1208
|
-
st.length = 0;
|
|
1209
1268
|
for (var j = m - 1; j >= 0; j--) {
|
|
1210
1269
|
var qj = a.words[b.length + j] * 0x4000000 + a.words[b.length + j - 1];
|
|
1211
1270
|
|
|
@@ -1213,13 +1272,12 @@ BN.prototype._wordDiv = function _wordDiv(num, mode) {
|
|
|
1213
1272
|
// (0x7ffffff)
|
|
1214
1273
|
qj = Math.min((qj / bhi) | 0, 0x3ffffff);
|
|
1215
1274
|
|
|
1216
|
-
|
|
1217
|
-
st.words[i] = b.words[i];
|
|
1218
|
-
st.length = b.length;
|
|
1219
|
-
a._ishlnsub(st.imuln(qj), j);
|
|
1275
|
+
a._ishlnsubmul(b, qj, j);
|
|
1220
1276
|
while (a.sign) {
|
|
1221
1277
|
qj--;
|
|
1222
|
-
a.
|
|
1278
|
+
a.sign = false;
|
|
1279
|
+
a._ishlnsubmul(b, 1, j);
|
|
1280
|
+
a.sign = !a.sign;
|
|
1223
1281
|
}
|
|
1224
1282
|
if (q)
|
|
1225
1283
|
q.words[j] = qj;
|
|
@@ -1229,7 +1287,7 @@ BN.prototype._wordDiv = function _wordDiv(num, mode) {
|
|
|
1229
1287
|
a.strip();
|
|
1230
1288
|
|
|
1231
1289
|
// Denormalize
|
|
1232
|
-
if (shift !== 0)
|
|
1290
|
+
if (mode !== 'div' && shift !== 0)
|
|
1233
1291
|
a.ishrn(shift);
|
|
1234
1292
|
return { div: q ? q : null, mod: a };
|
|
1235
1293
|
};
|
|
@@ -1429,11 +1487,11 @@ BN.prototype.invm = function invm(num) {
|
|
|
1429
1487
|
return this._egcd(new BN(1), num).mod(num);
|
|
1430
1488
|
};
|
|
1431
1489
|
|
|
1432
|
-
BN.prototype.isEven = function isEven(
|
|
1490
|
+
BN.prototype.isEven = function isEven() {
|
|
1433
1491
|
return (this.words[0] & 1) === 0;
|
|
1434
1492
|
};
|
|
1435
1493
|
|
|
1436
|
-
BN.prototype.isOdd = function isOdd(
|
|
1494
|
+
BN.prototype.isOdd = function isOdd() {
|
|
1437
1495
|
return (this.words[0] & 1) === 1;
|
|
1438
1496
|
};
|
|
1439
1497
|
|
|
@@ -1713,30 +1771,27 @@ K256.prototype.imulK = function imulK(num) {
|
|
|
1713
1771
|
num.words[num.length + 1] = 0;
|
|
1714
1772
|
num.length += 2;
|
|
1715
1773
|
|
|
1716
|
-
|
|
1717
|
-
var hi
|
|
1774
|
+
// bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390
|
|
1775
|
+
var hi;
|
|
1718
1776
|
var lo = 0;
|
|
1719
1777
|
for (var i = 0; i < num.length; i++) {
|
|
1720
1778
|
var w = num.words[i];
|
|
1721
|
-
hi
|
|
1779
|
+
hi = w * 0x40;
|
|
1722
1780
|
lo += w * 0x3d1;
|
|
1723
1781
|
hi += (lo / 0x4000000) | 0;
|
|
1724
|
-
uhi += (hi / 0x4000000) | 0;
|
|
1725
|
-
hi &= 0x3ffffff;
|
|
1726
1782
|
lo &= 0x3ffffff;
|
|
1727
1783
|
|
|
1728
1784
|
num.words[i] = lo;
|
|
1729
1785
|
|
|
1730
1786
|
lo = hi;
|
|
1731
|
-
hi = uhi;
|
|
1732
|
-
uhi = 0;
|
|
1733
1787
|
}
|
|
1734
1788
|
|
|
1735
1789
|
// Fast length reduction
|
|
1736
|
-
if (num.words[num.length - 1] === 0)
|
|
1737
|
-
num.length--;
|
|
1738
|
-
if (num.words[num.length - 1] === 0)
|
|
1790
|
+
if (num.words[num.length - 1] === 0) {
|
|
1739
1791
|
num.length--;
|
|
1792
|
+
if (num.words[num.length - 1] === 0)
|
|
1793
|
+
num.length--;
|
|
1794
|
+
}
|
|
1740
1795
|
return num;
|
|
1741
1796
|
};
|
|
1742
1797
|
|
|
@@ -1801,7 +1856,7 @@ BN._prime = function prime(name) {
|
|
|
1801
1856
|
primes[name] = prime;
|
|
1802
1857
|
|
|
1803
1858
|
return prime;
|
|
1804
|
-
}
|
|
1859
|
+
};
|
|
1805
1860
|
|
|
1806
1861
|
//
|
|
1807
1862
|
// Base reduction engine
|
|
@@ -2018,12 +2073,9 @@ function Mont(m) {
|
|
|
2018
2073
|
this.r2 = this.imod(this.r.sqr());
|
|
2019
2074
|
this.rinv = this.r.invm(this.m);
|
|
2020
2075
|
|
|
2021
|
-
|
|
2022
|
-
this.minv =
|
|
2023
|
-
|
|
2024
|
-
.div(this.m)
|
|
2025
|
-
.neg()
|
|
2026
|
-
.mod(this.r);
|
|
2076
|
+
this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
|
|
2077
|
+
this.minv.sign = true;
|
|
2078
|
+
this.minv = this.minv.mod(this.r);
|
|
2027
2079
|
}
|
|
2028
2080
|
inherits(Mont, Red);
|
|
2029
2081
|
|
|
@@ -2077,3 +2129,5 @@ Mont.prototype.invm = function invm(a) {
|
|
|
2077
2129
|
var res = this.imod(a.invm(this.m).mul(this.r2));
|
|
2078
2130
|
return res._forceRed(this);
|
|
2079
2131
|
};
|
|
2132
|
+
|
|
2133
|
+
})(typeof module === 'undefined' || module, this);
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bn.js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Big number implementation in pure javascript",
|
|
5
5
|
"main": "lib/bn.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "mocha --reporter=spec test/*-test.js"
|
|
7
|
+
"test": "jshint lib/*.js && mocha --reporter=spec test/*-test.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://github.com/indutny/bn.js",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"
|
|
27
|
+
"jshint": "^2.6.0",
|
|
28
|
+
"mocha": "^2.1.0"
|
|
28
29
|
}
|
|
29
30
|
}
|
package/test/bn-test.js
CHANGED
|
@@ -445,4 +445,62 @@ describe('BN', function() {
|
|
|
445
445
|
assert.equal(new BN(18).gcd(new BN(12)).toString(16), '6');
|
|
446
446
|
assert.equal(new BN(-18).gcd(new BN(12)).toString(16), '6');
|
|
447
447
|
});
|
|
448
|
+
|
|
449
|
+
it('should and numbers', function () {
|
|
450
|
+
assert.equal(new BN('1010101010101010101010101010101010101010', 2)
|
|
451
|
+
.and(new BN('101010101010101010101010101010101010101', 2))
|
|
452
|
+
.toString(2), '0');
|
|
453
|
+
});
|
|
454
|
+
it('should iand numbers', function () {
|
|
455
|
+
assert.equal(new BN('1010101010101010101010101010101010101010', 2)
|
|
456
|
+
.iand(new BN('101010101010101010101010101010101010101', 2))
|
|
457
|
+
.toString(2), '0');
|
|
458
|
+
assert.equal(new BN('1000000000000000000000000000000000000001', 2)
|
|
459
|
+
.iand(new BN('1', 2))
|
|
460
|
+
.toString(2), '1')
|
|
461
|
+
assert.equal(new BN('1', 2)
|
|
462
|
+
.iand(new BN('1000000000000000000000000000000000000001', 2))
|
|
463
|
+
.toString(2), '1')
|
|
464
|
+
});
|
|
465
|
+
it('should or numbers', function () {
|
|
466
|
+
assert.equal(new BN('1010101010101010101010101010101010101010', 2)
|
|
467
|
+
.or(new BN('101010101010101010101010101010101010101', 2))
|
|
468
|
+
.toString(2), '1111111111111111111111111111111111111111');
|
|
469
|
+
});
|
|
470
|
+
it('should ior numbers', function () {
|
|
471
|
+
assert.equal(new BN('1010101010101010101010101010101010101010', 2)
|
|
472
|
+
.ior(new BN('101010101010101010101010101010101010101', 2))
|
|
473
|
+
.toString(2), '1111111111111111111111111111111111111111');
|
|
474
|
+
assert.equal(new BN('1000000000000000000000000000000000000000', 2)
|
|
475
|
+
.ior(new BN('1', 2))
|
|
476
|
+
.toString(2), '1000000000000000000000000000000000000001');
|
|
477
|
+
assert.equal(new BN('1', 2)
|
|
478
|
+
.ior(new BN('1000000000000000000000000000000000000000', 2))
|
|
479
|
+
.toString(2), '1000000000000000000000000000000000000001');
|
|
480
|
+
});
|
|
481
|
+
it('should xor numbers', function () {
|
|
482
|
+
assert.equal(new BN('11001100110011001100110011001100', 2)
|
|
483
|
+
.xor(new BN('1100110011001100110011001100110', 2))
|
|
484
|
+
.toString(2), '10101010101010101010101010101010');
|
|
485
|
+
});
|
|
486
|
+
it('should ixor numbers', function () {
|
|
487
|
+
assert.equal(new BN('11001100110011001100110011001100', 2)
|
|
488
|
+
.ixor(new BN('1100110011001100110011001100110', 2))
|
|
489
|
+
.toString(2), '10101010101010101010101010101010');
|
|
490
|
+
assert.equal(new BN('11001100110011001100110011001100', 2)
|
|
491
|
+
.ixor(new BN('1', 2))
|
|
492
|
+
.toString(2), '11001100110011001100110011001101');
|
|
493
|
+
assert.equal(new BN('1', 2)
|
|
494
|
+
.ixor(new BN('11001100110011001100110011001100', 2))
|
|
495
|
+
.toString(2), '11001100110011001100110011001101');
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('should allow single bits to be set', function () {
|
|
499
|
+
assert.equal(new BN(0).setn(2, true).toString(2), '100');
|
|
500
|
+
assert.equal(new BN(0).setn(27, true).toString(2),
|
|
501
|
+
'1000000000000000000000000000');
|
|
502
|
+
assert.equal(new BN('1000000000000000000000000001', 2).setn(27, false)
|
|
503
|
+
.toString(2), '1');
|
|
504
|
+
assert.equal(new BN('101', 2).setn(2, false).toString(2), '1');
|
|
505
|
+
});
|
|
448
506
|
});
|
package/1.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
var BN = require('./');
|
|
2
|
-
|
|
3
|
-
var a = new BN("6582018229284824168619876730229320890292528855852623664389292032");
|
|
4
|
-
var b = new BN("730750818665451459101842416358132502628711530497");
|
|
5
|
-
console.log(b.words);
|
|
6
|
-
var q = a.div(b);
|
|
7
|
-
var m = a.mod(b);
|
|
8
|
-
console.log(q, m);
|