-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalogcode.js
More file actions
210 lines (105 loc) · 3.49 KB
/
Catalogcode.js
File metadata and controls
210 lines (105 loc) · 3.49 KB
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
// Prints c (constant term), and also reports points not lying on the curve with deviation.
import fs from "fs";
// ------------------ Utilities: BigInt fraction arithmetic ------------------
function bigIntAbs(x) { return x < 0n ? -x : x; }
function bigIntGcd(a, b) {
a = bigIntAbs(a);
b = bigIntAbs(b);
while (b !== 0n) { const t = a % b; a = b; b = t; }
return a;
}
class Fraction {
constructor(n, d = 1n) {
if (d === 0n) throw new Error("Division by zero in Fraction");
if (d < 0n) { n = -n; d = -d; }
const g = bigIntGcd(n, d);
this.n = n / g;
this.d = d / g;
}
add(o) { return new Fraction(this.n * o.d + o.n * this.d, this.d * o.d); }
sub(o) { return new Fraction(this.n * o.d - o.n * this.d, this.d * o.d); }
mul(o) { return new Fraction(this.n * o.n, this.d * o.d); }
div(o) {
if (o.n === 0n) throw new Error("Division by zero");
let n = this.n * o.d;
let d = this.d * o.n;
if (d < 0n) { n = -n; d = -d; }
return new Fraction(n, d);
}
isInteger() { return this.d === 1n; }
toBigInt() {
if (!this.isInteger()) throw new Error("Fraction is not an integer");
return this.n;
}
toString() { return this.isInteger() ? this.n.toString() : ${this.n}/${this.d}; }
}
// ------------------ Base decoder ------------------
function charToVal(ch) {
const c = ch.toLowerCase();
if (c >= '0' && c <= '9') return c.charCodeAt(0) - '0'.charCodeAt(0);
if (c >= 'a' && c <= 'z') return 10 + (c.charCodeAt(0) - 'a'.charCodeAt(0));
throw new Error(Invalid digit '${ch}');
}
function decodeBaseStringToBigInt(valueStr, baseNum) {
const B = BigInt(baseNum);
let acc = 0n;
for (const ch of valueStr.trim()) {
const v = charToVal(ch);
if (v >= baseNum) throw new Error(`Digit '${ch}' not valid for base ${baseNum}`);
acc = acc * B + BigInt(v);
}
return acc;
}
// ------------------ Lagrange interpolation ------------------
function lagrange(points, xval) {
let res = new Fraction(0n, 1n);
for (let i = 0; i < points.length; i++) {
const [xi, yi] = points[i];
let term = new Fraction(yi, 1n);
for (let j = 0; j < points.length; j++) {
if (i === j) continue;
const [xj] = points[j];
term = term.mul(new Fraction(xval - xj, 1n)).div(new Fraction(xi - xj, 1n));
}
res = res.add(term);
}
return res;
}
function lagrangeAtZero(points) { return lagrange(points, 0n); }
// ------------------ Main ------------------
function main() {
const file = process.argv[2] || "input.json";
const data = JSON.parse(fs.readFileSync(file, "utf-8"));
const k = Number(data.keys.k);
if (!Number.isInteger(k) || k < 2) throw new Error("Invalid k");
const points = [];
for (const key of Object.keys(data)) {
if (key === "keys") continue;
const ent = data[key];
if (!ent) continue;
const x = BigInt(key);
const baseNum = Number(ent.base);
const y = decodeBaseStringToBigInt(ent.value, baseNum);
points.push([x, y]);
}
if (points.length < k) throw new Error("Not enough points");
points.sort((a, b) => (a[0] < b[0] ? -1 : 1));
const kpoints = points.slice(0, k);
const secretFrac = lagrangeAtZero(kpoints);
console.log("Secret c =", secretFrac.toString());
// ---------- Check all points ----------
console.log("\nChecking all points:");
for (const [x, y] of points) {
const predFrac = lagrange(kpoints, x);
if (!predFrac.isInteger()) {
console.log(`x=${x} → predicted non-integer ${predFrac.toString()} (unexpected)`);
continue;
}
const pred = predFrac.toBigInt();
if (pred !== y) {
const deviation = y - pred;
console.log(`Point (x=${x}, y=${y}) deviates by ${deviation}`);
}
}
}
main();