-
Notifications
You must be signed in to change notification settings - Fork 1
/
goliath_number_theory.js
77 lines (71 loc) · 1.75 KB
/
goliath_number_theory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//this is the goliath number theoretic api
//this file contains the following algorithms
// pollard rho
// euclidean algorithm
// smallest factor
// Miller Rabin primality testing
(function(exports){
'use strict';
var BB = require('./bitBlocks');
var Bin = require('./binary');
//takes two GNs a and b, returns their greatest common divisor;
function euclid(a, b){
let zero = Bin.tenToBits(0);
if (Bin.isEqual(b, zero)){
return a;
} else {
let c = Bin.divmod(a, b).r;
return euclid(b, c);
}
}
function pollard(n){
let i = 1;
let num = Bin.stringToBits(n);
let zero = Bin.tenToBits(0);
let one = Bin.tenToBits(1);
let max = Bin.dec(num);
let x = Bin.tenToBits(3);
let y = x;
let k = 2;
var c;
while (true) {
i++;
if (Bin.isEqual(x, zero)){
x = max;
} else {
let a = Bin.pow(x, 2);
let b = Bin.dec(a);
x = Bin.divmod(b, num).r;
}
if (Bin.lt(x, y)){
c = Bin.sub(y, x);
} else {
c = Bin.sub(x, y);
}
let d = euclid(c, num);
//if (!Bin.isEqual(d, one) && !Bin.isEqual(d, num)){
console.log(Bin.bitsToString(d));
//}
if (i === k) {
y = x;
k = 2 * k;
}
}
}
//pseudoprimality test
function pseudoprimality(n){
let i = Bin.tenToBits(2);
let one = Bin.tenToBits(1);
let exp = Bin.sub(n, one);
if (Bin.isEqual(Bin.modex(i, exp, n), one)){
return true;
} else {
return false;
}
}
exports.euclid = euclid;
exports.pollard = pollard;
exports.pseudoprimality = pseudoprimality;
//exports.smallFac = smallFac;
//exports.millerRabin = millerRabin;
}) ((typeof exports === 'undefined') ? this.num = {} : exports);