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