@arcblock/did-noise 0.0.0-stage → 1.30.25

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.
Files changed (44) hide show
  1. package/LICENSE +13 -0
  2. package/esm/chacha20poly1305.d.mts +18 -0
  3. package/esm/chacha20poly1305.mjs +152 -0
  4. package/esm/chain-node-mesh.d.mts +79 -0
  5. package/esm/chain-node-mesh.mjs +187 -0
  6. package/esm/http-transport.d.mts +72 -0
  7. package/esm/http-transport.mjs +216 -0
  8. package/esm/index.d.mts +6 -0
  9. package/esm/index.mjs +7 -0
  10. package/esm/mesh-tunnel.d.mts +165 -0
  11. package/esm/mesh-tunnel.mjs +350 -0
  12. package/esm/noise-session.d.mts +157 -0
  13. package/esm/noise-session.mjs +491 -0
  14. package/lib/_vendor/@noble/curves/LICENSE +21 -0
  15. package/lib/_vendor/@noble/curves/abstract/curve.cjs +338 -0
  16. package/lib/_vendor/@noble/curves/abstract/edwards.cjs +542 -0
  17. package/lib/_vendor/@noble/curves/abstract/hash-to-curve.cjs +72 -0
  18. package/lib/_vendor/@noble/curves/abstract/modular.cjs +557 -0
  19. package/lib/_vendor/@noble/curves/abstract/montgomery.cjs +149 -0
  20. package/lib/_vendor/@noble/curves/ed25519.cjs +302 -0
  21. package/lib/_vendor/@noble/curves/utils.cjs +436 -0
  22. package/lib/_vendor/@noble/hashes/LICENSE +21 -0
  23. package/lib/_vendor/@noble/hashes/_blake.cjs +300 -0
  24. package/lib/_vendor/@noble/hashes/_md.cjs +194 -0
  25. package/lib/_vendor/@noble/hashes/_u64.cjs +60 -0
  26. package/lib/_vendor/@noble/hashes/blake2.cjs +291 -0
  27. package/lib/_vendor/@noble/hashes/hkdf.cjs +104 -0
  28. package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
  29. package/lib/_vendor/@noble/hashes/sha2.cjs +417 -0
  30. package/lib/_vendor/@noble/hashes/utils.cjs +422 -0
  31. package/lib/chacha20poly1305.cjs +153 -0
  32. package/lib/chacha20poly1305.d.cts +18 -0
  33. package/lib/chain-node-mesh.cjs +189 -0
  34. package/lib/chain-node-mesh.d.cts +79 -0
  35. package/lib/http-transport.cjs +219 -0
  36. package/lib/http-transport.d.cts +72 -0
  37. package/lib/index.cjs +31 -0
  38. package/lib/index.d.cts +6 -0
  39. package/lib/mesh-tunnel.cjs +354 -0
  40. package/lib/mesh-tunnel.d.cts +165 -0
  41. package/lib/noise-session.cjs +503 -0
  42. package/lib/noise-session.d.cts +157 -0
  43. package/package.json +80 -4
  44. package/README.md +0 -3
