-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolynomials.mjs
342 lines (249 loc) · 8.1 KB
/
polynomials.mjs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { bls12_381 } from '@noble/curves/bls12-381'
const defaultField = bls12_381.fields.Fr
export function demoPolynomials() {
let resultE = evaluate([10n, 100n, 1000n], bls12_381.fields.Fr.ONE + bls12_381.fields.Fr.ONE, bls12_381.fields.Fr)
let resultMP = multiply([10n, 20n], [300n, 500n, 700n], bls12_381.fields.Fr)
let resultME = multiplyScalar([10n, 20n], 33n, bls12_381.fields.Fr)
let resultD1 = divide([-4n, 0n, -2n, 1n], [-3n, 1n], bls12_381.fields.Fr)
console.log(resultD1)
let resultD2 = divide([-42n, 0n, -12n, 1n], [1n, -2n, 1n], bls12_381.fields.Fr)
console.log(resultD2)
let resultMD = add(
multiply(resultD2.quotient, [1n, -2n, 1n], bls12_381.fields.Fr),
resultD2.rest,
bls12_381.fields.Fr
)
console.log(resultMD)
let resultI = lagrange([2n, 5n], [3n, 4n], bls12_381.fields.Fr)
let output1 = evaluate(resultI, 2n, bls12_381.fields.Fr)
console.log(output1)
let output2 = evaluate(resultI, 5n, bls12_381.fields.Fr)
console.log(output2)
}
export function add(coefficients1, coefficients2, field = defaultField) {
if (coefficients1.length == 0 || coefficients2.length == 0) {
throw new Error("Coefficients should not be empty")
}
var maxLength = Math.max(coefficients1.length, coefficients2.length)
let result = []
for (var i = 0; i < maxLength; i++) {
result.push(field.add(coefficients1[i] || field.ZERO, coefficients2[i] || field.ZERO))
}
return result
}
export function sub(coefficients1, coefficients2, field = defaultField) {
if (coefficients1.length == 0 || coefficients2.length == 0) {
throw new Error("Coefficients should not be empty")
}
var maxLength = Math.max(coefficients1.length, coefficients2.length)
let result = []
for (var i = 0; i < maxLength; i++) {
result.push(field.sub(coefficients1[i] || field.ZERO, coefficients2[i] || field.ZERO))
}
return result
}
export function multiply(coefficients1, coefficients2, field = defaultField) {
if (coefficients1.length == 0 || coefficients2.length == 0) {
throw new Error("Coefficients should not be empty")
}
let result = [field.ZERO]
let copyCoefficient2 = Array.from(coefficients2)
for (let i = 0; i < coefficients1.length; i++) {
if(i > 0) {
copyCoefficient2.unshift(0n)
}
result = add(
result,
multiplyScalar(copyCoefficient2, coefficients1[i], field),
field
)
}
return result
}
export function normalize(coefficients, field = defaultField) {
while (coefficients.length > 1 && coefficients[coefficients.length - 1] == field.ZERO) {
coefficients.pop()
}
}
export function divide(coefficients1, coefficients2, field = defaultField) {
if (coefficients1.length == 0 || coefficients2.length == 0) {
throw new Error("Coefficients should not be empty")
}
if (coefficients1.length < coefficients2.length) {
return { quotient: [field.ZERO], rest: Array.from(coefficients1) }
}
let numerator = Array.from(coefficients1)
let denominator = Array.from(coefficients2)
normalize(numerator, field)
normalize(denominator, field)
const shiftLength = (numerator.length - denominator.length)
for(let i = 0; i < shiftLength; i++) {
denominator.unshift(field.ZERO)
}
let result = []
let divisor = denominator[denominator.length - 1]
for(let i = 0; i <= shiftLength; i++) {
let factor = field.div(
numerator[numerator.length - 1],
divisor
)
result.unshift(factor)
if (factor != 0) {
numerator = sub(
numerator,
multiplyScalar(denominator, factor, field),
field
)
}
// Remove last one
numerator.pop()
// Remove first one
denominator.shift()
}
normalize(numerator, field)
return { quotient: result, rest: numerator }
}
export function multiplyScalar(coefficients, scalar, field = defaultField) {
if (coefficients.length == 0) {
throw new Error("Coefficients should not be empty")
}
return coefficients.map(coefficient => field.mul(coefficient, scalar))
}
export function evaluate(coefficients, point, field = defaultField) {
if (coefficients.length == 0) {
throw new Error("Coefficients should not be empty")
}
let result = field.ZERO
for (let i = coefficients.length - 1; i >= 1; i--) {
result = field.mul(result, point)
result = field.add(result, field.mul(point, coefficients[i]))
}
result = field.add(result, coefficients[0])
return result
}
export function lagrange(xs, ys, field = defaultField) {
if (xs.length == 0 || ys.length == 0) {
throw new Error("Point arrays should not be empty")
}
if (xs.length != ys.length) {
throw new Error("Point arrays should have the same length")
}
let top = [field.ONE]
for (let k = 1; k < xs.length; k++) {
top = multiply(top, [field.neg(xs[k]), field.ONE], field)
}
let sum = [field.ZERO]
for (let j = 0; j < xs.length; j++) {
// top = (X - x_0)...(X - x_k) [ excluding (X - x_j)]
// bottom = (x_j - x_0) ... (x_j - x_k) [ excluding (x_j - x_j)]
let bottom = field.ONE
for(let k = 0; k < xs.length; k++) {
if (k != j) {
bottom = field.mul(bottom, field.sub(xs[j], xs[k]))
}
}
let lj = multiplyScalar(top, field.inv(bottom), field)
lj = lj.map(coefficient => field.mul(coefficient, ys[j]))
sum = add(sum, lj, field)
// Adjust top
if(j != xs.length - 1) {
top = divide(top, [field.neg(xs[j + 1]), field.ONE], field).quotient
top = multiply(top, [field.neg(xs[j]), field.ONE], field)
}
}
return sum
}
export function lagrangeLiteral(xs, ys, field = defaultField) {
if (xs.length == 0 || ys.length == 0) {
throw new Error("Point arrays should not be empty")
}
if (xs.length != ys.length) {
throw new Error("Point arrays should have the same length")
}
let sum = [field.ZERO]
for (let j = 0; j < xs.length; j++) {
// top = (X - x_0)...(X - x_k) [ excluding (X - x_j)]
let top = [field.ONE]
for (let k = 0; k < xs.length; k++) {
if (k != j) {
top = multiply(top, [field.neg(xs[k]), field.ONE], field)
}
}
// bottom = (x_j - x_0) ... (x_j - x_k) [ excluding (x_j - x_j)]
let bottom = field.ONE
for(let k = 0; k < xs.length; k++) {
if (k != j) {
bottom = field.mul(bottom, field.sub(xs[j], xs[k]))
}
}
let lj = multiplyScalar(top, field.inv(bottom), field)
lj = lj.map(coefficient => field.mul(coefficient, ys[j]))
sum = add(sum, lj, field)
}
return sum
}
export function lagrangeProjectivePointsAtZero(xs, ys, field = defaultField, zeroPoint = defaultField.ZERO) {
if (xs.length == 0 || ys.length == 0) {
throw new Error("Point arrays should not be empty")
}
if (xs.length != ys.length) {
throw new Error("Point arrays should have the same length")
}
let sum = zeroPoint
for (let j = 0; j < xs.length; j++) {
// top = (X - x_0)...(X - x_k) [ excluding (X - x_j)]
let top = field.ONE
for (let k = 0; k < xs.length; k++) {
if (k != j) {
top = field.mul(top, xs[k])
}
}
let bottom = field.ONE
for(let k = 0; k < xs.length; k++) {
if (k != j) {
bottom = field.mul(bottom, field.sub(xs[k], xs[j]))
}
}
let lj = field.mul(top, field.inv(bottom))
sum = sum.add(ys[j].multiply(lj))
}
return sum
}
export function lagrangeProjectivePoints(xs, ys, field = defaultField, zeroPoint = defaultField.ZERO) {
if (xs.length == 0 || ys.length == 0) {
throw new Error("Point arrays should not be empty")
}
if (xs.length != ys.length) {
throw new Error("Point arrays should have the same length")
}
let sum = [zeroPoint]
for (let j = 0; j < xs.length; j++) {
// top = (X - x_0)...(X - x_k) [ excluding (X - x_j)]
let top = [field.ONE]
for (let k = 0; k < xs.length; k++) {
if (k != j) {
top = multiply(top, [field.neg(xs[k]), field.ONE], field)
}
}
// bottom = (x_j - x_0) ... (x_j - x_k) [ excluding (x_j - x_j)]
let bottom = field.ONE
for(let k = 0; k < xs.length; k++) {
if (k != j) {
bottom = field.mul(bottom, field.sub(xs[j], xs[k]))
}
}
let lj = multiplyScalar(top, field.inv(bottom), field)
// Multiply coefficients by point
lj = lj.map(coefficient => ys[j].multiply(coefficient))
// Update sum
var maxLength = Math.max(sum.length, lj.length)
let result = []
for (var i = 0; i < maxLength; i++) {
const lhs = sum[i] || zeroPoint
const rhs = lj[i] || zeroPoint
result.push(lhs.add(rhs))
}
sum = result
}
return sum
}