@tinycloud/cli 1.5.0-beta.26 → 1.5.0-beta.28
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 +159 -15
- package/dist/courier-entry.js +1328 -8
- package/dist/courier-entry.js.map +1 -1
- package/dist/index.js +1462 -59
- package/dist/index.js.map +1 -1
- package/dist/legacy-entry.js +4164 -2034
- package/dist/legacy-entry.js.map +1 -1
- package/package.json +9 -9
- package/skills/tc-cli/AUTH.md +2 -2
- package/skills/tc-cli/REFERENCE.md +4 -4
- package/skills/tc-cli/SKILL.md +2 -2
package/dist/courier-entry.js
CHANGED
|
@@ -16298,6 +16298,12 @@ function abytes(b, ...lengths) {
|
|
|
16298
16298
|
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
16299
16299
|
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
16300
16300
|
}
|
|
16301
|
+
function ahash(h) {
|
|
16302
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
16303
|
+
throw new Error("Hash should be wrapped by utils.createHasher");
|
|
16304
|
+
anumber(h.outputLen);
|
|
16305
|
+
anumber(h.blockLen);
|
|
16306
|
+
}
|
|
16301
16307
|
function aexists(instance, checkFinished = true) {
|
|
16302
16308
|
if (instance.destroyed)
|
|
16303
16309
|
throw new Error("Hash instance has been destroyed");
|
|
@@ -16533,6 +16539,24 @@ var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
|
16533
16539
|
528734635,
|
|
16534
16540
|
1541459225
|
|
16535
16541
|
]);
|
|
16542
|
+
var SHA384_IV = /* @__PURE__ */ Uint32Array.from([
|
|
16543
|
+
3418070365,
|
|
16544
|
+
3238371032,
|
|
16545
|
+
1654270250,
|
|
16546
|
+
914150663,
|
|
16547
|
+
2438529370,
|
|
16548
|
+
812702999,
|
|
16549
|
+
355462360,
|
|
16550
|
+
4144912697,
|
|
16551
|
+
1731405415,
|
|
16552
|
+
4290775857,
|
|
16553
|
+
2394180231,
|
|
16554
|
+
1750603025,
|
|
16555
|
+
3675008525,
|
|
16556
|
+
1694076839,
|
|
16557
|
+
1203062813,
|
|
16558
|
+
3204075428
|
|
16559
|
+
]);
|
|
16536
16560
|
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
16537
16561
|
1779033703,
|
|
16538
16562
|
4089235720,
|
|
@@ -16922,8 +16946,30 @@ var SHA512 = class extends HashMD {
|
|
|
16922
16946
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
16923
16947
|
}
|
|
16924
16948
|
};
|
|
16949
|
+
var SHA384 = class extends SHA512 {
|
|
16950
|
+
constructor() {
|
|
16951
|
+
super(48);
|
|
16952
|
+
this.Ah = SHA384_IV[0] | 0;
|
|
16953
|
+
this.Al = SHA384_IV[1] | 0;
|
|
16954
|
+
this.Bh = SHA384_IV[2] | 0;
|
|
16955
|
+
this.Bl = SHA384_IV[3] | 0;
|
|
16956
|
+
this.Ch = SHA384_IV[4] | 0;
|
|
16957
|
+
this.Cl = SHA384_IV[5] | 0;
|
|
16958
|
+
this.Dh = SHA384_IV[6] | 0;
|
|
16959
|
+
this.Dl = SHA384_IV[7] | 0;
|
|
16960
|
+
this.Eh = SHA384_IV[8] | 0;
|
|
16961
|
+
this.El = SHA384_IV[9] | 0;
|
|
16962
|
+
this.Fh = SHA384_IV[10] | 0;
|
|
16963
|
+
this.Fl = SHA384_IV[11] | 0;
|
|
16964
|
+
this.Gh = SHA384_IV[12] | 0;
|
|
16965
|
+
this.Gl = SHA384_IV[13] | 0;
|
|
16966
|
+
this.Hh = SHA384_IV[14] | 0;
|
|
16967
|
+
this.Hl = SHA384_IV[15] | 0;
|
|
16968
|
+
}
|
|
16969
|
+
};
|
|
16925
16970
|
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
16926
16971
|
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
16972
|
+
var sha384 = /* @__PURE__ */ createHasher(() => new SHA384());
|
|
16927
16973
|
|
|
16928
16974
|
// ../../node_modules/@noble/hashes/esm/sha256.js
|
|
16929
16975
|
var sha2562 = sha256;
|
|
@@ -16950,6 +16996,10 @@ function _abytes2(value2, length2, title = "") {
|
|
|
16950
16996
|
}
|
|
16951
16997
|
return value2;
|
|
16952
16998
|
}
|
|
16999
|
+
function numberToHexUnpadded(num) {
|
|
17000
|
+
const hex = num.toString(16);
|
|
17001
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
17002
|
+
}
|
|
16953
17003
|
function hexToNumber(hex) {
|
|
16954
17004
|
if (typeof hex !== "string")
|
|
16955
17005
|
throw new Error("hex string expected, got " + typeof hex);
|
|
@@ -17012,6 +17062,56 @@ function bitLen(n) {
|
|
|
17012
17062
|
return len;
|
|
17013
17063
|
}
|
|
17014
17064
|
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
17065
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
17066
|
+
if (typeof hashLen !== "number" || hashLen < 2)
|
|
17067
|
+
throw new Error("hashLen must be a number");
|
|
17068
|
+
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
17069
|
+
throw new Error("qByteLen must be a number");
|
|
17070
|
+
if (typeof hmacFn !== "function")
|
|
17071
|
+
throw new Error("hmacFn must be a function");
|
|
17072
|
+
const u8n = (len) => new Uint8Array(len);
|
|
17073
|
+
const u8of = (byte) => Uint8Array.of(byte);
|
|
17074
|
+
let v = u8n(hashLen);
|
|
17075
|
+
let k2 = u8n(hashLen);
|
|
17076
|
+
let i = 0;
|
|
17077
|
+
const reset = () => {
|
|
17078
|
+
v.fill(1);
|
|
17079
|
+
k2.fill(0);
|
|
17080
|
+
i = 0;
|
|
17081
|
+
};
|
|
17082
|
+
const h = (...b) => hmacFn(k2, v, ...b);
|
|
17083
|
+
const reseed = (seed = u8n(0)) => {
|
|
17084
|
+
k2 = h(u8of(0), seed);
|
|
17085
|
+
v = h();
|
|
17086
|
+
if (seed.length === 0)
|
|
17087
|
+
return;
|
|
17088
|
+
k2 = h(u8of(1), seed);
|
|
17089
|
+
v = h();
|
|
17090
|
+
};
|
|
17091
|
+
const gen = () => {
|
|
17092
|
+
if (i++ >= 1e3)
|
|
17093
|
+
throw new Error("drbg: tried 1000 values");
|
|
17094
|
+
let len = 0;
|
|
17095
|
+
const out = [];
|
|
17096
|
+
while (len < qByteLen) {
|
|
17097
|
+
v = h();
|
|
17098
|
+
const sl = v.slice();
|
|
17099
|
+
out.push(sl);
|
|
17100
|
+
len += v.length;
|
|
17101
|
+
}
|
|
17102
|
+
return concatBytes(...out);
|
|
17103
|
+
};
|
|
17104
|
+
const genUntil = (seed, pred) => {
|
|
17105
|
+
reset();
|
|
17106
|
+
reseed(seed);
|
|
17107
|
+
let res = void 0;
|
|
17108
|
+
while (!(res = pred(gen())))
|
|
17109
|
+
reseed();
|
|
17110
|
+
reset();
|
|
17111
|
+
return res;
|
|
17112
|
+
};
|
|
17113
|
+
return genUntil;
|
|
17114
|
+
}
|
|
17015
17115
|
function _validateObject(object, fields, optFields = {}) {
|
|
17016
17116
|
if (!object || typeof object !== "object")
|
|
17017
17117
|
throw new Error("expected valid options object");
|
|
@@ -17366,6 +17466,26 @@ function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
|
17366
17466
|
});
|
|
17367
17467
|
return Object.freeze(f);
|
|
17368
17468
|
}
|
|
17469
|
+
function getFieldBytesLength(fieldOrder) {
|
|
17470
|
+
if (typeof fieldOrder !== "bigint")
|
|
17471
|
+
throw new Error("field order must be bigint");
|
|
17472
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
17473
|
+
return Math.ceil(bitLength / 8);
|
|
17474
|
+
}
|
|
17475
|
+
function getMinHashLength(fieldOrder) {
|
|
17476
|
+
const length2 = getFieldBytesLength(fieldOrder);
|
|
17477
|
+
return length2 + Math.ceil(length2 / 2);
|
|
17478
|
+
}
|
|
17479
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
17480
|
+
const len = key.length;
|
|
17481
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
17482
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
17483
|
+
if (len < 16 || len < minLen || len > 1024)
|
|
17484
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
17485
|
+
const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
17486
|
+
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
17487
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
17488
|
+
}
|
|
17369
17489
|
|
|
17370
17490
|
// ../../node_modules/@noble/curves/esm/abstract/curve.js
|
|
17371
17491
|
var _0n3 = BigInt(0);
|
|
@@ -17559,6 +17679,21 @@ var wNAF = class {
|
|
|
17559
17679
|
return getW(elm) !== 1;
|
|
17560
17680
|
}
|
|
17561
17681
|
};
|
|
17682
|
+
function mulEndoUnsafe(Point, point, k1, k2) {
|
|
17683
|
+
let acc = point;
|
|
17684
|
+
let p1 = Point.ZERO;
|
|
17685
|
+
let p2 = Point.ZERO;
|
|
17686
|
+
while (k1 > _0n3 || k2 > _0n3) {
|
|
17687
|
+
if (k1 & _1n3)
|
|
17688
|
+
p1 = p1.add(acc);
|
|
17689
|
+
if (k2 & _1n3)
|
|
17690
|
+
p2 = p2.add(acc);
|
|
17691
|
+
acc = acc.double();
|
|
17692
|
+
k1 >>= _1n3;
|
|
17693
|
+
k2 >>= _1n3;
|
|
17694
|
+
}
|
|
17695
|
+
return { p1, p2 };
|
|
17696
|
+
}
|
|
17562
17697
|
function pippenger(c, fieldN, points, scalars) {
|
|
17563
17698
|
validateMSMPoints(points, c);
|
|
17564
17699
|
validateMSMScalars(scalars, fieldN);
|
|
@@ -18404,6 +18539,1132 @@ _RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.
|
|
|
18404
18539
|
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
18405
18540
|
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
18406
18541
|
|
|
18542
|
+
// ../../node_modules/@noble/hashes/esm/hmac.js
|
|
18543
|
+
var HMAC = class extends Hash {
|
|
18544
|
+
constructor(hash, _key) {
|
|
18545
|
+
super();
|
|
18546
|
+
this.finished = false;
|
|
18547
|
+
this.destroyed = false;
|
|
18548
|
+
ahash(hash);
|
|
18549
|
+
const key = toBytes(_key);
|
|
18550
|
+
this.iHash = hash.create();
|
|
18551
|
+
if (typeof this.iHash.update !== "function")
|
|
18552
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
18553
|
+
this.blockLen = this.iHash.blockLen;
|
|
18554
|
+
this.outputLen = this.iHash.outputLen;
|
|
18555
|
+
const blockLen = this.blockLen;
|
|
18556
|
+
const pad = new Uint8Array(blockLen);
|
|
18557
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
18558
|
+
for (let i = 0; i < pad.length; i++)
|
|
18559
|
+
pad[i] ^= 54;
|
|
18560
|
+
this.iHash.update(pad);
|
|
18561
|
+
this.oHash = hash.create();
|
|
18562
|
+
for (let i = 0; i < pad.length; i++)
|
|
18563
|
+
pad[i] ^= 54 ^ 92;
|
|
18564
|
+
this.oHash.update(pad);
|
|
18565
|
+
clean(pad);
|
|
18566
|
+
}
|
|
18567
|
+
update(buf) {
|
|
18568
|
+
aexists(this);
|
|
18569
|
+
this.iHash.update(buf);
|
|
18570
|
+
return this;
|
|
18571
|
+
}
|
|
18572
|
+
digestInto(out) {
|
|
18573
|
+
aexists(this);
|
|
18574
|
+
abytes(out, this.outputLen);
|
|
18575
|
+
this.finished = true;
|
|
18576
|
+
this.iHash.digestInto(out);
|
|
18577
|
+
this.oHash.update(out);
|
|
18578
|
+
this.oHash.digestInto(out);
|
|
18579
|
+
this.destroy();
|
|
18580
|
+
}
|
|
18581
|
+
digest() {
|
|
18582
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
18583
|
+
this.digestInto(out);
|
|
18584
|
+
return out;
|
|
18585
|
+
}
|
|
18586
|
+
_cloneInto(to) {
|
|
18587
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
18588
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
18589
|
+
to = to;
|
|
18590
|
+
to.finished = finished;
|
|
18591
|
+
to.destroyed = destroyed;
|
|
18592
|
+
to.blockLen = blockLen;
|
|
18593
|
+
to.outputLen = outputLen;
|
|
18594
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
18595
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
18596
|
+
return to;
|
|
18597
|
+
}
|
|
18598
|
+
clone() {
|
|
18599
|
+
return this._cloneInto();
|
|
18600
|
+
}
|
|
18601
|
+
destroy() {
|
|
18602
|
+
this.destroyed = true;
|
|
18603
|
+
this.oHash.destroy();
|
|
18604
|
+
this.iHash.destroy();
|
|
18605
|
+
}
|
|
18606
|
+
};
|
|
18607
|
+
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
18608
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
18609
|
+
|
|
18610
|
+
// ../../node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
18611
|
+
var divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n4) / den;
|
|
18612
|
+
function _splitEndoScalar(k2, basis, n) {
|
|
18613
|
+
const [[a1, b1], [a2, b2]] = basis;
|
|
18614
|
+
const c1 = divNearest(b2 * k2, n);
|
|
18615
|
+
const c2 = divNearest(-b1 * k2, n);
|
|
18616
|
+
let k1 = k2 - c1 * a1 - c2 * a2;
|
|
18617
|
+
let k22 = -c1 * b1 - c2 * b2;
|
|
18618
|
+
const k1neg = k1 < _0n6;
|
|
18619
|
+
const k2neg = k22 < _0n6;
|
|
18620
|
+
if (k1neg)
|
|
18621
|
+
k1 = -k1;
|
|
18622
|
+
if (k2neg)
|
|
18623
|
+
k22 = -k22;
|
|
18624
|
+
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n6;
|
|
18625
|
+
if (k1 < _0n6 || k1 >= MAX_NUM || k22 < _0n6 || k22 >= MAX_NUM) {
|
|
18626
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + k2);
|
|
18627
|
+
}
|
|
18628
|
+
return { k1neg, k1, k2neg, k2: k22 };
|
|
18629
|
+
}
|
|
18630
|
+
function validateSigFormat(format2) {
|
|
18631
|
+
if (!["compact", "recovered", "der"].includes(format2))
|
|
18632
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
18633
|
+
return format2;
|
|
18634
|
+
}
|
|
18635
|
+
function validateSigOpts(opts, def) {
|
|
18636
|
+
const optsn = {};
|
|
18637
|
+
for (let optName of Object.keys(def)) {
|
|
18638
|
+
optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
18639
|
+
}
|
|
18640
|
+
_abool2(optsn.lowS, "lowS");
|
|
18641
|
+
_abool2(optsn.prehash, "prehash");
|
|
18642
|
+
if (optsn.format !== void 0)
|
|
18643
|
+
validateSigFormat(optsn.format);
|
|
18644
|
+
return optsn;
|
|
18645
|
+
}
|
|
18646
|
+
var DERErr = class extends Error {
|
|
18647
|
+
constructor(m = "") {
|
|
18648
|
+
super(m);
|
|
18649
|
+
}
|
|
18650
|
+
};
|
|
18651
|
+
var DER = {
|
|
18652
|
+
// asn.1 DER encoding utils
|
|
18653
|
+
Err: DERErr,
|
|
18654
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
18655
|
+
_tlv: {
|
|
18656
|
+
encode: (tag, data) => {
|
|
18657
|
+
const { Err: E } = DER;
|
|
18658
|
+
if (tag < 0 || tag > 256)
|
|
18659
|
+
throw new E("tlv.encode: wrong tag");
|
|
18660
|
+
if (data.length & 1)
|
|
18661
|
+
throw new E("tlv.encode: unpadded data");
|
|
18662
|
+
const dataLen = data.length / 2;
|
|
18663
|
+
const len = numberToHexUnpadded(dataLen);
|
|
18664
|
+
if (len.length / 2 & 128)
|
|
18665
|
+
throw new E("tlv.encode: long form length too big");
|
|
18666
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
18667
|
+
const t = numberToHexUnpadded(tag);
|
|
18668
|
+
return t + lenLen + len + data;
|
|
18669
|
+
},
|
|
18670
|
+
// v - value, l - left bytes (unparsed)
|
|
18671
|
+
decode(tag, data) {
|
|
18672
|
+
const { Err: E } = DER;
|
|
18673
|
+
let pos = 0;
|
|
18674
|
+
if (tag < 0 || tag > 256)
|
|
18675
|
+
throw new E("tlv.encode: wrong tag");
|
|
18676
|
+
if (data.length < 2 || data[pos++] !== tag)
|
|
18677
|
+
throw new E("tlv.decode: wrong tlv");
|
|
18678
|
+
const first = data[pos++];
|
|
18679
|
+
const isLong = !!(first & 128);
|
|
18680
|
+
let length2 = 0;
|
|
18681
|
+
if (!isLong)
|
|
18682
|
+
length2 = first;
|
|
18683
|
+
else {
|
|
18684
|
+
const lenLen = first & 127;
|
|
18685
|
+
if (!lenLen)
|
|
18686
|
+
throw new E("tlv.decode(long): indefinite length not supported");
|
|
18687
|
+
if (lenLen > 4)
|
|
18688
|
+
throw new E("tlv.decode(long): byte length is too big");
|
|
18689
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
18690
|
+
if (lengthBytes.length !== lenLen)
|
|
18691
|
+
throw new E("tlv.decode: length bytes not complete");
|
|
18692
|
+
if (lengthBytes[0] === 0)
|
|
18693
|
+
throw new E("tlv.decode(long): zero leftmost byte");
|
|
18694
|
+
for (const b of lengthBytes)
|
|
18695
|
+
length2 = length2 << 8 | b;
|
|
18696
|
+
pos += lenLen;
|
|
18697
|
+
if (length2 < 128)
|
|
18698
|
+
throw new E("tlv.decode(long): not minimal encoding");
|
|
18699
|
+
}
|
|
18700
|
+
const v = data.subarray(pos, pos + length2);
|
|
18701
|
+
if (v.length !== length2)
|
|
18702
|
+
throw new E("tlv.decode: wrong value length");
|
|
18703
|
+
return { v, l: data.subarray(pos + length2) };
|
|
18704
|
+
}
|
|
18705
|
+
},
|
|
18706
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
18707
|
+
// since we always use positive integers here. It must always be empty:
|
|
18708
|
+
// - add zero byte if exists
|
|
18709
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
18710
|
+
_int: {
|
|
18711
|
+
encode(num) {
|
|
18712
|
+
const { Err: E } = DER;
|
|
18713
|
+
if (num < _0n6)
|
|
18714
|
+
throw new E("integer: negative integers are not allowed");
|
|
18715
|
+
let hex = numberToHexUnpadded(num);
|
|
18716
|
+
if (Number.parseInt(hex[0], 16) & 8)
|
|
18717
|
+
hex = "00" + hex;
|
|
18718
|
+
if (hex.length & 1)
|
|
18719
|
+
throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
18720
|
+
return hex;
|
|
18721
|
+
},
|
|
18722
|
+
decode(data) {
|
|
18723
|
+
const { Err: E } = DER;
|
|
18724
|
+
if (data[0] & 128)
|
|
18725
|
+
throw new E("invalid signature integer: negative");
|
|
18726
|
+
if (data[0] === 0 && !(data[1] & 128))
|
|
18727
|
+
throw new E("invalid signature integer: unnecessary leading zero");
|
|
18728
|
+
return bytesToNumberBE(data);
|
|
18729
|
+
}
|
|
18730
|
+
},
|
|
18731
|
+
toSig(hex) {
|
|
18732
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
18733
|
+
const data = ensureBytes("signature", hex);
|
|
18734
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
18735
|
+
if (seqLeftBytes.length)
|
|
18736
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
18737
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
18738
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
18739
|
+
if (sLeftBytes.length)
|
|
18740
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
18741
|
+
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
18742
|
+
},
|
|
18743
|
+
hexFromSig(sig) {
|
|
18744
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
18745
|
+
const rs = tlv.encode(2, int.encode(sig.r));
|
|
18746
|
+
const ss = tlv.encode(2, int.encode(sig.s));
|
|
18747
|
+
const seq = rs + ss;
|
|
18748
|
+
return tlv.encode(48, seq);
|
|
18749
|
+
}
|
|
18750
|
+
};
|
|
18751
|
+
var _0n6 = BigInt(0);
|
|
18752
|
+
var _1n6 = BigInt(1);
|
|
18753
|
+
var _2n4 = BigInt(2);
|
|
18754
|
+
var _3n3 = BigInt(3);
|
|
18755
|
+
var _4n2 = BigInt(4);
|
|
18756
|
+
function _normFnElement(Fn2, key) {
|
|
18757
|
+
const { BYTES: expected } = Fn2;
|
|
18758
|
+
let num;
|
|
18759
|
+
if (typeof key === "bigint") {
|
|
18760
|
+
num = key;
|
|
18761
|
+
} else {
|
|
18762
|
+
let bytes = ensureBytes("private key", key);
|
|
18763
|
+
try {
|
|
18764
|
+
num = Fn2.fromBytes(bytes);
|
|
18765
|
+
} catch (error) {
|
|
18766
|
+
throw new Error(`invalid private key: expected ui8a of size ${expected}, got ${typeof key}`);
|
|
18767
|
+
}
|
|
18768
|
+
}
|
|
18769
|
+
if (!Fn2.isValidNot0(num))
|
|
18770
|
+
throw new Error("invalid private key: out of range [1..N-1]");
|
|
18771
|
+
return num;
|
|
18772
|
+
}
|
|
18773
|
+
function weierstrassN(params, extraOpts = {}) {
|
|
18774
|
+
const validated = _createCurveFields("weierstrass", params, extraOpts);
|
|
18775
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
18776
|
+
let CURVE = validated.CURVE;
|
|
18777
|
+
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
18778
|
+
_validateObject(extraOpts, {}, {
|
|
18779
|
+
allowInfinityPoint: "boolean",
|
|
18780
|
+
clearCofactor: "function",
|
|
18781
|
+
isTorsionFree: "function",
|
|
18782
|
+
fromBytes: "function",
|
|
18783
|
+
toBytes: "function",
|
|
18784
|
+
endo: "object",
|
|
18785
|
+
wrapPrivateKey: "boolean"
|
|
18786
|
+
});
|
|
18787
|
+
const { endo } = extraOpts;
|
|
18788
|
+
if (endo) {
|
|
18789
|
+
if (!Fp2.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
|
|
18790
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
18791
|
+
}
|
|
18792
|
+
}
|
|
18793
|
+
const lengths = getWLengths(Fp2, Fn2);
|
|
18794
|
+
function assertCompressionIsSupported() {
|
|
18795
|
+
if (!Fp2.isOdd)
|
|
18796
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
18797
|
+
}
|
|
18798
|
+
function pointToBytes(_c, point, isCompressed) {
|
|
18799
|
+
const { x, y } = point.toAffine();
|
|
18800
|
+
const bx = Fp2.toBytes(x);
|
|
18801
|
+
_abool2(isCompressed, "isCompressed");
|
|
18802
|
+
if (isCompressed) {
|
|
18803
|
+
assertCompressionIsSupported();
|
|
18804
|
+
const hasEvenY = !Fp2.isOdd(y);
|
|
18805
|
+
return concatBytes(pprefix(hasEvenY), bx);
|
|
18806
|
+
} else {
|
|
18807
|
+
return concatBytes(Uint8Array.of(4), bx, Fp2.toBytes(y));
|
|
18808
|
+
}
|
|
18809
|
+
}
|
|
18810
|
+
function pointFromBytes(bytes) {
|
|
18811
|
+
_abytes2(bytes, void 0, "Point");
|
|
18812
|
+
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
18813
|
+
const length2 = bytes.length;
|
|
18814
|
+
const head = bytes[0];
|
|
18815
|
+
const tail2 = bytes.subarray(1);
|
|
18816
|
+
if (length2 === comp && (head === 2 || head === 3)) {
|
|
18817
|
+
const x = Fp2.fromBytes(tail2);
|
|
18818
|
+
if (!Fp2.isValid(x))
|
|
18819
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
18820
|
+
const y2 = weierstrassEquation(x);
|
|
18821
|
+
let y;
|
|
18822
|
+
try {
|
|
18823
|
+
y = Fp2.sqrt(y2);
|
|
18824
|
+
} catch (sqrtError) {
|
|
18825
|
+
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
18826
|
+
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
18827
|
+
}
|
|
18828
|
+
assertCompressionIsSupported();
|
|
18829
|
+
const isYOdd = Fp2.isOdd(y);
|
|
18830
|
+
const isHeadOdd = (head & 1) === 1;
|
|
18831
|
+
if (isHeadOdd !== isYOdd)
|
|
18832
|
+
y = Fp2.neg(y);
|
|
18833
|
+
return { x, y };
|
|
18834
|
+
} else if (length2 === uncomp && head === 4) {
|
|
18835
|
+
const L = Fp2.BYTES;
|
|
18836
|
+
const x = Fp2.fromBytes(tail2.subarray(0, L));
|
|
18837
|
+
const y = Fp2.fromBytes(tail2.subarray(L, L * 2));
|
|
18838
|
+
if (!isValidXY(x, y))
|
|
18839
|
+
throw new Error("bad point: is not on curve");
|
|
18840
|
+
return { x, y };
|
|
18841
|
+
} else {
|
|
18842
|
+
throw new Error(`bad point: got length ${length2}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
18843
|
+
}
|
|
18844
|
+
}
|
|
18845
|
+
const encodePoint = extraOpts.toBytes || pointToBytes;
|
|
18846
|
+
const decodePoint2 = extraOpts.fromBytes || pointFromBytes;
|
|
18847
|
+
function weierstrassEquation(x) {
|
|
18848
|
+
const x2 = Fp2.sqr(x);
|
|
18849
|
+
const x3 = Fp2.mul(x2, x);
|
|
18850
|
+
return Fp2.add(Fp2.add(x3, Fp2.mul(x, CURVE.a)), CURVE.b);
|
|
18851
|
+
}
|
|
18852
|
+
function isValidXY(x, y) {
|
|
18853
|
+
const left = Fp2.sqr(y);
|
|
18854
|
+
const right = weierstrassEquation(x);
|
|
18855
|
+
return Fp2.eql(left, right);
|
|
18856
|
+
}
|
|
18857
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
|
18858
|
+
throw new Error("bad curve params: generator point");
|
|
18859
|
+
const _4a3 = Fp2.mul(Fp2.pow(CURVE.a, _3n3), _4n2);
|
|
18860
|
+
const _27b2 = Fp2.mul(Fp2.sqr(CURVE.b), BigInt(27));
|
|
18861
|
+
if (Fp2.is0(Fp2.add(_4a3, _27b2)))
|
|
18862
|
+
throw new Error("bad curve params: a or b");
|
|
18863
|
+
function acoord(title, n, banZero = false) {
|
|
18864
|
+
if (!Fp2.isValid(n) || banZero && Fp2.is0(n))
|
|
18865
|
+
throw new Error(`bad point coordinate ${title}`);
|
|
18866
|
+
return n;
|
|
18867
|
+
}
|
|
18868
|
+
function aprjpoint(other) {
|
|
18869
|
+
if (!(other instanceof Point))
|
|
18870
|
+
throw new Error("ProjectivePoint expected");
|
|
18871
|
+
}
|
|
18872
|
+
function splitEndoScalarN(k2) {
|
|
18873
|
+
if (!endo || !endo.basises)
|
|
18874
|
+
throw new Error("no endo");
|
|
18875
|
+
return _splitEndoScalar(k2, endo.basises, Fn2.ORDER);
|
|
18876
|
+
}
|
|
18877
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
18878
|
+
const { X, Y, Z } = p;
|
|
18879
|
+
if (Fp2.eql(Z, Fp2.ONE))
|
|
18880
|
+
return { x: X, y: Y };
|
|
18881
|
+
const is0 = p.is0();
|
|
18882
|
+
if (iz == null)
|
|
18883
|
+
iz = is0 ? Fp2.ONE : Fp2.inv(Z);
|
|
18884
|
+
const x = Fp2.mul(X, iz);
|
|
18885
|
+
const y = Fp2.mul(Y, iz);
|
|
18886
|
+
const zz = Fp2.mul(Z, iz);
|
|
18887
|
+
if (is0)
|
|
18888
|
+
return { x: Fp2.ZERO, y: Fp2.ZERO };
|
|
18889
|
+
if (!Fp2.eql(zz, Fp2.ONE))
|
|
18890
|
+
throw new Error("invZ was invalid");
|
|
18891
|
+
return { x, y };
|
|
18892
|
+
});
|
|
18893
|
+
const assertValidMemo = memoized((p) => {
|
|
18894
|
+
if (p.is0()) {
|
|
18895
|
+
if (extraOpts.allowInfinityPoint && !Fp2.is0(p.Y))
|
|
18896
|
+
return;
|
|
18897
|
+
throw new Error("bad point: ZERO");
|
|
18898
|
+
}
|
|
18899
|
+
const { x, y } = p.toAffine();
|
|
18900
|
+
if (!Fp2.isValid(x) || !Fp2.isValid(y))
|
|
18901
|
+
throw new Error("bad point: x or y not field elements");
|
|
18902
|
+
if (!isValidXY(x, y))
|
|
18903
|
+
throw new Error("bad point: equation left != right");
|
|
18904
|
+
if (!p.isTorsionFree())
|
|
18905
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
18906
|
+
return true;
|
|
18907
|
+
});
|
|
18908
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
18909
|
+
k2p = new Point(Fp2.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
18910
|
+
k1p = negateCt(k1neg, k1p);
|
|
18911
|
+
k2p = negateCt(k2neg, k2p);
|
|
18912
|
+
return k1p.add(k2p);
|
|
18913
|
+
}
|
|
18914
|
+
class Point {
|
|
18915
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
18916
|
+
constructor(X, Y, Z) {
|
|
18917
|
+
this.X = acoord("x", X);
|
|
18918
|
+
this.Y = acoord("y", Y, true);
|
|
18919
|
+
this.Z = acoord("z", Z);
|
|
18920
|
+
Object.freeze(this);
|
|
18921
|
+
}
|
|
18922
|
+
static CURVE() {
|
|
18923
|
+
return CURVE;
|
|
18924
|
+
}
|
|
18925
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
18926
|
+
static fromAffine(p) {
|
|
18927
|
+
const { x, y } = p || {};
|
|
18928
|
+
if (!p || !Fp2.isValid(x) || !Fp2.isValid(y))
|
|
18929
|
+
throw new Error("invalid affine point");
|
|
18930
|
+
if (p instanceof Point)
|
|
18931
|
+
throw new Error("projective point not allowed");
|
|
18932
|
+
if (Fp2.is0(x) && Fp2.is0(y))
|
|
18933
|
+
return Point.ZERO;
|
|
18934
|
+
return new Point(x, y, Fp2.ONE);
|
|
18935
|
+
}
|
|
18936
|
+
static fromBytes(bytes) {
|
|
18937
|
+
const P = Point.fromAffine(decodePoint2(_abytes2(bytes, void 0, "point")));
|
|
18938
|
+
P.assertValidity();
|
|
18939
|
+
return P;
|
|
18940
|
+
}
|
|
18941
|
+
static fromHex(hex) {
|
|
18942
|
+
return Point.fromBytes(ensureBytes("pointHex", hex));
|
|
18943
|
+
}
|
|
18944
|
+
get x() {
|
|
18945
|
+
return this.toAffine().x;
|
|
18946
|
+
}
|
|
18947
|
+
get y() {
|
|
18948
|
+
return this.toAffine().y;
|
|
18949
|
+
}
|
|
18950
|
+
/**
|
|
18951
|
+
*
|
|
18952
|
+
* @param windowSize
|
|
18953
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
18954
|
+
* @returns
|
|
18955
|
+
*/
|
|
18956
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
18957
|
+
wnaf.createCache(this, windowSize);
|
|
18958
|
+
if (!isLazy)
|
|
18959
|
+
this.multiply(_3n3);
|
|
18960
|
+
return this;
|
|
18961
|
+
}
|
|
18962
|
+
// TODO: return `this`
|
|
18963
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
18964
|
+
assertValidity() {
|
|
18965
|
+
assertValidMemo(this);
|
|
18966
|
+
}
|
|
18967
|
+
hasEvenY() {
|
|
18968
|
+
const { y } = this.toAffine();
|
|
18969
|
+
if (!Fp2.isOdd)
|
|
18970
|
+
throw new Error("Field doesn't support isOdd");
|
|
18971
|
+
return !Fp2.isOdd(y);
|
|
18972
|
+
}
|
|
18973
|
+
/** Compare one point to another. */
|
|
18974
|
+
equals(other) {
|
|
18975
|
+
aprjpoint(other);
|
|
18976
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
18977
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
18978
|
+
const U1 = Fp2.eql(Fp2.mul(X1, Z2), Fp2.mul(X2, Z1));
|
|
18979
|
+
const U2 = Fp2.eql(Fp2.mul(Y1, Z2), Fp2.mul(Y2, Z1));
|
|
18980
|
+
return U1 && U2;
|
|
18981
|
+
}
|
|
18982
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
18983
|
+
negate() {
|
|
18984
|
+
return new Point(this.X, Fp2.neg(this.Y), this.Z);
|
|
18985
|
+
}
|
|
18986
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
18987
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
18988
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
18989
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
18990
|
+
double() {
|
|
18991
|
+
const { a, b } = CURVE;
|
|
18992
|
+
const b3 = Fp2.mul(b, _3n3);
|
|
18993
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
18994
|
+
let X3 = Fp2.ZERO, Y3 = Fp2.ZERO, Z3 = Fp2.ZERO;
|
|
18995
|
+
let t0 = Fp2.mul(X1, X1);
|
|
18996
|
+
let t1 = Fp2.mul(Y1, Y1);
|
|
18997
|
+
let t2 = Fp2.mul(Z1, Z1);
|
|
18998
|
+
let t3 = Fp2.mul(X1, Y1);
|
|
18999
|
+
t3 = Fp2.add(t3, t3);
|
|
19000
|
+
Z3 = Fp2.mul(X1, Z1);
|
|
19001
|
+
Z3 = Fp2.add(Z3, Z3);
|
|
19002
|
+
X3 = Fp2.mul(a, Z3);
|
|
19003
|
+
Y3 = Fp2.mul(b3, t2);
|
|
19004
|
+
Y3 = Fp2.add(X3, Y3);
|
|
19005
|
+
X3 = Fp2.sub(t1, Y3);
|
|
19006
|
+
Y3 = Fp2.add(t1, Y3);
|
|
19007
|
+
Y3 = Fp2.mul(X3, Y3);
|
|
19008
|
+
X3 = Fp2.mul(t3, X3);
|
|
19009
|
+
Z3 = Fp2.mul(b3, Z3);
|
|
19010
|
+
t2 = Fp2.mul(a, t2);
|
|
19011
|
+
t3 = Fp2.sub(t0, t2);
|
|
19012
|
+
t3 = Fp2.mul(a, t3);
|
|
19013
|
+
t3 = Fp2.add(t3, Z3);
|
|
19014
|
+
Z3 = Fp2.add(t0, t0);
|
|
19015
|
+
t0 = Fp2.add(Z3, t0);
|
|
19016
|
+
t0 = Fp2.add(t0, t2);
|
|
19017
|
+
t0 = Fp2.mul(t0, t3);
|
|
19018
|
+
Y3 = Fp2.add(Y3, t0);
|
|
19019
|
+
t2 = Fp2.mul(Y1, Z1);
|
|
19020
|
+
t2 = Fp2.add(t2, t2);
|
|
19021
|
+
t0 = Fp2.mul(t2, t3);
|
|
19022
|
+
X3 = Fp2.sub(X3, t0);
|
|
19023
|
+
Z3 = Fp2.mul(t2, t1);
|
|
19024
|
+
Z3 = Fp2.add(Z3, Z3);
|
|
19025
|
+
Z3 = Fp2.add(Z3, Z3);
|
|
19026
|
+
return new Point(X3, Y3, Z3);
|
|
19027
|
+
}
|
|
19028
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
19029
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
19030
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
19031
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
19032
|
+
add(other) {
|
|
19033
|
+
aprjpoint(other);
|
|
19034
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
19035
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
19036
|
+
let X3 = Fp2.ZERO, Y3 = Fp2.ZERO, Z3 = Fp2.ZERO;
|
|
19037
|
+
const a = CURVE.a;
|
|
19038
|
+
const b3 = Fp2.mul(CURVE.b, _3n3);
|
|
19039
|
+
let t0 = Fp2.mul(X1, X2);
|
|
19040
|
+
let t1 = Fp2.mul(Y1, Y2);
|
|
19041
|
+
let t2 = Fp2.mul(Z1, Z2);
|
|
19042
|
+
let t3 = Fp2.add(X1, Y1);
|
|
19043
|
+
let t4 = Fp2.add(X2, Y2);
|
|
19044
|
+
t3 = Fp2.mul(t3, t4);
|
|
19045
|
+
t4 = Fp2.add(t0, t1);
|
|
19046
|
+
t3 = Fp2.sub(t3, t4);
|
|
19047
|
+
t4 = Fp2.add(X1, Z1);
|
|
19048
|
+
let t5 = Fp2.add(X2, Z2);
|
|
19049
|
+
t4 = Fp2.mul(t4, t5);
|
|
19050
|
+
t5 = Fp2.add(t0, t2);
|
|
19051
|
+
t4 = Fp2.sub(t4, t5);
|
|
19052
|
+
t5 = Fp2.add(Y1, Z1);
|
|
19053
|
+
X3 = Fp2.add(Y2, Z2);
|
|
19054
|
+
t5 = Fp2.mul(t5, X3);
|
|
19055
|
+
X3 = Fp2.add(t1, t2);
|
|
19056
|
+
t5 = Fp2.sub(t5, X3);
|
|
19057
|
+
Z3 = Fp2.mul(a, t4);
|
|
19058
|
+
X3 = Fp2.mul(b3, t2);
|
|
19059
|
+
Z3 = Fp2.add(X3, Z3);
|
|
19060
|
+
X3 = Fp2.sub(t1, Z3);
|
|
19061
|
+
Z3 = Fp2.add(t1, Z3);
|
|
19062
|
+
Y3 = Fp2.mul(X3, Z3);
|
|
19063
|
+
t1 = Fp2.add(t0, t0);
|
|
19064
|
+
t1 = Fp2.add(t1, t0);
|
|
19065
|
+
t2 = Fp2.mul(a, t2);
|
|
19066
|
+
t4 = Fp2.mul(b3, t4);
|
|
19067
|
+
t1 = Fp2.add(t1, t2);
|
|
19068
|
+
t2 = Fp2.sub(t0, t2);
|
|
19069
|
+
t2 = Fp2.mul(a, t2);
|
|
19070
|
+
t4 = Fp2.add(t4, t2);
|
|
19071
|
+
t0 = Fp2.mul(t1, t4);
|
|
19072
|
+
Y3 = Fp2.add(Y3, t0);
|
|
19073
|
+
t0 = Fp2.mul(t5, t4);
|
|
19074
|
+
X3 = Fp2.mul(t3, X3);
|
|
19075
|
+
X3 = Fp2.sub(X3, t0);
|
|
19076
|
+
t0 = Fp2.mul(t3, t1);
|
|
19077
|
+
Z3 = Fp2.mul(t5, Z3);
|
|
19078
|
+
Z3 = Fp2.add(Z3, t0);
|
|
19079
|
+
return new Point(X3, Y3, Z3);
|
|
19080
|
+
}
|
|
19081
|
+
subtract(other) {
|
|
19082
|
+
return this.add(other.negate());
|
|
19083
|
+
}
|
|
19084
|
+
is0() {
|
|
19085
|
+
return this.equals(Point.ZERO);
|
|
19086
|
+
}
|
|
19087
|
+
/**
|
|
19088
|
+
* Constant time multiplication.
|
|
19089
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
19090
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
19091
|
+
* Uses precomputes when available.
|
|
19092
|
+
* Uses endomorphism for Koblitz curves.
|
|
19093
|
+
* @param scalar by which the point would be multiplied
|
|
19094
|
+
* @returns New point
|
|
19095
|
+
*/
|
|
19096
|
+
multiply(scalar) {
|
|
19097
|
+
const { endo: endo2 } = extraOpts;
|
|
19098
|
+
if (!Fn2.isValidNot0(scalar))
|
|
19099
|
+
throw new Error("invalid scalar: out of range");
|
|
19100
|
+
let point, fake;
|
|
19101
|
+
const mul = (n) => wnaf.cached(this, n, (p) => normalizeZ(Point, p));
|
|
19102
|
+
if (endo2) {
|
|
19103
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
|
|
19104
|
+
const { p: k1p, f: k1f } = mul(k1);
|
|
19105
|
+
const { p: k2p, f: k2f } = mul(k2);
|
|
19106
|
+
fake = k1f.add(k2f);
|
|
19107
|
+
point = finishEndo(endo2.beta, k1p, k2p, k1neg, k2neg);
|
|
19108
|
+
} else {
|
|
19109
|
+
const { p, f } = mul(scalar);
|
|
19110
|
+
point = p;
|
|
19111
|
+
fake = f;
|
|
19112
|
+
}
|
|
19113
|
+
return normalizeZ(Point, [point, fake])[0];
|
|
19114
|
+
}
|
|
19115
|
+
/**
|
|
19116
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
19117
|
+
* It's faster, but should only be used when you don't care about
|
|
19118
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
19119
|
+
*/
|
|
19120
|
+
multiplyUnsafe(sc) {
|
|
19121
|
+
const { endo: endo2 } = extraOpts;
|
|
19122
|
+
const p = this;
|
|
19123
|
+
if (!Fn2.isValid(sc))
|
|
19124
|
+
throw new Error("invalid scalar: out of range");
|
|
19125
|
+
if (sc === _0n6 || p.is0())
|
|
19126
|
+
return Point.ZERO;
|
|
19127
|
+
if (sc === _1n6)
|
|
19128
|
+
return p;
|
|
19129
|
+
if (wnaf.hasCache(this))
|
|
19130
|
+
return this.multiply(sc);
|
|
19131
|
+
if (endo2) {
|
|
19132
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
|
|
19133
|
+
const { p1, p2 } = mulEndoUnsafe(Point, p, k1, k2);
|
|
19134
|
+
return finishEndo(endo2.beta, p1, p2, k1neg, k2neg);
|
|
19135
|
+
} else {
|
|
19136
|
+
return wnaf.unsafe(p, sc);
|
|
19137
|
+
}
|
|
19138
|
+
}
|
|
19139
|
+
multiplyAndAddUnsafe(Q, a, b) {
|
|
19140
|
+
const sum = this.multiplyUnsafe(a).add(Q.multiplyUnsafe(b));
|
|
19141
|
+
return sum.is0() ? void 0 : sum;
|
|
19142
|
+
}
|
|
19143
|
+
/**
|
|
19144
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
19145
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
19146
|
+
*/
|
|
19147
|
+
toAffine(invertedZ) {
|
|
19148
|
+
return toAffineMemo(this, invertedZ);
|
|
19149
|
+
}
|
|
19150
|
+
/**
|
|
19151
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
19152
|
+
* Always torsion-free for cofactor=1 curves.
|
|
19153
|
+
*/
|
|
19154
|
+
isTorsionFree() {
|
|
19155
|
+
const { isTorsionFree } = extraOpts;
|
|
19156
|
+
if (cofactor === _1n6)
|
|
19157
|
+
return true;
|
|
19158
|
+
if (isTorsionFree)
|
|
19159
|
+
return isTorsionFree(Point, this);
|
|
19160
|
+
return wnaf.unsafe(this, CURVE_ORDER).is0();
|
|
19161
|
+
}
|
|
19162
|
+
clearCofactor() {
|
|
19163
|
+
const { clearCofactor } = extraOpts;
|
|
19164
|
+
if (cofactor === _1n6)
|
|
19165
|
+
return this;
|
|
19166
|
+
if (clearCofactor)
|
|
19167
|
+
return clearCofactor(Point, this);
|
|
19168
|
+
return this.multiplyUnsafe(cofactor);
|
|
19169
|
+
}
|
|
19170
|
+
isSmallOrder() {
|
|
19171
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
19172
|
+
}
|
|
19173
|
+
toBytes(isCompressed = true) {
|
|
19174
|
+
_abool2(isCompressed, "isCompressed");
|
|
19175
|
+
this.assertValidity();
|
|
19176
|
+
return encodePoint(Point, this, isCompressed);
|
|
19177
|
+
}
|
|
19178
|
+
toHex(isCompressed = true) {
|
|
19179
|
+
return bytesToHex(this.toBytes(isCompressed));
|
|
19180
|
+
}
|
|
19181
|
+
toString() {
|
|
19182
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
19183
|
+
}
|
|
19184
|
+
// TODO: remove
|
|
19185
|
+
get px() {
|
|
19186
|
+
return this.X;
|
|
19187
|
+
}
|
|
19188
|
+
get py() {
|
|
19189
|
+
return this.X;
|
|
19190
|
+
}
|
|
19191
|
+
get pz() {
|
|
19192
|
+
return this.Z;
|
|
19193
|
+
}
|
|
19194
|
+
toRawBytes(isCompressed = true) {
|
|
19195
|
+
return this.toBytes(isCompressed);
|
|
19196
|
+
}
|
|
19197
|
+
_setWindowSize(windowSize) {
|
|
19198
|
+
this.precompute(windowSize);
|
|
19199
|
+
}
|
|
19200
|
+
static normalizeZ(points) {
|
|
19201
|
+
return normalizeZ(Point, points);
|
|
19202
|
+
}
|
|
19203
|
+
static msm(points, scalars) {
|
|
19204
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
19205
|
+
}
|
|
19206
|
+
static fromPrivateKey(privateKey) {
|
|
19207
|
+
return Point.BASE.multiply(_normFnElement(Fn2, privateKey));
|
|
19208
|
+
}
|
|
19209
|
+
}
|
|
19210
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp2.ONE);
|
|
19211
|
+
Point.ZERO = new Point(Fp2.ZERO, Fp2.ONE, Fp2.ZERO);
|
|
19212
|
+
Point.Fp = Fp2;
|
|
19213
|
+
Point.Fn = Fn2;
|
|
19214
|
+
const bits = Fn2.BITS;
|
|
19215
|
+
const wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
|
|
19216
|
+
Point.BASE.precompute(8);
|
|
19217
|
+
return Point;
|
|
19218
|
+
}
|
|
19219
|
+
function pprefix(hasEvenY) {
|
|
19220
|
+
return Uint8Array.of(hasEvenY ? 2 : 3);
|
|
19221
|
+
}
|
|
19222
|
+
function getWLengths(Fp2, Fn2) {
|
|
19223
|
+
return {
|
|
19224
|
+
secretKey: Fn2.BYTES,
|
|
19225
|
+
publicKey: 1 + Fp2.BYTES,
|
|
19226
|
+
publicKeyUncompressed: 1 + 2 * Fp2.BYTES,
|
|
19227
|
+
publicKeyHasPrefix: true,
|
|
19228
|
+
signature: 2 * Fn2.BYTES
|
|
19229
|
+
};
|
|
19230
|
+
}
|
|
19231
|
+
function ecdh(Point, ecdhOpts = {}) {
|
|
19232
|
+
const { Fn: Fn2 } = Point;
|
|
19233
|
+
const randomBytes_ = ecdhOpts.randomBytes || randomBytes6;
|
|
19234
|
+
const lengths = Object.assign(getWLengths(Point.Fp, Fn2), { seed: getMinHashLength(Fn2.ORDER) });
|
|
19235
|
+
function isValidSecretKey(secretKey) {
|
|
19236
|
+
try {
|
|
19237
|
+
return !!_normFnElement(Fn2, secretKey);
|
|
19238
|
+
} catch (error) {
|
|
19239
|
+
return false;
|
|
19240
|
+
}
|
|
19241
|
+
}
|
|
19242
|
+
function isValidPublicKey(publicKey, isCompressed) {
|
|
19243
|
+
const { publicKey: comp, publicKeyUncompressed } = lengths;
|
|
19244
|
+
try {
|
|
19245
|
+
const l = publicKey.length;
|
|
19246
|
+
if (isCompressed === true && l !== comp)
|
|
19247
|
+
return false;
|
|
19248
|
+
if (isCompressed === false && l !== publicKeyUncompressed)
|
|
19249
|
+
return false;
|
|
19250
|
+
return !!Point.fromBytes(publicKey);
|
|
19251
|
+
} catch (error) {
|
|
19252
|
+
return false;
|
|
19253
|
+
}
|
|
19254
|
+
}
|
|
19255
|
+
function randomSecretKey(seed = randomBytes_(lengths.seed)) {
|
|
19256
|
+
return mapHashToField(_abytes2(seed, lengths.seed, "seed"), Fn2.ORDER);
|
|
19257
|
+
}
|
|
19258
|
+
function getPublicKey(secretKey, isCompressed = true) {
|
|
19259
|
+
return Point.BASE.multiply(_normFnElement(Fn2, secretKey)).toBytes(isCompressed);
|
|
19260
|
+
}
|
|
19261
|
+
function keygen(seed) {
|
|
19262
|
+
const secretKey = randomSecretKey(seed);
|
|
19263
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
19264
|
+
}
|
|
19265
|
+
function isProbPub(item) {
|
|
19266
|
+
if (typeof item === "bigint")
|
|
19267
|
+
return false;
|
|
19268
|
+
if (item instanceof Point)
|
|
19269
|
+
return true;
|
|
19270
|
+
const { secretKey, publicKey, publicKeyUncompressed } = lengths;
|
|
19271
|
+
if (Fn2.allowedLengths || secretKey === publicKey)
|
|
19272
|
+
return void 0;
|
|
19273
|
+
const l = ensureBytes("key", item).length;
|
|
19274
|
+
return l === publicKey || l === publicKeyUncompressed;
|
|
19275
|
+
}
|
|
19276
|
+
function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
|
|
19277
|
+
if (isProbPub(secretKeyA) === true)
|
|
19278
|
+
throw new Error("first arg must be private key");
|
|
19279
|
+
if (isProbPub(publicKeyB) === false)
|
|
19280
|
+
throw new Error("second arg must be public key");
|
|
19281
|
+
const s = _normFnElement(Fn2, secretKeyA);
|
|
19282
|
+
const b = Point.fromHex(publicKeyB);
|
|
19283
|
+
return b.multiply(s).toBytes(isCompressed);
|
|
19284
|
+
}
|
|
19285
|
+
const utils = {
|
|
19286
|
+
isValidSecretKey,
|
|
19287
|
+
isValidPublicKey,
|
|
19288
|
+
randomSecretKey,
|
|
19289
|
+
// TODO: remove
|
|
19290
|
+
isValidPrivateKey: isValidSecretKey,
|
|
19291
|
+
randomPrivateKey: randomSecretKey,
|
|
19292
|
+
normPrivateKeyToScalar: (key) => _normFnElement(Fn2, key),
|
|
19293
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
19294
|
+
return point.precompute(windowSize, false);
|
|
19295
|
+
}
|
|
19296
|
+
};
|
|
19297
|
+
return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
|
|
19298
|
+
}
|
|
19299
|
+
function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
19300
|
+
ahash(hash);
|
|
19301
|
+
_validateObject(ecdsaOpts, {}, {
|
|
19302
|
+
hmac: "function",
|
|
19303
|
+
lowS: "boolean",
|
|
19304
|
+
randomBytes: "function",
|
|
19305
|
+
bits2int: "function",
|
|
19306
|
+
bits2int_modN: "function"
|
|
19307
|
+
});
|
|
19308
|
+
const randomBytes7 = ecdsaOpts.randomBytes || randomBytes6;
|
|
19309
|
+
const hmac2 = ecdsaOpts.hmac || ((key, ...msgs) => hmac(hash, key, concatBytes(...msgs)));
|
|
19310
|
+
const { Fp: Fp2, Fn: Fn2 } = Point;
|
|
19311
|
+
const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn2;
|
|
19312
|
+
const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
|
|
19313
|
+
const defaultSigOpts = {
|
|
19314
|
+
prehash: false,
|
|
19315
|
+
lowS: typeof ecdsaOpts.lowS === "boolean" ? ecdsaOpts.lowS : false,
|
|
19316
|
+
format: void 0,
|
|
19317
|
+
//'compact' as ECDSASigFormat,
|
|
19318
|
+
extraEntropy: false
|
|
19319
|
+
};
|
|
19320
|
+
const defaultSigOpts_format = "compact";
|
|
19321
|
+
function isBiggerThanHalfOrder(number) {
|
|
19322
|
+
const HALF = CURVE_ORDER >> _1n6;
|
|
19323
|
+
return number > HALF;
|
|
19324
|
+
}
|
|
19325
|
+
function validateRS(title, num) {
|
|
19326
|
+
if (!Fn2.isValidNot0(num))
|
|
19327
|
+
throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
|
|
19328
|
+
return num;
|
|
19329
|
+
}
|
|
19330
|
+
function validateSigLength(bytes, format2) {
|
|
19331
|
+
validateSigFormat(format2);
|
|
19332
|
+
const size = lengths.signature;
|
|
19333
|
+
const sizer = format2 === "compact" ? size : format2 === "recovered" ? size + 1 : void 0;
|
|
19334
|
+
return _abytes2(bytes, sizer, `${format2} signature`);
|
|
19335
|
+
}
|
|
19336
|
+
class Signature {
|
|
19337
|
+
constructor(r, s, recovery) {
|
|
19338
|
+
this.r = validateRS("r", r);
|
|
19339
|
+
this.s = validateRS("s", s);
|
|
19340
|
+
if (recovery != null)
|
|
19341
|
+
this.recovery = recovery;
|
|
19342
|
+
Object.freeze(this);
|
|
19343
|
+
}
|
|
19344
|
+
static fromBytes(bytes, format2 = defaultSigOpts_format) {
|
|
19345
|
+
validateSigLength(bytes, format2);
|
|
19346
|
+
let recid;
|
|
19347
|
+
if (format2 === "der") {
|
|
19348
|
+
const { r: r2, s: s2 } = DER.toSig(_abytes2(bytes));
|
|
19349
|
+
return new Signature(r2, s2);
|
|
19350
|
+
}
|
|
19351
|
+
if (format2 === "recovered") {
|
|
19352
|
+
recid = bytes[0];
|
|
19353
|
+
format2 = "compact";
|
|
19354
|
+
bytes = bytes.subarray(1);
|
|
19355
|
+
}
|
|
19356
|
+
const L = Fn2.BYTES;
|
|
19357
|
+
const r = bytes.subarray(0, L);
|
|
19358
|
+
const s = bytes.subarray(L, L * 2);
|
|
19359
|
+
return new Signature(Fn2.fromBytes(r), Fn2.fromBytes(s), recid);
|
|
19360
|
+
}
|
|
19361
|
+
static fromHex(hex, format2) {
|
|
19362
|
+
return this.fromBytes(hexToBytes(hex), format2);
|
|
19363
|
+
}
|
|
19364
|
+
addRecoveryBit(recovery) {
|
|
19365
|
+
return new Signature(this.r, this.s, recovery);
|
|
19366
|
+
}
|
|
19367
|
+
recoverPublicKey(messageHash) {
|
|
19368
|
+
const FIELD_ORDER = Fp2.ORDER;
|
|
19369
|
+
const { r, s, recovery: rec } = this;
|
|
19370
|
+
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
|
19371
|
+
throw new Error("recovery id invalid");
|
|
19372
|
+
const hasCofactor = CURVE_ORDER * _2n4 < FIELD_ORDER;
|
|
19373
|
+
if (hasCofactor && rec > 1)
|
|
19374
|
+
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
19375
|
+
const radj = rec === 2 || rec === 3 ? r + CURVE_ORDER : r;
|
|
19376
|
+
if (!Fp2.isValid(radj))
|
|
19377
|
+
throw new Error("recovery id 2 or 3 invalid");
|
|
19378
|
+
const x = Fp2.toBytes(radj);
|
|
19379
|
+
const R = Point.fromBytes(concatBytes(pprefix((rec & 1) === 0), x));
|
|
19380
|
+
const ir = Fn2.inv(radj);
|
|
19381
|
+
const h = bits2int_modN(ensureBytes("msgHash", messageHash));
|
|
19382
|
+
const u1 = Fn2.create(-h * ir);
|
|
19383
|
+
const u2 = Fn2.create(s * ir);
|
|
19384
|
+
const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
|
|
19385
|
+
if (Q.is0())
|
|
19386
|
+
throw new Error("point at infinify");
|
|
19387
|
+
Q.assertValidity();
|
|
19388
|
+
return Q;
|
|
19389
|
+
}
|
|
19390
|
+
// Signatures should be low-s, to prevent malleability.
|
|
19391
|
+
hasHighS() {
|
|
19392
|
+
return isBiggerThanHalfOrder(this.s);
|
|
19393
|
+
}
|
|
19394
|
+
toBytes(format2 = defaultSigOpts_format) {
|
|
19395
|
+
validateSigFormat(format2);
|
|
19396
|
+
if (format2 === "der")
|
|
19397
|
+
return hexToBytes(DER.hexFromSig(this));
|
|
19398
|
+
const r = Fn2.toBytes(this.r);
|
|
19399
|
+
const s = Fn2.toBytes(this.s);
|
|
19400
|
+
if (format2 === "recovered") {
|
|
19401
|
+
if (this.recovery == null)
|
|
19402
|
+
throw new Error("recovery bit must be present");
|
|
19403
|
+
return concatBytes(Uint8Array.of(this.recovery), r, s);
|
|
19404
|
+
}
|
|
19405
|
+
return concatBytes(r, s);
|
|
19406
|
+
}
|
|
19407
|
+
toHex(format2) {
|
|
19408
|
+
return bytesToHex(this.toBytes(format2));
|
|
19409
|
+
}
|
|
19410
|
+
// TODO: remove
|
|
19411
|
+
assertValidity() {
|
|
19412
|
+
}
|
|
19413
|
+
static fromCompact(hex) {
|
|
19414
|
+
return Signature.fromBytes(ensureBytes("sig", hex), "compact");
|
|
19415
|
+
}
|
|
19416
|
+
static fromDER(hex) {
|
|
19417
|
+
return Signature.fromBytes(ensureBytes("sig", hex), "der");
|
|
19418
|
+
}
|
|
19419
|
+
normalizeS() {
|
|
19420
|
+
return this.hasHighS() ? new Signature(this.r, Fn2.neg(this.s), this.recovery) : this;
|
|
19421
|
+
}
|
|
19422
|
+
toDERRawBytes() {
|
|
19423
|
+
return this.toBytes("der");
|
|
19424
|
+
}
|
|
19425
|
+
toDERHex() {
|
|
19426
|
+
return bytesToHex(this.toBytes("der"));
|
|
19427
|
+
}
|
|
19428
|
+
toCompactRawBytes() {
|
|
19429
|
+
return this.toBytes("compact");
|
|
19430
|
+
}
|
|
19431
|
+
toCompactHex() {
|
|
19432
|
+
return bytesToHex(this.toBytes("compact"));
|
|
19433
|
+
}
|
|
19434
|
+
}
|
|
19435
|
+
const bits2int = ecdsaOpts.bits2int || function bits2int_def(bytes) {
|
|
19436
|
+
if (bytes.length > 8192)
|
|
19437
|
+
throw new Error("input is too large");
|
|
19438
|
+
const num = bytesToNumberBE(bytes);
|
|
19439
|
+
const delta = bytes.length * 8 - fnBits;
|
|
19440
|
+
return delta > 0 ? num >> BigInt(delta) : num;
|
|
19441
|
+
};
|
|
19442
|
+
const bits2int_modN = ecdsaOpts.bits2int_modN || function bits2int_modN_def(bytes) {
|
|
19443
|
+
return Fn2.create(bits2int(bytes));
|
|
19444
|
+
};
|
|
19445
|
+
const ORDER_MASK = bitMask(fnBits);
|
|
19446
|
+
function int2octets(num) {
|
|
19447
|
+
aInRange("num < 2^" + fnBits, num, _0n6, ORDER_MASK);
|
|
19448
|
+
return Fn2.toBytes(num);
|
|
19449
|
+
}
|
|
19450
|
+
function validateMsgAndHash(message, prehash) {
|
|
19451
|
+
_abytes2(message, void 0, "message");
|
|
19452
|
+
return prehash ? _abytes2(hash(message), void 0, "prehashed message") : message;
|
|
19453
|
+
}
|
|
19454
|
+
function prepSig(message, privateKey, opts) {
|
|
19455
|
+
if (["recovered", "canonical"].some((k2) => k2 in opts))
|
|
19456
|
+
throw new Error("sign() legacy options not supported");
|
|
19457
|
+
const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
|
|
19458
|
+
message = validateMsgAndHash(message, prehash);
|
|
19459
|
+
const h1int = bits2int_modN(message);
|
|
19460
|
+
const d = _normFnElement(Fn2, privateKey);
|
|
19461
|
+
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
19462
|
+
if (extraEntropy != null && extraEntropy !== false) {
|
|
19463
|
+
const e = extraEntropy === true ? randomBytes7(lengths.secretKey) : extraEntropy;
|
|
19464
|
+
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
19465
|
+
}
|
|
19466
|
+
const seed = concatBytes(...seedArgs);
|
|
19467
|
+
const m = h1int;
|
|
19468
|
+
function k2sig(kBytes) {
|
|
19469
|
+
const k2 = bits2int(kBytes);
|
|
19470
|
+
if (!Fn2.isValidNot0(k2))
|
|
19471
|
+
return;
|
|
19472
|
+
const ik = Fn2.inv(k2);
|
|
19473
|
+
const q = Point.BASE.multiply(k2).toAffine();
|
|
19474
|
+
const r = Fn2.create(q.x);
|
|
19475
|
+
if (r === _0n6)
|
|
19476
|
+
return;
|
|
19477
|
+
const s = Fn2.create(ik * Fn2.create(m + r * d));
|
|
19478
|
+
if (s === _0n6)
|
|
19479
|
+
return;
|
|
19480
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n6);
|
|
19481
|
+
let normS = s;
|
|
19482
|
+
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
19483
|
+
normS = Fn2.neg(s);
|
|
19484
|
+
recovery ^= 1;
|
|
19485
|
+
}
|
|
19486
|
+
return new Signature(r, normS, recovery);
|
|
19487
|
+
}
|
|
19488
|
+
return { seed, k2sig };
|
|
19489
|
+
}
|
|
19490
|
+
function sign2(message, secretKey, opts = {}) {
|
|
19491
|
+
message = ensureBytes("message", message);
|
|
19492
|
+
const { seed, k2sig } = prepSig(message, secretKey, opts);
|
|
19493
|
+
const drbg = createHmacDrbg(hash.outputLen, Fn2.BYTES, hmac2);
|
|
19494
|
+
const sig = drbg(seed, k2sig);
|
|
19495
|
+
return sig;
|
|
19496
|
+
}
|
|
19497
|
+
function tryParsingSig(sg) {
|
|
19498
|
+
let sig = void 0;
|
|
19499
|
+
const isHex = typeof sg === "string" || isBytes(sg);
|
|
19500
|
+
const isObj = !isHex && sg !== null && typeof sg === "object" && typeof sg.r === "bigint" && typeof sg.s === "bigint";
|
|
19501
|
+
if (!isHex && !isObj)
|
|
19502
|
+
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
19503
|
+
if (isObj) {
|
|
19504
|
+
sig = new Signature(sg.r, sg.s);
|
|
19505
|
+
} else if (isHex) {
|
|
19506
|
+
try {
|
|
19507
|
+
sig = Signature.fromBytes(ensureBytes("sig", sg), "der");
|
|
19508
|
+
} catch (derError) {
|
|
19509
|
+
if (!(derError instanceof DER.Err))
|
|
19510
|
+
throw derError;
|
|
19511
|
+
}
|
|
19512
|
+
if (!sig) {
|
|
19513
|
+
try {
|
|
19514
|
+
sig = Signature.fromBytes(ensureBytes("sig", sg), "compact");
|
|
19515
|
+
} catch (error) {
|
|
19516
|
+
return false;
|
|
19517
|
+
}
|
|
19518
|
+
}
|
|
19519
|
+
}
|
|
19520
|
+
if (!sig)
|
|
19521
|
+
return false;
|
|
19522
|
+
return sig;
|
|
19523
|
+
}
|
|
19524
|
+
function verify2(signature, message, publicKey, opts = {}) {
|
|
19525
|
+
const { lowS, prehash, format: format2 } = validateSigOpts(opts, defaultSigOpts);
|
|
19526
|
+
publicKey = ensureBytes("publicKey", publicKey);
|
|
19527
|
+
message = validateMsgAndHash(ensureBytes("message", message), prehash);
|
|
19528
|
+
if ("strict" in opts)
|
|
19529
|
+
throw new Error("options.strict was renamed to lowS");
|
|
19530
|
+
const sig = format2 === void 0 ? tryParsingSig(signature) : Signature.fromBytes(ensureBytes("sig", signature), format2);
|
|
19531
|
+
if (sig === false)
|
|
19532
|
+
return false;
|
|
19533
|
+
try {
|
|
19534
|
+
const P = Point.fromBytes(publicKey);
|
|
19535
|
+
if (lowS && sig.hasHighS())
|
|
19536
|
+
return false;
|
|
19537
|
+
const { r, s } = sig;
|
|
19538
|
+
const h = bits2int_modN(message);
|
|
19539
|
+
const is = Fn2.inv(s);
|
|
19540
|
+
const u1 = Fn2.create(h * is);
|
|
19541
|
+
const u2 = Fn2.create(r * is);
|
|
19542
|
+
const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2));
|
|
19543
|
+
if (R.is0())
|
|
19544
|
+
return false;
|
|
19545
|
+
const v = Fn2.create(R.x);
|
|
19546
|
+
return v === r;
|
|
19547
|
+
} catch (e) {
|
|
19548
|
+
return false;
|
|
19549
|
+
}
|
|
19550
|
+
}
|
|
19551
|
+
function recoverPublicKey(signature, message, opts = {}) {
|
|
19552
|
+
const { prehash } = validateSigOpts(opts, defaultSigOpts);
|
|
19553
|
+
message = validateMsgAndHash(message, prehash);
|
|
19554
|
+
return Signature.fromBytes(signature, "recovered").recoverPublicKey(message).toBytes();
|
|
19555
|
+
}
|
|
19556
|
+
return Object.freeze({
|
|
19557
|
+
keygen,
|
|
19558
|
+
getPublicKey,
|
|
19559
|
+
getSharedSecret,
|
|
19560
|
+
utils,
|
|
19561
|
+
lengths,
|
|
19562
|
+
Point,
|
|
19563
|
+
sign: sign2,
|
|
19564
|
+
verify: verify2,
|
|
19565
|
+
recoverPublicKey,
|
|
19566
|
+
Signature,
|
|
19567
|
+
hash
|
|
19568
|
+
});
|
|
19569
|
+
}
|
|
19570
|
+
function _weierstrass_legacy_opts_to_new(c) {
|
|
19571
|
+
const CURVE = {
|
|
19572
|
+
a: c.a,
|
|
19573
|
+
b: c.b,
|
|
19574
|
+
p: c.Fp.ORDER,
|
|
19575
|
+
n: c.n,
|
|
19576
|
+
h: c.h,
|
|
19577
|
+
Gx: c.Gx,
|
|
19578
|
+
Gy: c.Gy
|
|
19579
|
+
};
|
|
19580
|
+
const Fp2 = c.Fp;
|
|
19581
|
+
let allowedLengths = c.allowedPrivateKeyLengths ? Array.from(new Set(c.allowedPrivateKeyLengths.map((l) => Math.ceil(l / 2)))) : void 0;
|
|
19582
|
+
const Fn2 = Field(CURVE.n, {
|
|
19583
|
+
BITS: c.nBitLength,
|
|
19584
|
+
allowedLengths,
|
|
19585
|
+
modFromBytes: c.wrapPrivateKey
|
|
19586
|
+
});
|
|
19587
|
+
const curveOpts = {
|
|
19588
|
+
Fp: Fp2,
|
|
19589
|
+
Fn: Fn2,
|
|
19590
|
+
allowInfinityPoint: c.allowInfinityPoint,
|
|
19591
|
+
endo: c.endo,
|
|
19592
|
+
isTorsionFree: c.isTorsionFree,
|
|
19593
|
+
clearCofactor: c.clearCofactor,
|
|
19594
|
+
fromBytes: c.fromBytes,
|
|
19595
|
+
toBytes: c.toBytes
|
|
19596
|
+
};
|
|
19597
|
+
return { CURVE, curveOpts };
|
|
19598
|
+
}
|
|
19599
|
+
function _ecdsa_legacy_opts_to_new(c) {
|
|
19600
|
+
const { CURVE, curveOpts } = _weierstrass_legacy_opts_to_new(c);
|
|
19601
|
+
const ecdsaOpts = {
|
|
19602
|
+
hmac: c.hmac,
|
|
19603
|
+
randomBytes: c.randomBytes,
|
|
19604
|
+
lowS: c.lowS,
|
|
19605
|
+
bits2int: c.bits2int,
|
|
19606
|
+
bits2int_modN: c.bits2int_modN
|
|
19607
|
+
};
|
|
19608
|
+
return { CURVE, curveOpts, hash: c.hash, ecdsaOpts };
|
|
19609
|
+
}
|
|
19610
|
+
function _ecdsa_new_output_to_legacy(c, _ecdsa) {
|
|
19611
|
+
const Point = _ecdsa.Point;
|
|
19612
|
+
return Object.assign({}, _ecdsa, {
|
|
19613
|
+
ProjectivePoint: Point,
|
|
19614
|
+
CURVE: Object.assign({}, c, nLength(Point.Fn.ORDER, Point.Fn.BITS))
|
|
19615
|
+
});
|
|
19616
|
+
}
|
|
19617
|
+
function weierstrass(c) {
|
|
19618
|
+
const { CURVE, curveOpts, hash, ecdsaOpts } = _ecdsa_legacy_opts_to_new(c);
|
|
19619
|
+
const Point = weierstrassN(CURVE, curveOpts);
|
|
19620
|
+
const signs = ecdsa(Point, hash, ecdsaOpts);
|
|
19621
|
+
return _ecdsa_new_output_to_legacy(c, signs);
|
|
19622
|
+
}
|
|
19623
|
+
|
|
19624
|
+
// ../../node_modules/@noble/curves/esm/_shortw_utils.js
|
|
19625
|
+
function createCurve(curveDef, defHash) {
|
|
19626
|
+
const create2 = (hash) => weierstrass({ ...curveDef, hash });
|
|
19627
|
+
return { ...create2(defHash), create: create2 };
|
|
19628
|
+
}
|
|
19629
|
+
|
|
19630
|
+
// ../../node_modules/@noble/curves/esm/nist.js
|
|
19631
|
+
var p256_CURVE = {
|
|
19632
|
+
p: BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"),
|
|
19633
|
+
n: BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),
|
|
19634
|
+
h: BigInt(1),
|
|
19635
|
+
a: BigInt("0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc"),
|
|
19636
|
+
b: BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),
|
|
19637
|
+
Gx: BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),
|
|
19638
|
+
Gy: BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5")
|
|
19639
|
+
};
|
|
19640
|
+
var p384_CURVE = {
|
|
19641
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"),
|
|
19642
|
+
n: BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"),
|
|
19643
|
+
h: BigInt(1),
|
|
19644
|
+
a: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc"),
|
|
19645
|
+
b: BigInt("0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"),
|
|
19646
|
+
Gx: BigInt("0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7"),
|
|
19647
|
+
Gy: BigInt("0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f")
|
|
19648
|
+
};
|
|
19649
|
+
var p521_CURVE = {
|
|
19650
|
+
p: BigInt("0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
|
|
19651
|
+
n: BigInt("0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409"),
|
|
19652
|
+
h: BigInt(1),
|
|
19653
|
+
a: BigInt("0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc"),
|
|
19654
|
+
b: BigInt("0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00"),
|
|
19655
|
+
Gx: BigInt("0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66"),
|
|
19656
|
+
Gy: BigInt("0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650")
|
|
19657
|
+
};
|
|
19658
|
+
var Fp256 = Field(p256_CURVE.p);
|
|
19659
|
+
var Fp384 = Field(p384_CURVE.p);
|
|
19660
|
+
var Fp521 = Field(p521_CURVE.p);
|
|
19661
|
+
var p256 = createCurve({ ...p256_CURVE, Fp: Fp256, lowS: false }, sha256);
|
|
19662
|
+
var p384 = createCurve({ ...p384_CURVE, Fp: Fp384, lowS: false }, sha384);
|
|
19663
|
+
var p521 = createCurve({ ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] }, sha512);
|
|
19664
|
+
|
|
19665
|
+
// ../../node_modules/@noble/curves/esm/p256.js
|
|
19666
|
+
var p2562 = p256;
|
|
19667
|
+
|
|
18407
19668
|
// ../share-envelope/dist/links.js
|
|
18408
19669
|
function toBase64Url(bytes) {
|
|
18409
19670
|
return base64url.baseEncode(bytes);
|
|
@@ -18564,11 +19825,12 @@ function serialize(value2) {
|
|
|
18564
19825
|
function canonicalize2(value2) {
|
|
18565
19826
|
return serialize(value2);
|
|
18566
19827
|
}
|
|
18567
|
-
var LINK_TYPES = ["openkey-login", "courier", "share"];
|
|
19828
|
+
var LINK_TYPES = ["openkey-login", "openkey-approval-request", "courier", "share"];
|
|
18568
19829
|
var MAX_LINK_BODY_BYTES = 65536;
|
|
18569
19830
|
var MAX_LINK_REQUEST_BYTES = Math.ceil(MAX_LINK_BODY_BYTES * 4 / 3) + 3072;
|
|
18570
19831
|
var LINK_TYPE_SPECS = {
|
|
18571
19832
|
"openkey-login": { maxLifeSeconds: 600, defaultLifeSeconds: 600, pathLetter: "l", readOnce: true },
|
|
19833
|
+
"openkey-approval-request": { maxLifeSeconds: 86400, defaultLifeSeconds: 3600, pathLetter: "a", readOnce: false },
|
|
18572
19834
|
courier: { maxLifeSeconds: 604800, defaultLifeSeconds: 86400, pathLetter: "c", readOnce: false },
|
|
18573
19835
|
share: { maxLifeSeconds: 2592e3, defaultLifeSeconds: 2592e3, pathLetter: "s", readOnce: false }
|
|
18574
19836
|
};
|
|
@@ -18821,19 +20083,19 @@ function linkTypeForPathLetter(letter) {
|
|
|
18821
20083
|
return LINK_TYPES.find((type) => LINK_TYPE_SPECS[type].pathLetter === letter);
|
|
18822
20084
|
}
|
|
18823
20085
|
var LINK_FRAGMENT_VERSION = 4;
|
|
18824
|
-
var SHORT_PATH = /^\/(s|c|l)\/([A-Za-z2-7]{26})$/;
|
|
20086
|
+
var SHORT_PATH = /^\/(s|c|l|a)\/([A-Za-z2-7]{26})$/;
|
|
18825
20087
|
var SHORT_FRAGMENT = /^#v=4&k=([A-Za-z0-9_-]{43})&c=(bafkrei[a-z2-7]{52})$/;
|
|
18826
20088
|
var FULL_FRAGMENT = /^#v=4&k=([A-Za-z0-9_-]{43})&r=([A-Za-z0-9_-]+)$/;
|
|
18827
20089
|
var REMOVED_FRAGMENT = /^#v=[23](&|$)/;
|
|
18828
20090
|
var MAX_FULL_RECORD_JSON_BYTES = MAX_LINK_REQUEST_BYTES;
|
|
18829
|
-
function canonicalHttpsOrigin(
|
|
20091
|
+
function canonicalHttpsOrigin(origin2) {
|
|
18830
20092
|
let url;
|
|
18831
20093
|
try {
|
|
18832
|
-
url = new URL(
|
|
20094
|
+
url = new URL(origin2);
|
|
18833
20095
|
} catch {
|
|
18834
20096
|
return false;
|
|
18835
20097
|
}
|
|
18836
|
-
return url.protocol === "https:" && url.username === "" && url.password === "" && url.origin ===
|
|
20098
|
+
return url.protocol === "https:" && url.username === "" && url.password === "" && url.origin === origin2;
|
|
18837
20099
|
}
|
|
18838
20100
|
function assertKey2(key) {
|
|
18839
20101
|
if (key.length !== 32) throw new LinkError("invalid_argument", "a link key is 32 bytes");
|
|
@@ -18950,14 +20212,64 @@ async function runBodyValidator(opened, validateBody) {
|
|
|
18950
20212
|
throw new LinkError("body_validation_failed", "the opened body was refused by the caller's validator", { cause });
|
|
18951
20213
|
}
|
|
18952
20214
|
}
|
|
20215
|
+
var bytes32 = external_exports.string().refine((value2) => decodeStrictBase64Url(value2)?.length === 32);
|
|
20216
|
+
var ed25519Jwk = external_exports.object({ kty: external_exports.literal("OKP"), crv: external_exports.literal("Ed25519"), x: bytes32 }).strict();
|
|
20217
|
+
var p256Jwk = external_exports.object({ kty: external_exports.literal("EC"), crv: external_exports.literal("P-256"), x: bytes32, y: bytes32 }).strict().refine((key) => {
|
|
20218
|
+
try {
|
|
20219
|
+
const point = new Uint8Array(65);
|
|
20220
|
+
point[0] = 4;
|
|
20221
|
+
point.set(decodeStrictBase64Url(key.x), 1);
|
|
20222
|
+
point.set(decodeStrictBase64Url(key.y), 33);
|
|
20223
|
+
p2562.ProjectivePoint.fromHex(point).assertValidity();
|
|
20224
|
+
return true;
|
|
20225
|
+
} catch {
|
|
20226
|
+
return false;
|
|
20227
|
+
}
|
|
20228
|
+
});
|
|
20229
|
+
var origin = external_exports.string().refine((value2) => {
|
|
20230
|
+
try {
|
|
20231
|
+
const url = new URL(value2);
|
|
20232
|
+
return url.origin === value2 && (url.protocol === "https:" || url.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(url.hostname));
|
|
20233
|
+
} catch {
|
|
20234
|
+
return false;
|
|
20235
|
+
}
|
|
20236
|
+
});
|
|
20237
|
+
var permission = external_exports.object({
|
|
20238
|
+
service: external_exports.string().min(1),
|
|
20239
|
+
space: external_exports.string().min(1),
|
|
20240
|
+
path: external_exports.string(),
|
|
20241
|
+
actions: external_exports.array(external_exports.string().min(1)).min(1),
|
|
20242
|
+
caveats: external_exports.array(external_exports.unknown()).optional()
|
|
20243
|
+
}).strict();
|
|
20244
|
+
var requestSchema = external_exports.object({
|
|
20245
|
+
issuer: external_exports.string(),
|
|
20246
|
+
requestVersion: external_exports.literal(1),
|
|
20247
|
+
targetDid: external_exports.string(),
|
|
20248
|
+
targetPublicJwk: ed25519Jwk,
|
|
20249
|
+
nodeOrigin: origin,
|
|
20250
|
+
permissions: external_exports.array(permission).min(1),
|
|
20251
|
+
reason: external_exports.string().optional(),
|
|
20252
|
+
grantLifetimeSeconds: external_exports.number().int().positive(),
|
|
20253
|
+
resultEncryptionPublicJwk: p256Jwk
|
|
20254
|
+
}).strict();
|
|
20255
|
+
function parseApprovalRequest(value2, options = {}) {
|
|
20256
|
+
const parsed = requestSchema.safeParse(value2);
|
|
20257
|
+
if (!parsed.success) throw new LinkError("body_validation_failed", "invalid approval request");
|
|
20258
|
+
const request = parsed.data;
|
|
20259
|
+
const publicKey = decodeStrictBase64Url(request.targetPublicJwk.x);
|
|
20260
|
+
if (ed25519KeyProblem(publicKey) !== null || didKeyFromEd25519PublicKey(publicKey) !== request.targetDid || request.issuer !== request.targetDid || options.issuer !== void 0 && request.issuer !== options.issuer) {
|
|
20261
|
+
throw new LinkError("issuer_binding_mismatch");
|
|
20262
|
+
}
|
|
20263
|
+
return request;
|
|
20264
|
+
}
|
|
18953
20265
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
18954
20266
|
var MAX_JSON_RESPONSE_BYTES = 4096;
|
|
18955
20267
|
var AUDIENCE_ORIGIN = /^https:\/\/[a-z0-9]([a-z0-9.-]*[a-z0-9])?(:[0-9]{1,5})?$/;
|
|
18956
20268
|
var createdSchema = external_exports.object({ id: external_exports.string(), expiresAt: external_exports.string() }).strict();
|
|
18957
20269
|
var errorBodySchema = external_exports.object({ error: external_exports.string() }).passthrough();
|
|
18958
|
-
function checkApiOrigin(
|
|
18959
|
-
if (!AUDIENCE_ORIGIN.test(
|
|
18960
|
-
return
|
|
20270
|
+
function checkApiOrigin(origin2) {
|
|
20271
|
+
if (!AUDIENCE_ORIGIN.test(origin2)) throw new LinkError("invalid_argument", "the API origin is not a canonical https origin");
|
|
20272
|
+
return origin2;
|
|
18961
20273
|
}
|
|
18962
20274
|
function requestSignal(options) {
|
|
18963
20275
|
const timeout = AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
@@ -19115,6 +20427,10 @@ async function resolveLink(link, input) {
|
|
|
19115
20427
|
} else if (opened.type !== "share") {
|
|
19116
20428
|
throw new LinkError("full_non_share");
|
|
19117
20429
|
}
|
|
20430
|
+
if (opened.type === "openkey-approval-request") {
|
|
20431
|
+
if (opened.header.audience !== input.apiOrigin) throw new LinkError("audience_mismatch");
|
|
20432
|
+
parseApprovalRequest(opened.value, { issuer: opened.header.issuer });
|
|
20433
|
+
}
|
|
19118
20434
|
await runBodyValidator(opened, input.validateBody);
|
|
19119
20435
|
return { ...opened, form: parsed.form, id: parsed.form === "short" ? parsed.id : void 0 };
|
|
19120
20436
|
}
|
|
@@ -19866,6 +21182,10 @@ export {
|
|
|
19866
21182
|
@noble/curves/esm/abstract/curve.js:
|
|
19867
21183
|
@noble/curves/esm/abstract/edwards.js:
|
|
19868
21184
|
@noble/curves/esm/ed25519.js:
|
|
21185
|
+
@noble/curves/esm/abstract/weierstrass.js:
|
|
21186
|
+
@noble/curves/esm/_shortw_utils.js:
|
|
21187
|
+
@noble/curves/esm/nist.js:
|
|
21188
|
+
@noble/curves/esm/p256.js:
|
|
19869
21189
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
19870
21190
|
*/
|
|
19871
21191
|
//# sourceMappingURL=courier-entry.js.map
|