@@ -0,0 +1,302 @@
1
+ const require_utils = require('../hashes/utils.cjs');
2
+ const require_sha2 = require('../hashes/sha2.cjs');
3
+ const require_utils$1 = require('./utils.cjs');
4
+ const require_modular = require('./abstract/modular.cjs');
5
+ const require_edwards = require('./abstract/edwards.cjs');
6
+ const require_hash_to_curve = require('./abstract/hash-to-curve.cjs');
7
+ const require_montgomery = require('./abstract/montgomery.cjs');
8
+
9
+ //#region ../../node_modules/@noble/curves/ed25519.js
10
+ /**
11
+ * ed25519 Twisted Edwards curve with following addons:
12
+ * - X25519 ECDH
13
+ * - Ristretto cofactor elimination
14
+ * - Elligator hash-to-group / point indistinguishability
15
+ * @module
16
+ */
17
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
18
+ const _0n = /* @__PURE__ */ BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
19
+ const _5n = /* @__PURE__ */ BigInt(5), _8n = /* @__PURE__ */ BigInt(8);
20
+ const ed25519_CURVE_p = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
21
+ const ed25519_CURVE = /* @__PURE__ */ (() => ({
22
+ p: ed25519_CURVE_p,
23
+ n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
24
+ h: _8n,
25
+ a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
26
+ d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
27
+ Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
28
+ Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
29
+ }))();
30
+ function ed25519_pow_2_252_3(x) {
31
+ const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
32
+ const P = ed25519_CURVE_p;
33
+ const b2 = x * x % P * x % P;
34
+ const b5 = require_modular.pow2(require_modular.pow2(b2, _2n, P) * b2 % P, _1n, P) * x % P;
35
+ const b10 = require_modular.pow2(b5, _5n, P) * b5 % P;
36
+ const b20 = require_modular.pow2(b10, _10n, P) * b10 % P;
37
+ const b40 = require_modular.pow2(b20, _20n, P) * b20 % P;
38
+ const b80 = require_modular.pow2(b40, _40n, P) * b40 % P;
39
+ return {
40
+ pow_p_5_8: require_modular.pow2(require_modular.pow2(require_modular.pow2(require_modular.pow2(b80, _80n, P) * b80 % P, _80n, P) * b80 % P, _10n, P) * b10 % P, _2n, P) * x % P,
41
+ b2
42
+ };
43
+ }
44
+ function adjustScalarBytes(bytes) {
45
+ bytes[0] &= 248;
46
+ bytes[31] &= 127;
47
+ bytes[31] |= 64;
48
+ return bytes;
49
+ }
50
+ const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
51
+ function uvRatio(u, v) {
52
+ const P = ed25519_CURVE_p;
53
+ const v3 = require_modular.mod(v * v * v, P);
54
+ const pow = ed25519_pow_2_252_3(u * require_modular.mod(v3 * v3 * v, P)).pow_p_5_8;
55
+ let x = require_modular.mod(u * v3 * pow, P);
56
+ const vx2 = require_modular.mod(v * x * x, P);
57
+ const root1 = x;
58
+ const root2 = require_modular.mod(x * ED25519_SQRT_M1, P);
59
+ const useRoot1 = vx2 === u;
60
+ const useRoot2 = vx2 === require_modular.mod(-u, P);
61
+ const noRoot = vx2 === require_modular.mod(-u * ED25519_SQRT_M1, P);
62
+ if (useRoot1) x = root1;
63
+ if (useRoot2 || noRoot) x = root2;
64
+ if (require_modular.isNegativeLE(x, P)) x = require_modular.mod(-x, P);
65
+ return {
66
+ isValid: useRoot1 || useRoot2,
67
+ value: x
68
+ };
69
+ }
70
+ const ed25519_Point = /* @__PURE__ */ require_edwards.edwards(ed25519_CURVE, { uvRatio });
71
+ const Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
72
+ const Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
73
+ function ed(opts) {
74
+ return require_edwards.eddsa(ed25519_Point, require_sha2.sha512, Object.assign({
75
+ adjustScalarBytes,
76
+ zip215: true
77
+ }, opts));
78
+ }
79
+ /**
80
+ * ed25519 curve with EdDSA signatures.
81
+ * Seeded `keygen(seed)` / `utils.randomSecretKey(seed)` reuse the provided
82
+ * 32-byte seed buffer instead of copying it.
83
+ * @example
84
+ * Generate one Ed25519 keypair, sign a message, and verify it.
85
+ *
86
+ * ```js
87
+ * import { ed25519 } from '@noble/curves/ed25519.js';
88
+ * const { secretKey, publicKey } = ed25519.keygen();
89
+ * // const publicKey = ed25519.getPublicKey(secretKey);
90
+ * const msg = new TextEncoder().encode('hello noble');
91
+ * const sig = ed25519.sign(msg, secretKey);
92
+ * const isValid = ed25519.verify(sig, msg, publicKey); // ZIP215
93
+ * // RFC8032 / FIPS 186-5
94
+ * const isValid2 = ed25519.verify(sig, msg, publicKey, { zip215: false });
95
+ * ```
96
+ */
97
+ const ed25519 = /* @__PURE__ */ ed({});
98
+ /**
99
+ * ECDH using curve25519 aka x25519.
100
+ * `getSharedSecret()` rejects low-order peer inputs by default, and seeded
101
+ * `keygen(seed)` reuses the provided 32-byte seed buffer instead of copying it.
102
+ * @example
103
+ * Derive one shared secret between two X25519 peers.
104
+ *
105
+ * ```js
106
+ * import { x25519 } from '@noble/curves/ed25519.js';
107
+ * const alice = x25519.keygen();
108
+ * const bob = x25519.keygen();
109
+ * const shared = x25519.getSharedSecret(alice.secretKey, bob.publicKey);
110
+ * ```
111
+ */
112
+ const x25519 = /* @__PURE__ */ (() => {
113
+ const P = ed25519_CURVE_p;
114
+ return require_montgomery.montgomery({
115
+ P,
116
+ type: "x25519",
117
+ powPminus2: (x) => {
118
+ const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
119
+ return require_modular.mod(require_modular.pow2(pow_p_5_8, _3n, P) * b2, P);
120
+ },
121
+ adjustScalarBytes
122
+ });
123
+ })();
124
+ const SQRT_M1 = ED25519_SQRT_M1;
125
+ const SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
126
+ const INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
127
+ const ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
128
+ const D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
129
+ const invertSqrt = (number) => uvRatio(_1n, number);
130
+ const MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
131
+ const bytes255ToNumberLE = (bytes) => Fp.create(require_utils$1.bytesToNumberLE(bytes) & MAX_255B);
132
+ /**
133
+ * Computes Elligator map for Ristretto255.
134
+ * Primary formula source is RFC 9496 §4.3.4 MAP; RFC 9380 Appendix B builds
135
+ * `hash_to_ristretto255` on top of this helper.
136
+ * Returns an internal Edwards representative, not a public `_RistrettoPoint`.
137
+ */
138
+ function calcElligatorRistrettoMap(r0) {
139
+ const { d } = ed25519_CURVE;
140
+ const P = ed25519_CURVE_p;
141
+ const mod$1 = (n) => Fp.create(n);
142
+ const r = mod$1(SQRT_M1 * r0 * r0);
143
+ const Ns = mod$1((r + _1n) * ONE_MINUS_D_SQ);
144
+ let c = BigInt(-1);
145
+ const D = mod$1((c - d * r) * mod$1(r + d));
146
+ let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
147
+ let s_ = mod$1(s * r0);
148
+ if (!require_modular.isNegativeLE(s_, P)) s_ = mod$1(-s_);
149
+ if (!Ns_D_is_sq) s = s_;
150
+ if (!Ns_D_is_sq) c = r;
151
+ const Nt = mod$1(c * (r - _1n) * D_MINUS_ONE_SQ - D);
152
+ const s2 = s * s;
153
+ const W0 = mod$1((s + s) * D);
154
+ const W1 = mod$1(Nt * SQRT_AD_MINUS_ONE);
155
+ const W2 = mod$1(_1n - s2);
156
+ const W3 = mod$1(_1n + s2);
157
+ return new ed25519_Point(mod$1(W0 * W3), mod$1(W2 * W1), mod$1(W1 * W3), mod$1(W0 * W2));
158
+ }
159
+ /**
160
+ * Wrapper over Edwards Point for ristretto255.
161
+ *
162
+ * Each ed25519/EdwardsPoint has 8 different equivalent points. This can be
163
+ * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
164
+ * Ristretto point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
165
+ * but it should work in its own namespace: do not combine those two.
166
+ * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
167
+ */
168
+ var _RistrettoPoint = class _RistrettoPoint extends require_edwards.PrimeEdwardsPoint {
169
+ static BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.BASE))();
170
+ static ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.ZERO))();
171
+ static Fp = /* @__PURE__ */ (() => Fp)();
172
+ static Fn = /* @__PURE__ */ (() => Fn)();
173
+ constructor(ep) {
174
+ super(ep);
175
+ }
176
+ /**
177
+ * Create one Ristretto255 point from affine Edwards coordinates.
178
+ * This wraps the internal Edwards representative directly and is not a
179
+ * canonical ristretto255 decoding path.
180
+ * Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
181
+ */
182
+ static fromAffine(ap) {
183
+ return new _RistrettoPoint(ed25519_Point.fromAffine(ap));
184
+ }
185
+ assertSame(other) {
186
+ if (!(other instanceof _RistrettoPoint)) throw new Error("RistrettoPoint expected");
187
+ }
188
+ init(ep) {
189
+ return new _RistrettoPoint(ep);
190
+ }
191
+ static fromBytes(bytes) {
192
+ require_utils.abytes(bytes, 32);
193
+ const { a, d } = ed25519_CURVE;
194
+ const P = ed25519_CURVE_p;
195
+ const mod$1 = (n) => Fp.create(n);
196
+ const s = bytes255ToNumberLE(bytes);
197
+ if (!require_utils$1.equalBytes(Fp.toBytes(s), bytes) || require_modular.isNegativeLE(s, P)) throw new Error("invalid ristretto255 encoding 1");
198
+ const s2 = mod$1(s * s);
199
+ const u1 = mod$1(_1n + a * s2);
200
+ const u2 = mod$1(_1n - a * s2);
201
+ const u1_2 = mod$1(u1 * u1);
202
+ const u2_2 = mod$1(u2 * u2);
203
+ const v = mod$1(a * d * u1_2 - u2_2);
204
+ const { isValid, value: I } = invertSqrt(mod$1(v * u2_2));
205
+ const Dx = mod$1(I * u2);
206
+ const Dy = mod$1(I * Dx * v);
207
+ let x = mod$1((s + s) * Dx);
208
+ if (require_modular.isNegativeLE(x, P)) x = mod$1(-x);
209
+ const y = mod$1(u1 * Dy);
210
+ const t = mod$1(x * y);
211
+ if (!isValid || require_modular.isNegativeLE(t, P) || y === _0n) throw new Error("invalid ristretto255 encoding 2");
212
+ return new _RistrettoPoint(new ed25519_Point(x, y, _1n, t));
213
+ }
214
+ /**
215
+ * Converts ristretto-encoded string to ristretto point.
216
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
217
+ * @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
218
+ */
219
+ static fromHex(hex) {
220
+ return _RistrettoPoint.fromBytes(require_utils.hexToBytes(hex));
221
+ }
222
+ /**
223
+ * Encodes ristretto point to Uint8Array.
224
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
225
+ */
226
+ toBytes() {
227
+ let { X, Y, Z, T } = this.ep;
228
+ const P = ed25519_CURVE_p;
229
+ const mod$1 = (n) => Fp.create(n);
230
+ const u1 = mod$1(mod$1(Z + Y) * mod$1(Z - Y));
231
+ const u2 = mod$1(X * Y);
232
+ const { value: invsqrt } = invertSqrt(mod$1(u1 * mod$1(u2 * u2)));
233
+ const D1 = mod$1(invsqrt * u1);
234
+ const D2 = mod$1(invsqrt * u2);
235
+ const zInv = mod$1(D1 * D2 * T);
236
+ let D;
237
+ if (require_modular.isNegativeLE(T * zInv, P)) {
238
+ let _x = mod$1(Y * SQRT_M1);
239
+ let _y = mod$1(X * SQRT_M1);
240
+ X = _x;
241
+ Y = _y;
242
+ D = mod$1(D1 * INVSQRT_A_MINUS_D);
243
+ } else D = D2;
244
+ if (require_modular.isNegativeLE(X * zInv, P)) Y = mod$1(-Y);
245
+ let s = mod$1((Z - Y) * D);
246
+ if (require_modular.isNegativeLE(s, P)) s = mod$1(-s);
247
+ return Fp.toBytes(s);
248
+ }
249
+ /**
250
+ * Compares two Ristretto points.
251
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
252
+ */
253
+ equals(other) {
254
+ this.assertSame(other);
255
+ const { X: X1, Y: Y1 } = this.ep;
256
+ const { X: X2, Y: Y2 } = other.ep;
257
+ const mod$1 = (n) => Fp.create(n);
258
+ const one = mod$1(X1 * Y2) === mod$1(Y1 * X2);
259
+ const two = mod$1(Y1 * Y2) === mod$1(X1 * X2);
260
+ return one || two;
261
+ }
262
+ is0() {
263
+ return this.equals(_RistrettoPoint.ZERO);
264
+ }
265
+ };
266
+ Object.freeze(_RistrettoPoint.BASE);
267
+ Object.freeze(_RistrettoPoint.ZERO);
268
+ Object.freeze(_RistrettoPoint.prototype);
269
+ Object.freeze(_RistrettoPoint);
270
+ /**
271
+ * Hashing to ristretto255 points / field. RFC 9380 methods.
272
+ * `hashToCurve()` is RFC 9380 Appendix B, `deriveToCurve()` is the RFC 9496
273
+ * §4.3.4 element-derivation building block, and `hashToScalar()` is a
274
+ * library-specific helper for OPRF-style use.
275
+ * @example
276
+ * Hash one message onto ristretto255.
277
+ *
278
+ * ```ts
279
+ * const point = ristretto255_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
280
+ * ```
281
+ */
282
+ const ristretto255_hasher = Object.freeze({
283
+ Point: _RistrettoPoint,
284
+ hashToCurve(msg, options) {
285
+ const xmd = require_hash_to_curve.expand_message_xmd(msg, options?.DST === void 0 ? "ristretto255_XMD:SHA-512_R255MAP_RO_" : options.DST, 64, require_sha2.sha512);
286
+ return ristretto255_hasher.deriveToCurve(xmd);
287
+ },
288
+ hashToScalar(msg, options = { DST: require_hash_to_curve._DST_scalar }) {
289
+ const xmd = require_hash_to_curve.expand_message_xmd(msg, options.DST, 64, require_sha2.sha512);
290
+ return Fn.create(require_utils$1.bytesToNumberLE(xmd));
291
+ },
292
+ deriveToCurve(bytes) {
293
+ require_utils.abytes(bytes, 64);
294
+ const R1 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(0, 32)));
295
+ const R2 = calcElligatorRistrettoMap(bytes255ToNumberLE(bytes.subarray(32, 64)));
296
+ return new _RistrettoPoint(R1.add(R2));
297
+ }
298
+ });
299
+
300
+ //#endregion
301
+ exports.ed25519 = ed25519;
302
+ exports.x25519 = x25519;