@invisi-labs/primitives 1.2.4 → 1.2.5

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/dist/index.cjs CHANGED
@@ -155,6 +155,33 @@ function multiply(point, scalar) {
155
155
  function inverseScalar(scalar) {
156
156
  return (0, import_modular.Field)(FR).inv(scalar);
157
157
  }
158
+ var sqrtOddOrder = FQ - 1n >> 47n;
159
+ var sqrtRootOfUnity = field.pow(11n, sqrtOddOrder);
160
+ function inverseSquareRoot(denominator) {
161
+ if (denominator <= 0n || denominator >= FQ)
162
+ throw new Error("Invalid Decaf377 square root input");
163
+ let root = field.pow(denominator, sqrtOddOrder - 1n >> 1n);
164
+ let residue = f(root * root * denominator);
165
+ let unity = sqrtRootOfUnity;
166
+ let order = 47;
167
+ while (residue !== 1n) {
168
+ let index = 0;
169
+ let power = residue;
170
+ while (power !== 1n && index < order) {
171
+ power = f(power * power);
172
+ index++;
173
+ }
174
+ if (index >= order) throw new Error("Invalid Decaf377 square root");
175
+ let adjustment = unity;
176
+ for (let i = 0; i < order - index - 1; i++) adjustment = f(adjustment * adjustment);
177
+ root = f(root * adjustment);
178
+ unity = f(adjustment * adjustment);
179
+ residue = f(residue * unity);
180
+ order = index;
181
+ }
182
+ if (f(root * root * denominator) !== 1n) throw new Error("Invalid Decaf377 square root");
183
+ return root;
184
+ }
158
185
  function decodePoint(bytes3) {
159
186
  if (!(bytes3 instanceof Uint8Array) || bytes3.length !== 32)
160
187
  throw new Error("Invalid Decaf377 encoding length");
@@ -168,7 +195,7 @@ function decodePoint(bytes3) {
168
195
  if (denominator === 0n) throw new Error("Invalid Decaf377 encoding");
169
196
  let v;
170
197
  try {
171
- v = field.sqrt(field.inv(denominator));
198
+ v = inverseSquareRoot(denominator);
172
199
  } catch {
173
200
  throw new Error("Invalid Decaf377 encoding");
174
201
  }
@@ -184,7 +211,7 @@ function encodePoint(point) {
184
211
  const aMinusD = -3022n;
185
212
  const u1 = f((x + t) * (x - t));
186
213
  const denominator = f(u1 * aMinusD * x * x);
187
- const v = field.sqrt(field.inv(denominator));
214
+ const v = inverseSquareRoot(denominator);
188
215
  const u2 = abs(f(v * u1));
189
216
  const u3 = f(u2 * z - t);
190
217
  return writeLE(abs(f(aMinusD * v * u3 * x)));
@@ -217,27 +244,45 @@ var hashBit = (p, shared, q) => hash("bit", p, shared, q)[0] & 1;
217
244
  function deriveClueKey(detectionKey) {
218
245
  return encodePoint(multiply(generator, detectionScalar(detectionKey)));
219
246
  }
220
- function validateClueKey(clueKey) {
247
+ var clueKeys = /* @__PURE__ */ new Map();
248
+ function expandedClueKey(clueKey) {
221
249
  requireBytes(clueKey, 32, "Clue key");
222
- if (isIdentity(decodePoint(clueKey))) throw new Error("Clue key must not be the identity");
250
+ const key = (0, import_utils.bytesToHex)(clueKey);
251
+ let expanded = clueKeys.get(key);
252
+ if (expanded) clueKeys.delete(key);
253
+ else {
254
+ const root = decodePoint(clueKey);
255
+ if (isIdentity(root)) throw new Error("Clue key must not be the identity");
256
+ root._setWindowSize(4);
257
+ expanded = { root };
258
+ }
259
+ clueKeys.set(key, expanded);
260
+ if (clueKeys.size > 8) clueKeys.delete(clueKeys.keys().next().value);
261
+ return expanded;
262
+ }
263
+ function validateClueKey(clueKey) {
264
+ expandedClueKey(clueKey);
223
265
  }
224
266
  function createClue(clueKey) {
225
267
  validateClueKey(clueKey);
226
268
  return createClueFromSeed(clueKey, secureRandomBytes(32));
227
269
  }
228
270
  function createClueFromSeed(clueKey, seed32) {
229
- validateClueKey(clueKey);
271
+ const expanded = expandedClueKey(clueKey);
230
272
  requireBytes(seed32, 32, "Clue seed");
231
- const root = decodePoint(clueKey);
273
+ const offsets = expanded.offsets ??= Array.from(
274
+ { length: PRECISION },
275
+ (_, i) => childOffset(clueKey, i)
276
+ );
232
277
  const r = hashScalar("rdv", clueKey, seed32);
233
278
  const z = hashScalar("zdv", clueKey, seed32);
234
279
  if (r === 0n) throw new Error("Degenerate clue seed");
235
280
  const p = encodePoint(multiply(generator, r));
236
281
  const q = encodePoint(multiply(generator, z));
237
282
  const ciphertext = new Uint8Array(3);
283
+ const rootShared = multiply(expanded.root, r);
238
284
  for (let i = 0; i < PRECISION; i++) {
239
- const child = root.add(multiply(generator, childOffset(clueKey, i)));
240
- const shared = encodePoint(multiply(child, r));
285
+ const shared = encodePoint(rootShared.add(multiply(generator, offsets[i] * r % FR)));
241
286
  ciphertext[i >> 3] = ciphertext[i >> 3] | (hashBit(p, shared, q) ^ 1) << (i & 7);
242
287
  }
243
288
  const m = hashScalar("sca", p, Uint8Array.of(PRECISION), ciphertext);