This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
172 lines (151 loc) · 3.39 KB
/
mod.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { randomBytes } from 'crypto';
import { Curve } from './curve';
import { bufferToBigInt } from './utils';
/**
* Get the modular result of an operation.
*
* @param {Curve} curve
* @param {bigint} n
* @return {bigint}
*/
export const mod = (curve: Curve | bigint, n: bigint): bigint => {
if (typeof curve === 'bigint') {
return ((n % curve) + curve) % curve;
}
return ((n % curve.p) + curve.p) % curve.p;
};
/**
* Returns the greatest common divisor of a and b.
*
* @param {bigint} a
* @param {bigint} b
* @return {{ g: bigint, x: bigint, y: bigint }}
*/
export const eGcd = (a: bigint, b: bigint): { g: bigint; x: bigint; y: bigint } => {
if (a <= 0 || b <= 0) {
throw new Error('a and b must be larger than 0');
}
let [x, y, u, v] = [0n, 1n, 1n, 0n];
while (a !== 0n) {
const q = b / a;
const r = b % a;
const m = x - u * q;
const n = y - v * q;
[b, a] = [a, r];
[x, y] = [u, v];
[u, v] = [m, n];
}
return { g: b, x, y };
};
/**
* Get the ring of integers modulo n.
*
* @param {bigint} a
* @param {bigint} n
* @return {bigint}
*/
export const toZn = (a: bigint, n: bigint): bigint => {
a = a % n;
return a < 0 ? a + n : a;
};
/**
* Returns the modular inverse.
*
* @param {bigint} a
* @param {bigint} modulo
*/
export const modInverse = (a: bigint, modulo: bigint): bigint => {
const { g, x } = eGcd(toZn(a, modulo), modulo);
if (g !== 1n) {
throw new Error('Modular inverse does not exist');
}
return toZn(x, modulo);
};
/**
* a + b
*
* @param {Curve} curve
* @param {bigint} a
* @param {bigint} b
* @return {bigint}
*/
export const add = (curve: Curve, a: bigint, b: bigint): bigint => {
return mod(curve, a + b);
};
/**
* a - b
*
* @param {Curve} curve
* @param {bigint} a
* @param {bigint} b
* @return {bigint}
*/
export const subtract = (curve: Curve, a: bigint, b: bigint): bigint => {
return mod(curve, a - b);
};
/**
* a * b
*
* @param {Curve} curve
* @param {bigint} a
* @param {bigint} b
* @return {bigint}
*/
export const multiply = (curve: Curve, a: bigint, b: bigint): bigint => {
return mod(curve, a * b);
};
/**
* a / b
*
* @param {Curve} curve
* @param {bigint} a
* @param {bigint} b
* @return {bigint}
*/
export const divide = (curve: Curve, a: bigint, b: bigint): bigint => {
const ap = power(curve, b, curve.p - 2n);
return mod(curve, multiply(curve, a, ap));
};
/**
* a ^ b
*
* @param {Curve} curve
* @param {bigint} a
* @param {bigint} b
* @return {bigint}
*/
export const power = (curve: Curve, a: bigint, b: bigint): bigint => {
let x = 1n;
while (b > 0n) {
if (a === 0n) {
return 0n;
}
if (b % 2n === 1n) {
x = multiply(curve, x, a);
}
b = b / 2n;
a = multiply(curve, a, a);
}
return x;
};
/**
* Get the square roots from a y² value on the curve.
*
* @param {Curve} curve
* @param {bigint} value
* @return {[bigint, bigint]}
*/
export const squareRoots = (curve: Curve, value: bigint): [bigint, bigint] => {
const p1 = (curve.p - 1n) / 2n;
const p2 = (curve.p + 1n) / 4n;
if (power(curve, value, p1) !== 1n) {
throw new Error('Square root is not an integer');
}
const root = power(curve, value, p2);
const negativeRoot = curve.p - root;
return [root, negativeRoot];
};
export const random = (curve: Curve): bigint => {
const randomValue = randomBytes(64);
return mod(curve.n, bufferToBigInt(randomValue));
};