@basmilius/apple-common 0.9.13 → 0.9.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +16 -15
- package/dist/index.mjs +2069 -45
- package/package.json +7 -6
package/dist/index.mjs
CHANGED
|
@@ -9,9 +9,9 @@ import { networkInterfaces } from "node:os";
|
|
|
9
9
|
import { EventEmitter } from "node:events";
|
|
10
10
|
import { NTP, OPack, TLV8 } from "@basmilius/apple-encoding";
|
|
11
11
|
import { Chacha20, Curve25519, Ed25519, hkdf } from "@basmilius/apple-encryption";
|
|
12
|
-
import { SRP, SrpClient } from "fast-srp-hap";
|
|
13
12
|
|
|
14
13
|
//#region \0rolldown/runtime.js
|
|
14
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
15
15
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
16
16
|
|
|
17
17
|
//#endregion
|
|
@@ -110,7 +110,7 @@ const AirPlayFeatureFlags = {
|
|
|
110
110
|
const PASSWORD_BIT = 128n;
|
|
111
111
|
const LEGACY_PAIRING_BIT = 512n;
|
|
112
112
|
const PIN_REQUIRED_BIT = 8n;
|
|
113
|
-
|
|
113
|
+
function parseFeatures(features) {
|
|
114
114
|
const parts = features.split(",").map((part) => part.trim());
|
|
115
115
|
if (parts.length === 1) return BigInt(parts[0]);
|
|
116
116
|
if (parts.length === 2) {
|
|
@@ -118,22 +118,24 @@ const parseFeatures = (features) => {
|
|
|
118
118
|
return BigInt(parts[1]) << 32n | low;
|
|
119
119
|
}
|
|
120
120
|
throw new Error(`Invalid features format: ${features}`);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
}
|
|
122
|
+
function hasFeatureFlag(features, flag) {
|
|
123
|
+
return (features & flag) !== 0n;
|
|
124
|
+
}
|
|
125
|
+
function describeFlags(features) {
|
|
124
126
|
const result = [];
|
|
125
127
|
for (const [name, flag] of Object.entries(AirPlayFeatureFlags)) if (hasFeatureFlag(features, flag)) result.push(name);
|
|
126
128
|
return result;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
+
}
|
|
130
|
+
function getProtocolVersion(txt) {
|
|
129
131
|
const featuresStr = txt.features ?? txt.ft;
|
|
130
132
|
if (!featuresStr) return 1;
|
|
131
133
|
const features = parseFeatures(featuresStr);
|
|
132
134
|
if (hasFeatureFlag(features, AirPlayFeatureFlags.SupportsUnifiedMediaControl)) return 2;
|
|
133
135
|
if (hasFeatureFlag(features, AirPlayFeatureFlags.SupportsCoreUtilsPairingAndEncryption)) return 2;
|
|
134
136
|
return 1;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
+
}
|
|
138
|
+
function getPairingRequirement(txt) {
|
|
137
139
|
const featuresStr = txt.features ?? txt.ft;
|
|
138
140
|
if (!featuresStr) return "none";
|
|
139
141
|
const features = parseFeatures(featuresStr);
|
|
@@ -143,16 +145,16 @@ const getPairingRequirement = (txt) => {
|
|
|
143
145
|
if (hasFeatureFlag(features, AirPlayFeatureFlags.SupportsSystemPairing)) return "transient";
|
|
144
146
|
if ((sf & LEGACY_PAIRING_BIT) !== 0n) return "pin";
|
|
145
147
|
return "none";
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
+
}
|
|
149
|
+
function isPasswordRequired(txt) {
|
|
148
150
|
if (txt.pw === "true") return true;
|
|
149
151
|
return ((txt.sf ? BigInt(txt.sf) : 0n) & PASSWORD_BIT) !== 0n;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
+
}
|
|
153
|
+
function isRemoteControlSupported(txt) {
|
|
152
154
|
const featuresStr = txt.features ?? txt.ft;
|
|
153
155
|
if (!featuresStr) return false;
|
|
154
156
|
return hasFeatureFlag(parseFeatures(featuresStr), AirPlayFeatureFlags.SupportsHangdogRemoteControl);
|
|
155
|
-
}
|
|
157
|
+
}
|
|
156
158
|
|
|
157
159
|
//#endregion
|
|
158
160
|
//#region src/storage.ts
|
|
@@ -302,7 +304,7 @@ const encodeDnsQuestion = (name, qtype, unicastResponse = false) => {
|
|
|
302
304
|
suffix.writeUInt16BE(unicastResponse ? 32769 : 1, 2);
|
|
303
305
|
return Buffer.concat([qname, suffix]);
|
|
304
306
|
};
|
|
305
|
-
|
|
307
|
+
function createQueryPackets(services, qtype = QueryType.PTR, unicastResponse = false) {
|
|
306
308
|
const packets = [];
|
|
307
309
|
for (let i = 0; i < services.length; i += SERVICES_PER_MSG) {
|
|
308
310
|
const chunk = services.slice(i, i + SERVICES_PER_MSG);
|
|
@@ -318,7 +320,7 @@ const createQueryPackets = (services, qtype = QueryType.PTR, unicastResponse = f
|
|
|
318
320
|
packets.push(Buffer.concat([header, ...questions]));
|
|
319
321
|
}
|
|
320
322
|
return packets;
|
|
321
|
-
}
|
|
323
|
+
}
|
|
322
324
|
const decodeQName = (buf, offset) => {
|
|
323
325
|
const labels = [];
|
|
324
326
|
let currentOffset = offset;
|
|
@@ -529,7 +531,7 @@ const knock = (address) => {
|
|
|
529
531
|
}));
|
|
530
532
|
return Promise.all(promises).then(() => {});
|
|
531
533
|
};
|
|
532
|
-
|
|
534
|
+
function unicast(hosts, services, timeout = 4) {
|
|
533
535
|
return new Promise((resolve) => {
|
|
534
536
|
const queries = createQueryPackets(services);
|
|
535
537
|
const collector = new ServiceCollector();
|
|
@@ -565,8 +567,8 @@ const unicast = (hosts, services, timeout = 4) => {
|
|
|
565
567
|
setTimeout(finish, timeout * 1e3);
|
|
566
568
|
});
|
|
567
569
|
});
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
+
}
|
|
571
|
+
function multicast(services, timeout = 4) {
|
|
570
572
|
return new Promise((resolve) => {
|
|
571
573
|
const collector = new ServiceCollector();
|
|
572
574
|
const queries = createQueryPackets(services);
|
|
@@ -640,7 +642,7 @@ const multicast = (services, timeout = 4) => {
|
|
|
640
642
|
};
|
|
641
643
|
setup();
|
|
642
644
|
});
|
|
643
|
-
}
|
|
645
|
+
}
|
|
644
646
|
|
|
645
647
|
//#endregion
|
|
646
648
|
//#region src/discovery.ts
|
|
@@ -747,16 +749,16 @@ var Discovery = class Discovery {
|
|
|
747
749
|
const result = toDiscoveryResult(service);
|
|
748
750
|
const existing = devices.get(result.id);
|
|
749
751
|
if (existing) {
|
|
750
|
-
if (service.type ===
|
|
751
|
-
else if (service.type ===
|
|
752
|
-
else if (service.type ===
|
|
752
|
+
if (service.type === "_airplay._tcp.local") existing.airplay = result;
|
|
753
|
+
else if (service.type === "_companion-link._tcp.local") existing.companionLink = result;
|
|
754
|
+
else if (service.type === "_raop._tcp.local") existing.raop = result;
|
|
753
755
|
} else devices.set(result.id, {
|
|
754
756
|
id: result.id,
|
|
755
757
|
name: result.fqdn,
|
|
756
758
|
address: result.address,
|
|
757
|
-
airplay: service.type ===
|
|
758
|
-
companionLink: service.type ===
|
|
759
|
-
raop: service.type ===
|
|
759
|
+
airplay: service.type === "_airplay._tcp.local" ? result : void 0,
|
|
760
|
+
companionLink: service.type === "_companion-link._tcp.local" ? result : void 0,
|
|
761
|
+
raop: service.type === "_raop._tcp.local" ? result : void 0
|
|
760
762
|
});
|
|
761
763
|
}
|
|
762
764
|
return [...devices.values()];
|
|
@@ -772,10 +774,6 @@ var Discovery = class Discovery {
|
|
|
772
774
|
}
|
|
773
775
|
};
|
|
774
776
|
|
|
775
|
-
//#endregion
|
|
776
|
-
//#region src/symbols.ts
|
|
777
|
-
const ENCRYPTION = Symbol();
|
|
778
|
-
|
|
779
777
|
//#endregion
|
|
780
778
|
//#region src/connection.ts
|
|
781
779
|
const NOOP_PROMISE_HANDLER = {
|
|
@@ -807,6 +805,7 @@ var Connection = class extends EventEmitter {
|
|
|
807
805
|
#address;
|
|
808
806
|
#port;
|
|
809
807
|
#context;
|
|
808
|
+
#emitInternal = (event, ...args) => this.emit(event, ...args);
|
|
810
809
|
#debug = false;
|
|
811
810
|
#retryAttempt = 0;
|
|
812
811
|
#retryAttempts = 3;
|
|
@@ -860,13 +859,13 @@ var Connection = class extends EventEmitter {
|
|
|
860
859
|
}
|
|
861
860
|
write(data) {
|
|
862
861
|
if (!this.#socket || this.state !== "connected" || !this.#socket.writable) {
|
|
863
|
-
this
|
|
862
|
+
this.#emitInternal("error", /* @__PURE__ */ new Error("Cannot write to a disconnected connection."));
|
|
864
863
|
return;
|
|
865
864
|
}
|
|
866
865
|
this.#socket.write(data, (err) => {
|
|
867
866
|
if (!err) return;
|
|
868
867
|
this.#context.logger.error("Failed to write data to socket.");
|
|
869
|
-
this
|
|
868
|
+
this.#emitInternal("error", err);
|
|
870
869
|
});
|
|
871
870
|
}
|
|
872
871
|
async #attemptConnect() {
|
|
@@ -890,8 +889,7 @@ var Connection = class extends EventEmitter {
|
|
|
890
889
|
this.#context.logger.net(`Connecting to ${this.#address}:${this.#port}...`);
|
|
891
890
|
this.#socket.connect({
|
|
892
891
|
host: this.#address,
|
|
893
|
-
port: this.#port
|
|
894
|
-
keepAlive: true
|
|
892
|
+
port: this.#port
|
|
895
893
|
});
|
|
896
894
|
});
|
|
897
895
|
}
|
|
@@ -941,7 +939,7 @@ var Connection = class extends EventEmitter {
|
|
|
941
939
|
this.#state = "disconnected";
|
|
942
940
|
this.#context.logger.net(`Connection closed (${hadError ? "with error" : "normally"}).`);
|
|
943
941
|
}
|
|
944
|
-
this
|
|
942
|
+
this.#emitInternal("close", hadError);
|
|
945
943
|
if (wasConnected && this.#retryEnabled && hadError) this.#scheduleRetry(/* @__PURE__ */ new Error("Connection closed unexpectedly."));
|
|
946
944
|
}
|
|
947
945
|
#onConnect() {
|
|
@@ -949,7 +947,7 @@ var Connection = class extends EventEmitter {
|
|
|
949
947
|
this.#retryAttempt = 0;
|
|
950
948
|
this.#socket.setKeepAlive(true, 1e4);
|
|
951
949
|
this.#socket.setTimeout(0);
|
|
952
|
-
this
|
|
950
|
+
this.#emitInternal("connect");
|
|
953
951
|
this.#connectPromise?.resolve();
|
|
954
952
|
this.#connectPromise = void 0;
|
|
955
953
|
}
|
|
@@ -960,14 +958,14 @@ var Connection = class extends EventEmitter {
|
|
|
960
958
|
this.#context.logger.debug(`hex=${data.subarray(0, cutoff).toString("hex")}`);
|
|
961
959
|
this.#context.logger.debug(`ascii=${data.toString("ascii").replace(/[^\x20-\x7E]/g, ".").substring(0, cutoff)}`);
|
|
962
960
|
}
|
|
963
|
-
this
|
|
961
|
+
this.#emitInternal("data", data);
|
|
964
962
|
}
|
|
965
963
|
#onEnd() {
|
|
966
|
-
this
|
|
964
|
+
this.#emitInternal("end");
|
|
967
965
|
}
|
|
968
966
|
#onError(err) {
|
|
969
967
|
this.#context.logger.error(`Connection error: ${err.message}`);
|
|
970
|
-
if (this.listenerCount("error") > 0) this
|
|
968
|
+
if (this.listenerCount("error") > 0) this.#emitInternal("error", err);
|
|
971
969
|
else this.#context.logger.warn("No error handler registered. This is likely a bug.", this.constructor.name, "#onError");
|
|
972
970
|
if (this.#state === "connecting") this.#scheduleRetry(err);
|
|
973
971
|
else this.#state = "failed";
|
|
@@ -975,7 +973,7 @@ var Connection = class extends EventEmitter {
|
|
|
975
973
|
#onTimeout() {
|
|
976
974
|
this.#context.logger.error("Connection timed out.");
|
|
977
975
|
const err = /* @__PURE__ */ new Error("Connection timed out.");
|
|
978
|
-
this
|
|
976
|
+
this.#emitInternal("timeout");
|
|
979
977
|
if (this.#state === "connecting") this.#scheduleRetry(err);
|
|
980
978
|
else {
|
|
981
979
|
this.#state = "failed";
|
|
@@ -985,11 +983,11 @@ var Connection = class extends EventEmitter {
|
|
|
985
983
|
};
|
|
986
984
|
var EncryptionAwareConnection = class extends Connection {
|
|
987
985
|
get isEncrypted() {
|
|
988
|
-
return !!this
|
|
986
|
+
return !!this._encryption;
|
|
989
987
|
}
|
|
990
|
-
|
|
988
|
+
_encryption;
|
|
991
989
|
enableEncryption(readKey, writeKey) {
|
|
992
|
-
this
|
|
990
|
+
this._encryption = new EncryptionState(readKey, writeKey);
|
|
993
991
|
}
|
|
994
992
|
};
|
|
995
993
|
var EncryptionState = class {
|
|
@@ -1098,8 +1096,2030 @@ var Context = class {
|
|
|
1098
1096
|
}
|
|
1099
1097
|
};
|
|
1100
1098
|
|
|
1099
|
+
//#endregion
|
|
1100
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/jsbn/jsbn.js
|
|
1101
|
+
var require_jsbn = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
1102
|
+
var crypt = __require("crypto");
|
|
1103
|
+
var dbits;
|
|
1104
|
+
function BigInteger(a, b) {
|
|
1105
|
+
if (a != null) if ("number" == typeof a) this.fromNumber(a, b);
|
|
1106
|
+
else if (Buffer.isBuffer(a)) this.fromBuffer(a);
|
|
1107
|
+
else if (b == null && "string" != typeof a) this.fromByteArray(a);
|
|
1108
|
+
else this.fromString(a, b);
|
|
1109
|
+
}
|
|
1110
|
+
function nbi() {
|
|
1111
|
+
return new BigInteger(null);
|
|
1112
|
+
}
|
|
1113
|
+
function am3(i, x, w, j, c, n) {
|
|
1114
|
+
var xl = x & 16383, xh = x >> 14;
|
|
1115
|
+
while (--n >= 0) {
|
|
1116
|
+
var l = this[i] & 16383;
|
|
1117
|
+
var h = this[i++] >> 14;
|
|
1118
|
+
var m = xh * l + h * xl;
|
|
1119
|
+
l = xl * l + ((m & 16383) << 14) + w[j] + c;
|
|
1120
|
+
c = (l >> 28) + (m >> 14) + xh * h;
|
|
1121
|
+
w[j++] = l & 268435455;
|
|
1122
|
+
}
|
|
1123
|
+
return c;
|
|
1124
|
+
}
|
|
1125
|
+
BigInteger.prototype.am = am3;
|
|
1126
|
+
dbits = 28;
|
|
1127
|
+
BigInteger.prototype.DB = dbits;
|
|
1128
|
+
BigInteger.prototype.DM = (1 << dbits) - 1;
|
|
1129
|
+
BigInteger.prototype.DV = 1 << dbits;
|
|
1130
|
+
var BI_FP = 52;
|
|
1131
|
+
BigInteger.prototype.FV = Math.pow(2, BI_FP);
|
|
1132
|
+
BigInteger.prototype.F1 = BI_FP - dbits;
|
|
1133
|
+
BigInteger.prototype.F2 = 2 * dbits - BI_FP;
|
|
1134
|
+
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
1135
|
+
var BI_RC = new Array();
|
|
1136
|
+
var rr = "0".charCodeAt(0), vv;
|
|
1137
|
+
for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
|
|
1138
|
+
rr = "a".charCodeAt(0);
|
|
1139
|
+
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|
1140
|
+
rr = "A".charCodeAt(0);
|
|
1141
|
+
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|
1142
|
+
function int2char(n) {
|
|
1143
|
+
return BI_RM.charAt(n);
|
|
1144
|
+
}
|
|
1145
|
+
function intAt(s, i) {
|
|
1146
|
+
var c = BI_RC[s.charCodeAt(i)];
|
|
1147
|
+
return c == null ? -1 : c;
|
|
1148
|
+
}
|
|
1149
|
+
function bnpCopyTo(r) {
|
|
1150
|
+
for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
|
|
1151
|
+
r.t = this.t;
|
|
1152
|
+
r.s = this.s;
|
|
1153
|
+
}
|
|
1154
|
+
function bnpFromInt(x) {
|
|
1155
|
+
this.t = 1;
|
|
1156
|
+
this.s = x < 0 ? -1 : 0;
|
|
1157
|
+
if (x > 0) this[0] = x;
|
|
1158
|
+
else if (x < -1) this[0] = x + DV;
|
|
1159
|
+
else this.t = 0;
|
|
1160
|
+
}
|
|
1161
|
+
function nbv(i) {
|
|
1162
|
+
var r = nbi();
|
|
1163
|
+
r.fromInt(i);
|
|
1164
|
+
return r;
|
|
1165
|
+
}
|
|
1166
|
+
function bnpFromString(data, radix, unsigned) {
|
|
1167
|
+
var k;
|
|
1168
|
+
switch (radix) {
|
|
1169
|
+
case 2:
|
|
1170
|
+
k = 1;
|
|
1171
|
+
break;
|
|
1172
|
+
case 4:
|
|
1173
|
+
k = 2;
|
|
1174
|
+
break;
|
|
1175
|
+
case 8:
|
|
1176
|
+
k = 3;
|
|
1177
|
+
break;
|
|
1178
|
+
case 16:
|
|
1179
|
+
k = 4;
|
|
1180
|
+
break;
|
|
1181
|
+
case 32:
|
|
1182
|
+
k = 5;
|
|
1183
|
+
break;
|
|
1184
|
+
case 256:
|
|
1185
|
+
k = 8;
|
|
1186
|
+
break;
|
|
1187
|
+
default:
|
|
1188
|
+
this.fromRadix(data, radix);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
this.t = 0;
|
|
1192
|
+
this.s = 0;
|
|
1193
|
+
var i = data.length;
|
|
1194
|
+
var mi = false;
|
|
1195
|
+
var sh = 0;
|
|
1196
|
+
while (--i >= 0) {
|
|
1197
|
+
var x = k == 8 ? data[i] & 255 : intAt(data, i);
|
|
1198
|
+
if (x < 0) {
|
|
1199
|
+
if (data.charAt(i) == "-") mi = true;
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
mi = false;
|
|
1203
|
+
if (sh === 0) this[this.t++] = x;
|
|
1204
|
+
else if (sh + k > this.DB) {
|
|
1205
|
+
this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
|
|
1206
|
+
this[this.t++] = x >> this.DB - sh;
|
|
1207
|
+
} else this[this.t - 1] |= x << sh;
|
|
1208
|
+
sh += k;
|
|
1209
|
+
if (sh >= this.DB) sh -= this.DB;
|
|
1210
|
+
}
|
|
1211
|
+
if (!unsigned && k == 8 && (data[0] & 128) != 0) {
|
|
1212
|
+
this.s = -1;
|
|
1213
|
+
if (sh > 0) this[this.t - 1] |= (1 << this.DB - sh) - 1 << sh;
|
|
1214
|
+
}
|
|
1215
|
+
this.clamp();
|
|
1216
|
+
if (mi) BigInteger.ZERO.subTo(this, this);
|
|
1217
|
+
}
|
|
1218
|
+
function bnpFromByteArray(a, unsigned) {
|
|
1219
|
+
this.fromString(a, 256, unsigned);
|
|
1220
|
+
}
|
|
1221
|
+
function bnpFromBuffer(a) {
|
|
1222
|
+
this.fromString(a, 256, true);
|
|
1223
|
+
}
|
|
1224
|
+
function bnpClamp() {
|
|
1225
|
+
var c = this.s & this.DM;
|
|
1226
|
+
while (this.t > 0 && this[this.t - 1] == c) --this.t;
|
|
1227
|
+
}
|
|
1228
|
+
function bnToString(b) {
|
|
1229
|
+
if (this.s < 0) return "-" + this.negate().toString(b);
|
|
1230
|
+
var k;
|
|
1231
|
+
if (b == 16) k = 4;
|
|
1232
|
+
else if (b == 8) k = 3;
|
|
1233
|
+
else if (b == 2) k = 1;
|
|
1234
|
+
else if (b == 32) k = 5;
|
|
1235
|
+
else if (b == 4) k = 2;
|
|
1236
|
+
else return this.toRadix(b);
|
|
1237
|
+
var km = (1 << k) - 1, d, m = false, r = "", i = this.t;
|
|
1238
|
+
var p = this.DB - i * this.DB % k;
|
|
1239
|
+
if (i-- > 0) {
|
|
1240
|
+
if (p < this.DB && (d = this[i] >> p) > 0) {
|
|
1241
|
+
m = true;
|
|
1242
|
+
r = int2char(d);
|
|
1243
|
+
}
|
|
1244
|
+
while (i >= 0) {
|
|
1245
|
+
if (p < k) {
|
|
1246
|
+
d = (this[i] & (1 << p) - 1) << k - p;
|
|
1247
|
+
d |= this[--i] >> (p += this.DB - k);
|
|
1248
|
+
} else {
|
|
1249
|
+
d = this[i] >> (p -= k) & km;
|
|
1250
|
+
if (p <= 0) {
|
|
1251
|
+
p += this.DB;
|
|
1252
|
+
--i;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (d > 0) m = true;
|
|
1256
|
+
if (m) r += int2char(d);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
//! Fix to be compatible with node >0.12.7 Buffer.js
|
|
1260
|
+
if (b == 16 && r.length % 2 != 0) r = "0" + r;
|
|
1261
|
+
return m ? r : "0";
|
|
1262
|
+
}
|
|
1263
|
+
function bnNegate() {
|
|
1264
|
+
var r = nbi();
|
|
1265
|
+
BigInteger.ZERO.subTo(this, r);
|
|
1266
|
+
return r;
|
|
1267
|
+
}
|
|
1268
|
+
function bnAbs() {
|
|
1269
|
+
return this.s < 0 ? this.negate() : this;
|
|
1270
|
+
}
|
|
1271
|
+
function bnCompareTo(a) {
|
|
1272
|
+
var r = this.s - a.s;
|
|
1273
|
+
if (r != 0) return r;
|
|
1274
|
+
var i = this.t;
|
|
1275
|
+
r = i - a.t;
|
|
1276
|
+
if (r != 0) return this.s < 0 ? -r : r;
|
|
1277
|
+
while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
|
|
1278
|
+
return 0;
|
|
1279
|
+
}
|
|
1280
|
+
function bnEqual(a) {
|
|
1281
|
+
console.log(this.compareTo(a));
|
|
1282
|
+
return this.compareTo(a) == 0 ? true : false;
|
|
1283
|
+
}
|
|
1284
|
+
function bnGreater(a) {
|
|
1285
|
+
return this.compareTo(a) > 0 ? true : false;
|
|
1286
|
+
}
|
|
1287
|
+
function bnGreaterOrEqual(a) {
|
|
1288
|
+
return this.compareTo(a) >= 0 ? true : false;
|
|
1289
|
+
}
|
|
1290
|
+
function bnLesser(a) {
|
|
1291
|
+
return this.compareTo(a) < 0 ? true : false;
|
|
1292
|
+
}
|
|
1293
|
+
function bnLesserOrEqual(a) {
|
|
1294
|
+
return this.compareTo(a) <= 0 ? true : false;
|
|
1295
|
+
}
|
|
1296
|
+
function nbits(x) {
|
|
1297
|
+
var r = 1, t;
|
|
1298
|
+
if ((t = x >>> 16) != 0) {
|
|
1299
|
+
x = t;
|
|
1300
|
+
r += 16;
|
|
1301
|
+
}
|
|
1302
|
+
if ((t = x >> 8) != 0) {
|
|
1303
|
+
x = t;
|
|
1304
|
+
r += 8;
|
|
1305
|
+
}
|
|
1306
|
+
if ((t = x >> 4) != 0) {
|
|
1307
|
+
x = t;
|
|
1308
|
+
r += 4;
|
|
1309
|
+
}
|
|
1310
|
+
if ((t = x >> 2) != 0) {
|
|
1311
|
+
x = t;
|
|
1312
|
+
r += 2;
|
|
1313
|
+
}
|
|
1314
|
+
if ((t = x >> 1) != 0) {
|
|
1315
|
+
x = t;
|
|
1316
|
+
r += 1;
|
|
1317
|
+
}
|
|
1318
|
+
return r;
|
|
1319
|
+
}
|
|
1320
|
+
function bnBitLength() {
|
|
1321
|
+
if (this.t <= 0) return 0;
|
|
1322
|
+
return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
|
|
1323
|
+
}
|
|
1324
|
+
function bnpDLShiftTo(n, r) {
|
|
1325
|
+
var i;
|
|
1326
|
+
for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
|
|
1327
|
+
for (i = n - 1; i >= 0; --i) r[i] = 0;
|
|
1328
|
+
r.t = this.t + n;
|
|
1329
|
+
r.s = this.s;
|
|
1330
|
+
}
|
|
1331
|
+
function bnpDRShiftTo(n, r) {
|
|
1332
|
+
for (var i = n; i < this.t; ++i) r[i - n] = this[i];
|
|
1333
|
+
r.t = Math.max(this.t - n, 0);
|
|
1334
|
+
r.s = this.s;
|
|
1335
|
+
}
|
|
1336
|
+
function bnpLShiftTo(n, r) {
|
|
1337
|
+
var bs = n % this.DB;
|
|
1338
|
+
var cbs = this.DB - bs;
|
|
1339
|
+
var bm = (1 << cbs) - 1;
|
|
1340
|
+
var ds = Math.floor(n / this.DB), c = this.s << bs & this.DM, i;
|
|
1341
|
+
for (i = this.t - 1; i >= 0; --i) {
|
|
1342
|
+
r[i + ds + 1] = this[i] >> cbs | c;
|
|
1343
|
+
c = (this[i] & bm) << bs;
|
|
1344
|
+
}
|
|
1345
|
+
for (i = ds - 1; i >= 0; --i) r[i] = 0;
|
|
1346
|
+
r[ds] = c;
|
|
1347
|
+
r.t = this.t + ds + 1;
|
|
1348
|
+
r.s = this.s;
|
|
1349
|
+
r.clamp();
|
|
1350
|
+
}
|
|
1351
|
+
function bnpRShiftTo(n, r) {
|
|
1352
|
+
r.s = this.s;
|
|
1353
|
+
var ds = Math.floor(n / this.DB);
|
|
1354
|
+
if (ds >= this.t) {
|
|
1355
|
+
r.t = 0;
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
var bs = n % this.DB;
|
|
1359
|
+
var cbs = this.DB - bs;
|
|
1360
|
+
var bm = (1 << bs) - 1;
|
|
1361
|
+
r[0] = this[ds] >> bs;
|
|
1362
|
+
for (var i = ds + 1; i < this.t; ++i) {
|
|
1363
|
+
r[i - ds - 1] |= (this[i] & bm) << cbs;
|
|
1364
|
+
r[i - ds] = this[i] >> bs;
|
|
1365
|
+
}
|
|
1366
|
+
if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
|
|
1367
|
+
r.t = this.t - ds;
|
|
1368
|
+
r.clamp();
|
|
1369
|
+
}
|
|
1370
|
+
function bnpSubTo(a, r) {
|
|
1371
|
+
var i = 0, c = 0, m = Math.min(a.t, this.t);
|
|
1372
|
+
while (i < m) {
|
|
1373
|
+
c += this[i] - a[i];
|
|
1374
|
+
r[i++] = c & this.DM;
|
|
1375
|
+
c >>= this.DB;
|
|
1376
|
+
}
|
|
1377
|
+
if (a.t < this.t) {
|
|
1378
|
+
c -= a.s;
|
|
1379
|
+
while (i < this.t) {
|
|
1380
|
+
c += this[i];
|
|
1381
|
+
r[i++] = c & this.DM;
|
|
1382
|
+
c >>= this.DB;
|
|
1383
|
+
}
|
|
1384
|
+
c += this.s;
|
|
1385
|
+
} else {
|
|
1386
|
+
c += this.s;
|
|
1387
|
+
while (i < a.t) {
|
|
1388
|
+
c -= a[i];
|
|
1389
|
+
r[i++] = c & this.DM;
|
|
1390
|
+
c >>= this.DB;
|
|
1391
|
+
}
|
|
1392
|
+
c -= a.s;
|
|
1393
|
+
}
|
|
1394
|
+
r.s = c < 0 ? -1 : 0;
|
|
1395
|
+
if (c < -1) r[i++] = this.DV + c;
|
|
1396
|
+
else if (c > 0) r[i++] = c;
|
|
1397
|
+
r.t = i;
|
|
1398
|
+
r.clamp();
|
|
1399
|
+
}
|
|
1400
|
+
function bnpMultiplyTo(a, r) {
|
|
1401
|
+
var x = this.abs(), y = a.abs();
|
|
1402
|
+
var i = x.t;
|
|
1403
|
+
r.t = i + y.t;
|
|
1404
|
+
while (--i >= 0) r[i] = 0;
|
|
1405
|
+
for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
|
|
1406
|
+
r.s = 0;
|
|
1407
|
+
r.clamp();
|
|
1408
|
+
if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
|
|
1409
|
+
}
|
|
1410
|
+
function bnpSquareTo(r) {
|
|
1411
|
+
var x = this.abs();
|
|
1412
|
+
var i = r.t = 2 * x.t;
|
|
1413
|
+
while (--i >= 0) r[i] = 0;
|
|
1414
|
+
for (i = 0; i < x.t - 1; ++i) {
|
|
1415
|
+
var c = x.am(i, x[i], r, 2 * i, 0, 1);
|
|
1416
|
+
if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
|
|
1417
|
+
r[i + x.t] -= x.DV;
|
|
1418
|
+
r[i + x.t + 1] = 1;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
|
|
1422
|
+
r.s = 0;
|
|
1423
|
+
r.clamp();
|
|
1424
|
+
}
|
|
1425
|
+
function bnpDivRemTo(m, q, r) {
|
|
1426
|
+
var pm = m.abs();
|
|
1427
|
+
if (pm.t <= 0) return;
|
|
1428
|
+
var pt = this.abs();
|
|
1429
|
+
if (pt.t < pm.t) {
|
|
1430
|
+
if (q != null) q.fromInt(0);
|
|
1431
|
+
if (r != null) this.copyTo(r);
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
1434
|
+
if (r == null) r = nbi();
|
|
1435
|
+
var y = nbi(), ts = this.s, ms = m.s;
|
|
1436
|
+
var nsh = this.DB - nbits(pm[pm.t - 1]);
|
|
1437
|
+
if (nsh > 0) {
|
|
1438
|
+
pm.lShiftTo(nsh, y);
|
|
1439
|
+
pt.lShiftTo(nsh, r);
|
|
1440
|
+
} else {
|
|
1441
|
+
pm.copyTo(y);
|
|
1442
|
+
pt.copyTo(r);
|
|
1443
|
+
}
|
|
1444
|
+
var ys = y.t;
|
|
1445
|
+
var y0 = y[ys - 1];
|
|
1446
|
+
if (y0 === 0) return;
|
|
1447
|
+
var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
|
|
1448
|
+
var d1 = this.FV / yt, d2 = (1 << this.F1) / yt, e = 1 << this.F2;
|
|
1449
|
+
var i = r.t, j = i - ys, t = q == null ? nbi() : q;
|
|
1450
|
+
y.dlShiftTo(j, t);
|
|
1451
|
+
if (r.compareTo(t) >= 0) {
|
|
1452
|
+
r[r.t++] = 1;
|
|
1453
|
+
r.subTo(t, r);
|
|
1454
|
+
}
|
|
1455
|
+
BigInteger.ONE.dlShiftTo(ys, t);
|
|
1456
|
+
t.subTo(y, y);
|
|
1457
|
+
while (y.t < ys) y[y.t++] = 0;
|
|
1458
|
+
while (--j >= 0) {
|
|
1459
|
+
var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
|
|
1460
|
+
if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
|
|
1461
|
+
y.dlShiftTo(j, t);
|
|
1462
|
+
r.subTo(t, r);
|
|
1463
|
+
while (r[i] < --qd) r.subTo(t, r);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
if (q != null) {
|
|
1467
|
+
r.drShiftTo(ys, q);
|
|
1468
|
+
if (ts != ms) BigInteger.ZERO.subTo(q, q);
|
|
1469
|
+
}
|
|
1470
|
+
r.t = ys;
|
|
1471
|
+
r.clamp();
|
|
1472
|
+
if (nsh > 0) r.rShiftTo(nsh, r);
|
|
1473
|
+
if (ts < 0) BigInteger.ZERO.subTo(r, r);
|
|
1474
|
+
}
|
|
1475
|
+
function bnMod(a) {
|
|
1476
|
+
var r = nbi();
|
|
1477
|
+
this.abs().divRemTo(a, null, r);
|
|
1478
|
+
if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
|
|
1479
|
+
return r;
|
|
1480
|
+
}
|
|
1481
|
+
function Classic(m) {
|
|
1482
|
+
this.m = m;
|
|
1483
|
+
}
|
|
1484
|
+
function cConvert(x) {
|
|
1485
|
+
if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
|
|
1486
|
+
else return x;
|
|
1487
|
+
}
|
|
1488
|
+
function cRevert(x) {
|
|
1489
|
+
return x;
|
|
1490
|
+
}
|
|
1491
|
+
function cReduce(x) {
|
|
1492
|
+
x.divRemTo(this.m, null, x);
|
|
1493
|
+
}
|
|
1494
|
+
function cMulTo(x, y, r) {
|
|
1495
|
+
x.multiplyTo(y, r);
|
|
1496
|
+
this.reduce(r);
|
|
1497
|
+
}
|
|
1498
|
+
function cSqrTo(x, r) {
|
|
1499
|
+
x.squareTo(r);
|
|
1500
|
+
this.reduce(r);
|
|
1501
|
+
}
|
|
1502
|
+
Classic.prototype.convert = cConvert;
|
|
1503
|
+
Classic.prototype.revert = cRevert;
|
|
1504
|
+
Classic.prototype.reduce = cReduce;
|
|
1505
|
+
Classic.prototype.mulTo = cMulTo;
|
|
1506
|
+
Classic.prototype.sqrTo = cSqrTo;
|
|
1507
|
+
function bnpInvDigit() {
|
|
1508
|
+
if (this.t < 1) return 0;
|
|
1509
|
+
var x = this[0];
|
|
1510
|
+
if ((x & 1) === 0) return 0;
|
|
1511
|
+
var y = x & 3;
|
|
1512
|
+
y = y * (2 - (x & 15) * y) & 15;
|
|
1513
|
+
y = y * (2 - (x & 255) * y) & 255;
|
|
1514
|
+
y = y * (2 - ((x & 65535) * y & 65535)) & 65535;
|
|
1515
|
+
y = y * (2 - x * y % this.DV) % this.DV;
|
|
1516
|
+
return y > 0 ? this.DV - y : -y;
|
|
1517
|
+
}
|
|
1518
|
+
function Montgomery(m) {
|
|
1519
|
+
this.m = m;
|
|
1520
|
+
this.mp = m.invDigit();
|
|
1521
|
+
this.mpl = this.mp & 32767;
|
|
1522
|
+
this.mph = this.mp >> 15;
|
|
1523
|
+
this.um = (1 << m.DB - 15) - 1;
|
|
1524
|
+
this.mt2 = 2 * m.t;
|
|
1525
|
+
}
|
|
1526
|
+
function montConvert(x) {
|
|
1527
|
+
var r = nbi();
|
|
1528
|
+
x.abs().dlShiftTo(this.m.t, r);
|
|
1529
|
+
r.divRemTo(this.m, null, r);
|
|
1530
|
+
if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
|
|
1531
|
+
return r;
|
|
1532
|
+
}
|
|
1533
|
+
function montRevert(x) {
|
|
1534
|
+
var r = nbi();
|
|
1535
|
+
x.copyTo(r);
|
|
1536
|
+
this.reduce(r);
|
|
1537
|
+
return r;
|
|
1538
|
+
}
|
|
1539
|
+
function montReduce(x) {
|
|
1540
|
+
while (x.t <= this.mt2) x[x.t++] = 0;
|
|
1541
|
+
for (var i = 0; i < this.m.t; ++i) {
|
|
1542
|
+
var j = x[i] & 32767;
|
|
1543
|
+
var u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM;
|
|
1544
|
+
j = i + this.m.t;
|
|
1545
|
+
x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
|
|
1546
|
+
while (x[j] >= x.DV) {
|
|
1547
|
+
x[j] -= x.DV;
|
|
1548
|
+
x[++j]++;
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
x.clamp();
|
|
1552
|
+
x.drShiftTo(this.m.t, x);
|
|
1553
|
+
if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
|
|
1554
|
+
}
|
|
1555
|
+
function montSqrTo(x, r) {
|
|
1556
|
+
x.squareTo(r);
|
|
1557
|
+
this.reduce(r);
|
|
1558
|
+
}
|
|
1559
|
+
function montMulTo(x, y, r) {
|
|
1560
|
+
x.multiplyTo(y, r);
|
|
1561
|
+
this.reduce(r);
|
|
1562
|
+
}
|
|
1563
|
+
Montgomery.prototype.convert = montConvert;
|
|
1564
|
+
Montgomery.prototype.revert = montRevert;
|
|
1565
|
+
Montgomery.prototype.reduce = montReduce;
|
|
1566
|
+
Montgomery.prototype.mulTo = montMulTo;
|
|
1567
|
+
Montgomery.prototype.sqrTo = montSqrTo;
|
|
1568
|
+
function bnpIsEven() {
|
|
1569
|
+
return (this.t > 0 ? this[0] & 1 : this.s) === 0;
|
|
1570
|
+
}
|
|
1571
|
+
function bnpExp(e, z) {
|
|
1572
|
+
if (e > 4294967295 || e < 1) return BigInteger.ONE;
|
|
1573
|
+
var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e) - 1;
|
|
1574
|
+
g.copyTo(r);
|
|
1575
|
+
while (--i >= 0) {
|
|
1576
|
+
z.sqrTo(r, r2);
|
|
1577
|
+
if ((e & 1 << i) > 0) z.mulTo(r2, g, r);
|
|
1578
|
+
else {
|
|
1579
|
+
var t = r;
|
|
1580
|
+
r = r2;
|
|
1581
|
+
r2 = t;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
return z.revert(r);
|
|
1585
|
+
}
|
|
1586
|
+
function bnModPowInt(e, m) {
|
|
1587
|
+
var z;
|
|
1588
|
+
if (e < 256 || m.isEven()) z = new Classic(m);
|
|
1589
|
+
else z = new Montgomery(m);
|
|
1590
|
+
return this.exp(e, z);
|
|
1591
|
+
}
|
|
1592
|
+
function bnClone() {
|
|
1593
|
+
var r = nbi();
|
|
1594
|
+
this.copyTo(r);
|
|
1595
|
+
return r;
|
|
1596
|
+
}
|
|
1597
|
+
function bnIntValue() {
|
|
1598
|
+
if (this.s < 0) {
|
|
1599
|
+
if (this.t == 1) return this[0] - this.DV;
|
|
1600
|
+
else if (this.t === 0) return -1;
|
|
1601
|
+
} else if (this.t == 1) return this[0];
|
|
1602
|
+
else if (this.t === 0) return 0;
|
|
1603
|
+
return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0];
|
|
1604
|
+
}
|
|
1605
|
+
function bnByteValue() {
|
|
1606
|
+
return this.t == 0 ? this.s : this[0] << 24 >> 24;
|
|
1607
|
+
}
|
|
1608
|
+
function bnShortValue() {
|
|
1609
|
+
return this.t == 0 ? this.s : this[0] << 16 >> 16;
|
|
1610
|
+
}
|
|
1611
|
+
function bnpChunkSize(r) {
|
|
1612
|
+
return Math.floor(Math.LN2 * this.DB / Math.log(r));
|
|
1613
|
+
}
|
|
1614
|
+
function bnSigNum() {
|
|
1615
|
+
if (this.s < 0) return -1;
|
|
1616
|
+
else if (this.t <= 0 || this.t == 1 && this[0] <= 0) return 0;
|
|
1617
|
+
else return 1;
|
|
1618
|
+
}
|
|
1619
|
+
function bnpToRadix(b) {
|
|
1620
|
+
if (b == null) b = 10;
|
|
1621
|
+
if (this.signum() === 0 || b < 2 || b > 36) return "0";
|
|
1622
|
+
var cs = this.chunkSize(b);
|
|
1623
|
+
var a = Math.pow(b, cs);
|
|
1624
|
+
var d = nbv(a), y = nbi(), z = nbi(), r = "";
|
|
1625
|
+
this.divRemTo(d, y, z);
|
|
1626
|
+
while (y.signum() > 0) {
|
|
1627
|
+
r = (a + z.intValue()).toString(b).substr(1) + r;
|
|
1628
|
+
y.divRemTo(d, y, z);
|
|
1629
|
+
}
|
|
1630
|
+
return z.intValue().toString(b) + r;
|
|
1631
|
+
}
|
|
1632
|
+
function bnpFromRadix(s, b) {
|
|
1633
|
+
this.fromInt(0);
|
|
1634
|
+
if (b == null) b = 10;
|
|
1635
|
+
var cs = this.chunkSize(b);
|
|
1636
|
+
var d = Math.pow(b, cs), mi = false, j = 0, w = 0;
|
|
1637
|
+
for (var i = 0; i < s.length; ++i) {
|
|
1638
|
+
var x = intAt(s, i);
|
|
1639
|
+
if (x < 0) {
|
|
1640
|
+
if (s.charAt(i) == "-" && this.signum() === 0) mi = true;
|
|
1641
|
+
continue;
|
|
1642
|
+
}
|
|
1643
|
+
w = b * w + x;
|
|
1644
|
+
if (++j >= cs) {
|
|
1645
|
+
this.dMultiply(d);
|
|
1646
|
+
this.dAddOffset(w, 0);
|
|
1647
|
+
j = 0;
|
|
1648
|
+
w = 0;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
if (j > 0) {
|
|
1652
|
+
this.dMultiply(Math.pow(b, j));
|
|
1653
|
+
this.dAddOffset(w, 0);
|
|
1654
|
+
}
|
|
1655
|
+
if (mi) BigInteger.ZERO.subTo(this, this);
|
|
1656
|
+
}
|
|
1657
|
+
function bnpFromNumber(a, b) {
|
|
1658
|
+
if ("number" == typeof b) if (a < 2) this.fromInt(1);
|
|
1659
|
+
else {
|
|
1660
|
+
this.fromNumber(a);
|
|
1661
|
+
if (!this.testBit(a - 1)) this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
|
|
1662
|
+
if (this.isEven()) this.dAddOffset(1, 0);
|
|
1663
|
+
while (!this.isProbablePrime(b)) {
|
|
1664
|
+
this.dAddOffset(2, 0);
|
|
1665
|
+
if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
else {
|
|
1669
|
+
var x = crypt.randomBytes((a >> 3) + 1);
|
|
1670
|
+
var t = a & 7;
|
|
1671
|
+
if (t > 0) x[0] &= (1 << t) - 1;
|
|
1672
|
+
else x[0] = 0;
|
|
1673
|
+
this.fromByteArray(x);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
function bnToByteArray() {
|
|
1677
|
+
var i = this.t, r = new Array();
|
|
1678
|
+
r[0] = this.s;
|
|
1679
|
+
var p = this.DB - i * this.DB % 8, d, k = 0;
|
|
1680
|
+
if (i-- > 0) {
|
|
1681
|
+
if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) r[k++] = d | this.s << this.DB - p;
|
|
1682
|
+
while (i >= 0) {
|
|
1683
|
+
if (p < 8) {
|
|
1684
|
+
d = (this[i] & (1 << p) - 1) << 8 - p;
|
|
1685
|
+
d |= this[--i] >> (p += this.DB - 8);
|
|
1686
|
+
} else {
|
|
1687
|
+
d = this[i] >> (p -= 8) & 255;
|
|
1688
|
+
if (p <= 0) {
|
|
1689
|
+
p += this.DB;
|
|
1690
|
+
--i;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
if ((d & 128) != 0) d |= -256;
|
|
1694
|
+
if (k === 0 && (this.s & 128) != (d & 128)) ++k;
|
|
1695
|
+
if (k > 0 || d != this.s) r[k++] = d;
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
return r;
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* return Buffer object
|
|
1702
|
+
* @param trim {boolean} slice buffer if first element == 0
|
|
1703
|
+
* @returns {Buffer}
|
|
1704
|
+
*/
|
|
1705
|
+
function bnToBuffer(trimOrSize) {
|
|
1706
|
+
var res = Buffer.from(this.toByteArray());
|
|
1707
|
+
if (trimOrSize === true && res[0] === 0) res = res.slice(1);
|
|
1708
|
+
else if (typeof trimOrSize == "number") {
|
|
1709
|
+
if (res.length > trimOrSize) {
|
|
1710
|
+
for (var i = 0; i < res.length - trimOrSize; i++) if (res[i] !== 0) return null;
|
|
1711
|
+
return res.slice(res.length - trimOrSize);
|
|
1712
|
+
} else if (res.length < trimOrSize) {
|
|
1713
|
+
var padded = Buffer.alloc(trimOrSize);
|
|
1714
|
+
padded.fill(0, 0, trimOrSize - res.length);
|
|
1715
|
+
res.copy(padded, trimOrSize - res.length);
|
|
1716
|
+
return padded;
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
return res;
|
|
1720
|
+
}
|
|
1721
|
+
function bnEquals(a) {
|
|
1722
|
+
return this.compareTo(a) == 0;
|
|
1723
|
+
}
|
|
1724
|
+
function bnMin(a) {
|
|
1725
|
+
return this.compareTo(a) < 0 ? this : a;
|
|
1726
|
+
}
|
|
1727
|
+
function bnMax(a) {
|
|
1728
|
+
return this.compareTo(a) > 0 ? this : a;
|
|
1729
|
+
}
|
|
1730
|
+
function bnpBitwiseTo(a, op, r) {
|
|
1731
|
+
var i, f, m = Math.min(a.t, this.t);
|
|
1732
|
+
for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
|
|
1733
|
+
if (a.t < this.t) {
|
|
1734
|
+
f = a.s & this.DM;
|
|
1735
|
+
for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
|
|
1736
|
+
r.t = this.t;
|
|
1737
|
+
} else {
|
|
1738
|
+
f = this.s & this.DM;
|
|
1739
|
+
for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
|
|
1740
|
+
r.t = a.t;
|
|
1741
|
+
}
|
|
1742
|
+
r.s = op(this.s, a.s);
|
|
1743
|
+
r.clamp();
|
|
1744
|
+
}
|
|
1745
|
+
function op_and(x, y) {
|
|
1746
|
+
return x & y;
|
|
1747
|
+
}
|
|
1748
|
+
function bnAnd(a) {
|
|
1749
|
+
var r = nbi();
|
|
1750
|
+
this.bitwiseTo(a, op_and, r);
|
|
1751
|
+
return r;
|
|
1752
|
+
}
|
|
1753
|
+
function op_or(x, y) {
|
|
1754
|
+
return x | y;
|
|
1755
|
+
}
|
|
1756
|
+
function bnOr(a) {
|
|
1757
|
+
var r = nbi();
|
|
1758
|
+
this.bitwiseTo(a, op_or, r);
|
|
1759
|
+
return r;
|
|
1760
|
+
}
|
|
1761
|
+
function op_xor(x, y) {
|
|
1762
|
+
return x ^ y;
|
|
1763
|
+
}
|
|
1764
|
+
function bnXor(a) {
|
|
1765
|
+
var r = nbi();
|
|
1766
|
+
this.bitwiseTo(a, op_xor, r);
|
|
1767
|
+
return r;
|
|
1768
|
+
}
|
|
1769
|
+
function op_andnot(x, y) {
|
|
1770
|
+
return x & ~y;
|
|
1771
|
+
}
|
|
1772
|
+
function bnAndNot(a) {
|
|
1773
|
+
var r = nbi();
|
|
1774
|
+
this.bitwiseTo(a, op_andnot, r);
|
|
1775
|
+
return r;
|
|
1776
|
+
}
|
|
1777
|
+
function bnNot() {
|
|
1778
|
+
var r = nbi();
|
|
1779
|
+
for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i];
|
|
1780
|
+
r.t = this.t;
|
|
1781
|
+
r.s = ~this.s;
|
|
1782
|
+
return r;
|
|
1783
|
+
}
|
|
1784
|
+
function bnShiftLeft(n) {
|
|
1785
|
+
var r = nbi();
|
|
1786
|
+
if (n < 0) this.rShiftTo(-n, r);
|
|
1787
|
+
else this.lShiftTo(n, r);
|
|
1788
|
+
return r;
|
|
1789
|
+
}
|
|
1790
|
+
function bnShiftRight(n) {
|
|
1791
|
+
var r = nbi();
|
|
1792
|
+
if (n < 0) this.lShiftTo(-n, r);
|
|
1793
|
+
else this.rShiftTo(n, r);
|
|
1794
|
+
return r;
|
|
1795
|
+
}
|
|
1796
|
+
function lbit(x) {
|
|
1797
|
+
if (x === 0) return -1;
|
|
1798
|
+
var r = 0;
|
|
1799
|
+
if ((x & 65535) === 0) {
|
|
1800
|
+
x >>= 16;
|
|
1801
|
+
r += 16;
|
|
1802
|
+
}
|
|
1803
|
+
if ((x & 255) === 0) {
|
|
1804
|
+
x >>= 8;
|
|
1805
|
+
r += 8;
|
|
1806
|
+
}
|
|
1807
|
+
if ((x & 15) === 0) {
|
|
1808
|
+
x >>= 4;
|
|
1809
|
+
r += 4;
|
|
1810
|
+
}
|
|
1811
|
+
if ((x & 3) === 0) {
|
|
1812
|
+
x >>= 2;
|
|
1813
|
+
r += 2;
|
|
1814
|
+
}
|
|
1815
|
+
if ((x & 1) === 0) ++r;
|
|
1816
|
+
return r;
|
|
1817
|
+
}
|
|
1818
|
+
function bnGetLowestSetBit() {
|
|
1819
|
+
for (var i = 0; i < this.t; ++i) if (this[i] != 0) return i * this.DB + lbit(this[i]);
|
|
1820
|
+
if (this.s < 0) return this.t * this.DB;
|
|
1821
|
+
return -1;
|
|
1822
|
+
}
|
|
1823
|
+
function cbit(x) {
|
|
1824
|
+
var r = 0;
|
|
1825
|
+
while (x != 0) {
|
|
1826
|
+
x &= x - 1;
|
|
1827
|
+
++r;
|
|
1828
|
+
}
|
|
1829
|
+
return r;
|
|
1830
|
+
}
|
|
1831
|
+
function bnBitCount() {
|
|
1832
|
+
var r = 0, x = this.s & this.DM;
|
|
1833
|
+
for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
|
|
1834
|
+
return r;
|
|
1835
|
+
}
|
|
1836
|
+
function bnTestBit(n) {
|
|
1837
|
+
var j = Math.floor(n / this.DB);
|
|
1838
|
+
if (j >= this.t) return this.s != 0;
|
|
1839
|
+
return (this[j] & 1 << n % this.DB) != 0;
|
|
1840
|
+
}
|
|
1841
|
+
function bnpChangeBit(n, op) {
|
|
1842
|
+
var r = BigInteger.ONE.shiftLeft(n);
|
|
1843
|
+
this.bitwiseTo(r, op, r);
|
|
1844
|
+
return r;
|
|
1845
|
+
}
|
|
1846
|
+
function bnSetBit(n) {
|
|
1847
|
+
return this.changeBit(n, op_or);
|
|
1848
|
+
}
|
|
1849
|
+
function bnClearBit(n) {
|
|
1850
|
+
return this.changeBit(n, op_andnot);
|
|
1851
|
+
}
|
|
1852
|
+
function bnFlipBit(n) {
|
|
1853
|
+
return this.changeBit(n, op_xor);
|
|
1854
|
+
}
|
|
1855
|
+
function bnpAddTo(a, r) {
|
|
1856
|
+
var i = 0, c = 0, m = Math.min(a.t, this.t);
|
|
1857
|
+
while (i < m) {
|
|
1858
|
+
c += this[i] + a[i];
|
|
1859
|
+
r[i++] = c & this.DM;
|
|
1860
|
+
c >>= this.DB;
|
|
1861
|
+
}
|
|
1862
|
+
if (a.t < this.t) {
|
|
1863
|
+
c += a.s;
|
|
1864
|
+
while (i < this.t) {
|
|
1865
|
+
c += this[i];
|
|
1866
|
+
r[i++] = c & this.DM;
|
|
1867
|
+
c >>= this.DB;
|
|
1868
|
+
}
|
|
1869
|
+
c += this.s;
|
|
1870
|
+
} else {
|
|
1871
|
+
c += this.s;
|
|
1872
|
+
while (i < a.t) {
|
|
1873
|
+
c += a[i];
|
|
1874
|
+
r[i++] = c & this.DM;
|
|
1875
|
+
c >>= this.DB;
|
|
1876
|
+
}
|
|
1877
|
+
c += a.s;
|
|
1878
|
+
}
|
|
1879
|
+
r.s = c < 0 ? -1 : 0;
|
|
1880
|
+
if (c > 0) r[i++] = c;
|
|
1881
|
+
else if (c < -1) r[i++] = this.DV + c;
|
|
1882
|
+
r.t = i;
|
|
1883
|
+
r.clamp();
|
|
1884
|
+
}
|
|
1885
|
+
function bnAdd(a) {
|
|
1886
|
+
var r = nbi();
|
|
1887
|
+
this.addTo(a, r);
|
|
1888
|
+
return r;
|
|
1889
|
+
}
|
|
1890
|
+
function bnSubtract(a) {
|
|
1891
|
+
var r = nbi();
|
|
1892
|
+
this.subTo(a, r);
|
|
1893
|
+
return r;
|
|
1894
|
+
}
|
|
1895
|
+
function bnMultiply(a) {
|
|
1896
|
+
var r = nbi();
|
|
1897
|
+
this.multiplyTo(a, r);
|
|
1898
|
+
return r;
|
|
1899
|
+
}
|
|
1900
|
+
function bnSquare() {
|
|
1901
|
+
var r = nbi();
|
|
1902
|
+
this.squareTo(r);
|
|
1903
|
+
return r;
|
|
1904
|
+
}
|
|
1905
|
+
function bnDivide(a) {
|
|
1906
|
+
var r = nbi();
|
|
1907
|
+
this.divRemTo(a, r, null);
|
|
1908
|
+
return r;
|
|
1909
|
+
}
|
|
1910
|
+
function bnRemainder(a) {
|
|
1911
|
+
var r = nbi();
|
|
1912
|
+
this.divRemTo(a, null, r);
|
|
1913
|
+
return r;
|
|
1914
|
+
}
|
|
1915
|
+
function bnDivideAndRemainder(a) {
|
|
1916
|
+
var q = nbi(), r = nbi();
|
|
1917
|
+
this.divRemTo(a, q, r);
|
|
1918
|
+
return new Array(q, r);
|
|
1919
|
+
}
|
|
1920
|
+
function bnpDMultiply(n) {
|
|
1921
|
+
this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
|
|
1922
|
+
++this.t;
|
|
1923
|
+
this.clamp();
|
|
1924
|
+
}
|
|
1925
|
+
function bnpDAddOffset(n, w) {
|
|
1926
|
+
if (n === 0) return;
|
|
1927
|
+
while (this.t <= w) this[this.t++] = 0;
|
|
1928
|
+
this[w] += n;
|
|
1929
|
+
while (this[w] >= this.DV) {
|
|
1930
|
+
this[w] -= this.DV;
|
|
1931
|
+
if (++w >= this.t) this[this.t++] = 0;
|
|
1932
|
+
++this[w];
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
function NullExp() {}
|
|
1936
|
+
function nNop(x) {
|
|
1937
|
+
return x;
|
|
1938
|
+
}
|
|
1939
|
+
function nMulTo(x, y, r) {
|
|
1940
|
+
x.multiplyTo(y, r);
|
|
1941
|
+
}
|
|
1942
|
+
function nSqrTo(x, r) {
|
|
1943
|
+
x.squareTo(r);
|
|
1944
|
+
}
|
|
1945
|
+
NullExp.prototype.convert = nNop;
|
|
1946
|
+
NullExp.prototype.revert = nNop;
|
|
1947
|
+
NullExp.prototype.mulTo = nMulTo;
|
|
1948
|
+
NullExp.prototype.sqrTo = nSqrTo;
|
|
1949
|
+
function bnPow(e) {
|
|
1950
|
+
return this.exp(e, new NullExp());
|
|
1951
|
+
}
|
|
1952
|
+
function bnpMultiplyLowerTo(a, n, r) {
|
|
1953
|
+
var i = Math.min(this.t + a.t, n);
|
|
1954
|
+
r.s = 0;
|
|
1955
|
+
r.t = i;
|
|
1956
|
+
while (i > 0) r[--i] = 0;
|
|
1957
|
+
var j;
|
|
1958
|
+
for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
|
|
1959
|
+
for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
|
|
1960
|
+
r.clamp();
|
|
1961
|
+
}
|
|
1962
|
+
function bnpMultiplyUpperTo(a, n, r) {
|
|
1963
|
+
--n;
|
|
1964
|
+
var i = r.t = this.t + a.t - n;
|
|
1965
|
+
r.s = 0;
|
|
1966
|
+
while (--i >= 0) r[i] = 0;
|
|
1967
|
+
for (i = Math.max(n - this.t, 0); i < a.t; ++i) r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
|
|
1968
|
+
r.clamp();
|
|
1969
|
+
r.drShiftTo(1, r);
|
|
1970
|
+
}
|
|
1971
|
+
function Barrett(m) {
|
|
1972
|
+
this.r2 = nbi();
|
|
1973
|
+
this.q3 = nbi();
|
|
1974
|
+
BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
|
|
1975
|
+
this.mu = this.r2.divide(m);
|
|
1976
|
+
this.m = m;
|
|
1977
|
+
}
|
|
1978
|
+
function barrettConvert(x) {
|
|
1979
|
+
if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);
|
|
1980
|
+
else if (x.compareTo(this.m) < 0) return x;
|
|
1981
|
+
else {
|
|
1982
|
+
var r = nbi();
|
|
1983
|
+
x.copyTo(r);
|
|
1984
|
+
this.reduce(r);
|
|
1985
|
+
return r;
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
function barrettRevert(x) {
|
|
1989
|
+
return x;
|
|
1990
|
+
}
|
|
1991
|
+
function barrettReduce(x) {
|
|
1992
|
+
x.drShiftTo(this.m.t - 1, this.r2);
|
|
1993
|
+
if (x.t > this.m.t + 1) {
|
|
1994
|
+
x.t = this.m.t + 1;
|
|
1995
|
+
x.clamp();
|
|
1996
|
+
}
|
|
1997
|
+
this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
|
|
1998
|
+
this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
|
|
1999
|
+
while (x.compareTo(this.r2) < 0) x.dAddOffset(1, this.m.t + 1);
|
|
2000
|
+
x.subTo(this.r2, x);
|
|
2001
|
+
while (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
|
|
2002
|
+
}
|
|
2003
|
+
function barrettSqrTo(x, r) {
|
|
2004
|
+
x.squareTo(r);
|
|
2005
|
+
this.reduce(r);
|
|
2006
|
+
}
|
|
2007
|
+
function barrettMulTo(x, y, r) {
|
|
2008
|
+
x.multiplyTo(y, r);
|
|
2009
|
+
this.reduce(r);
|
|
2010
|
+
}
|
|
2011
|
+
Barrett.prototype.convert = barrettConvert;
|
|
2012
|
+
Barrett.prototype.revert = barrettRevert;
|
|
2013
|
+
Barrett.prototype.reduce = barrettReduce;
|
|
2014
|
+
Barrett.prototype.mulTo = barrettMulTo;
|
|
2015
|
+
Barrett.prototype.sqrTo = barrettSqrTo;
|
|
2016
|
+
function bnModPow(e, m) {
|
|
2017
|
+
var i = e.bitLength(), k, r = nbv(1), z;
|
|
2018
|
+
if (i <= 0) return r;
|
|
2019
|
+
else if (i < 18) k = 1;
|
|
2020
|
+
else if (i < 48) k = 3;
|
|
2021
|
+
else if (i < 144) k = 4;
|
|
2022
|
+
else if (i < 768) k = 5;
|
|
2023
|
+
else k = 6;
|
|
2024
|
+
if (i < 8) z = new Classic(m);
|
|
2025
|
+
else if (m.isEven()) z = new Barrett(m);
|
|
2026
|
+
else z = new Montgomery(m);
|
|
2027
|
+
var g = new Array(), n = 3, k1 = k - 1, km = (1 << k) - 1;
|
|
2028
|
+
g[1] = z.convert(this);
|
|
2029
|
+
if (k > 1) {
|
|
2030
|
+
var g2 = nbi();
|
|
2031
|
+
z.sqrTo(g[1], g2);
|
|
2032
|
+
while (n <= km) {
|
|
2033
|
+
g[n] = nbi();
|
|
2034
|
+
z.mulTo(g2, g[n - 2], g[n]);
|
|
2035
|
+
n += 2;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
var j = e.t - 1, w, is1 = true, r2 = nbi(), t;
|
|
2039
|
+
i = nbits(e[j]) - 1;
|
|
2040
|
+
while (j >= 0) {
|
|
2041
|
+
if (i >= k1) w = e[j] >> i - k1 & km;
|
|
2042
|
+
else {
|
|
2043
|
+
w = (e[j] & (1 << i + 1) - 1) << k1 - i;
|
|
2044
|
+
if (j > 0) w |= e[j - 1] >> this.DB + i - k1;
|
|
2045
|
+
}
|
|
2046
|
+
n = k;
|
|
2047
|
+
while ((w & 1) === 0) {
|
|
2048
|
+
w >>= 1;
|
|
2049
|
+
--n;
|
|
2050
|
+
}
|
|
2051
|
+
if ((i -= n) < 0) {
|
|
2052
|
+
i += this.DB;
|
|
2053
|
+
--j;
|
|
2054
|
+
}
|
|
2055
|
+
if (is1) {
|
|
2056
|
+
g[w].copyTo(r);
|
|
2057
|
+
is1 = false;
|
|
2058
|
+
} else {
|
|
2059
|
+
while (n > 1) {
|
|
2060
|
+
z.sqrTo(r, r2);
|
|
2061
|
+
z.sqrTo(r2, r);
|
|
2062
|
+
n -= 2;
|
|
2063
|
+
}
|
|
2064
|
+
if (n > 0) z.sqrTo(r, r2);
|
|
2065
|
+
else {
|
|
2066
|
+
t = r;
|
|
2067
|
+
r = r2;
|
|
2068
|
+
r2 = t;
|
|
2069
|
+
}
|
|
2070
|
+
z.mulTo(r2, g[w], r);
|
|
2071
|
+
}
|
|
2072
|
+
while (j >= 0 && (e[j] & 1 << i) === 0) {
|
|
2073
|
+
z.sqrTo(r, r2);
|
|
2074
|
+
t = r;
|
|
2075
|
+
r = r2;
|
|
2076
|
+
r2 = t;
|
|
2077
|
+
if (--i < 0) {
|
|
2078
|
+
i = this.DB - 1;
|
|
2079
|
+
--j;
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
return z.revert(r);
|
|
2084
|
+
}
|
|
2085
|
+
function bnGCD(a) {
|
|
2086
|
+
var x = this.s < 0 ? this.negate() : this.clone();
|
|
2087
|
+
var y = a.s < 0 ? a.negate() : a.clone();
|
|
2088
|
+
if (x.compareTo(y) < 0) {
|
|
2089
|
+
var t = x;
|
|
2090
|
+
x = y;
|
|
2091
|
+
y = t;
|
|
2092
|
+
}
|
|
2093
|
+
var i = x.getLowestSetBit(), g = y.getLowestSetBit();
|
|
2094
|
+
if (g < 0) return x;
|
|
2095
|
+
if (i < g) g = i;
|
|
2096
|
+
if (g > 0) {
|
|
2097
|
+
x.rShiftTo(g, x);
|
|
2098
|
+
y.rShiftTo(g, y);
|
|
2099
|
+
}
|
|
2100
|
+
while (x.signum() > 0) {
|
|
2101
|
+
if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
|
|
2102
|
+
if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
|
|
2103
|
+
if (x.compareTo(y) >= 0) {
|
|
2104
|
+
x.subTo(y, x);
|
|
2105
|
+
x.rShiftTo(1, x);
|
|
2106
|
+
} else {
|
|
2107
|
+
y.subTo(x, y);
|
|
2108
|
+
y.rShiftTo(1, y);
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
if (g > 0) y.lShiftTo(g, y);
|
|
2112
|
+
return y;
|
|
2113
|
+
}
|
|
2114
|
+
function bnpModInt(n) {
|
|
2115
|
+
if (n <= 0) return 0;
|
|
2116
|
+
var d = this.DV % n, r = this.s < 0 ? n - 1 : 0;
|
|
2117
|
+
if (this.t > 0) if (d === 0) r = this[0] % n;
|
|
2118
|
+
else for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
|
|
2119
|
+
return r;
|
|
2120
|
+
}
|
|
2121
|
+
function bnModInverse(m) {
|
|
2122
|
+
var ac = m.isEven();
|
|
2123
|
+
if (this.isEven() && ac || m.signum() === 0) return BigInteger.ZERO;
|
|
2124
|
+
var u = m.clone(), v = this.clone();
|
|
2125
|
+
var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
|
|
2126
|
+
while (u.signum() != 0) {
|
|
2127
|
+
while (u.isEven()) {
|
|
2128
|
+
u.rShiftTo(1, u);
|
|
2129
|
+
if (ac) {
|
|
2130
|
+
if (!a.isEven() || !b.isEven()) {
|
|
2131
|
+
a.addTo(this, a);
|
|
2132
|
+
b.subTo(m, b);
|
|
2133
|
+
}
|
|
2134
|
+
a.rShiftTo(1, a);
|
|
2135
|
+
} else if (!b.isEven()) b.subTo(m, b);
|
|
2136
|
+
b.rShiftTo(1, b);
|
|
2137
|
+
}
|
|
2138
|
+
while (v.isEven()) {
|
|
2139
|
+
v.rShiftTo(1, v);
|
|
2140
|
+
if (ac) {
|
|
2141
|
+
if (!c.isEven() || !d.isEven()) {
|
|
2142
|
+
c.addTo(this, c);
|
|
2143
|
+
d.subTo(m, d);
|
|
2144
|
+
}
|
|
2145
|
+
c.rShiftTo(1, c);
|
|
2146
|
+
} else if (!d.isEven()) d.subTo(m, d);
|
|
2147
|
+
d.rShiftTo(1, d);
|
|
2148
|
+
}
|
|
2149
|
+
if (u.compareTo(v) >= 0) {
|
|
2150
|
+
u.subTo(v, u);
|
|
2151
|
+
if (ac) a.subTo(c, a);
|
|
2152
|
+
b.subTo(d, b);
|
|
2153
|
+
} else {
|
|
2154
|
+
v.subTo(u, v);
|
|
2155
|
+
if (ac) c.subTo(a, c);
|
|
2156
|
+
d.subTo(b, d);
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
|
|
2160
|
+
if (d.compareTo(m) >= 0) return d.subtract(m);
|
|
2161
|
+
if (d.signum() < 0) d.addTo(m, d);
|
|
2162
|
+
else return d;
|
|
2163
|
+
if (d.signum() < 0) return d.add(m);
|
|
2164
|
+
else return d;
|
|
2165
|
+
}
|
|
2166
|
+
var lowprimes = [
|
|
2167
|
+
2,
|
|
2168
|
+
3,
|
|
2169
|
+
5,
|
|
2170
|
+
7,
|
|
2171
|
+
11,
|
|
2172
|
+
13,
|
|
2173
|
+
17,
|
|
2174
|
+
19,
|
|
2175
|
+
23,
|
|
2176
|
+
29,
|
|
2177
|
+
31,
|
|
2178
|
+
37,
|
|
2179
|
+
41,
|
|
2180
|
+
43,
|
|
2181
|
+
47,
|
|
2182
|
+
53,
|
|
2183
|
+
59,
|
|
2184
|
+
61,
|
|
2185
|
+
67,
|
|
2186
|
+
71,
|
|
2187
|
+
73,
|
|
2188
|
+
79,
|
|
2189
|
+
83,
|
|
2190
|
+
89,
|
|
2191
|
+
97,
|
|
2192
|
+
101,
|
|
2193
|
+
103,
|
|
2194
|
+
107,
|
|
2195
|
+
109,
|
|
2196
|
+
113,
|
|
2197
|
+
127,
|
|
2198
|
+
131,
|
|
2199
|
+
137,
|
|
2200
|
+
139,
|
|
2201
|
+
149,
|
|
2202
|
+
151,
|
|
2203
|
+
157,
|
|
2204
|
+
163,
|
|
2205
|
+
167,
|
|
2206
|
+
173,
|
|
2207
|
+
179,
|
|
2208
|
+
181,
|
|
2209
|
+
191,
|
|
2210
|
+
193,
|
|
2211
|
+
197,
|
|
2212
|
+
199,
|
|
2213
|
+
211,
|
|
2214
|
+
223,
|
|
2215
|
+
227,
|
|
2216
|
+
229,
|
|
2217
|
+
233,
|
|
2218
|
+
239,
|
|
2219
|
+
241,
|
|
2220
|
+
251,
|
|
2221
|
+
257,
|
|
2222
|
+
263,
|
|
2223
|
+
269,
|
|
2224
|
+
271,
|
|
2225
|
+
277,
|
|
2226
|
+
281,
|
|
2227
|
+
283,
|
|
2228
|
+
293,
|
|
2229
|
+
307,
|
|
2230
|
+
311,
|
|
2231
|
+
313,
|
|
2232
|
+
317,
|
|
2233
|
+
331,
|
|
2234
|
+
337,
|
|
2235
|
+
347,
|
|
2236
|
+
349,
|
|
2237
|
+
353,
|
|
2238
|
+
359,
|
|
2239
|
+
367,
|
|
2240
|
+
373,
|
|
2241
|
+
379,
|
|
2242
|
+
383,
|
|
2243
|
+
389,
|
|
2244
|
+
397,
|
|
2245
|
+
401,
|
|
2246
|
+
409,
|
|
2247
|
+
419,
|
|
2248
|
+
421,
|
|
2249
|
+
431,
|
|
2250
|
+
433,
|
|
2251
|
+
439,
|
|
2252
|
+
443,
|
|
2253
|
+
449,
|
|
2254
|
+
457,
|
|
2255
|
+
461,
|
|
2256
|
+
463,
|
|
2257
|
+
467,
|
|
2258
|
+
479,
|
|
2259
|
+
487,
|
|
2260
|
+
491,
|
|
2261
|
+
499,
|
|
2262
|
+
503,
|
|
2263
|
+
509,
|
|
2264
|
+
521,
|
|
2265
|
+
523,
|
|
2266
|
+
541,
|
|
2267
|
+
547,
|
|
2268
|
+
557,
|
|
2269
|
+
563,
|
|
2270
|
+
569,
|
|
2271
|
+
571,
|
|
2272
|
+
577,
|
|
2273
|
+
587,
|
|
2274
|
+
593,
|
|
2275
|
+
599,
|
|
2276
|
+
601,
|
|
2277
|
+
607,
|
|
2278
|
+
613,
|
|
2279
|
+
617,
|
|
2280
|
+
619,
|
|
2281
|
+
631,
|
|
2282
|
+
641,
|
|
2283
|
+
643,
|
|
2284
|
+
647,
|
|
2285
|
+
653,
|
|
2286
|
+
659,
|
|
2287
|
+
661,
|
|
2288
|
+
673,
|
|
2289
|
+
677,
|
|
2290
|
+
683,
|
|
2291
|
+
691,
|
|
2292
|
+
701,
|
|
2293
|
+
709,
|
|
2294
|
+
719,
|
|
2295
|
+
727,
|
|
2296
|
+
733,
|
|
2297
|
+
739,
|
|
2298
|
+
743,
|
|
2299
|
+
751,
|
|
2300
|
+
757,
|
|
2301
|
+
761,
|
|
2302
|
+
769,
|
|
2303
|
+
773,
|
|
2304
|
+
787,
|
|
2305
|
+
797,
|
|
2306
|
+
809,
|
|
2307
|
+
811,
|
|
2308
|
+
821,
|
|
2309
|
+
823,
|
|
2310
|
+
827,
|
|
2311
|
+
829,
|
|
2312
|
+
839,
|
|
2313
|
+
853,
|
|
2314
|
+
857,
|
|
2315
|
+
859,
|
|
2316
|
+
863,
|
|
2317
|
+
877,
|
|
2318
|
+
881,
|
|
2319
|
+
883,
|
|
2320
|
+
887,
|
|
2321
|
+
907,
|
|
2322
|
+
911,
|
|
2323
|
+
919,
|
|
2324
|
+
929,
|
|
2325
|
+
937,
|
|
2326
|
+
941,
|
|
2327
|
+
947,
|
|
2328
|
+
953,
|
|
2329
|
+
967,
|
|
2330
|
+
971,
|
|
2331
|
+
977,
|
|
2332
|
+
983,
|
|
2333
|
+
991,
|
|
2334
|
+
997
|
|
2335
|
+
];
|
|
2336
|
+
var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
|
|
2337
|
+
function bnIsProbablePrime(t) {
|
|
2338
|
+
var i, x = this.abs();
|
|
2339
|
+
if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
|
|
2340
|
+
for (i = 0; i < lowprimes.length; ++i) if (x[0] == lowprimes[i]) return true;
|
|
2341
|
+
return false;
|
|
2342
|
+
}
|
|
2343
|
+
if (x.isEven()) return false;
|
|
2344
|
+
i = 1;
|
|
2345
|
+
while (i < lowprimes.length) {
|
|
2346
|
+
var m = lowprimes[i], j = i + 1;
|
|
2347
|
+
while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
|
|
2348
|
+
m = x.modInt(m);
|
|
2349
|
+
while (i < j) if (m % lowprimes[i++] === 0) return false;
|
|
2350
|
+
}
|
|
2351
|
+
return x.millerRabin(t);
|
|
2352
|
+
}
|
|
2353
|
+
function bnpMillerRabin(t) {
|
|
2354
|
+
var n1 = this.subtract(BigInteger.ONE);
|
|
2355
|
+
var k = n1.getLowestSetBit();
|
|
2356
|
+
if (k <= 0) return false;
|
|
2357
|
+
var r = n1.shiftRight(k);
|
|
2358
|
+
t = t + 1 >> 1;
|
|
2359
|
+
if (t > lowprimes.length) t = lowprimes.length;
|
|
2360
|
+
var a = nbi();
|
|
2361
|
+
for (var i = 0; i < t; ++i) {
|
|
2362
|
+
a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
|
|
2363
|
+
var y = a.modPow(r, this);
|
|
2364
|
+
if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
|
|
2365
|
+
var j = 1;
|
|
2366
|
+
while (j++ < k && y.compareTo(n1) != 0) {
|
|
2367
|
+
y = y.modPowInt(2, this);
|
|
2368
|
+
if (y.compareTo(BigInteger.ONE) === 0) return false;
|
|
2369
|
+
}
|
|
2370
|
+
if (y.compareTo(n1) != 0) return false;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
return true;
|
|
2374
|
+
}
|
|
2375
|
+
BigInteger.prototype.copyTo = bnpCopyTo;
|
|
2376
|
+
BigInteger.prototype.fromInt = bnpFromInt;
|
|
2377
|
+
BigInteger.prototype.fromString = bnpFromString;
|
|
2378
|
+
BigInteger.prototype.fromByteArray = bnpFromByteArray;
|
|
2379
|
+
BigInteger.prototype.fromBuffer = bnpFromBuffer;
|
|
2380
|
+
BigInteger.prototype.clamp = bnpClamp;
|
|
2381
|
+
BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
|
|
2382
|
+
BigInteger.prototype.drShiftTo = bnpDRShiftTo;
|
|
2383
|
+
BigInteger.prototype.lShiftTo = bnpLShiftTo;
|
|
2384
|
+
BigInteger.prototype.rShiftTo = bnpRShiftTo;
|
|
2385
|
+
BigInteger.prototype.subTo = bnpSubTo;
|
|
2386
|
+
BigInteger.prototype.multiplyTo = bnpMultiplyTo;
|
|
2387
|
+
BigInteger.prototype.squareTo = bnpSquareTo;
|
|
2388
|
+
BigInteger.prototype.divRemTo = bnpDivRemTo;
|
|
2389
|
+
BigInteger.prototype.invDigit = bnpInvDigit;
|
|
2390
|
+
BigInteger.prototype.isEven = bnpIsEven;
|
|
2391
|
+
BigInteger.prototype.exp = bnpExp;
|
|
2392
|
+
BigInteger.prototype.chunkSize = bnpChunkSize;
|
|
2393
|
+
BigInteger.prototype.toRadix = bnpToRadix;
|
|
2394
|
+
BigInteger.prototype.fromRadix = bnpFromRadix;
|
|
2395
|
+
BigInteger.prototype.fromNumber = bnpFromNumber;
|
|
2396
|
+
BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
|
|
2397
|
+
BigInteger.prototype.changeBit = bnpChangeBit;
|
|
2398
|
+
BigInteger.prototype.addTo = bnpAddTo;
|
|
2399
|
+
BigInteger.prototype.dMultiply = bnpDMultiply;
|
|
2400
|
+
BigInteger.prototype.dAddOffset = bnpDAddOffset;
|
|
2401
|
+
BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
|
|
2402
|
+
BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
|
|
2403
|
+
BigInteger.prototype.modInt = bnpModInt;
|
|
2404
|
+
BigInteger.prototype.millerRabin = bnpMillerRabin;
|
|
2405
|
+
BigInteger.prototype.toString = bnToString;
|
|
2406
|
+
BigInteger.prototype.negate = bnNegate;
|
|
2407
|
+
BigInteger.prototype.abs = bnAbs;
|
|
2408
|
+
BigInteger.prototype.compareTo = bnCompareTo;
|
|
2409
|
+
BigInteger.prototype.bitLength = bnBitLength;
|
|
2410
|
+
BigInteger.prototype.mod = bnMod;
|
|
2411
|
+
BigInteger.prototype.modPowInt = bnModPowInt;
|
|
2412
|
+
BigInteger.prototype.clone = bnClone;
|
|
2413
|
+
BigInteger.prototype.intValue = bnIntValue;
|
|
2414
|
+
BigInteger.prototype.byteValue = bnByteValue;
|
|
2415
|
+
BigInteger.prototype.shortValue = bnShortValue;
|
|
2416
|
+
BigInteger.prototype.signum = bnSigNum;
|
|
2417
|
+
BigInteger.prototype.toByteArray = bnToByteArray;
|
|
2418
|
+
BigInteger.prototype.toBuffer = bnToBuffer;
|
|
2419
|
+
BigInteger.prototype.equals = bnEquals;
|
|
2420
|
+
BigInteger.prototype.eq = bnEqual;
|
|
2421
|
+
BigInteger.prototype.gt = bnGreater;
|
|
2422
|
+
BigInteger.prototype.gte = bnGreaterOrEqual;
|
|
2423
|
+
BigInteger.prototype.lt = bnLesser;
|
|
2424
|
+
BigInteger.prototype.lte = bnLesserOrEqual;
|
|
2425
|
+
BigInteger.prototype.min = bnMin;
|
|
2426
|
+
BigInteger.prototype.max = bnMax;
|
|
2427
|
+
BigInteger.prototype.and = bnAnd;
|
|
2428
|
+
BigInteger.prototype.or = bnOr;
|
|
2429
|
+
BigInteger.prototype.xor = bnXor;
|
|
2430
|
+
BigInteger.prototype.andNot = bnAndNot;
|
|
2431
|
+
BigInteger.prototype.not = bnNot;
|
|
2432
|
+
BigInteger.prototype.shiftLeft = bnShiftLeft;
|
|
2433
|
+
BigInteger.prototype.shiftRight = bnShiftRight;
|
|
2434
|
+
BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
|
|
2435
|
+
BigInteger.prototype.bitCount = bnBitCount;
|
|
2436
|
+
BigInteger.prototype.testBit = bnTestBit;
|
|
2437
|
+
BigInteger.prototype.setBit = bnSetBit;
|
|
2438
|
+
BigInteger.prototype.clearBit = bnClearBit;
|
|
2439
|
+
BigInteger.prototype.flipBit = bnFlipBit;
|
|
2440
|
+
BigInteger.prototype.add = bnAdd;
|
|
2441
|
+
BigInteger.prototype.subtract = bnSubtract;
|
|
2442
|
+
BigInteger.prototype.multiply = bnMultiply;
|
|
2443
|
+
BigInteger.prototype.divide = bnDivide;
|
|
2444
|
+
BigInteger.prototype.remainder = bnRemainder;
|
|
2445
|
+
BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
|
|
2446
|
+
BigInteger.prototype.modPow = bnModPow;
|
|
2447
|
+
BigInteger.prototype.modInverse = bnModInverse;
|
|
2448
|
+
BigInteger.prototype.pow = bnPow;
|
|
2449
|
+
BigInteger.prototype.gcd = bnGCD;
|
|
2450
|
+
BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
|
|
2451
|
+
BigInteger.int2char = int2char;
|
|
2452
|
+
BigInteger.ZERO = nbv(0);
|
|
2453
|
+
BigInteger.ONE = nbv(1);
|
|
2454
|
+
BigInteger.prototype.square = bnSquare;
|
|
2455
|
+
module.exports = BigInteger;
|
|
2456
|
+
}));
|
|
2457
|
+
|
|
2458
|
+
//#endregion
|
|
2459
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/lib/params.js
|
|
2460
|
+
var require_params = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
2461
|
+
/**
|
|
2462
|
+
* SRP Group Parameters
|
|
2463
|
+
* http://tools.ietf.org/html/rfc5054#appendix-A
|
|
2464
|
+
*
|
|
2465
|
+
* The 1024-, 1536-, and 2048-bit groups are taken from software
|
|
2466
|
+
* developed by Tom Wu and Eugene Jhong for the Stanford SRP
|
|
2467
|
+
* distribution, and subsequently proven to be prime. The larger primes
|
|
2468
|
+
* are taken from [MODP], but generators have been calculated that are
|
|
2469
|
+
* primitive roots of N, unlike the generators in [MODP].
|
|
2470
|
+
*
|
|
2471
|
+
* The 1024-bit and 1536-bit groups MUST be supported.
|
|
2472
|
+
*/
|
|
2473
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2474
|
+
exports.params = exports.hex = void 0;
|
|
2475
|
+
const BigInteger = require_jsbn();
|
|
2476
|
+
function hex(s) {
|
|
2477
|
+
return new BigInteger(s.split(/\s|\n/).join(""), 16);
|
|
2478
|
+
}
|
|
2479
|
+
exports.hex = hex;
|
|
2480
|
+
exports.params = {
|
|
2481
|
+
1024: {
|
|
2482
|
+
N_length_bits: 1024,
|
|
2483
|
+
N: hex(`
|
|
2484
|
+
EEAF0AB9 ADB38DD6 9C33F80A FA8FC5E8 60726187 75FF3C0B 9EA2314C
|
|
2485
|
+
9C256576 D674DF74 96EA81D3 383B4813 D692C6E0 E0D5D8E2 50B98BE4
|
|
2486
|
+
8E495C1D 6089DAD1 5DC7D7B4 6154D6B6 CE8EF4AD 69B15D49 82559B29
|
|
2487
|
+
7BCF1885 C529F566 660E57EC 68EDBC3C 05726CC0 2FD4CBF4 976EAA9A
|
|
2488
|
+
FD5138FE 8376435B 9FC61D2F C0EB06E3
|
|
2489
|
+
`),
|
|
2490
|
+
g: hex("02"),
|
|
2491
|
+
hash: "sha1"
|
|
2492
|
+
},
|
|
2493
|
+
1536: {
|
|
2494
|
+
N_length_bits: 1536,
|
|
2495
|
+
N: hex(`
|
|
2496
|
+
9DEF3CAF B939277A B1F12A86 17A47BBB DBA51DF4 99AC4C80 BEEEA961
|
|
2497
|
+
4B19CC4D 5F4F5F55 6E27CBDE 51C6A94B E4607A29 1558903B A0D0F843
|
|
2498
|
+
80B655BB 9A22E8DC DF028A7C EC67F0D0 8134B1C8 B9798914 9B609E0B
|
|
2499
|
+
E3BAB63D 47548381 DBC5B1FC 764E3F4B 53DD9DA1 158BFD3E 2B9C8CF5
|
|
2500
|
+
6EDF0195 39349627 DB2FD53D 24B7C486 65772E43 7D6C7F8C E442734A
|
|
2501
|
+
F7CCB7AE 837C264A E3A9BEB8 7F8A2FE9 B8B5292E 5A021FFF 5E91479E
|
|
2502
|
+
8CE7A28C 2442C6F3 15180F93 499A234D CF76E3FE D135F9BB
|
|
2503
|
+
`),
|
|
2504
|
+
g: hex("02"),
|
|
2505
|
+
hash: "sha1"
|
|
2506
|
+
},
|
|
2507
|
+
2048: {
|
|
2508
|
+
N_length_bits: 2048,
|
|
2509
|
+
N: hex(`
|
|
2510
|
+
AC6BDB41 324A9A9B F166DE5E 1389582F AF72B665 1987EE07 FC319294
|
|
2511
|
+
3DB56050 A37329CB B4A099ED 8193E075 7767A13D D52312AB 4B03310D
|
|
2512
|
+
CD7F48A9 DA04FD50 E8083969 EDB767B0 CF609517 9A163AB3 661A05FB
|
|
2513
|
+
D5FAAAE8 2918A996 2F0B93B8 55F97993 EC975EEA A80D740A DBF4FF74
|
|
2514
|
+
7359D041 D5C33EA7 1D281E44 6B14773B CA97B43A 23FB8016 76BD207A
|
|
2515
|
+
436C6481 F1D2B907 8717461A 5B9D32E6 88F87748 544523B5 24B0D57D
|
|
2516
|
+
5EA77A27 75D2ECFA 032CFBDB F52FB378 61602790 04E57AE6 AF874E73
|
|
2517
|
+
03CE5329 9CCC041C 7BC308D8 2A5698F3 A8D0C382 71AE35F8 E9DBFBB6
|
|
2518
|
+
94B5C803 D89F7AE4 35DE236D 525F5475 9B65E372 FCD68EF2 0FA7111F
|
|
2519
|
+
9E4AFF73
|
|
2520
|
+
`),
|
|
2521
|
+
g: hex("02"),
|
|
2522
|
+
hash: "sha256"
|
|
2523
|
+
},
|
|
2524
|
+
3072: {
|
|
2525
|
+
N_length_bits: 3072,
|
|
2526
|
+
N: hex(`
|
|
2527
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
2528
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
2529
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
2530
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
2531
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
2532
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
2533
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
2534
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
2535
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
2536
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
2537
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
2538
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
2539
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
2540
|
+
E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
|
|
2541
|
+
`),
|
|
2542
|
+
g: hex("05"),
|
|
2543
|
+
hash: "sha256"
|
|
2544
|
+
},
|
|
2545
|
+
hap: {
|
|
2546
|
+
N_length_bits: 3072,
|
|
2547
|
+
N: hex(`
|
|
2548
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
2549
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
2550
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
2551
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
2552
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
2553
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
2554
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
2555
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
2556
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
2557
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
2558
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
2559
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
2560
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
2561
|
+
E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
|
|
2562
|
+
`),
|
|
2563
|
+
g: hex("05"),
|
|
2564
|
+
hash: "sha512"
|
|
2565
|
+
},
|
|
2566
|
+
4096: {
|
|
2567
|
+
N_length_bits: 4096,
|
|
2568
|
+
N: hex(`
|
|
2569
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
2570
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
2571
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
2572
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
2573
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
2574
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
2575
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
2576
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
2577
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
2578
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
2579
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
2580
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
2581
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
2582
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
2583
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
2584
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
2585
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
2586
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199
|
|
2587
|
+
FFFFFFFF FFFFFFFF
|
|
2588
|
+
`),
|
|
2589
|
+
g: hex("05"),
|
|
2590
|
+
hash: "sha256"
|
|
2591
|
+
},
|
|
2592
|
+
6244: {
|
|
2593
|
+
N_length_bits: 6244,
|
|
2594
|
+
N: hex(`
|
|
2595
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
2596
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
2597
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
2598
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
2599
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
2600
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
2601
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
2602
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
2603
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
2604
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
2605
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
2606
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
2607
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
2608
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
2609
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
2610
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
2611
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
2612
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
|
|
2613
|
+
36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD F8FF9406
|
|
2614
|
+
AD9E530E E5DB382F 413001AE B06A53ED 9027D831 179727B0 865A8918
|
|
2615
|
+
DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B DB7F1447 E6CC254B 33205151
|
|
2616
|
+
2BD7AF42 6FB8F401 378CD2BF 5983CA01 C64B92EC F032EA15 D1721D03
|
|
2617
|
+
F482D7CE 6E74FEF6 D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F
|
|
2618
|
+
BEC7E8F3 23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
|
|
2619
|
+
CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328 06A1D58B
|
|
2620
|
+
B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C DA56C9EC 2EF29632
|
|
2621
|
+
387FE8D7 6E3C0468 043E8F66 3F4860EE 12BF2D5B 0B7474D6 E694F91E
|
|
2622
|
+
6DCC4024 FFFFFFFF FFFFFFFF
|
|
2623
|
+
`),
|
|
2624
|
+
g: hex("05"),
|
|
2625
|
+
hash: "sha256"
|
|
2626
|
+
},
|
|
2627
|
+
8192: {
|
|
2628
|
+
N_length_bits: 8192,
|
|
2629
|
+
N: hex(`
|
|
2630
|
+
FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
|
|
2631
|
+
8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
|
|
2632
|
+
302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
|
|
2633
|
+
A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
|
|
2634
|
+
49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
|
|
2635
|
+
FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
|
|
2636
|
+
670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
|
|
2637
|
+
180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
|
|
2638
|
+
3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
|
|
2639
|
+
04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
|
|
2640
|
+
B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
|
|
2641
|
+
1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
|
|
2642
|
+
BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
|
|
2643
|
+
E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7 88719A10 BDBA5B26
|
|
2644
|
+
99C32718 6AF4E23C 1A946834 B6150BDA 2583E9CA 2AD44CE8 DBBBC2DB
|
|
2645
|
+
04DE8EF9 2E8EFC14 1FBECAA6 287C5947 4E6BC05D 99B2964F A090C3A2
|
|
2646
|
+
233BA186 515BE7ED 1F612970 CEE2D7AF B81BDD76 2170481C D0069127
|
|
2647
|
+
D5B05AA9 93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492
|
|
2648
|
+
36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD F8FF9406
|
|
2649
|
+
AD9E530E E5DB382F 413001AE B06A53ED 9027D831 179727B0 865A8918
|
|
2650
|
+
DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B DB7F1447 E6CC254B 33205151
|
|
2651
|
+
2BD7AF42 6FB8F401 378CD2BF 5983CA01 C64B92EC F032EA15 D1721D03
|
|
2652
|
+
F482D7CE 6E74FEF6 D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F
|
|
2653
|
+
BEC7E8F3 23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA
|
|
2654
|
+
CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328 06A1D58B
|
|
2655
|
+
B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C DA56C9EC 2EF29632
|
|
2656
|
+
387FE8D7 6E3C0468 043E8F66 3F4860EE 12BF2D5B 0B7474D6 E694F91E
|
|
2657
|
+
6DBE1159 74A3926F 12FEE5E4 38777CB6 A932DF8C D8BEC4D0 73B931BA
|
|
2658
|
+
3BC832B6 8D9DD300 741FA7BF 8AFC47ED 2576F693 6BA42466 3AAB639C
|
|
2659
|
+
5AE4F568 3423B474 2BF1C978 238F16CB E39D652D E3FDB8BE FC848AD9
|
|
2660
|
+
22222E04 A4037C07 13EB57A8 1A23F0C7 3473FC64 6CEA306B 4BCBC886
|
|
2661
|
+
2F8385DD FA9D4B7F A2C087E8 79683303 ED5BDD3A 062B3CF5 B3A278A6
|
|
2662
|
+
6D2A13F8 3F44F82D DF310EE0 74AB6A36 4597E899 A0255DC1 64F31CC5
|
|
2663
|
+
0846851D F9AB4819 5DED7EA1 B1D510BD 7EE74D73 FAF36BC3 1ECFA268
|
|
2664
|
+
359046F4 EB879F92 4009438B 481C6CD7 889A002E D5EE382B C9190DA6
|
|
2665
|
+
FC026E47 9558E447 5677E9AA 9E3050E2 765694DF C81F56E8 80B96E71
|
|
2666
|
+
60C980DD 98EDD3DF FFFFFFFF FFFFFFFF
|
|
2667
|
+
`),
|
|
2668
|
+
g: hex("13"),
|
|
2669
|
+
hash: "sha256"
|
|
2670
|
+
}
|
|
2671
|
+
};
|
|
2672
|
+
exports.default = exports.params;
|
|
2673
|
+
}));
|
|
2674
|
+
|
|
2675
|
+
//#endregion
|
|
2676
|
+
//#region ../../node_modules/.bun/fast-srp-hap@2.0.4/node_modules/fast-srp-hap/lib/srp.js
|
|
2677
|
+
var require_srp = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
2678
|
+
var __importDefault = exports && exports.__importDefault || function(mod) {
|
|
2679
|
+
return mod && mod.__esModule ? mod : { "default": mod };
|
|
2680
|
+
};
|
|
2681
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2682
|
+
exports.SrpServer = exports.SrpClient = exports.SRP = void 0;
|
|
2683
|
+
const crypto_1 = __importDefault(__require("crypto"));
|
|
2684
|
+
const assert_1 = __importDefault(__require("assert"));
|
|
2685
|
+
const BigInteger = require_jsbn();
|
|
2686
|
+
const params_1 = require_params();
|
|
2687
|
+
const zero = new BigInteger(0, 10);
|
|
2688
|
+
function assert_(val, msg) {
|
|
2689
|
+
if (!val) throw new Error(msg || "assertion");
|
|
2690
|
+
}
|
|
2691
|
+
function assertIsBuffer(arg, argname = "arg") {
|
|
2692
|
+
assert_(Buffer.isBuffer(arg), `Type error: ${argname} must be a buffer`);
|
|
2693
|
+
}
|
|
2694
|
+
function assertIsBigInteger(arg, argname = "arg") {
|
|
2695
|
+
assert_(arg instanceof BigInteger, `Type error: ${argname} must be a BigInteger`);
|
|
2696
|
+
}
|
|
2697
|
+
/**
|
|
2698
|
+
* If a conversion is explicitly specified with the operator PAD(),
|
|
2699
|
+
* the integer will first be implicitly converted, then the resultant
|
|
2700
|
+
* byte-string will be left-padded with zeros (if necessary) until its
|
|
2701
|
+
* length equals the implicitly-converted length of N.
|
|
2702
|
+
*
|
|
2703
|
+
* @param {Buffer} n Number to pad
|
|
2704
|
+
* @param {number} len Length of the resulting Buffer
|
|
2705
|
+
* @return {Buffer}
|
|
2706
|
+
*/
|
|
2707
|
+
function padTo(n, len) {
|
|
2708
|
+
assertIsBuffer(n, "n");
|
|
2709
|
+
const padding = len - n.length;
|
|
2710
|
+
assert_(padding > -1, "Negative padding. Very uncomfortable.");
|
|
2711
|
+
const result = Buffer.alloc(len);
|
|
2712
|
+
result.fill(0, 0, padding);
|
|
2713
|
+
n.copy(result, padding);
|
|
2714
|
+
assert_1.default.strictEqual(result.length, len);
|
|
2715
|
+
return result;
|
|
2716
|
+
}
|
|
2717
|
+
function padToN(number, params) {
|
|
2718
|
+
assertIsBigInteger(number);
|
|
2719
|
+
const n = number.toString(16).length % 2 !== 0 ? "0" + number.toString(16) : number.toString(16);
|
|
2720
|
+
return padTo(Buffer.from(n, "hex"), params.N_length_bits / 8);
|
|
2721
|
+
}
|
|
2722
|
+
/**
|
|
2723
|
+
* Compute the intermediate value x as a hash of three buffers:
|
|
2724
|
+
* salt, identity, and password. And a colon. FOUR buffers.
|
|
2725
|
+
*
|
|
2726
|
+
* x = H(s | H(I | ":" | P))
|
|
2727
|
+
*
|
|
2728
|
+
* @param {object} params
|
|
2729
|
+
* @param {Buffer} salt
|
|
2730
|
+
* @param {Buffer} I User identity
|
|
2731
|
+
* @param {Buffer} P User password
|
|
2732
|
+
* @return {BigInteger} User secret
|
|
2733
|
+
*/
|
|
2734
|
+
function getx(params, salt, I, P) {
|
|
2735
|
+
assertIsBuffer(salt, "salt (salt)");
|
|
2736
|
+
assertIsBuffer(I, "identity (I)");
|
|
2737
|
+
assertIsBuffer(P, "password (P)");
|
|
2738
|
+
const hashIP = crypto_1.default.createHash(params.hash).update(Buffer.concat([
|
|
2739
|
+
I,
|
|
2740
|
+
Buffer.from(":"),
|
|
2741
|
+
P
|
|
2742
|
+
])).digest();
|
|
2743
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(salt).update(hashIP).digest());
|
|
2744
|
+
}
|
|
2745
|
+
var SRP = class SRP {
|
|
2746
|
+
/**
|
|
2747
|
+
* The verifier is calculated as described in Section 3 of [SRP-RFC].
|
|
2748
|
+
* We give the algorithm here for convenience.
|
|
2749
|
+
*
|
|
2750
|
+
* The verifier (v) is computed based on the salt (s), user name (I),
|
|
2751
|
+
* password (P), and group parameters (N, g).
|
|
2752
|
+
*
|
|
2753
|
+
* x = H(s | H(I | ":" | P))
|
|
2754
|
+
* v = g^x % N
|
|
2755
|
+
*
|
|
2756
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2757
|
+
* @param {Buffer} salt
|
|
2758
|
+
* @param {Buffer} I User identity
|
|
2759
|
+
* @param {Buffer} P User password
|
|
2760
|
+
* @return {Buffer}
|
|
2761
|
+
*/
|
|
2762
|
+
static computeVerifier(params, salt, I, P) {
|
|
2763
|
+
assertIsBuffer(salt, "salt (salt)");
|
|
2764
|
+
assertIsBuffer(I, "identity (I)");
|
|
2765
|
+
assertIsBuffer(P, "password (P)");
|
|
2766
|
+
return params.g.modPow(getx(params, salt, I, P), params.N).toBuffer(params.N_length_bits / 8);
|
|
2767
|
+
}
|
|
2768
|
+
static genKey(bytes = 32, callback) {
|
|
2769
|
+
if (typeof bytes !== "number") {
|
|
2770
|
+
callback = bytes;
|
|
2771
|
+
bytes = 32;
|
|
2772
|
+
}
|
|
2773
|
+
if (!callback) return new Promise((rs, rj) => SRP.genKey(bytes, (err, data) => err ? rj(err) : rs(data)));
|
|
2774
|
+
crypto_1.default.randomBytes(bytes, (err, buf) => {
|
|
2775
|
+
if (err) return callback(err, null);
|
|
2776
|
+
return callback(null, buf);
|
|
2777
|
+
});
|
|
2778
|
+
}
|
|
2779
|
+
};
|
|
2780
|
+
exports.SRP = SRP;
|
|
2781
|
+
SRP.params = params_1.params;
|
|
2782
|
+
/**
|
|
2783
|
+
* Calculate the SRP-6 multiplier.
|
|
2784
|
+
*
|
|
2785
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2786
|
+
* @return {BigInteger}
|
|
2787
|
+
*/
|
|
2788
|
+
function getk(params) {
|
|
2789
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(padToN(params.N, params)).update(padToN(params.g, params)).digest());
|
|
2790
|
+
}
|
|
2791
|
+
/**
|
|
2792
|
+
* The server key exchange message also contains the server's public
|
|
2793
|
+
* value (B). The server calculates this value as B = k*v + g^b % N,
|
|
2794
|
+
* where b is a random number that SHOULD be at least 256 bits in length
|
|
2795
|
+
* and k = H(N | PAD(g)).
|
|
2796
|
+
*
|
|
2797
|
+
* Note: as the tests imply, the entire expression is mod N.
|
|
2798
|
+
*
|
|
2799
|
+
* @param {SrpParams} params Group parameters, with .N, .g, .hash
|
|
2800
|
+
* @param {BigInteger} k
|
|
2801
|
+
* @param {BigInteger} v Verifier (stored)
|
|
2802
|
+
* @param {BigInteger} b Server secret exponent
|
|
2803
|
+
* @return {Buffer} B - The server public message
|
|
2804
|
+
*/
|
|
2805
|
+
function getB(params, k, v, b) {
|
|
2806
|
+
assertIsBigInteger(v);
|
|
2807
|
+
assertIsBigInteger(k);
|
|
2808
|
+
assertIsBigInteger(b);
|
|
2809
|
+
return k.multiply(v).add(params.g.modPow(b, params.N)).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
* The client key exchange message carries the client's public value
|
|
2813
|
+
* (A). The client calculates this value as A = g^a % N, where a is a
|
|
2814
|
+
* random number that SHOULD be at least 256 bits in length.
|
|
2815
|
+
*
|
|
2816
|
+
* Note: for this implementation, we take that to mean 256/8 bytes.
|
|
2817
|
+
*
|
|
2818
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2819
|
+
* @param {BigInteger} a_num Client secret exponent
|
|
2820
|
+
* @return {Buffer} A - The client public message
|
|
2821
|
+
*/
|
|
2822
|
+
function getA(params, a_num) {
|
|
2823
|
+
assertIsBigInteger(a_num);
|
|
2824
|
+
if (Math.ceil(a_num.toString(16).length / 2) < 32) console.warn("getA: client key length %d is less than the recommended 256 bits", a_num.bitLength());
|
|
2825
|
+
return params.g.modPow(a_num, params.N).toBuffer(params.N_length_bits / 8);
|
|
2826
|
+
}
|
|
2827
|
+
/**
|
|
2828
|
+
* getu() hashes the two public messages together, to obtain a scrambling
|
|
2829
|
+
* parameter "u" which cannot be predicted by either party ahead of time.
|
|
2830
|
+
* This makes it safe to use the message ordering defined in the SRP-6a
|
|
2831
|
+
* paper, in which the server reveals their "B" value before the client
|
|
2832
|
+
* commits to their "A" value.
|
|
2833
|
+
*
|
|
2834
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2835
|
+
* @param {Buffer} A Client ephemeral public key
|
|
2836
|
+
* @param {Buffer} B Server ephemeral public key
|
|
2837
|
+
* @return {BigInteger} u - Shared scrambling parameter
|
|
2838
|
+
*/
|
|
2839
|
+
function getu(params, A, B) {
|
|
2840
|
+
assertIsBuffer(A, "A");
|
|
2841
|
+
assertIsBuffer(B, "B");
|
|
2842
|
+
return new BigInteger(crypto_1.default.createHash(params.hash).update(padTo(A, params.N_length_bits / 8)).update(padTo(B, params.N_length_bits / 8)).digest());
|
|
2843
|
+
}
|
|
2844
|
+
/**
|
|
2845
|
+
* The TLS premaster secret as calculated by the client
|
|
2846
|
+
*
|
|
2847
|
+
* @param {SrpParams} params Group parameters, with .N, .g, .hash
|
|
2848
|
+
* @param {BigInteger} k_num
|
|
2849
|
+
* @param {BigInteger} x_num
|
|
2850
|
+
* @param {BigInteger} a_num
|
|
2851
|
+
* @param {BigInteger} B_num
|
|
2852
|
+
* @param {BigInteger} u_num
|
|
2853
|
+
* @return {Buffer}
|
|
2854
|
+
*/
|
|
2855
|
+
function client_getS(params, k_num, x_num, a_num, B_num, u_num) {
|
|
2856
|
+
assertIsBigInteger(k_num);
|
|
2857
|
+
assertIsBigInteger(x_num);
|
|
2858
|
+
assertIsBigInteger(a_num);
|
|
2859
|
+
assertIsBigInteger(B_num);
|
|
2860
|
+
assertIsBigInteger(u_num);
|
|
2861
|
+
if (zero.compareTo(B_num) >= 0 || params.N.compareTo(B_num) <= 0) throw new Error("invalid server-supplied \"B\", must be 1..N-1");
|
|
2862
|
+
return B_num.subtract(k_num.multiply(params.g.modPow(x_num, params.N))).modPow(a_num.add(u_num.multiply(x_num)), params.N).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
2863
|
+
}
|
|
2864
|
+
/**
|
|
2865
|
+
* The TLS premastersecret as calculated by the server
|
|
2866
|
+
*
|
|
2867
|
+
* @param {BigInteger} params Group parameters, with .N, .g, .hash
|
|
2868
|
+
* @param {BigInteger} v_num Verifier (stored on server)
|
|
2869
|
+
* @param {BigInteger} A_num Ephemeral client public key (read from client)
|
|
2870
|
+
* @param {BigInteger} b_num Server ephemeral private key (generated for session)
|
|
2871
|
+
* @param {BigInteger} u_num {@see getu}
|
|
2872
|
+
* @return {Buffer}
|
|
2873
|
+
*/
|
|
2874
|
+
function server_getS(params, v_num, A_num, b_num, u_num) {
|
|
2875
|
+
assertIsBigInteger(v_num);
|
|
2876
|
+
assertIsBigInteger(A_num);
|
|
2877
|
+
assertIsBigInteger(b_num);
|
|
2878
|
+
assertIsBigInteger(u_num);
|
|
2879
|
+
if (zero.compareTo(A_num) >= 0 || params.N.compareTo(A_num) <= 0) throw new Error("invalid client-supplied \"A\", must be 1..N-1");
|
|
2880
|
+
return A_num.multiply(v_num.modPow(u_num, params.N)).modPow(b_num, params.N).mod(params.N).toBuffer(params.N_length_bits / 8);
|
|
2881
|
+
}
|
|
2882
|
+
/**
|
|
2883
|
+
* Compute the shared session key K from S
|
|
2884
|
+
*
|
|
2885
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2886
|
+
* @param {Buffer} S_buf Session key
|
|
2887
|
+
* @return {Buffer}
|
|
2888
|
+
*/
|
|
2889
|
+
function getK(params, S_buf) {
|
|
2890
|
+
assertIsBuffer(S_buf, "S");
|
|
2891
|
+
if (params.hash === "sha1") return Buffer.concat([crypto_1.default.createHash(params.hash).update(S_buf).update(Buffer.from([
|
|
2892
|
+
0,
|
|
2893
|
+
0,
|
|
2894
|
+
0,
|
|
2895
|
+
0
|
|
2896
|
+
])).digest(), crypto_1.default.createHash(params.hash).update(S_buf).update(Buffer.from([
|
|
2897
|
+
0,
|
|
2898
|
+
0,
|
|
2899
|
+
0,
|
|
2900
|
+
1
|
|
2901
|
+
])).digest()]);
|
|
2902
|
+
else return crypto_1.default.createHash(params.hash).update(S_buf).digest();
|
|
2903
|
+
}
|
|
2904
|
+
function getM1(params, u_buf, s_buf, A_buf, B_buf, K_buf) {
|
|
2905
|
+
if (arguments.length > 4) {
|
|
2906
|
+
assertIsBuffer(u_buf, "identity (I)");
|
|
2907
|
+
assertIsBuffer(s_buf, "salt (s)");
|
|
2908
|
+
assertIsBuffer(A_buf, "client public key (A)");
|
|
2909
|
+
assertIsBuffer(B_buf, "server public key (B)");
|
|
2910
|
+
assertIsBuffer(K_buf, "session key (K)");
|
|
2911
|
+
const hN = crypto_1.default.createHash(params.hash).update(params.N.toBuffer(true)).digest();
|
|
2912
|
+
const hG = crypto_1.default.createHash(params.hash).update(params.g.toBuffer(true)).digest();
|
|
2913
|
+
for (let i = 0; i < hN.length; i++) hN[i] ^= hG[i];
|
|
2914
|
+
const hU = crypto_1.default.createHash(params.hash).update(u_buf).digest();
|
|
2915
|
+
return crypto_1.default.createHash(params.hash).update(hN).update(hU).update(s_buf).update(A_buf).update(B_buf).update(K_buf).digest();
|
|
2916
|
+
} else {
|
|
2917
|
+
[A_buf, B_buf, s_buf] = [
|
|
2918
|
+
u_buf,
|
|
2919
|
+
s_buf,
|
|
2920
|
+
A_buf
|
|
2921
|
+
];
|
|
2922
|
+
assertIsBuffer(A_buf, "A");
|
|
2923
|
+
assertIsBuffer(B_buf, "B");
|
|
2924
|
+
assertIsBuffer(s_buf, "S");
|
|
2925
|
+
return crypto_1.default.createHash(params.hash).update(A_buf).update(B_buf).update(s_buf).digest();
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
function getM2(params, A_buf, M1_buf, K_buf) {
|
|
2929
|
+
assertIsBuffer(A_buf, "A");
|
|
2930
|
+
assertIsBuffer(M1_buf, "M1");
|
|
2931
|
+
assertIsBuffer(K_buf, "K");
|
|
2932
|
+
return crypto_1.default.createHash(params.hash).update(A_buf).update(M1_buf).update(K_buf).digest();
|
|
2933
|
+
}
|
|
2934
|
+
function equal(buf1, buf2) {
|
|
2935
|
+
return buf1.toString("hex") === buf2.toString("hex");
|
|
2936
|
+
}
|
|
2937
|
+
var SrpClient = class {
|
|
2938
|
+
/**
|
|
2939
|
+
* Create an SRP client.
|
|
2940
|
+
*
|
|
2941
|
+
* @param {object} params Group parameters, with .N, .g, .hash
|
|
2942
|
+
* @param {Buffer} salt_buf User salt (from server)
|
|
2943
|
+
* @param {Buffer} identity_buf Identity/username
|
|
2944
|
+
* @param {Buffer} password_buf Password
|
|
2945
|
+
* @param {Buffer} secret1_buf Client private key {@see genKey}
|
|
2946
|
+
* @param {boolean} hap
|
|
2947
|
+
*/
|
|
2948
|
+
constructor(params, salt_buf, identity_buf, password_buf, secret1_buf, hap = true) {
|
|
2949
|
+
assertIsBuffer(salt_buf, "salt (s)");
|
|
2950
|
+
assertIsBuffer(identity_buf, "identity (I)");
|
|
2951
|
+
assertIsBuffer(password_buf, "password (P)");
|
|
2952
|
+
assertIsBuffer(secret1_buf, "secret1");
|
|
2953
|
+
this._params = params;
|
|
2954
|
+
this._k = getk(params);
|
|
2955
|
+
this._x = getx(params, salt_buf, identity_buf, password_buf);
|
|
2956
|
+
this._a = new BigInteger(secret1_buf);
|
|
2957
|
+
if (hap) {
|
|
2958
|
+
this._I = identity_buf;
|
|
2959
|
+
this._s = salt_buf;
|
|
2960
|
+
}
|
|
2961
|
+
this._A = getA(params, this._a);
|
|
2962
|
+
}
|
|
2963
|
+
/**
|
|
2964
|
+
* Returns the client's public key (A).
|
|
2965
|
+
*
|
|
2966
|
+
* @return {Buffer}
|
|
2967
|
+
*/
|
|
2968
|
+
computeA() {
|
|
2969
|
+
return this._A;
|
|
2970
|
+
}
|
|
2971
|
+
/**
|
|
2972
|
+
* Sets the server's public key (B).
|
|
2973
|
+
*
|
|
2974
|
+
* @param {Buffer} B_buf The server's public key
|
|
2975
|
+
*/
|
|
2976
|
+
setB(B_buf) {
|
|
2977
|
+
const u_num = getu(this._params, this._A, B_buf);
|
|
2978
|
+
const S_buf_x = client_getS(this._params, this._k, this._x, this._a, new BigInteger(B_buf), u_num);
|
|
2979
|
+
this._K = getK(this._params, S_buf_x);
|
|
2980
|
+
this._u = u_num;
|
|
2981
|
+
this._S = S_buf_x;
|
|
2982
|
+
this._B = B_buf;
|
|
2983
|
+
if (this._I && this._s) this._M1 = getM1(this._params, this._I, this._s, this._A, this._B, this._K);
|
|
2984
|
+
else this._M1 = getM1(this._params, this._A, this._B, this._S);
|
|
2985
|
+
this._M2 = getM2(this._params, this._A, this._M1, this._K);
|
|
2986
|
+
}
|
|
2987
|
+
/**
|
|
2988
|
+
* Gets the M1 value.
|
|
2989
|
+
* This requires setting the server's public key {@see Client.setB}.
|
|
2990
|
+
*
|
|
2991
|
+
* @return {Buffer}
|
|
2992
|
+
*/
|
|
2993
|
+
computeM1() {
|
|
2994
|
+
if (this._M1 === void 0) throw new Error("incomplete protocol");
|
|
2995
|
+
return this._M1;
|
|
2996
|
+
}
|
|
2997
|
+
/**
|
|
2998
|
+
* Checks the server was able to calculate M2.
|
|
2999
|
+
* This requires setting the server's public key {@see Client.setB}.
|
|
3000
|
+
*
|
|
3001
|
+
* @param M2 The server's M2 value
|
|
3002
|
+
*/
|
|
3003
|
+
checkM2(M2) {
|
|
3004
|
+
if (!equal(this._M2, M2)) throw new Error("server is not authentic");
|
|
3005
|
+
}
|
|
3006
|
+
/**
|
|
3007
|
+
* Returns the shared session key.
|
|
3008
|
+
*
|
|
3009
|
+
* @return {Buffer}
|
|
3010
|
+
*/
|
|
3011
|
+
computeK() {
|
|
3012
|
+
if (this._K === void 0) throw new Error("incomplete protocol");
|
|
3013
|
+
return this._K;
|
|
3014
|
+
}
|
|
3015
|
+
};
|
|
3016
|
+
exports.SrpClient = SrpClient;
|
|
3017
|
+
var SrpServer = class {
|
|
3018
|
+
constructor(params, salt_buf, identity_buf, password_buf, secret2_buf) {
|
|
3019
|
+
this._params = params;
|
|
3020
|
+
this._k = getk(params);
|
|
3021
|
+
if (arguments.length > 3) {
|
|
3022
|
+
assertIsBuffer(salt_buf, "salt (salt)");
|
|
3023
|
+
assertIsBuffer(identity_buf, "identity (I)");
|
|
3024
|
+
assertIsBuffer(password_buf, "password (P)");
|
|
3025
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
3026
|
+
this._b = new BigInteger(secret2_buf);
|
|
3027
|
+
this._v = new BigInteger(SRP.computeVerifier(params, salt_buf, identity_buf, password_buf));
|
|
3028
|
+
this._I = identity_buf;
|
|
3029
|
+
this._s = salt_buf;
|
|
3030
|
+
} else if (salt_buf instanceof Buffer) {
|
|
3031
|
+
const verifier_buf = salt_buf;
|
|
3032
|
+
[secret2_buf, salt_buf, identity_buf, password_buf] = [
|
|
3033
|
+
identity_buf,
|
|
3034
|
+
void 0,
|
|
3035
|
+
void 0,
|
|
3036
|
+
void 0
|
|
3037
|
+
];
|
|
3038
|
+
assertIsBuffer(verifier_buf, "verifier (v)");
|
|
3039
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
3040
|
+
this._b = new BigInteger(secret2_buf);
|
|
3041
|
+
this._v = new BigInteger(verifier_buf);
|
|
3042
|
+
} else {
|
|
3043
|
+
const identity = salt_buf;
|
|
3044
|
+
[secret2_buf, salt_buf, identity_buf, password_buf] = [
|
|
3045
|
+
identity_buf,
|
|
3046
|
+
void 0,
|
|
3047
|
+
void 0,
|
|
3048
|
+
void 0
|
|
3049
|
+
];
|
|
3050
|
+
(0, assert_1.default)(identity.username instanceof Buffer || typeof identity.username === "string", "identity.username (I) must be a string or Buffer");
|
|
3051
|
+
assertIsBuffer(identity.salt, "identity.salt (s)");
|
|
3052
|
+
(0, assert_1.default)("password" in identity || "verifier" in identity, "identity requires a password or verifier");
|
|
3053
|
+
if ("verifier" in identity) assertIsBuffer(identity.verifier, "identity.verifier (v)");
|
|
3054
|
+
else (0, assert_1.default)(identity.password instanceof Buffer || typeof identity.password === "string", "identity.password (p) must be a string or Buffer");
|
|
3055
|
+
assertIsBuffer(secret2_buf, "secret2");
|
|
3056
|
+
const username = typeof identity.username === "string" ? Buffer.from(identity.username) : identity.username;
|
|
3057
|
+
this._b = new BigInteger(secret2_buf);
|
|
3058
|
+
if ("verifier" in identity) this._v = new BigInteger(identity.verifier);
|
|
3059
|
+
else this._v = new BigInteger(SRP.computeVerifier(params, identity.salt, username, typeof identity.password === "string" ? Buffer.from(identity.password) : identity.password));
|
|
3060
|
+
this._I = username;
|
|
3061
|
+
this._s = identity.salt;
|
|
3062
|
+
}
|
|
3063
|
+
this._B = getB(params, this._k, this._v, this._b);
|
|
3064
|
+
}
|
|
3065
|
+
/**
|
|
3066
|
+
* Returns the server's public key (B).
|
|
3067
|
+
*
|
|
3068
|
+
* @return {Buffer}
|
|
3069
|
+
*/
|
|
3070
|
+
computeB() {
|
|
3071
|
+
return this._B;
|
|
3072
|
+
}
|
|
3073
|
+
/**
|
|
3074
|
+
* Sets the client's public key (A).
|
|
3075
|
+
*
|
|
3076
|
+
* @param {Buffer} A The client's public key
|
|
3077
|
+
*/
|
|
3078
|
+
setA(A) {
|
|
3079
|
+
const u_num = getu(this._params, A, this._B);
|
|
3080
|
+
const S_buf = server_getS(this._params, this._v, new BigInteger(A), this._b, u_num);
|
|
3081
|
+
this._K = getK(this._params, S_buf);
|
|
3082
|
+
this._u = u_num;
|
|
3083
|
+
this._S = S_buf;
|
|
3084
|
+
if (this._I && this._s) this._M1 = getM1(this._params, this._I, this._s, A, this._B, this._K);
|
|
3085
|
+
else this._M1 = getM1(this._params, A, this._B, this._S);
|
|
3086
|
+
this._M2 = getM2(this._params, A, this._M1, this._K);
|
|
3087
|
+
}
|
|
3088
|
+
/**
|
|
3089
|
+
* Checks the client was able to calculate M1.
|
|
3090
|
+
*
|
|
3091
|
+
* @param {Buffer} M1 The client's M1 value
|
|
3092
|
+
*/
|
|
3093
|
+
checkM1(M1) {
|
|
3094
|
+
if (this._M1 === void 0) throw new Error("incomplete protocol");
|
|
3095
|
+
if (!equal(this._M1, M1)) throw new Error("client did not use the same password");
|
|
3096
|
+
}
|
|
3097
|
+
/**
|
|
3098
|
+
* Returns the shared session key.
|
|
3099
|
+
*
|
|
3100
|
+
* @return {Buffer}
|
|
3101
|
+
*/
|
|
3102
|
+
computeK() {
|
|
3103
|
+
if (this._K === void 0) throw new Error("incomplete protocol");
|
|
3104
|
+
return this._K;
|
|
3105
|
+
}
|
|
3106
|
+
/**
|
|
3107
|
+
* Gets the M2 value.
|
|
3108
|
+
* This requires setting the client's public key {@see Server.setA}.
|
|
3109
|
+
*
|
|
3110
|
+
* @return {Buffer}
|
|
3111
|
+
*/
|
|
3112
|
+
computeM2() {
|
|
3113
|
+
if (this._M2 === void 0) throw new Error("incomplete protocol");
|
|
3114
|
+
return this._M2;
|
|
3115
|
+
}
|
|
3116
|
+
};
|
|
3117
|
+
exports.SrpServer = SrpServer;
|
|
3118
|
+
}));
|
|
3119
|
+
|
|
1101
3120
|
//#endregion
|
|
1102
3121
|
//#region src/pairing.ts
|
|
3122
|
+
var import_srp = require_srp();
|
|
1103
3123
|
var BasePairing = class {
|
|
1104
3124
|
get context() {
|
|
1105
3125
|
return this.#context;
|
|
@@ -1182,8 +3202,8 @@ var AccessoryPair = class extends BasePairing {
|
|
|
1182
3202
|
};
|
|
1183
3203
|
}
|
|
1184
3204
|
async m2(m1, pin = AIRPLAY_TRANSIENT_PIN) {
|
|
1185
|
-
const srpKey = await SRP.genKey(32);
|
|
1186
|
-
this.#srp = new SrpClient(SRP.params.hap, m1.salt, Buffer.from("Pair-Setup"), Buffer.from(pin), srpKey, true);
|
|
3205
|
+
const srpKey = await import_srp.SRP.genKey(32);
|
|
3206
|
+
this.#srp = new import_srp.SrpClient(import_srp.SRP.params.hap, m1.salt, Buffer.from("Pair-Setup"), Buffer.from(pin), srpKey, true);
|
|
1187
3207
|
this.#srp.setB(m1.publicKey);
|
|
1188
3208
|
return {
|
|
1189
3209
|
publicKey: this.#srp.computeA(),
|
|
@@ -1342,6 +3362,10 @@ var AccessoryVerify = class extends BasePairing {
|
|
|
1342
3362
|
}
|
|
1343
3363
|
};
|
|
1344
3364
|
|
|
3365
|
+
//#endregion
|
|
3366
|
+
//#region src/symbols.ts
|
|
3367
|
+
const ENCRYPTION = Symbol();
|
|
3368
|
+
|
|
1345
3369
|
//#endregion
|
|
1346
3370
|
//#region src/timing.ts
|
|
1347
3371
|
var TimingServer = class {
|