@rialo/ts-cdk 0.5.0-alpha.0 → 0.9.0
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/README.md +355 -294
- package/dist/index.d.mts +166 -227
- package/dist/index.d.ts +166 -227
- package/dist/index.js +1463 -173
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1455 -164
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CipherSuite, HkdfSha256, DhkemX25519HkdfSha256 } from '@hpke/core';
|
|
1
|
+
import { chacha20poly1305 } from '@noble/ciphers/chacha';
|
|
3
2
|
|
|
4
3
|
var __create = Object.create;
|
|
5
4
|
var __defProp = Object.defineProperty;
|
|
@@ -10,8 +9,8 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
|
10
9
|
var __esm = (fn, res) => function __init() {
|
|
11
10
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
12
11
|
};
|
|
13
|
-
var __commonJS = (cb,
|
|
14
|
-
return
|
|
12
|
+
var __commonJS = (cb, mod2) => function __require() {
|
|
13
|
+
return mod2 || (0, cb[__getOwnPropNames(cb)[0]])((mod2 = { exports: {} }).exports, mod2), mod2.exports;
|
|
15
14
|
};
|
|
16
15
|
var __export = (target, all) => {
|
|
17
16
|
for (var name in all)
|
|
@@ -25,13 +24,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
25
24
|
}
|
|
26
25
|
return to;
|
|
27
26
|
};
|
|
28
|
-
var __toESM = (
|
|
27
|
+
var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
|
|
29
28
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
29
|
// file that has been converted to a CommonJS file using a Babel-
|
|
31
30
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
31
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
-
__defProp(target, "default", { value:
|
|
34
|
-
|
|
32
|
+
__defProp(target, "default", { value: mod2, enumerable: true }) ,
|
|
33
|
+
mod2
|
|
35
34
|
));
|
|
36
35
|
|
|
37
36
|
// node_modules/@scure/base/index.js
|
|
@@ -5744,82 +5743,52 @@ var RialoError = class _RialoError extends Error {
|
|
|
5744
5743
|
};
|
|
5745
5744
|
|
|
5746
5745
|
// src/rex/errors.ts
|
|
5747
|
-
var
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
5752
|
-
|
|
5753
|
-
return
|
|
5754
|
-
})(
|
|
5755
|
-
var
|
|
5746
|
+
var EncryptionErrorCode = /* @__PURE__ */ ((EncryptionErrorCode2) => {
|
|
5747
|
+
EncryptionErrorCode2["INVALID_INPUT"] = "INVALID_INPUT";
|
|
5748
|
+
EncryptionErrorCode2["INVALID_THRESHOLD_KEY"] = "INVALID_THRESHOLD_KEY";
|
|
5749
|
+
EncryptionErrorCode2["ENCRYPTION_FAILED"] = "ENCRYPTION_FAILED";
|
|
5750
|
+
EncryptionErrorCode2["BORSH_DESERIALIZE_FAILED"] = "BORSH_DESERIALIZE_FAILED";
|
|
5751
|
+
EncryptionErrorCode2["INVALID_REX_VALUE"] = "INVALID_REX_VALUE";
|
|
5752
|
+
return EncryptionErrorCode2;
|
|
5753
|
+
})(EncryptionErrorCode || {});
|
|
5754
|
+
var EncryptionError = class _EncryptionError extends Error {
|
|
5756
5755
|
code;
|
|
5757
5756
|
cause;
|
|
5758
5757
|
constructor(code, message, cause) {
|
|
5759
5758
|
super(message);
|
|
5760
|
-
this.name = "
|
|
5759
|
+
this.name = "EncryptionError";
|
|
5761
5760
|
this.code = code;
|
|
5762
5761
|
this.cause = cause;
|
|
5763
5762
|
if (Error.captureStackTrace) {
|
|
5764
|
-
Error.captureStackTrace(this,
|
|
5763
|
+
Error.captureStackTrace(this, _EncryptionError);
|
|
5765
5764
|
}
|
|
5766
5765
|
}
|
|
5767
|
-
|
|
5768
|
-
|
|
5769
|
-
*
|
|
5770
|
-
* @param expected - Expected key length in bytes
|
|
5771
|
-
* @param actual - Actual key length in bytes
|
|
5772
|
-
* @param keyType - Description of the key type (e.g., "REX public key")
|
|
5773
|
-
*/
|
|
5774
|
-
static invalidKeyLength(expected, actual, keyType) {
|
|
5775
|
-
return new _HpkeError(
|
|
5776
|
-
"INVALID_KEY_LENGTH" /* INVALID_KEY_LENGTH */,
|
|
5777
|
-
`Invalid ${keyType} length: expected ${expected} bytes, got ${actual}`
|
|
5778
|
-
);
|
|
5766
|
+
static invalidInput(message) {
|
|
5767
|
+
return new _EncryptionError("INVALID_INPUT" /* INVALID_INPUT */, message);
|
|
5779
5768
|
}
|
|
5780
|
-
|
|
5781
|
-
|
|
5782
|
-
|
|
5783
|
-
|
|
5784
|
-
|
|
5785
|
-
*/
|
|
5786
|
-
static ciphertextTooShort(minLength, actual) {
|
|
5787
|
-
return new _HpkeError(
|
|
5788
|
-
"CIPHERTEXT_TOO_SHORT" /* CIPHERTEXT_TOO_SHORT */,
|
|
5789
|
-
`Ciphertext too short: minimum ${minLength} bytes required, got ${actual}`
|
|
5769
|
+
static invalidThresholdKey(cause) {
|
|
5770
|
+
return new _EncryptionError(
|
|
5771
|
+
"INVALID_THRESHOLD_KEY" /* INVALID_THRESHOLD_KEY */,
|
|
5772
|
+
`Invalid threshold public key: ${cause.message}`,
|
|
5773
|
+
cause
|
|
5790
5774
|
);
|
|
5791
5775
|
}
|
|
5792
|
-
/**
|
|
5793
|
-
* Create an error for encryption failure.
|
|
5794
|
-
*
|
|
5795
|
-
* @param cause - The underlying error
|
|
5796
|
-
*/
|
|
5797
5776
|
static encryptionFailed(cause) {
|
|
5798
|
-
return new
|
|
5777
|
+
return new _EncryptionError(
|
|
5799
5778
|
"ENCRYPTION_FAILED" /* ENCRYPTION_FAILED */,
|
|
5800
|
-
`
|
|
5779
|
+
`DKG encryption failed: ${cause.message}`,
|
|
5801
5780
|
cause
|
|
5802
5781
|
);
|
|
5803
5782
|
}
|
|
5804
|
-
/**
|
|
5805
|
-
* Create an error for Borsh deserialization failure.
|
|
5806
|
-
*
|
|
5807
|
-
* @param cause - The underlying error
|
|
5808
|
-
*/
|
|
5809
5783
|
static borshDeserializeFailed(cause) {
|
|
5810
|
-
return new
|
|
5784
|
+
return new _EncryptionError(
|
|
5811
5785
|
"BORSH_DESERIALIZE_FAILED" /* BORSH_DESERIALIZE_FAILED */,
|
|
5812
5786
|
`Borsh deserialization failed: ${cause.message}`,
|
|
5813
5787
|
cause
|
|
5814
5788
|
);
|
|
5815
5789
|
}
|
|
5816
|
-
/**
|
|
5817
|
-
* Create an error for invalid RexValue variant.
|
|
5818
|
-
*
|
|
5819
|
-
* @param variant - The invalid variant byte
|
|
5820
|
-
*/
|
|
5821
5790
|
static invalidRexValue(variant) {
|
|
5822
|
-
return new
|
|
5791
|
+
return new _EncryptionError(
|
|
5823
5792
|
"INVALID_REX_VALUE" /* INVALID_REX_VALUE */,
|
|
5824
5793
|
`Invalid RexValue variant: ${variant}`
|
|
5825
5794
|
);
|
|
@@ -5827,15 +5796,12 @@ var HpkeError = class _HpkeError extends Error {
|
|
|
5827
5796
|
};
|
|
5828
5797
|
|
|
5829
5798
|
// src/rex/constants.ts
|
|
5830
|
-
var
|
|
5831
|
-
var
|
|
5832
|
-
|
|
5833
|
-
);
|
|
5834
|
-
var X25519_PUBLIC_KEY_LENGTH = 32;
|
|
5799
|
+
var DKG_PAYLOAD_VERSION = 2;
|
|
5800
|
+
var MAX_SECRET_LENGTH = 64 * 1024;
|
|
5801
|
+
var RISTRETTO_POINT_BYTES = 32;
|
|
5835
5802
|
var ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
5836
|
-
var
|
|
5803
|
+
var CHACHA20_POLY1305_NONCE_LENGTH = 12;
|
|
5837
5804
|
var CHACHA20_POLY1305_TAG_LENGTH = 16;
|
|
5838
|
-
var HPKE_OVERHEAD_LENGTH = HPKE_ENC_LENGTH + CHACHA20_POLY1305_TAG_LENGTH;
|
|
5839
5805
|
|
|
5840
5806
|
// src/rex/rex-value.ts
|
|
5841
5807
|
var RexValueVariant = /* @__PURE__ */ ((RexValueVariant2) => {
|
|
@@ -5872,9 +5838,9 @@ var RexValue = class _RexValue {
|
|
|
5872
5838
|
);
|
|
5873
5839
|
}
|
|
5874
5840
|
/**
|
|
5875
|
-
* Create an encrypted RexValue from
|
|
5841
|
+
* Create an encrypted RexValue from a DKG threshold-encrypted payload.
|
|
5876
5842
|
*
|
|
5877
|
-
* @param ciphertext - The
|
|
5843
|
+
* @param ciphertext - The DKG-encrypted payload bytes (`[0x02] || borsh(DkgEncryptedPayload)`)
|
|
5878
5844
|
* @returns A new RexValue with Encrypted variant
|
|
5879
5845
|
*/
|
|
5880
5846
|
static encrypted(ciphertext) {
|
|
@@ -5942,22 +5908,22 @@ var RexValue = class _RexValue {
|
|
|
5942
5908
|
*
|
|
5943
5909
|
* @param data - The Borsh-serialized bytes
|
|
5944
5910
|
* @returns A new RexValue
|
|
5945
|
-
* @throws {
|
|
5911
|
+
* @throws {EncryptionError} If deserialization fails
|
|
5946
5912
|
*/
|
|
5947
5913
|
static fromBorsh(data) {
|
|
5948
5914
|
if (data.length < 5) {
|
|
5949
|
-
throw
|
|
5915
|
+
throw EncryptionError.borshDeserializeFailed(
|
|
5950
5916
|
new Error(`Buffer too short: expected at least 5 bytes, got ${data.length}`)
|
|
5951
5917
|
);
|
|
5952
5918
|
}
|
|
5953
5919
|
const variant = data[0];
|
|
5954
5920
|
if (variant !== 0 /* Plain */ && variant !== 1 /* Encrypted */) {
|
|
5955
|
-
throw
|
|
5921
|
+
throw EncryptionError.invalidRexValue(variant);
|
|
5956
5922
|
}
|
|
5957
5923
|
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
5958
5924
|
const length = dataView.getUint32(1, true);
|
|
5959
5925
|
if (data.length < 5 + length) {
|
|
5960
|
-
throw
|
|
5926
|
+
throw EncryptionError.borshDeserializeFailed(
|
|
5961
5927
|
new Error(`Buffer too short: expected ${5 + length} bytes, got ${data.length}`)
|
|
5962
5928
|
);
|
|
5963
5929
|
}
|
|
@@ -5965,78 +5931,1408 @@ var RexValue = class _RexValue {
|
|
|
5965
5931
|
return new _RexValue(variant, payload);
|
|
5966
5932
|
}
|
|
5967
5933
|
};
|
|
5968
|
-
|
|
5969
|
-
|
|
5970
|
-
|
|
5971
|
-
|
|
5972
|
-
}
|
|
5973
|
-
function
|
|
5974
|
-
|
|
5975
|
-
|
|
5976
|
-
|
|
5934
|
+
|
|
5935
|
+
// node_modules/@noble/curves/node_modules/@noble/hashes/utils.js
|
|
5936
|
+
function isBytes5(a) {
|
|
5937
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
5938
|
+
}
|
|
5939
|
+
function anumber4(n, title = "") {
|
|
5940
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
5941
|
+
const prefix = title && `"${title}" `;
|
|
5942
|
+
throw new Error(`${prefix}expected integer >= 0, got ${n}`);
|
|
5943
|
+
}
|
|
5944
|
+
}
|
|
5945
|
+
function abytes5(value, length, title = "") {
|
|
5946
|
+
const bytes2 = isBytes5(value);
|
|
5947
|
+
const len = value?.length;
|
|
5948
|
+
const needsLen = length !== void 0;
|
|
5949
|
+
if (!bytes2 || needsLen && len !== length) {
|
|
5950
|
+
const prefix = title && `"${title}" `;
|
|
5951
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
5952
|
+
const got = bytes2 ? `length=${len}` : `type=${typeof value}`;
|
|
5953
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
5954
|
+
}
|
|
5955
|
+
return value;
|
|
5956
|
+
}
|
|
5957
|
+
var hasHexBuiltin2 = /* @__PURE__ */ (() => (
|
|
5958
|
+
// @ts-ignore
|
|
5959
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
5960
|
+
))();
|
|
5961
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
5962
|
+
function bytesToHex2(bytes2) {
|
|
5963
|
+
abytes5(bytes2);
|
|
5964
|
+
if (hasHexBuiltin2)
|
|
5965
|
+
return bytes2.toHex();
|
|
5966
|
+
let hex2 = "";
|
|
5967
|
+
for (let i = 0; i < bytes2.length; i++) {
|
|
5968
|
+
hex2 += hexes[bytes2[i]];
|
|
5969
|
+
}
|
|
5970
|
+
return hex2;
|
|
5971
|
+
}
|
|
5972
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
5973
|
+
function asciiToBase16(ch) {
|
|
5974
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
5975
|
+
return ch - asciis._0;
|
|
5976
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
5977
|
+
return ch - (asciis.A - 10);
|
|
5978
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
5979
|
+
return ch - (asciis.a - 10);
|
|
5980
|
+
return;
|
|
5981
|
+
}
|
|
5982
|
+
function hexToBytes2(hex2) {
|
|
5983
|
+
if (typeof hex2 !== "string")
|
|
5984
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
5985
|
+
if (hasHexBuiltin2)
|
|
5986
|
+
return Uint8Array.fromHex(hex2);
|
|
5987
|
+
const hl = hex2.length;
|
|
5988
|
+
const al = hl / 2;
|
|
5989
|
+
if (hl % 2)
|
|
5990
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
5991
|
+
const array = new Uint8Array(al);
|
|
5992
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
5993
|
+
const n1 = asciiToBase16(hex2.charCodeAt(hi));
|
|
5994
|
+
const n2 = asciiToBase16(hex2.charCodeAt(hi + 1));
|
|
5995
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
5996
|
+
const char = hex2[hi] + hex2[hi + 1];
|
|
5997
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
5998
|
+
}
|
|
5999
|
+
array[ai] = n1 * 16 + n2;
|
|
6000
|
+
}
|
|
6001
|
+
return array;
|
|
6002
|
+
}
|
|
6003
|
+
|
|
6004
|
+
// node_modules/@noble/curves/utils.js
|
|
6005
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
6006
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
6007
|
+
function abool(value, title = "") {
|
|
6008
|
+
if (typeof value !== "boolean") {
|
|
6009
|
+
const prefix = title && `"${title}" `;
|
|
6010
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
6011
|
+
}
|
|
6012
|
+
return value;
|
|
6013
|
+
}
|
|
6014
|
+
function abignumber(n) {
|
|
6015
|
+
if (typeof n === "bigint") {
|
|
6016
|
+
if (!isPosBig(n))
|
|
6017
|
+
throw new Error("positive bigint expected, got " + n);
|
|
6018
|
+
} else
|
|
6019
|
+
anumber4(n);
|
|
6020
|
+
return n;
|
|
6021
|
+
}
|
|
6022
|
+
function hexToNumber(hex2) {
|
|
6023
|
+
if (typeof hex2 !== "string")
|
|
6024
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
6025
|
+
return hex2 === "" ? _0n : BigInt("0x" + hex2);
|
|
6026
|
+
}
|
|
6027
|
+
function bytesToNumberBE(bytes2) {
|
|
6028
|
+
return hexToNumber(bytesToHex2(bytes2));
|
|
6029
|
+
}
|
|
6030
|
+
function bytesToNumberLE(bytes2) {
|
|
6031
|
+
return hexToNumber(bytesToHex2(copyBytes(abytes5(bytes2)).reverse()));
|
|
6032
|
+
}
|
|
6033
|
+
function numberToBytesBE(n, len) {
|
|
6034
|
+
anumber4(len);
|
|
6035
|
+
n = abignumber(n);
|
|
6036
|
+
const res = hexToBytes2(n.toString(16).padStart(len * 2, "0"));
|
|
6037
|
+
if (res.length !== len)
|
|
6038
|
+
throw new Error("number too large");
|
|
6039
|
+
return res;
|
|
6040
|
+
}
|
|
6041
|
+
function numberToBytesLE(n, len) {
|
|
6042
|
+
return numberToBytesBE(n, len).reverse();
|
|
6043
|
+
}
|
|
6044
|
+
function equalBytes(a, b) {
|
|
6045
|
+
if (a.length !== b.length)
|
|
6046
|
+
return false;
|
|
6047
|
+
let diff = 0;
|
|
6048
|
+
for (let i = 0; i < a.length; i++)
|
|
6049
|
+
diff |= a[i] ^ b[i];
|
|
6050
|
+
return diff === 0;
|
|
6051
|
+
}
|
|
6052
|
+
function copyBytes(bytes2) {
|
|
6053
|
+
return Uint8Array.from(bytes2);
|
|
6054
|
+
}
|
|
6055
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
6056
|
+
function inRange(n, min, max) {
|
|
6057
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
6058
|
+
}
|
|
6059
|
+
function aInRange(title, n, min, max) {
|
|
6060
|
+
if (!inRange(n, min, max))
|
|
6061
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
6062
|
+
}
|
|
6063
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
6064
|
+
function validateObject(object, fields = {}, optFields = {}) {
|
|
6065
|
+
if (!object || typeof object !== "object")
|
|
6066
|
+
throw new Error("expected valid options object");
|
|
6067
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
6068
|
+
const val = object[fieldName];
|
|
6069
|
+
if (isOpt && val === void 0)
|
|
6070
|
+
return;
|
|
6071
|
+
const current = typeof val;
|
|
6072
|
+
if (current !== expectedType || val === null)
|
|
6073
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
6074
|
+
}
|
|
6075
|
+
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
6076
|
+
iter(fields, false);
|
|
6077
|
+
iter(optFields, true);
|
|
6078
|
+
}
|
|
6079
|
+
var notImplemented = () => {
|
|
6080
|
+
throw new Error("not implemented");
|
|
6081
|
+
};
|
|
6082
|
+
function memoized(fn) {
|
|
6083
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
6084
|
+
return (arg, ...args) => {
|
|
6085
|
+
const val = map.get(arg);
|
|
6086
|
+
if (val !== void 0)
|
|
6087
|
+
return val;
|
|
6088
|
+
const computed = fn(arg, ...args);
|
|
6089
|
+
map.set(arg, computed);
|
|
6090
|
+
return computed;
|
|
6091
|
+
};
|
|
6092
|
+
}
|
|
6093
|
+
|
|
6094
|
+
// node_modules/@noble/curves/abstract/modular.js
|
|
6095
|
+
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
6096
|
+
var _1n2 = /* @__PURE__ */ BigInt(1);
|
|
6097
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
6098
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
6099
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
6100
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
6101
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
6102
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
6103
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
6104
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
6105
|
+
function mod(a, b) {
|
|
6106
|
+
const result = a % b;
|
|
6107
|
+
return result >= _0n2 ? result : b + result;
|
|
6108
|
+
}
|
|
6109
|
+
function pow22(x, power, modulo) {
|
|
6110
|
+
let res = x;
|
|
6111
|
+
while (power-- > _0n2) {
|
|
6112
|
+
res *= res;
|
|
6113
|
+
res %= modulo;
|
|
6114
|
+
}
|
|
6115
|
+
return res;
|
|
6116
|
+
}
|
|
6117
|
+
function invert2(number, modulo) {
|
|
6118
|
+
if (number === _0n2)
|
|
6119
|
+
throw new Error("invert: expected non-zero number");
|
|
6120
|
+
if (modulo <= _0n2)
|
|
6121
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
6122
|
+
let a = mod(number, modulo);
|
|
6123
|
+
let b = modulo;
|
|
6124
|
+
let x = _0n2, u = _1n2;
|
|
6125
|
+
while (a !== _0n2) {
|
|
6126
|
+
const q = b / a;
|
|
6127
|
+
const r = b % a;
|
|
6128
|
+
const m = x - u * q;
|
|
6129
|
+
b = a, a = r, x = u, u = m;
|
|
6130
|
+
}
|
|
6131
|
+
const gcd2 = b;
|
|
6132
|
+
if (gcd2 !== _1n2)
|
|
6133
|
+
throw new Error("invert: does not exist");
|
|
6134
|
+
return mod(x, modulo);
|
|
6135
|
+
}
|
|
6136
|
+
function assertIsSquare(Fp2, root, n) {
|
|
6137
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
6138
|
+
throw new Error("Cannot find square root");
|
|
6139
|
+
}
|
|
6140
|
+
function sqrt3mod4(Fp2, n) {
|
|
6141
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
6142
|
+
const root = Fp2.pow(n, p1div4);
|
|
6143
|
+
assertIsSquare(Fp2, root, n);
|
|
6144
|
+
return root;
|
|
6145
|
+
}
|
|
6146
|
+
function sqrt5mod8(Fp2, n) {
|
|
6147
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
6148
|
+
const n2 = Fp2.mul(n, _2n);
|
|
6149
|
+
const v = Fp2.pow(n2, p5div8);
|
|
6150
|
+
const nv = Fp2.mul(n, v);
|
|
6151
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
6152
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
6153
|
+
assertIsSquare(Fp2, root, n);
|
|
6154
|
+
return root;
|
|
6155
|
+
}
|
|
6156
|
+
function sqrt9mod16(P2) {
|
|
6157
|
+
const Fp_ = Field(P2);
|
|
6158
|
+
const tn = tonelliShanks(P2);
|
|
6159
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
6160
|
+
const c2 = tn(Fp_, c1);
|
|
6161
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
6162
|
+
const c4 = (P2 + _7n) / _16n;
|
|
6163
|
+
return (Fp2, n) => {
|
|
6164
|
+
let tv1 = Fp2.pow(n, c4);
|
|
6165
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
6166
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
6167
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
6168
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
6169
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
6170
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
6171
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
6172
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
6173
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
6174
|
+
assertIsSquare(Fp2, root, n);
|
|
6175
|
+
return root;
|
|
6176
|
+
};
|
|
6177
|
+
}
|
|
6178
|
+
function tonelliShanks(P2) {
|
|
6179
|
+
if (P2 < _3n)
|
|
6180
|
+
throw new Error("sqrt is not defined for small field");
|
|
6181
|
+
let Q = P2 - _1n2;
|
|
6182
|
+
let S = 0;
|
|
6183
|
+
while (Q % _2n === _0n2) {
|
|
6184
|
+
Q /= _2n;
|
|
6185
|
+
S++;
|
|
6186
|
+
}
|
|
6187
|
+
let Z = _2n;
|
|
6188
|
+
const _Fp = Field(P2);
|
|
6189
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
6190
|
+
if (Z++ > 1e3)
|
|
6191
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
6192
|
+
}
|
|
6193
|
+
if (S === 1)
|
|
6194
|
+
return sqrt3mod4;
|
|
6195
|
+
let cc = _Fp.pow(Z, Q);
|
|
6196
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
6197
|
+
return function tonelliSlow(Fp2, n) {
|
|
6198
|
+
if (Fp2.is0(n))
|
|
6199
|
+
return n;
|
|
6200
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
6201
|
+
throw new Error("Cannot find square root");
|
|
6202
|
+
let M2 = S;
|
|
6203
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
6204
|
+
let t = Fp2.pow(n, Q);
|
|
6205
|
+
let R = Fp2.pow(n, Q1div2);
|
|
6206
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
6207
|
+
if (Fp2.is0(t))
|
|
6208
|
+
return Fp2.ZERO;
|
|
6209
|
+
let i = 1;
|
|
6210
|
+
let t_tmp = Fp2.sqr(t);
|
|
6211
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
6212
|
+
i++;
|
|
6213
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
6214
|
+
if (i === M2)
|
|
6215
|
+
throw new Error("Cannot find square root");
|
|
6216
|
+
}
|
|
6217
|
+
const exponent = _1n2 << BigInt(M2 - i - 1);
|
|
6218
|
+
const b = Fp2.pow(c, exponent);
|
|
6219
|
+
M2 = i;
|
|
6220
|
+
c = Fp2.sqr(b);
|
|
6221
|
+
t = Fp2.mul(t, c);
|
|
6222
|
+
R = Fp2.mul(R, b);
|
|
6223
|
+
}
|
|
6224
|
+
return R;
|
|
6225
|
+
};
|
|
6226
|
+
}
|
|
6227
|
+
function FpSqrt(P2) {
|
|
6228
|
+
if (P2 % _4n === _3n)
|
|
6229
|
+
return sqrt3mod4;
|
|
6230
|
+
if (P2 % _8n === _5n)
|
|
6231
|
+
return sqrt5mod8;
|
|
6232
|
+
if (P2 % _16n === _9n)
|
|
6233
|
+
return sqrt9mod16(P2);
|
|
6234
|
+
return tonelliShanks(P2);
|
|
6235
|
+
}
|
|
6236
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
6237
|
+
var FIELD_FIELDS = [
|
|
6238
|
+
"create",
|
|
6239
|
+
"isValid",
|
|
6240
|
+
"is0",
|
|
6241
|
+
"neg",
|
|
6242
|
+
"inv",
|
|
6243
|
+
"sqrt",
|
|
6244
|
+
"sqr",
|
|
6245
|
+
"eql",
|
|
6246
|
+
"add",
|
|
6247
|
+
"sub",
|
|
6248
|
+
"mul",
|
|
6249
|
+
"pow",
|
|
6250
|
+
"div",
|
|
6251
|
+
"addN",
|
|
6252
|
+
"subN",
|
|
6253
|
+
"mulN",
|
|
6254
|
+
"sqrN"
|
|
6255
|
+
];
|
|
6256
|
+
function validateField(field2) {
|
|
6257
|
+
const initial = {
|
|
6258
|
+
ORDER: "bigint",
|
|
6259
|
+
BYTES: "number",
|
|
6260
|
+
BITS: "number"
|
|
6261
|
+
};
|
|
6262
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
6263
|
+
map[val] = "function";
|
|
6264
|
+
return map;
|
|
6265
|
+
}, initial);
|
|
6266
|
+
validateObject(field2, opts);
|
|
6267
|
+
return field2;
|
|
6268
|
+
}
|
|
6269
|
+
function FpPow(Fp2, num, power) {
|
|
6270
|
+
if (power < _0n2)
|
|
6271
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
6272
|
+
if (power === _0n2)
|
|
6273
|
+
return Fp2.ONE;
|
|
6274
|
+
if (power === _1n2)
|
|
6275
|
+
return num;
|
|
6276
|
+
let p = Fp2.ONE;
|
|
6277
|
+
let d = num;
|
|
6278
|
+
while (power > _0n2) {
|
|
6279
|
+
if (power & _1n2)
|
|
6280
|
+
p = Fp2.mul(p, d);
|
|
6281
|
+
d = Fp2.sqr(d);
|
|
6282
|
+
power >>= _1n2;
|
|
6283
|
+
}
|
|
6284
|
+
return p;
|
|
6285
|
+
}
|
|
6286
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
6287
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
6288
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
6289
|
+
if (Fp2.is0(num))
|
|
6290
|
+
return acc;
|
|
6291
|
+
inverted[i] = acc;
|
|
6292
|
+
return Fp2.mul(acc, num);
|
|
6293
|
+
}, Fp2.ONE);
|
|
6294
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
6295
|
+
nums.reduceRight((acc, num, i) => {
|
|
6296
|
+
if (Fp2.is0(num))
|
|
6297
|
+
return acc;
|
|
6298
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
6299
|
+
return Fp2.mul(acc, num);
|
|
6300
|
+
}, invertedAcc);
|
|
6301
|
+
return inverted;
|
|
6302
|
+
}
|
|
6303
|
+
function FpLegendre(Fp2, n) {
|
|
6304
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
6305
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
6306
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
6307
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
6308
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
6309
|
+
if (!yes && !zero && !no)
|
|
6310
|
+
throw new Error("invalid Legendre symbol result");
|
|
6311
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
6312
|
+
}
|
|
6313
|
+
function nLength(n, nBitLength) {
|
|
6314
|
+
if (nBitLength !== void 0)
|
|
6315
|
+
anumber4(nBitLength);
|
|
6316
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
6317
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
6318
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
6319
|
+
}
|
|
6320
|
+
var _Field = class {
|
|
6321
|
+
ORDER;
|
|
6322
|
+
BITS;
|
|
6323
|
+
BYTES;
|
|
6324
|
+
isLE;
|
|
6325
|
+
ZERO = _0n2;
|
|
6326
|
+
ONE = _1n2;
|
|
6327
|
+
_lengths;
|
|
6328
|
+
_sqrt;
|
|
6329
|
+
// cached sqrt
|
|
6330
|
+
_mod;
|
|
6331
|
+
constructor(ORDER, opts = {}) {
|
|
6332
|
+
if (ORDER <= _0n2)
|
|
6333
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
6334
|
+
let _nbitLength = void 0;
|
|
6335
|
+
this.isLE = false;
|
|
6336
|
+
if (opts != null && typeof opts === "object") {
|
|
6337
|
+
if (typeof opts.BITS === "number")
|
|
6338
|
+
_nbitLength = opts.BITS;
|
|
6339
|
+
if (typeof opts.sqrt === "function")
|
|
6340
|
+
this.sqrt = opts.sqrt;
|
|
6341
|
+
if (typeof opts.isLE === "boolean")
|
|
6342
|
+
this.isLE = opts.isLE;
|
|
6343
|
+
if (opts.allowedLengths)
|
|
6344
|
+
this._lengths = opts.allowedLengths?.slice();
|
|
6345
|
+
if (typeof opts.modFromBytes === "boolean")
|
|
6346
|
+
this._mod = opts.modFromBytes;
|
|
6347
|
+
}
|
|
6348
|
+
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
6349
|
+
if (nByteLength > 2048)
|
|
6350
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
6351
|
+
this.ORDER = ORDER;
|
|
6352
|
+
this.BITS = nBitLength;
|
|
6353
|
+
this.BYTES = nByteLength;
|
|
6354
|
+
this._sqrt = void 0;
|
|
6355
|
+
Object.preventExtensions(this);
|
|
6356
|
+
}
|
|
6357
|
+
create(num) {
|
|
6358
|
+
return mod(num, this.ORDER);
|
|
6359
|
+
}
|
|
6360
|
+
isValid(num) {
|
|
6361
|
+
if (typeof num !== "bigint")
|
|
6362
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
6363
|
+
return _0n2 <= num && num < this.ORDER;
|
|
6364
|
+
}
|
|
6365
|
+
is0(num) {
|
|
6366
|
+
return num === _0n2;
|
|
6367
|
+
}
|
|
6368
|
+
// is valid and invertible
|
|
6369
|
+
isValidNot0(num) {
|
|
6370
|
+
return !this.is0(num) && this.isValid(num);
|
|
6371
|
+
}
|
|
6372
|
+
isOdd(num) {
|
|
6373
|
+
return (num & _1n2) === _1n2;
|
|
6374
|
+
}
|
|
6375
|
+
neg(num) {
|
|
6376
|
+
return mod(-num, this.ORDER);
|
|
6377
|
+
}
|
|
6378
|
+
eql(lhs, rhs) {
|
|
6379
|
+
return lhs === rhs;
|
|
6380
|
+
}
|
|
6381
|
+
sqr(num) {
|
|
6382
|
+
return mod(num * num, this.ORDER);
|
|
6383
|
+
}
|
|
6384
|
+
add(lhs, rhs) {
|
|
6385
|
+
return mod(lhs + rhs, this.ORDER);
|
|
6386
|
+
}
|
|
6387
|
+
sub(lhs, rhs) {
|
|
6388
|
+
return mod(lhs - rhs, this.ORDER);
|
|
6389
|
+
}
|
|
6390
|
+
mul(lhs, rhs) {
|
|
6391
|
+
return mod(lhs * rhs, this.ORDER);
|
|
6392
|
+
}
|
|
6393
|
+
pow(num, power) {
|
|
6394
|
+
return FpPow(this, num, power);
|
|
6395
|
+
}
|
|
6396
|
+
div(lhs, rhs) {
|
|
6397
|
+
return mod(lhs * invert2(rhs, this.ORDER), this.ORDER);
|
|
6398
|
+
}
|
|
6399
|
+
// Same as above, but doesn't normalize
|
|
6400
|
+
sqrN(num) {
|
|
6401
|
+
return num * num;
|
|
6402
|
+
}
|
|
6403
|
+
addN(lhs, rhs) {
|
|
6404
|
+
return lhs + rhs;
|
|
6405
|
+
}
|
|
6406
|
+
subN(lhs, rhs) {
|
|
6407
|
+
return lhs - rhs;
|
|
6408
|
+
}
|
|
6409
|
+
mulN(lhs, rhs) {
|
|
6410
|
+
return lhs * rhs;
|
|
6411
|
+
}
|
|
6412
|
+
inv(num) {
|
|
6413
|
+
return invert2(num, this.ORDER);
|
|
6414
|
+
}
|
|
6415
|
+
sqrt(num) {
|
|
6416
|
+
if (!this._sqrt)
|
|
6417
|
+
this._sqrt = FpSqrt(this.ORDER);
|
|
6418
|
+
return this._sqrt(this, num);
|
|
6419
|
+
}
|
|
6420
|
+
toBytes(num) {
|
|
6421
|
+
return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
|
|
6422
|
+
}
|
|
6423
|
+
fromBytes(bytes2, skipValidation = false) {
|
|
6424
|
+
abytes5(bytes2);
|
|
6425
|
+
const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
6426
|
+
if (allowedLengths) {
|
|
6427
|
+
if (!allowedLengths.includes(bytes2.length) || bytes2.length > BYTES) {
|
|
6428
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes2.length);
|
|
6429
|
+
}
|
|
6430
|
+
const padded = new Uint8Array(BYTES);
|
|
6431
|
+
padded.set(bytes2, isLE ? 0 : padded.length - bytes2.length);
|
|
6432
|
+
bytes2 = padded;
|
|
6433
|
+
}
|
|
6434
|
+
if (bytes2.length !== BYTES)
|
|
6435
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes2.length);
|
|
6436
|
+
let scalar = isLE ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
|
|
6437
|
+
if (modFromBytes)
|
|
6438
|
+
scalar = mod(scalar, ORDER);
|
|
6439
|
+
if (!skipValidation) {
|
|
6440
|
+
if (!this.isValid(scalar))
|
|
6441
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
6442
|
+
}
|
|
6443
|
+
return scalar;
|
|
6444
|
+
}
|
|
6445
|
+
// TODO: we don't need it here, move out to separate fn
|
|
6446
|
+
invertBatch(lst) {
|
|
6447
|
+
return FpInvertBatch(this, lst);
|
|
6448
|
+
}
|
|
6449
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
6450
|
+
// and it's unclear what to return in there.
|
|
6451
|
+
cmov(a, b, condition) {
|
|
6452
|
+
return condition ? b : a;
|
|
6453
|
+
}
|
|
6454
|
+
};
|
|
6455
|
+
function Field(ORDER, opts = {}) {
|
|
6456
|
+
return new _Field(ORDER, opts);
|
|
6457
|
+
}
|
|
6458
|
+
|
|
6459
|
+
// node_modules/@noble/curves/abstract/curve.js
|
|
6460
|
+
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
6461
|
+
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
6462
|
+
function negateCt(condition, item) {
|
|
6463
|
+
const neg = item.negate();
|
|
6464
|
+
return condition ? neg : item;
|
|
6465
|
+
}
|
|
6466
|
+
function normalizeZ(c, points) {
|
|
6467
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
6468
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
6469
|
+
}
|
|
6470
|
+
function validateW(W2, bits) {
|
|
6471
|
+
if (!Number.isSafeInteger(W2) || W2 <= 0 || W2 > bits)
|
|
6472
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W2);
|
|
6473
|
+
}
|
|
6474
|
+
function calcWOpts(W2, scalarBits2) {
|
|
6475
|
+
validateW(W2, scalarBits2);
|
|
6476
|
+
const windows = Math.ceil(scalarBits2 / W2) + 1;
|
|
6477
|
+
const windowSize = 2 ** (W2 - 1);
|
|
6478
|
+
const maxNumber = 2 ** W2;
|
|
6479
|
+
const mask = bitMask(W2);
|
|
6480
|
+
const shiftBy = BigInt(W2);
|
|
6481
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
6482
|
+
}
|
|
6483
|
+
function calcOffsets(n, window, wOpts) {
|
|
6484
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
6485
|
+
let wbits = Number(n & mask);
|
|
6486
|
+
let nextN = n >> shiftBy;
|
|
6487
|
+
if (wbits > windowSize) {
|
|
6488
|
+
wbits -= maxNumber;
|
|
6489
|
+
nextN += _1n3;
|
|
6490
|
+
}
|
|
6491
|
+
const offsetStart = window * windowSize;
|
|
6492
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
6493
|
+
const isZero = wbits === 0;
|
|
6494
|
+
const isNeg = wbits < 0;
|
|
6495
|
+
const isNegF = window % 2 !== 0;
|
|
6496
|
+
const offsetF = offsetStart;
|
|
6497
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
6498
|
+
}
|
|
6499
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
6500
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
6501
|
+
function getW(P2) {
|
|
6502
|
+
return pointWindowSizes.get(P2) || 1;
|
|
6503
|
+
}
|
|
6504
|
+
function assert0(n) {
|
|
6505
|
+
if (n !== _0n3)
|
|
6506
|
+
throw new Error("invalid wNAF");
|
|
6507
|
+
}
|
|
6508
|
+
var wNAF2 = class {
|
|
6509
|
+
BASE;
|
|
6510
|
+
ZERO;
|
|
6511
|
+
Fn;
|
|
6512
|
+
bits;
|
|
6513
|
+
// Parametrized with a given Point class (not individual point)
|
|
6514
|
+
constructor(Point2, bits) {
|
|
6515
|
+
this.BASE = Point2.BASE;
|
|
6516
|
+
this.ZERO = Point2.ZERO;
|
|
6517
|
+
this.Fn = Point2.Fn;
|
|
6518
|
+
this.bits = bits;
|
|
6519
|
+
}
|
|
6520
|
+
// non-const time multiplication ladder
|
|
6521
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
6522
|
+
let d = elm;
|
|
6523
|
+
while (n > _0n3) {
|
|
6524
|
+
if (n & _1n3)
|
|
6525
|
+
p = p.add(d);
|
|
6526
|
+
d = d.double();
|
|
6527
|
+
n >>= _1n3;
|
|
6528
|
+
}
|
|
6529
|
+
return p;
|
|
6530
|
+
}
|
|
6531
|
+
/**
|
|
6532
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
6533
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
6534
|
+
* Number of precomputed points depends on the curve size:
|
|
6535
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
6536
|
+
* - 𝑊 is the window size
|
|
6537
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
6538
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
6539
|
+
* @param point Point instance
|
|
6540
|
+
* @param W window size
|
|
6541
|
+
* @returns precomputed point tables flattened to a single array
|
|
6542
|
+
*/
|
|
6543
|
+
precomputeWindow(point, W2) {
|
|
6544
|
+
const { windows, windowSize } = calcWOpts(W2, this.bits);
|
|
6545
|
+
const points = [];
|
|
6546
|
+
let p = point;
|
|
6547
|
+
let base = p;
|
|
6548
|
+
for (let window = 0; window < windows; window++) {
|
|
6549
|
+
base = p;
|
|
6550
|
+
points.push(base);
|
|
6551
|
+
for (let i = 1; i < windowSize; i++) {
|
|
6552
|
+
base = base.add(p);
|
|
6553
|
+
points.push(base);
|
|
6554
|
+
}
|
|
6555
|
+
p = base.double();
|
|
6556
|
+
}
|
|
6557
|
+
return points;
|
|
6558
|
+
}
|
|
6559
|
+
/**
|
|
6560
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
6561
|
+
* More compact implementation:
|
|
6562
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
6563
|
+
* @returns real and fake (for const-time) points
|
|
6564
|
+
*/
|
|
6565
|
+
wNAF(W2, precomputes, n) {
|
|
6566
|
+
if (!this.Fn.isValid(n))
|
|
6567
|
+
throw new Error("invalid scalar");
|
|
6568
|
+
let p = this.ZERO;
|
|
6569
|
+
let f = this.BASE;
|
|
6570
|
+
const wo = calcWOpts(W2, this.bits);
|
|
6571
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
6572
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
6573
|
+
n = nextN;
|
|
6574
|
+
if (isZero) {
|
|
6575
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
6576
|
+
} else {
|
|
6577
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
6578
|
+
}
|
|
6579
|
+
}
|
|
6580
|
+
assert0(n);
|
|
6581
|
+
return { p, f };
|
|
6582
|
+
}
|
|
6583
|
+
/**
|
|
6584
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
6585
|
+
* @param acc accumulator point to add result of multiplication
|
|
6586
|
+
* @returns point
|
|
6587
|
+
*/
|
|
6588
|
+
wNAFUnsafe(W2, precomputes, n, acc = this.ZERO) {
|
|
6589
|
+
const wo = calcWOpts(W2, this.bits);
|
|
6590
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
6591
|
+
if (n === _0n3)
|
|
6592
|
+
break;
|
|
6593
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
6594
|
+
n = nextN;
|
|
6595
|
+
if (isZero) {
|
|
6596
|
+
continue;
|
|
6597
|
+
} else {
|
|
6598
|
+
const item = precomputes[offset];
|
|
6599
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
6600
|
+
}
|
|
6601
|
+
}
|
|
6602
|
+
assert0(n);
|
|
6603
|
+
return acc;
|
|
6604
|
+
}
|
|
6605
|
+
getPrecomputes(W2, point, transform) {
|
|
6606
|
+
let comp = pointPrecomputes.get(point);
|
|
6607
|
+
if (!comp) {
|
|
6608
|
+
comp = this.precomputeWindow(point, W2);
|
|
6609
|
+
if (W2 !== 1) {
|
|
6610
|
+
if (typeof transform === "function")
|
|
6611
|
+
comp = transform(comp);
|
|
6612
|
+
pointPrecomputes.set(point, comp);
|
|
6613
|
+
}
|
|
6614
|
+
}
|
|
6615
|
+
return comp;
|
|
6616
|
+
}
|
|
6617
|
+
cached(point, scalar, transform) {
|
|
6618
|
+
const W2 = getW(point);
|
|
6619
|
+
return this.wNAF(W2, this.getPrecomputes(W2, point, transform), scalar);
|
|
6620
|
+
}
|
|
6621
|
+
unsafe(point, scalar, transform, prev) {
|
|
6622
|
+
const W2 = getW(point);
|
|
6623
|
+
if (W2 === 1)
|
|
6624
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
6625
|
+
return this.wNAFUnsafe(W2, this.getPrecomputes(W2, point, transform), scalar, prev);
|
|
6626
|
+
}
|
|
6627
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
6628
|
+
// using windowed method. This specifies window size and
|
|
6629
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
6630
|
+
createCache(P2, W2) {
|
|
6631
|
+
validateW(W2, this.bits);
|
|
6632
|
+
pointWindowSizes.set(P2, W2);
|
|
6633
|
+
pointPrecomputes.delete(P2);
|
|
6634
|
+
}
|
|
6635
|
+
hasCache(elm) {
|
|
6636
|
+
return getW(elm) !== 1;
|
|
6637
|
+
}
|
|
6638
|
+
};
|
|
6639
|
+
function createField(order, field2, isLE) {
|
|
6640
|
+
if (field2) {
|
|
6641
|
+
if (field2.ORDER !== order)
|
|
6642
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
6643
|
+
validateField(field2);
|
|
6644
|
+
return field2;
|
|
6645
|
+
} else {
|
|
6646
|
+
return Field(order, { isLE });
|
|
6647
|
+
}
|
|
6648
|
+
}
|
|
6649
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
6650
|
+
if (FpFnLE === void 0)
|
|
6651
|
+
FpFnLE = type === "edwards";
|
|
6652
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
6653
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
6654
|
+
for (const p of ["p", "n", "h"]) {
|
|
6655
|
+
const val = CURVE[p];
|
|
6656
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
6657
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
6658
|
+
}
|
|
6659
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
6660
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
6661
|
+
const _b = "d";
|
|
6662
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
6663
|
+
for (const p of params) {
|
|
6664
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
6665
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
6666
|
+
}
|
|
6667
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
6668
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
6669
|
+
}
|
|
6670
|
+
|
|
6671
|
+
// node_modules/@noble/curves/abstract/edwards.js
|
|
6672
|
+
var _0n4 = BigInt(0);
|
|
6673
|
+
var _1n4 = BigInt(1);
|
|
6674
|
+
var _2n2 = BigInt(2);
|
|
6675
|
+
var _8n2 = BigInt(8);
|
|
6676
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
6677
|
+
const x2 = Fp2.sqr(x);
|
|
6678
|
+
const y2 = Fp2.sqr(y);
|
|
6679
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
6680
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
6681
|
+
return Fp2.eql(left, right);
|
|
6682
|
+
}
|
|
6683
|
+
function edwards(params, extraOpts = {}) {
|
|
6684
|
+
const validated = createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
6685
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
6686
|
+
let CURVE = validated.CURVE;
|
|
6687
|
+
const { h: cofactor } = CURVE;
|
|
6688
|
+
validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
6689
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
6690
|
+
const modP = (n) => Fp2.create(n);
|
|
6691
|
+
const uvRatio3 = extraOpts.uvRatio || ((u, v) => {
|
|
6692
|
+
try {
|
|
6693
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
6694
|
+
} catch (e) {
|
|
6695
|
+
return { isValid: false, value: _0n4 };
|
|
6696
|
+
}
|
|
6697
|
+
});
|
|
6698
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
6699
|
+
throw new Error("bad curve params: generator point");
|
|
6700
|
+
function acoord(title, n, banZero = false) {
|
|
6701
|
+
const min = banZero ? _1n4 : _0n4;
|
|
6702
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
6703
|
+
return n;
|
|
6704
|
+
}
|
|
6705
|
+
function aedpoint(other) {
|
|
6706
|
+
if (!(other instanceof Point2))
|
|
6707
|
+
throw new Error("EdwardsPoint expected");
|
|
6708
|
+
}
|
|
6709
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
6710
|
+
const { X, Y, Z } = p;
|
|
6711
|
+
const is0 = p.is0();
|
|
6712
|
+
if (iz == null)
|
|
6713
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
6714
|
+
const x = modP(X * iz);
|
|
6715
|
+
const y = modP(Y * iz);
|
|
6716
|
+
const zz = Fp2.mul(Z, iz);
|
|
6717
|
+
if (is0)
|
|
6718
|
+
return { x: _0n4, y: _1n4 };
|
|
6719
|
+
if (zz !== _1n4)
|
|
6720
|
+
throw new Error("invZ was invalid");
|
|
6721
|
+
return { x, y };
|
|
6722
|
+
});
|
|
6723
|
+
const assertValidMemo = memoized((p) => {
|
|
6724
|
+
const { a, d } = CURVE;
|
|
6725
|
+
if (p.is0())
|
|
6726
|
+
throw new Error("bad point: ZERO");
|
|
6727
|
+
const { X, Y, Z, T } = p;
|
|
6728
|
+
const X2 = modP(X * X);
|
|
6729
|
+
const Y2 = modP(Y * Y);
|
|
6730
|
+
const Z2 = modP(Z * Z);
|
|
6731
|
+
const Z4 = modP(Z2 * Z2);
|
|
6732
|
+
const aX2 = modP(X2 * a);
|
|
6733
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
6734
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
6735
|
+
if (left !== right)
|
|
6736
|
+
throw new Error("bad point: equation left != right (1)");
|
|
6737
|
+
const XY = modP(X * Y);
|
|
6738
|
+
const ZT = modP(Z * T);
|
|
6739
|
+
if (XY !== ZT)
|
|
6740
|
+
throw new Error("bad point: equation left != right (2)");
|
|
6741
|
+
return true;
|
|
6742
|
+
});
|
|
6743
|
+
class Point2 {
|
|
6744
|
+
// base / generator point
|
|
6745
|
+
static BASE = new Point2(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
6746
|
+
// zero / infinity / identity point
|
|
6747
|
+
static ZERO = new Point2(_0n4, _1n4, _1n4, _0n4);
|
|
6748
|
+
// 0, 1, 1, 0
|
|
6749
|
+
// math field
|
|
6750
|
+
static Fp = Fp2;
|
|
6751
|
+
// scalar field
|
|
6752
|
+
static Fn = Fn2;
|
|
6753
|
+
X;
|
|
6754
|
+
Y;
|
|
6755
|
+
Z;
|
|
6756
|
+
T;
|
|
6757
|
+
constructor(X, Y, Z, T) {
|
|
6758
|
+
this.X = acoord("x", X);
|
|
6759
|
+
this.Y = acoord("y", Y);
|
|
6760
|
+
this.Z = acoord("z", Z, true);
|
|
6761
|
+
this.T = acoord("t", T);
|
|
6762
|
+
Object.freeze(this);
|
|
6763
|
+
}
|
|
6764
|
+
static CURVE() {
|
|
6765
|
+
return CURVE;
|
|
6766
|
+
}
|
|
6767
|
+
static fromAffine(p) {
|
|
6768
|
+
if (p instanceof Point2)
|
|
6769
|
+
throw new Error("extended point not allowed");
|
|
6770
|
+
const { x, y } = p || {};
|
|
6771
|
+
acoord("x", x);
|
|
6772
|
+
acoord("y", y);
|
|
6773
|
+
return new Point2(x, y, _1n4, modP(x * y));
|
|
6774
|
+
}
|
|
6775
|
+
// Uses algo from RFC8032 5.1.3.
|
|
6776
|
+
static fromBytes(bytes2, zip215 = false) {
|
|
6777
|
+
const len = Fp2.BYTES;
|
|
6778
|
+
const { a, d } = CURVE;
|
|
6779
|
+
bytes2 = copyBytes(abytes5(bytes2, len, "point"));
|
|
6780
|
+
abool(zip215, "zip215");
|
|
6781
|
+
const normed = copyBytes(bytes2);
|
|
6782
|
+
const lastByte = bytes2[len - 1];
|
|
6783
|
+
normed[len - 1] = lastByte & -129;
|
|
6784
|
+
const y = bytesToNumberLE(normed);
|
|
6785
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
6786
|
+
aInRange("point.y", y, _0n4, max);
|
|
6787
|
+
const y2 = modP(y * y);
|
|
6788
|
+
const u = modP(y2 - _1n4);
|
|
6789
|
+
const v = modP(d * y2 - a);
|
|
6790
|
+
let { isValid, value: x } = uvRatio3(u, v);
|
|
6791
|
+
if (!isValid)
|
|
6792
|
+
throw new Error("bad point: invalid y coordinate");
|
|
6793
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
6794
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
6795
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
6796
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
6797
|
+
if (isLastByteOdd !== isXOdd)
|
|
6798
|
+
x = modP(-x);
|
|
6799
|
+
return Point2.fromAffine({ x, y });
|
|
6800
|
+
}
|
|
6801
|
+
static fromHex(hex2, zip215 = false) {
|
|
6802
|
+
return Point2.fromBytes(hexToBytes2(hex2), zip215);
|
|
6803
|
+
}
|
|
6804
|
+
get x() {
|
|
6805
|
+
return this.toAffine().x;
|
|
6806
|
+
}
|
|
6807
|
+
get y() {
|
|
6808
|
+
return this.toAffine().y;
|
|
6809
|
+
}
|
|
6810
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
6811
|
+
wnaf.createCache(this, windowSize);
|
|
6812
|
+
if (!isLazy)
|
|
6813
|
+
this.multiply(_2n2);
|
|
6814
|
+
return this;
|
|
6815
|
+
}
|
|
6816
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
6817
|
+
assertValidity() {
|
|
6818
|
+
assertValidMemo(this);
|
|
6819
|
+
}
|
|
6820
|
+
// Compare one point to another.
|
|
6821
|
+
equals(other) {
|
|
6822
|
+
aedpoint(other);
|
|
6823
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
6824
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
6825
|
+
const X1Z2 = modP(X1 * Z2);
|
|
6826
|
+
const X2Z1 = modP(X2 * Z1);
|
|
6827
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
6828
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
6829
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
6830
|
+
}
|
|
6831
|
+
is0() {
|
|
6832
|
+
return this.equals(Point2.ZERO);
|
|
6833
|
+
}
|
|
6834
|
+
negate() {
|
|
6835
|
+
return new Point2(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
6836
|
+
}
|
|
6837
|
+
// Fast algo for doubling Extended Point.
|
|
6838
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
6839
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
6840
|
+
double() {
|
|
6841
|
+
const { a } = CURVE;
|
|
6842
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
6843
|
+
const A = modP(X1 * X1);
|
|
6844
|
+
const B = modP(Y1 * Y1);
|
|
6845
|
+
const C2 = modP(_2n2 * modP(Z1 * Z1));
|
|
6846
|
+
const D = modP(a * A);
|
|
6847
|
+
const x1y1 = X1 + Y1;
|
|
6848
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
6849
|
+
const G2 = D + B;
|
|
6850
|
+
const F = G2 - C2;
|
|
6851
|
+
const H = D - B;
|
|
6852
|
+
const X3 = modP(E * F);
|
|
6853
|
+
const Y3 = modP(G2 * H);
|
|
6854
|
+
const T3 = modP(E * H);
|
|
6855
|
+
const Z3 = modP(F * G2);
|
|
6856
|
+
return new Point2(X3, Y3, Z3, T3);
|
|
6857
|
+
}
|
|
6858
|
+
// Fast algo for adding 2 Extended Points.
|
|
6859
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
6860
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
6861
|
+
add(other) {
|
|
6862
|
+
aedpoint(other);
|
|
6863
|
+
const { a, d } = CURVE;
|
|
6864
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
6865
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
6866
|
+
const A = modP(X1 * X2);
|
|
6867
|
+
const B = modP(Y1 * Y2);
|
|
6868
|
+
const C2 = modP(T1 * d * T2);
|
|
6869
|
+
const D = modP(Z1 * Z2);
|
|
6870
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
6871
|
+
const F = D - C2;
|
|
6872
|
+
const G2 = D + C2;
|
|
6873
|
+
const H = modP(B - a * A);
|
|
6874
|
+
const X3 = modP(E * F);
|
|
6875
|
+
const Y3 = modP(G2 * H);
|
|
6876
|
+
const T3 = modP(E * H);
|
|
6877
|
+
const Z3 = modP(F * G2);
|
|
6878
|
+
return new Point2(X3, Y3, Z3, T3);
|
|
6879
|
+
}
|
|
6880
|
+
subtract(other) {
|
|
6881
|
+
return this.add(other.negate());
|
|
6882
|
+
}
|
|
6883
|
+
// Constant-time multiplication.
|
|
6884
|
+
multiply(scalar) {
|
|
6885
|
+
if (!Fn2.isValidNot0(scalar))
|
|
6886
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
6887
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point2, p2));
|
|
6888
|
+
return normalizeZ(Point2, [p, f])[0];
|
|
6889
|
+
}
|
|
6890
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
6891
|
+
// It's faster, but should only be used when you don't care about
|
|
6892
|
+
// an exposed private key e.g. sig verification.
|
|
6893
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
6894
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
6895
|
+
multiplyUnsafe(scalar, acc = Point2.ZERO) {
|
|
6896
|
+
if (!Fn2.isValid(scalar))
|
|
6897
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
6898
|
+
if (scalar === _0n4)
|
|
6899
|
+
return Point2.ZERO;
|
|
6900
|
+
if (this.is0() || scalar === _1n4)
|
|
6901
|
+
return this;
|
|
6902
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point2, p), acc);
|
|
6903
|
+
}
|
|
6904
|
+
// Checks if point is of small order.
|
|
6905
|
+
// If you add something to small order point, you will have "dirty"
|
|
6906
|
+
// point with torsion component.
|
|
6907
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
6908
|
+
isSmallOrder() {
|
|
6909
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
6910
|
+
}
|
|
6911
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
6912
|
+
// Returns `false` is the point is dirty.
|
|
6913
|
+
isTorsionFree() {
|
|
6914
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
6915
|
+
}
|
|
6916
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
6917
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
6918
|
+
toAffine(invertedZ) {
|
|
6919
|
+
return toAffineMemo(this, invertedZ);
|
|
6920
|
+
}
|
|
6921
|
+
clearCofactor() {
|
|
6922
|
+
if (cofactor === _1n4)
|
|
6923
|
+
return this;
|
|
6924
|
+
return this.multiplyUnsafe(cofactor);
|
|
6925
|
+
}
|
|
6926
|
+
toBytes() {
|
|
6927
|
+
const { x, y } = this.toAffine();
|
|
6928
|
+
const bytes2 = Fp2.toBytes(y);
|
|
6929
|
+
bytes2[bytes2.length - 1] |= x & _1n4 ? 128 : 0;
|
|
6930
|
+
return bytes2;
|
|
6931
|
+
}
|
|
6932
|
+
toHex() {
|
|
6933
|
+
return bytesToHex2(this.toBytes());
|
|
6934
|
+
}
|
|
6935
|
+
toString() {
|
|
6936
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
6937
|
+
}
|
|
6938
|
+
}
|
|
6939
|
+
const wnaf = new wNAF2(Point2, Fn2.BITS);
|
|
6940
|
+
Point2.BASE.precompute(8);
|
|
6941
|
+
return Point2;
|
|
6942
|
+
}
|
|
6943
|
+
var PrimeEdwardsPoint = class {
|
|
6944
|
+
static BASE;
|
|
6945
|
+
static ZERO;
|
|
6946
|
+
static Fp;
|
|
6947
|
+
static Fn;
|
|
6948
|
+
ep;
|
|
6949
|
+
constructor(ep) {
|
|
6950
|
+
this.ep = ep;
|
|
6951
|
+
}
|
|
6952
|
+
// Static methods that must be implemented by subclasses
|
|
6953
|
+
static fromBytes(_bytes) {
|
|
6954
|
+
notImplemented();
|
|
6955
|
+
}
|
|
6956
|
+
static fromHex(_hex) {
|
|
6957
|
+
notImplemented();
|
|
6958
|
+
}
|
|
6959
|
+
get x() {
|
|
6960
|
+
return this.toAffine().x;
|
|
6961
|
+
}
|
|
6962
|
+
get y() {
|
|
6963
|
+
return this.toAffine().y;
|
|
6964
|
+
}
|
|
6965
|
+
// Common implementations
|
|
6966
|
+
clearCofactor() {
|
|
6967
|
+
return this;
|
|
6968
|
+
}
|
|
6969
|
+
assertValidity() {
|
|
6970
|
+
this.ep.assertValidity();
|
|
6971
|
+
}
|
|
6972
|
+
toAffine(invertedZ) {
|
|
6973
|
+
return this.ep.toAffine(invertedZ);
|
|
6974
|
+
}
|
|
6975
|
+
toHex() {
|
|
6976
|
+
return bytesToHex2(this.toBytes());
|
|
6977
|
+
}
|
|
6978
|
+
toString() {
|
|
6979
|
+
return this.toHex();
|
|
6980
|
+
}
|
|
6981
|
+
isTorsionFree() {
|
|
6982
|
+
return true;
|
|
6983
|
+
}
|
|
6984
|
+
isSmallOrder() {
|
|
6985
|
+
return false;
|
|
6986
|
+
}
|
|
6987
|
+
add(other) {
|
|
6988
|
+
this.assertSame(other);
|
|
6989
|
+
return this.init(this.ep.add(other.ep));
|
|
6990
|
+
}
|
|
6991
|
+
subtract(other) {
|
|
6992
|
+
this.assertSame(other);
|
|
6993
|
+
return this.init(this.ep.subtract(other.ep));
|
|
6994
|
+
}
|
|
6995
|
+
multiply(scalar) {
|
|
6996
|
+
return this.init(this.ep.multiply(scalar));
|
|
6997
|
+
}
|
|
6998
|
+
multiplyUnsafe(scalar) {
|
|
6999
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
7000
|
+
}
|
|
7001
|
+
double() {
|
|
7002
|
+
return this.init(this.ep.double());
|
|
7003
|
+
}
|
|
7004
|
+
negate() {
|
|
7005
|
+
return this.init(this.ep.negate());
|
|
7006
|
+
}
|
|
7007
|
+
precompute(windowSize, isLazy) {
|
|
7008
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
7009
|
+
}
|
|
7010
|
+
};
|
|
7011
|
+
|
|
7012
|
+
// node_modules/@noble/curves/ed25519.js
|
|
7013
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
7014
|
+
var _1n5 = BigInt(1);
|
|
7015
|
+
var _2n3 = BigInt(2);
|
|
7016
|
+
var _5n2 = BigInt(5);
|
|
7017
|
+
var _8n3 = BigInt(8);
|
|
7018
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
7019
|
+
var ed25519_CURVE2 = /* @__PURE__ */ (() => ({
|
|
7020
|
+
p: ed25519_CURVE_p,
|
|
7021
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
7022
|
+
h: _8n3,
|
|
7023
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
7024
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
7025
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
7026
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
7027
|
+
}))();
|
|
7028
|
+
function ed25519_pow_2_252_3(x) {
|
|
7029
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
7030
|
+
const P2 = ed25519_CURVE_p;
|
|
7031
|
+
const x2 = x * x % P2;
|
|
7032
|
+
const b2 = x2 * x % P2;
|
|
7033
|
+
const b4 = pow22(b2, _2n3, P2) * b2 % P2;
|
|
7034
|
+
const b5 = pow22(b4, _1n5, P2) * x % P2;
|
|
7035
|
+
const b10 = pow22(b5, _5n2, P2) * b5 % P2;
|
|
7036
|
+
const b20 = pow22(b10, _10n, P2) * b10 % P2;
|
|
7037
|
+
const b40 = pow22(b20, _20n, P2) * b20 % P2;
|
|
7038
|
+
const b80 = pow22(b40, _40n, P2) * b40 % P2;
|
|
7039
|
+
const b160 = pow22(b80, _80n, P2) * b80 % P2;
|
|
7040
|
+
const b240 = pow22(b160, _80n, P2) * b80 % P2;
|
|
7041
|
+
const b250 = pow22(b240, _10n, P2) * b10 % P2;
|
|
7042
|
+
const pow_p_5_8 = pow22(b250, _2n3, P2) * x % P2;
|
|
7043
|
+
return { pow_p_5_8, b2 };
|
|
7044
|
+
}
|
|
7045
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
7046
|
+
function uvRatio2(u, v) {
|
|
7047
|
+
const P2 = ed25519_CURVE_p;
|
|
7048
|
+
const v3 = mod(v * v * v, P2);
|
|
7049
|
+
const v7 = mod(v3 * v3 * v, P2);
|
|
7050
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
7051
|
+
let x = mod(u * v3 * pow, P2);
|
|
7052
|
+
const vx2 = mod(v * x * x, P2);
|
|
7053
|
+
const root1 = x;
|
|
7054
|
+
const root2 = mod(x * ED25519_SQRT_M1, P2);
|
|
7055
|
+
const useRoot1 = vx2 === u;
|
|
7056
|
+
const useRoot2 = vx2 === mod(-u, P2);
|
|
7057
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P2);
|
|
7058
|
+
if (useRoot1)
|
|
7059
|
+
x = root1;
|
|
7060
|
+
if (useRoot2 || noRoot)
|
|
7061
|
+
x = root2;
|
|
7062
|
+
if (isNegativeLE(x, P2))
|
|
7063
|
+
x = mod(-x, P2);
|
|
7064
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
7065
|
+
}
|
|
7066
|
+
var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE2, { uvRatio: uvRatio2 });
|
|
7067
|
+
var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
7068
|
+
var Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
7069
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
7070
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
7071
|
+
var invertSqrt = (number) => uvRatio2(_1n5, number);
|
|
7072
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
7073
|
+
var bytes255ToNumberLE = (bytes2) => Fp.create(bytesToNumberLE(bytes2) & MAX_255B);
|
|
7074
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
7075
|
+
// Do NOT change syntax: the following gymnastics is done,
|
|
7076
|
+
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
7077
|
+
// prettier-ignore
|
|
7078
|
+
static BASE = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))();
|
|
7079
|
+
// prettier-ignore
|
|
7080
|
+
static ZERO = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))();
|
|
7081
|
+
// prettier-ignore
|
|
7082
|
+
static Fp = /* @__PURE__ */ (() => Fp)();
|
|
7083
|
+
// prettier-ignore
|
|
7084
|
+
static Fn = /* @__PURE__ */ (() => Fn)();
|
|
7085
|
+
constructor(ep) {
|
|
7086
|
+
super(ep);
|
|
7087
|
+
}
|
|
7088
|
+
static fromAffine(ap) {
|
|
7089
|
+
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
7090
|
+
}
|
|
7091
|
+
assertSame(other) {
|
|
7092
|
+
if (!(other instanceof __RistrettoPoint))
|
|
7093
|
+
throw new Error("RistrettoPoint expected");
|
|
7094
|
+
}
|
|
7095
|
+
init(ep) {
|
|
7096
|
+
return new __RistrettoPoint(ep);
|
|
7097
|
+
}
|
|
7098
|
+
static fromBytes(bytes2) {
|
|
7099
|
+
abytes5(bytes2, 32);
|
|
7100
|
+
const { a, d } = ed25519_CURVE2;
|
|
7101
|
+
const P2 = ed25519_CURVE_p;
|
|
7102
|
+
const mod2 = (n) => Fp.create(n);
|
|
7103
|
+
const s = bytes255ToNumberLE(bytes2);
|
|
7104
|
+
if (!equalBytes(Fp.toBytes(s), bytes2) || isNegativeLE(s, P2))
|
|
7105
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
7106
|
+
const s2 = mod2(s * s);
|
|
7107
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
7108
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
7109
|
+
const u1_2 = mod2(u1 * u1);
|
|
7110
|
+
const u2_2 = mod2(u2 * u2);
|
|
7111
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
7112
|
+
const { isValid, value: I2 } = invertSqrt(mod2(v * u2_2));
|
|
7113
|
+
const Dx = mod2(I2 * u2);
|
|
7114
|
+
const Dy = mod2(I2 * Dx * v);
|
|
7115
|
+
let x = mod2((s + s) * Dx);
|
|
7116
|
+
if (isNegativeLE(x, P2))
|
|
7117
|
+
x = mod2(-x);
|
|
7118
|
+
const y = mod2(u1 * Dy);
|
|
7119
|
+
const t = mod2(x * y);
|
|
7120
|
+
if (!isValid || isNegativeLE(t, P2) || y === _0n5)
|
|
7121
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
7122
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, _1n5, t));
|
|
7123
|
+
}
|
|
7124
|
+
/**
|
|
7125
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
7126
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
7127
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
7128
|
+
*/
|
|
7129
|
+
static fromHex(hex2) {
|
|
7130
|
+
return __RistrettoPoint.fromBytes(hexToBytes2(hex2));
|
|
7131
|
+
}
|
|
7132
|
+
/**
|
|
7133
|
+
* Encodes ristretto point to Uint8Array.
|
|
7134
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
7135
|
+
*/
|
|
7136
|
+
toBytes() {
|
|
7137
|
+
let { X, Y, Z, T } = this.ep;
|
|
7138
|
+
const P2 = ed25519_CURVE_p;
|
|
7139
|
+
const mod2 = (n) => Fp.create(n);
|
|
7140
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
7141
|
+
const u2 = mod2(X * Y);
|
|
7142
|
+
const u2sq = mod2(u2 * u2);
|
|
7143
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
7144
|
+
const D1 = mod2(invsqrt * u1);
|
|
7145
|
+
const D2 = mod2(invsqrt * u2);
|
|
7146
|
+
const zInv = mod2(D1 * D2 * T);
|
|
7147
|
+
let D;
|
|
7148
|
+
if (isNegativeLE(T * zInv, P2)) {
|
|
7149
|
+
let _x = mod2(Y * SQRT_M1);
|
|
7150
|
+
let _y = mod2(X * SQRT_M1);
|
|
7151
|
+
X = _x;
|
|
7152
|
+
Y = _y;
|
|
7153
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
7154
|
+
} else {
|
|
7155
|
+
D = D2;
|
|
7156
|
+
}
|
|
7157
|
+
if (isNegativeLE(X * zInv, P2))
|
|
7158
|
+
Y = mod2(-Y);
|
|
7159
|
+
let s = mod2((Z - Y) * D);
|
|
7160
|
+
if (isNegativeLE(s, P2))
|
|
7161
|
+
s = mod2(-s);
|
|
7162
|
+
return Fp.toBytes(s);
|
|
7163
|
+
}
|
|
7164
|
+
/**
|
|
7165
|
+
* Compares two Ristretto points.
|
|
7166
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
7167
|
+
*/
|
|
7168
|
+
equals(other) {
|
|
7169
|
+
this.assertSame(other);
|
|
7170
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
7171
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
7172
|
+
const mod2 = (n) => Fp.create(n);
|
|
7173
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
7174
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
7175
|
+
return one || two;
|
|
7176
|
+
}
|
|
7177
|
+
is0() {
|
|
7178
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
7179
|
+
}
|
|
7180
|
+
};
|
|
7181
|
+
var ristretto255 = { Point: _RistrettoPoint };
|
|
7182
|
+
|
|
7183
|
+
// node_modules/@noble/hashes/hkdf.js
|
|
7184
|
+
function extract(hash, ikm, salt) {
|
|
7185
|
+
ahash(hash);
|
|
7186
|
+
if (salt === void 0)
|
|
7187
|
+
salt = new Uint8Array(hash.outputLen);
|
|
7188
|
+
return hmac(hash, salt, ikm);
|
|
7189
|
+
}
|
|
7190
|
+
var HKDF_COUNTER = /* @__PURE__ */ Uint8Array.of(0);
|
|
7191
|
+
var EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
|
7192
|
+
function expand(hash, prk, info, length = 32) {
|
|
7193
|
+
ahash(hash);
|
|
7194
|
+
anumber(length, "length");
|
|
7195
|
+
const olen = hash.outputLen;
|
|
7196
|
+
if (length > 255 * olen)
|
|
7197
|
+
throw new Error("Length must be <= 255*HashLen");
|
|
7198
|
+
const blocks = Math.ceil(length / olen);
|
|
7199
|
+
if (info === void 0)
|
|
7200
|
+
info = EMPTY_BUFFER;
|
|
7201
|
+
else
|
|
7202
|
+
abytes2(info, void 0, "info");
|
|
7203
|
+
const okm = new Uint8Array(blocks * olen);
|
|
7204
|
+
const HMAC = hmac.create(hash, prk);
|
|
7205
|
+
const HMACTmp = HMAC._cloneInto();
|
|
7206
|
+
const T = new Uint8Array(HMAC.outputLen);
|
|
7207
|
+
for (let counter = 0; counter < blocks; counter++) {
|
|
7208
|
+
HKDF_COUNTER[0] = counter + 1;
|
|
7209
|
+
HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T).update(info).update(HKDF_COUNTER).digestInto(T);
|
|
7210
|
+
okm.set(T, olen * counter);
|
|
7211
|
+
HMAC._cloneInto(HMACTmp);
|
|
7212
|
+
}
|
|
7213
|
+
HMAC.destroy();
|
|
7214
|
+
HMACTmp.destroy();
|
|
7215
|
+
clean(T, HKDF_COUNTER);
|
|
7216
|
+
return okm.slice(0, length);
|
|
7217
|
+
}
|
|
7218
|
+
var hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
|
|
7219
|
+
|
|
7220
|
+
// src/rex/dkg.ts
|
|
7221
|
+
var RISTRETTO255_ORDER = BigInt(
|
|
7222
|
+
"0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"
|
|
7223
|
+
);
|
|
7224
|
+
var DKG_KDF_SALT = /* @__PURE__ */ new TextEncoder().encode(
|
|
7225
|
+
"RIALO_DKG_THRESHOLD_V1"
|
|
7226
|
+
);
|
|
7227
|
+
var DKG_ACL_AAD_DOMAIN = /* @__PURE__ */ new TextEncoder().encode(
|
|
7228
|
+
"RIALO_DKG_ACL_V1"
|
|
7229
|
+
// exactly 16 bytes
|
|
7230
|
+
);
|
|
7231
|
+
var DKG_PAYLOAD_VERSION2 = 2;
|
|
7232
|
+
var MAX_SECRET_LENGTH2 = 64 * 1024;
|
|
7233
|
+
var RISTRETTO_POINT_BYTES2 = 32;
|
|
7234
|
+
var ED25519_PUBKEY_BYTES = 32;
|
|
7235
|
+
var CHACHA20_NONCE_BYTES = 12;
|
|
7236
|
+
function buildAad(creatorPubkey) {
|
|
7237
|
+
const aad = new Uint8Array(DKG_ACL_AAD_DOMAIN.length + creatorPubkey.length);
|
|
7238
|
+
aad.set(DKG_ACL_AAD_DOMAIN, 0);
|
|
7239
|
+
aad.set(creatorPubkey, DKG_ACL_AAD_DOMAIN.length);
|
|
5977
7240
|
return aad;
|
|
5978
7241
|
}
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
|
|
5982
|
-
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
7242
|
+
function buildKdfInfo(epoch, headerU, aad) {
|
|
7243
|
+
const info = new Uint8Array(8 + RISTRETTO_POINT_BYTES2 + aad.length);
|
|
7244
|
+
new DataView(info.buffer).setBigUint64(0, epoch, true);
|
|
7245
|
+
info.set(headerU, 8);
|
|
7246
|
+
info.set(aad, 8 + RISTRETTO_POINT_BYTES2);
|
|
7247
|
+
return info;
|
|
7248
|
+
}
|
|
7249
|
+
function encodeDkgPayload(epoch, ciphertextU, ciphertextBody, ciphertextNonce, ciphertextAad) {
|
|
7250
|
+
const size = 8 + RISTRETTO_POINT_BYTES2 + 4 + ciphertextBody.length + CHACHA20_NONCE_BYTES + 4 + ciphertextAad.length;
|
|
7251
|
+
const buf = new Uint8Array(size);
|
|
7252
|
+
const view = new DataView(buf.buffer);
|
|
7253
|
+
let off = 0;
|
|
7254
|
+
view.setBigUint64(off, epoch, true);
|
|
7255
|
+
off += 8;
|
|
7256
|
+
buf.set(ciphertextU, off);
|
|
7257
|
+
off += RISTRETTO_POINT_BYTES2;
|
|
7258
|
+
view.setUint32(off, ciphertextBody.length, true);
|
|
7259
|
+
off += 4;
|
|
7260
|
+
buf.set(ciphertextBody, off);
|
|
7261
|
+
off += ciphertextBody.length;
|
|
7262
|
+
buf.set(ciphertextNonce, off);
|
|
7263
|
+
off += CHACHA20_NONCE_BYTES;
|
|
7264
|
+
view.setUint32(off, ciphertextAad.length, true);
|
|
7265
|
+
off += 4;
|
|
7266
|
+
buf.set(ciphertextAad, off);
|
|
7267
|
+
return buf;
|
|
7268
|
+
}
|
|
7269
|
+
function encryptSecretBytesWithEpoch(plaintext, creatorPubkey, epoch, thresholdPubkeyHex) {
|
|
7270
|
+
if (plaintext.length === 0) {
|
|
7271
|
+
throw EncryptionError.invalidInput("Secret cannot be empty");
|
|
5986
7272
|
}
|
|
5987
|
-
if (
|
|
5988
|
-
throw
|
|
5989
|
-
|
|
5990
|
-
senderPubkey.length,
|
|
5991
|
-
"sender public key"
|
|
7273
|
+
if (plaintext.length > MAX_SECRET_LENGTH2) {
|
|
7274
|
+
throw EncryptionError.invalidInput(
|
|
7275
|
+
`Secret exceeds maximum length of ${MAX_SECRET_LENGTH2} bytes (got ${plaintext.length} bytes)`
|
|
5992
7276
|
);
|
|
5993
7277
|
}
|
|
5994
|
-
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
rexPubkey.buffer.slice(
|
|
5998
|
-
rexPubkey.byteOffset,
|
|
5999
|
-
rexPubkey.byteOffset + rexPubkey.byteLength
|
|
6000
|
-
)
|
|
7278
|
+
if (creatorPubkey.length !== ED25519_PUBKEY_BYTES) {
|
|
7279
|
+
throw EncryptionError.invalidInput(
|
|
7280
|
+
`Creator public key must be ${ED25519_PUBKEY_BYTES} bytes, got ${creatorPubkey.length}`
|
|
6001
7281
|
);
|
|
6002
|
-
|
|
6003
|
-
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
SECRET_SHARING_HPKE_INFO.byteOffset + SECRET_SHARING_HPKE_INFO.byteLength
|
|
6007
|
-
)
|
|
6008
|
-
});
|
|
6009
|
-
const aad = buildAad(senderPubkey);
|
|
6010
|
-
const ciphertext = await sender.seal(
|
|
6011
|
-
data.buffer.slice(
|
|
6012
|
-
data.byteOffset,
|
|
6013
|
-
data.byteOffset + data.byteLength
|
|
6014
|
-
),
|
|
6015
|
-
aad.buffer.slice(
|
|
6016
|
-
aad.byteOffset,
|
|
6017
|
-
aad.byteOffset + aad.byteLength
|
|
6018
|
-
)
|
|
7282
|
+
}
|
|
7283
|
+
if (epoch < 0n || epoch > 0xffffffffffffffffn) {
|
|
7284
|
+
throw EncryptionError.invalidInput(
|
|
7285
|
+
`Epoch must be a valid u64 (0 to 2^64-1), got ${epoch}`
|
|
6019
7286
|
);
|
|
6020
|
-
|
|
6021
|
-
|
|
6022
|
-
|
|
6023
|
-
|
|
6024
|
-
|
|
6025
|
-
|
|
6026
|
-
|
|
6027
|
-
error instanceof Error ? error : new Error(String(error))
|
|
7287
|
+
}
|
|
7288
|
+
let jointPubKey;
|
|
7289
|
+
try {
|
|
7290
|
+
jointPubKey = ristretto255.Point.fromHex(thresholdPubkeyHex);
|
|
7291
|
+
} catch (e) {
|
|
7292
|
+
throw EncryptionError.invalidThresholdKey(
|
|
7293
|
+
e instanceof Error ? e : new Error(String(e))
|
|
6028
7294
|
);
|
|
6029
7295
|
}
|
|
7296
|
+
const aad = buildAad(creatorPubkey);
|
|
7297
|
+
const r = bytesToNumberLE(randomBytes(64)) % RISTRETTO255_ORDER;
|
|
7298
|
+
const headerUBytes = ristretto255.Point.BASE.multiply(r).toBytes();
|
|
7299
|
+
const combinedZBytes = jointPubKey.multiply(r).toBytes();
|
|
7300
|
+
const sessionKey = hkdf(
|
|
7301
|
+
sha256,
|
|
7302
|
+
combinedZBytes,
|
|
7303
|
+
DKG_KDF_SALT,
|
|
7304
|
+
buildKdfInfo(epoch, headerUBytes, aad),
|
|
7305
|
+
32
|
|
7306
|
+
);
|
|
7307
|
+
const nonce = randomBytes(CHACHA20_NONCE_BYTES);
|
|
7308
|
+
const ciphertextBody = chacha20poly1305(sessionKey, nonce, aad).encrypt(
|
|
7309
|
+
plaintext
|
|
7310
|
+
);
|
|
7311
|
+
const payload = encodeDkgPayload(epoch, headerUBytes, ciphertextBody, nonce, aad);
|
|
7312
|
+
const result = new Uint8Array(1 + payload.length);
|
|
7313
|
+
result[0] = DKG_PAYLOAD_VERSION2;
|
|
7314
|
+
result.set(payload, 1);
|
|
7315
|
+
return result;
|
|
6030
7316
|
}
|
|
6031
|
-
|
|
6032
|
-
|
|
6033
|
-
|
|
7317
|
+
function encryptSecretBytes(plaintext, creatorPubkey, secretSharingPubkey) {
|
|
7318
|
+
return encryptSecretBytesWithEpoch(
|
|
7319
|
+
plaintext,
|
|
7320
|
+
creatorPubkey,
|
|
7321
|
+
secretSharingPubkey.epoch,
|
|
7322
|
+
secretSharingPubkey.publicKey
|
|
7323
|
+
);
|
|
6034
7324
|
}
|
|
6035
|
-
function
|
|
6036
|
-
return
|
|
7325
|
+
function encryptSecret(secret, creatorPubkey, secretSharingPubkey) {
|
|
7326
|
+
return encryptSecretBytes(
|
|
7327
|
+
new TextEncoder().encode(secret),
|
|
7328
|
+
creatorPubkey,
|
|
7329
|
+
secretSharingPubkey
|
|
7330
|
+
);
|
|
6037
7331
|
}
|
|
6038
|
-
function
|
|
6039
|
-
return
|
|
7332
|
+
function encryptForRex(plaintext, creatorPubkey, secretSharingPubkey) {
|
|
7333
|
+
return RexValue.encrypted(
|
|
7334
|
+
encryptSecretBytes(plaintext, creatorPubkey, secretSharingPubkey)
|
|
7335
|
+
);
|
|
6040
7336
|
}
|
|
6041
7337
|
|
|
6042
7338
|
// src/rpc/errors.ts
|
|
@@ -6668,39 +7964,28 @@ var QueryRpcClient = class extends BaseRpcClient {
|
|
|
6668
7964
|
}));
|
|
6669
7965
|
}
|
|
6670
7966
|
/**
|
|
6671
|
-
* Retrieve the
|
|
6672
|
-
*
|
|
6673
|
-
* This key is used for HPKE encryption when sending encrypted data
|
|
6674
|
-
* that should only be decryptable within the REX execution environment.
|
|
7967
|
+
* Retrieve the active threshold public key metadata.
|
|
6675
7968
|
*
|
|
6676
|
-
*
|
|
6677
|
-
*
|
|
6678
|
-
*
|
|
6679
|
-
*
|
|
6680
|
-
* import { encryptForREX } from "@rialo/ts-cdk";
|
|
6681
|
-
*
|
|
6682
|
-
* // Get the REX public key
|
|
6683
|
-
* const rexPubkey = await client.getSecretSharingPubkey();
|
|
6684
|
-
*
|
|
6685
|
-
* // Use it for HPKE encryption
|
|
6686
|
-
* const encrypted = await encryptForRex(
|
|
6687
|
-
* rexPubkey,
|
|
6688
|
-
* new TextEncoder().encode("secret data"),
|
|
6689
|
-
* keypair.publicKey.toBytes()
|
|
6690
|
-
* );
|
|
6691
|
-
* ```
|
|
7969
|
+
* Returns the active threshold public key (a compressed Ristretto point) and
|
|
7970
|
+
* its DKG epoch. Pass the result directly to `encryptSecretBytes`,
|
|
7971
|
+
* `encryptSecret`, or `encryptForRex` to produce a threshold-encrypted
|
|
7972
|
+
* `DkgEncryptedPayload` the network can decrypt during REX execution.
|
|
6692
7973
|
*/
|
|
6693
7974
|
async getSecretSharingPubkey() {
|
|
6694
7975
|
const result = await this.call(
|
|
6695
7976
|
"getSecretSharingPubkey",
|
|
6696
7977
|
[]
|
|
6697
7978
|
);
|
|
6698
|
-
|
|
6699
|
-
|
|
6700
|
-
|
|
6701
|
-
|
|
6702
|
-
|
|
6703
|
-
|
|
7979
|
+
return {
|
|
7980
|
+
publicKey: result.pubkey,
|
|
7981
|
+
epoch: BigInt(result.epoch)
|
|
7982
|
+
};
|
|
7983
|
+
}
|
|
7984
|
+
/**
|
|
7985
|
+
* @deprecated Use `getSecretSharingPubkey()` instead.
|
|
7986
|
+
*/
|
|
7987
|
+
async getSecretSharingPubkeyInfo() {
|
|
7988
|
+
return await this.getSecretSharingPubkey();
|
|
6704
7989
|
}
|
|
6705
7990
|
/**
|
|
6706
7991
|
* Get the config hash prefix for replay protection.
|
|
@@ -6959,7 +8244,7 @@ var QueryRpcClient = class extends BaseRpcClient {
|
|
|
6959
8244
|
withdrawalKey: v.withdrawal_key,
|
|
6960
8245
|
stake: BigInt(v.stake),
|
|
6961
8246
|
address: v.address,
|
|
6962
|
-
|
|
8247
|
+
subdagSyncAddress: v.subdag_sync_address
|
|
6963
8248
|
}));
|
|
6964
8249
|
}
|
|
6965
8250
|
/**
|
|
@@ -7595,12 +8880,10 @@ var RialoClient = class extends RpcClient {
|
|
|
7595
8880
|
return await this.queryClient.getConnectedFullNodes();
|
|
7596
8881
|
}
|
|
7597
8882
|
/**
|
|
7598
|
-
* Gets the
|
|
8883
|
+
* Gets the active secret-sharing public key metadata.
|
|
7599
8884
|
*/
|
|
7600
8885
|
async getSecretSharingPubkey() {
|
|
7601
|
-
|
|
7602
|
-
const hex2 = Array.from(rawBytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
7603
|
-
return { publicKey: hex2 };
|
|
8886
|
+
return await this.queryClient.getSecretSharingPubkey();
|
|
7604
8887
|
}
|
|
7605
8888
|
/**
|
|
7606
8889
|
* Gets the config hash prefix for protecting against replay attacks.
|
|
@@ -11531,14 +12814,22 @@ var ProgramDeployment = class {
|
|
|
11531
12814
|
@noble/ed25519/index.js:
|
|
11532
12815
|
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
11533
12816
|
|
|
12817
|
+
@noble/hashes/utils.js:
|
|
11534
12818
|
@noble/hashes/utils.js:
|
|
11535
12819
|
@noble/hashes/utils.js:
|
|
11536
12820
|
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
11537
12821
|
|
|
11538
12822
|
@scure/bip39/index.js:
|
|
11539
12823
|
(*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) *)
|
|
12824
|
+
|
|
12825
|
+
@noble/curves/utils.js:
|
|
12826
|
+
@noble/curves/abstract/modular.js:
|
|
12827
|
+
@noble/curves/abstract/curve.js:
|
|
12828
|
+
@noble/curves/abstract/edwards.js:
|
|
12829
|
+
@noble/curves/ed25519.js:
|
|
12830
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
11540
12831
|
*/
|
|
11541
12832
|
|
|
11542
|
-
export { AccountMetaTable, BASE_DERIVATION_PATH, BUFFER_BALANCE_FACTOR, BaseRpcClient, BincodeReader, BincodeWriter, CHACHA20_POLY1305_TAG_LENGTH, CryptoError, CryptoErrorCode, DEFAULT_CHUNK_SIZE, DEFAULT_CONFIRMATION_BATCH_SIZE, DEFAULT_MAX_RETRIES2 as DEFAULT_MAX_RETRIES, DEFAULT_NUM_ACCOUNTS, DEFAULT_RETRY_BASE_DELAY_MS, DEFAULT_RETRY_MAX_DELAY_MS, DeploymentError, DeploymentErrorCode, ED25519_PUBLIC_KEY_LENGTH,
|
|
12833
|
+
export { AccountMetaTable, BASE_DERIVATION_PATH, BUFFER_BALANCE_FACTOR, BaseRpcClient, BincodeReader, BincodeWriter, CHACHA20_POLY1305_NONCE_LENGTH, CHACHA20_POLY1305_TAG_LENGTH, CryptoError, CryptoErrorCode, DEFAULT_CHUNK_SIZE, DEFAULT_CONFIRMATION_BATCH_SIZE, DEFAULT_MAX_RETRIES2 as DEFAULT_MAX_RETRIES, DEFAULT_NUM_ACCOUNTS, DEFAULT_RETRY_BASE_DELAY_MS, DEFAULT_RETRY_MAX_DELAY_MS, DKG_PAYLOAD_VERSION, DeploymentError, DeploymentErrorCode, ED25519_PUBLIC_KEY_LENGTH, EncryptionError, EncryptionErrorCode, HttpTransport, InMemoryKeyringProvider, KELVIN_PER_RLO, Keypair, KeypairSigner, Keyring, KeyringProvider, LOADER_V4_PROGRAM_ID, MAX_SECRET_LENGTH, Message, Mnemonic, PROGRAM_DATA_OFFSET, PUBLIC_KEY_LENGTH, ProgramDeployment, PublicKey, QueryRpcClient, RIALO_DEVNET_CHAIN, RIALO_LOCALNET_CHAIN, RIALO_MAINNET_CHAIN, RIALO_TESTNET_CHAIN, RISCV_LOADER_PROGRAM_ID, RISTRETTO_POINT_BYTES, RexValue, RexValueVariant, RialoClient, RialoError, RialoErrorType, RialoKeyring, RiscVLoaderInstruction, RpcError, RpcErrorCode, SECRET_KEY_LENGTH, SIGNATURE_LENGTH, SYSTEM_PROGRAM_ID, Schema, Signature, SystemInstruction, Transaction, TransactionBuilder, TransactionError, TransactionErrorCode, TransactionRpcClient, URL_DEVNET, URL_LOCALNET, URL_MAINNET, URL_TESTNET, allocateInstruction, assignInstruction, calculateBackoff, concatBytes2 as concatBytes, createAccount, createBorshInstruction, createRialoClient, deployInstruction, deserialize, deserializeBorsh, deserializeCompactU162 as deserializeCompactU16, deserializeStrict, encodeBorshData, encryptForRex, encryptSecret, encryptSecretBytes, encryptSecretBytesWithEpoch, field, fixedArray, fromBase64, getDefaultRialoClientConfig, getDevnetUrl, getLocalnetUrl, getMainnetUrl, getTestnetUrl, isOnCurve, option, retractInstruction, seedToBytes, serialize, serializeBorsh, serializeCompactU16, setProgramLengthInstruction, sleep, toBase64, transferInstruction, vec, writeCompactU16, writeInstruction };
|
|
11543
12834
|
//# sourceMappingURL=index.mjs.map
|
|
11544
12835
|
//# sourceMappingURL=index.mjs.map
|