bn.js 1.2.4 → 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/README.md +1 -1
- package/lib/bn.js +109 -0
- package/package.json +1 -1
- package/test/bn-test.js +58 -0
- package/1.js +0 -11
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
|
@@ -494,6 +494,115 @@ BN.prototype.neg = function neg() {
|
|
|
494
494
|
return r;
|
|
495
495
|
};
|
|
496
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
|
+
|
|
497
606
|
// Add `num` to `this` in-place
|
|
498
607
|
BN.prototype.iadd = function iadd(num) {
|
|
499
608
|
// negative + positive
|
package/package.json
CHANGED
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,11 +0,0 @@
|
|
|
1
|
-
var bn = require('./');
|
|
2
|
-
|
|
3
|
-
var a1 = new bn('123456789abcdef0123456789abcdef0123456789abcdef0123', 'hex');
|
|
4
|
-
|
|
5
|
-
var as1 = a1.mul(a1).iaddn(0xdeadbeef & 0x3ffffff);
|
|
6
|
-
|
|
7
|
-
console.time('div');
|
|
8
|
-
for (var i = 0; i < 2000000; i++)
|
|
9
|
-
var q = as1.div(a1);
|
|
10
|
-
console.timeEnd('div');
|
|
11
|
-
console.log(q);
|