@ohos-ports/crypto 0.0.0-stage → 0.3.1-beta.1

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 (121) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +60 -2
  3. package/dist/bech32.d.ts +53 -0
  4. package/dist/bech32.js +187 -0
  5. package/dist/blake2b.d.ts +7 -0
  6. package/dist/blake2b.js +213 -0
  7. package/dist/bls12_318.d.ts +48 -0
  8. package/dist/bls12_318.js +268 -0
  9. package/dist/ed25519.d.ts +55 -0
  10. package/dist/ed25519.js +721 -0
  11. package/dist/hasGlobalWebCrypto.d.ts +1 -0
  12. package/dist/hasGlobalWebCrypto.js +21 -0
  13. package/dist/hmacSHA512.d.ts +6 -0
  14. package/dist/hmacSHA512.js +14 -0
  15. package/dist/index.d.ts +14 -0
  16. package/dist/index.js +30 -0
  17. package/dist/keccak.d.ts +4 -0
  18. package/dist/keccak.js +43 -0
  19. package/dist/kes/constants.d.ts +2 -0
  20. package/dist/kes/constants.js +5 -0
  21. package/dist/kes/index.d.ts +0 -0
  22. package/dist/kes/index.js +0 -0
  23. package/dist/kes/macro/sum_kes.d.ts +15 -0
  24. package/dist/kes/macro/sum_kes.js +9 -0
  25. package/dist/kes/singleKes.d.ts +0 -0
  26. package/dist/kes/singleKes.js +0 -0
  27. package/dist/noble/_assert.d.ts +23 -0
  28. package/dist/noble/_assert.js +55 -0
  29. package/dist/noble/_md.d.ts +29 -0
  30. package/dist/noble/_md.js +170 -0
  31. package/dist/noble/_shortw_utils.d.ts +61 -0
  32. package/dist/noble/_shortw_utils.js +63 -0
  33. package/dist/noble/_u64.d.ts +28 -0
  34. package/dist/noble/_u64.js +116 -0
  35. package/dist/noble/abstract/bls.d.ts +119 -0
  36. package/dist/noble/abstract/bls.js +334 -0
  37. package/dist/noble/abstract/curve.d.ts +83 -0
  38. package/dist/noble/abstract/curve.js +319 -0
  39. package/dist/noble/abstract/edwards.d.ts +85 -0
  40. package/dist/noble/abstract/edwards.js +508 -0
  41. package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
  42. package/dist/noble/abstract/hash-to-curve.js +220 -0
  43. package/dist/noble/abstract/modular.d.ts +122 -0
  44. package/dist/noble/abstract/modular.js +455 -0
  45. package/dist/noble/abstract/montgomery.d.ts +28 -0
  46. package/dist/noble/abstract/montgomery.js +172 -0
  47. package/dist/noble/abstract/utils.d.ts +106 -0
  48. package/dist/noble/abstract/utils.js +442 -0
  49. package/dist/noble/bls12_381.d.ts +95 -0
  50. package/dist/noble/bls12_381.js +1434 -0
  51. package/dist/noble/crypto.d.ts +1 -0
  52. package/dist/noble/crypto.js +4 -0
  53. package/dist/noble/curves/curve.d.ts +69 -0
  54. package/dist/noble/curves/curve.js +167 -0
  55. package/dist/noble/curves/modular.d.ts +122 -0
  56. package/dist/noble/curves/modular.js +442 -0
  57. package/dist/noble/curves/utils.d.ts +93 -0
  58. package/dist/noble/curves/utils.js +385 -0
  59. package/dist/noble/curves/weierstrass.d.ts +240 -0
  60. package/dist/noble/curves/weierstrass.js +1178 -0
  61. package/dist/noble/ed25519.d.ts +100 -0
  62. package/dist/noble/ed25519.js +494 -0
  63. package/dist/noble/hmac.d.ts +25 -0
  64. package/dist/noble/hmac.js +101 -0
  65. package/dist/noble/index.d.ts +2 -0
  66. package/dist/noble/index.js +20 -0
  67. package/dist/noble/keccak.d.ts +58 -0
  68. package/dist/noble/keccak.js +289 -0
  69. package/dist/noble/ripemd_160.d.ts +20 -0
  70. package/dist/noble/ripemd_160.js +147 -0
  71. package/dist/noble/secp256k1.d.ts +98 -0
  72. package/dist/noble/secp256k1.js +309 -0
  73. package/dist/noble/sha256.d.ts +34 -0
  74. package/dist/noble/sha256.js +146 -0
  75. package/dist/noble/sha512.d.ts +66 -0
  76. package/dist/noble/sha512.js +301 -0
  77. package/dist/noble/utils.d.ts +90 -0
  78. package/dist/noble/utils.js +278 -0
  79. package/dist/ripemd_160.d.ts +1 -0
  80. package/dist/ripemd_160.js +23 -0
  81. package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
  82. package/dist/rust_vrf_reimpl/curves.js +4225 -0
  83. package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
  84. package/dist/rust_vrf_reimpl/vrf03.js +373 -0
  85. package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
  86. package/dist/rust_vrf_reimpl/vrf10.js +468 -0
  87. package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
  88. package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
  89. package/dist/secp256k1.d.ts +4 -0
  90. package/dist/secp256k1.js +33 -0
  91. package/dist/sha2_256.d.ts +16 -0
  92. package/dist/sha2_256.js +403 -0
  93. package/dist/sha2_512.d.ts +16 -0
  94. package/dist/sha2_512.js +256 -0
  95. package/dist/sha3.d.ts +17 -0
  96. package/dist/sha3.js +181 -0
  97. package/dist/types.d.ts +26 -0
  98. package/dist/types.js +120 -0
  99. package/dist/utils/assert.d.ts +3 -0
  100. package/dist/utils/assert.js +55 -0
  101. package/dist/utils/base32.d.ts +5 -0
  102. package/dist/utils/base32.js +52 -0
  103. package/dist/utils/bigintToBuffer.d.ts +1 -0
  104. package/dist/utils/bigintToBuffer.js +27 -0
  105. package/dist/utils/bytesToHex.d.ts +2 -0
  106. package/dist/utils/bytesToHex.js +15 -0
  107. package/dist/utils/getRandomValues.d.ts +4 -0
  108. package/dist/utils/getRandomValues.js +12 -0
  109. package/dist/utils/hexToBytes.d.ts +2 -0
  110. package/dist/utils/hexToBytes.js +14 -0
  111. package/dist/utils/index.d.ts +3 -0
  112. package/dist/utils/index.js +19 -0
  113. package/dist/utils/positiveMod.d.ts +1 -0
  114. package/dist/utils/positiveMod.js +11 -0
  115. package/dist/utils/promisifySync.d.ts +0 -0
  116. package/dist/utils/promisifySync.js +0 -0
  117. package/dist/utils/rotr32.d.ts +2 -0
  118. package/dist/utils/rotr32.js +7 -0
  119. package/dist/vrf.d.ts +74 -0
  120. package/dist/vrf.js +286 -0
  121. package/package.json +49 -4
@@ -0,0 +1,100 @@
1
+ import { type AffinePoint, type Group } from './abstract/curve.js';
2
+ import { type CurveFn, type ExtPointType } from './abstract/edwards.js';
3
+ import { type htfBasicOpts, type HTFMethod } from './abstract/hash-to-curve.js';
4
+ import { InternalCurveFn } from "./abstract/montgomery.js";
5
+ import { type Hex } from './abstract/utils.js';
6
+ export declare const ED25519_TORSION_SUBGROUP: string[];
7
+ /**
8
+ * ed25519 curve with EdDSA signatures.
9
+ * @example
10
+ * import { ed25519 } from '@noble/curves/ed25519';
11
+ * const priv = ed25519.utils.randomPrivateKey();
12
+ * const pub = ed25519.getPublicKey(priv);
13
+ * const msg = new TextEncoder().encode('hello');
14
+ * const sig = ed25519.sign(msg, priv);
15
+ * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
16
+ * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
17
+ */
18
+ export declare const ed25519: CurveFn;
19
+ export declare const ed25519ctx: CurveFn;
20
+ export declare const ed25519ph: CurveFn;
21
+ /**
22
+ * ECDH using curve25519 aka x25519.
23
+ * @example
24
+ * import { x25519 } from '@noble/curves/ed25519';
25
+ * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';
26
+ * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';
27
+ * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases
28
+ * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
29
+ * x25519.getPublicKey(x25519.utils.randomPrivateKey());
30
+ */
31
+ export declare const x25519: InternalCurveFn;
32
+ /**
33
+ * Converts ed25519 public key to x25519 public key. Uses formula:
34
+ * * `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
35
+ * * `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
36
+ * @example
37
+ * const someonesPub = ed25519.getPublicKey(ed25519.utils.randomPrivateKey());
38
+ * const aPriv = x25519.utils.randomPrivateKey();
39
+ * x25519.getSharedSecret(aPriv, edwardsToMontgomeryPub(someonesPub))
40
+ */
41
+ export declare function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array;
42
+ export declare const edwardsToMontgomery: typeof edwardsToMontgomeryPub;
43
+ /**
44
+ * Converts ed25519 secret key to x25519 secret key.
45
+ * @example
46
+ * const someonesPub = x25519.getPublicKey(x25519.utils.randomPrivateKey());
47
+ * const aPriv = ed25519.utils.randomPrivateKey();
48
+ * x25519.getSharedSecret(edwardsToMontgomeryPriv(aPriv), someonesPub)
49
+ */
50
+ export declare function edwardsToMontgomeryPriv(edwardsPriv: Uint8Array): Uint8Array;
51
+ export declare const hashToCurve: HTFMethod<bigint>;
52
+ export declare const encodeToCurve: HTFMethod<bigint>;
53
+ type ExtendedPoint = ExtPointType;
54
+ /**
55
+ * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
56
+ * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
57
+ * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
58
+ * but it should work in its own namespace: do not combine those two.
59
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448
60
+ */
61
+ declare class RistPoint implements Group<RistPoint> {
62
+ static BASE: RistPoint;
63
+ static ZERO: RistPoint;
64
+ private readonly ep;
65
+ constructor(ep: ExtendedPoint);
66
+ static fromAffine(ap: AffinePoint<bigint>): RistPoint;
67
+ /**
68
+ * Takes uniform output of 64-byte hash function like sha512 and converts it to `RistrettoPoint`.
69
+ * The hash-to-group operation applies Elligator twice and adds the results.
70
+ * **Note:** this is one-way map, there is no conversion from point to hash.
71
+ * https://ristretto.group/formulas/elligator.html
72
+ * @param hex 64-byte output of a hash function
73
+ */
74
+ static hashToCurve(hex: Hex): RistPoint;
75
+ /**
76
+ * Converts ristretto-encoded string to ristretto point.
77
+ * https://ristretto.group/formulas/decoding.html
78
+ * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
79
+ */
80
+ static fromHex(hex: Hex): RistPoint;
81
+ static msm(points: RistPoint[], scalars: bigint[]): RistPoint;
82
+ /**
83
+ * Encodes ristretto point to Uint8Array.
84
+ * https://ristretto.group/formulas/encoding.html
85
+ */
86
+ toRawBytes(): Uint8Array;
87
+ toHex(): string;
88
+ toString(): string;
89
+ equals(other: RistPoint): boolean;
90
+ add(other: RistPoint): RistPoint;
91
+ subtract(other: RistPoint): RistPoint;
92
+ multiply(scalar: bigint): RistPoint;
93
+ multiplyUnsafe(scalar: bigint): RistPoint;
94
+ double(): RistPoint;
95
+ negate(): RistPoint;
96
+ }
97
+ export declare const RistrettoPoint: typeof RistPoint;
98
+ export declare const hashToRistretto255: (msg: Uint8Array, options: htfBasicOpts) => RistPoint;
99
+ export declare const hash_to_ristretto255: (msg: Uint8Array, options: htfBasicOpts) => RistPoint;
100
+ export {};
@@ -0,0 +1,494 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.hash_to_ristretto255 = exports.hashToRistretto255 = exports.RistrettoPoint = exports.encodeToCurve = exports.hashToCurve = exports.edwardsToMontgomeryPriv = exports.edwardsToMontgomery = exports.edwardsToMontgomeryPub = exports.x25519 = exports.ed25519ph = exports.ed25519ctx = exports.ed25519 = exports.ED25519_TORSION_SUBGROUP = void 0;
15
+ /**
16
+ * ed25519 Twisted Edwards curve with following addons:
17
+ * - X25519 ECDH
18
+ * - Ristretto cofactor elimination
19
+ * - Elligator hash-to-group / point indistinguishability
20
+ * @module
21
+ */
22
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
23
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
24
+ var sha512_1 = require("./sha512.js");
25
+ var utils_1 = require("./utils.js");
26
+ var curve_js_1 = require("./abstract/curve.js");
27
+ var edwards_1 = require("./abstract/edwards.js");
28
+ var hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
29
+ var modular_js_1 = require("./abstract/modular.js");
30
+ var montgomery_1 = require("./abstract/montgomery.js");
31
+ var utils_js_1 = require("./abstract/utils.js");
32
+ var ED25519_P = BigInt('57896044618658097711785492504343953926634992332820282019728792003956564819949');
33
+ // √(-1) aka √(a) aka 2^((p-1)/4)
34
+ var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt('19681161376707505956807079304988542015446066515923890162744021073123829784752');
35
+ // prettier-ignore
36
+ var _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
37
+ // prettier-ignore
38
+ var _5n = BigInt(5), _8n = BigInt(8);
39
+ function ed25519_pow_2_252_3(x) {
40
+ // prettier-ignore
41
+ var _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
42
+ var P = ED25519_P;
43
+ var x2 = (x * x) % P;
44
+ var b2 = (x2 * x) % P; // x^3, 11
45
+ var b4 = ((0, modular_js_1.pow2)(b2, _2n, P) * b2) % P; // x^15, 1111
46
+ var b5 = ((0, modular_js_1.pow2)(b4, _1n, P) * x) % P; // x^31
47
+ var b10 = ((0, modular_js_1.pow2)(b5, _5n, P) * b5) % P;
48
+ var b20 = ((0, modular_js_1.pow2)(b10, _10n, P) * b10) % P;
49
+ var b40 = ((0, modular_js_1.pow2)(b20, _20n, P) * b20) % P;
50
+ var b80 = ((0, modular_js_1.pow2)(b40, _40n, P) * b40) % P;
51
+ var b160 = ((0, modular_js_1.pow2)(b80, _80n, P) * b80) % P;
52
+ var b240 = ((0, modular_js_1.pow2)(b160, _80n, P) * b80) % P;
53
+ var b250 = ((0, modular_js_1.pow2)(b240, _10n, P) * b10) % P;
54
+ var pow_p_5_8 = ((0, modular_js_1.pow2)(b250, _2n, P) * x) % P;
55
+ // ^ To pow to (p+3)/8, multiply it by x.
56
+ return { pow_p_5_8: pow_p_5_8, b2: b2 };
57
+ }
58
+ function adjustScalarBytes(bytes) {
59
+ // Section 5: For X25519, in order to decode 32 random bytes as an integer scalar,
60
+ // set the three least significant bits of the first byte
61
+ bytes[0] &= 248; // 0b1111_1000
62
+ // and the most significant bit of the last to zero,
63
+ bytes[31] &= 127; // 0b0111_1111
64
+ // set the second most significant bit of the last byte to 1
65
+ bytes[31] |= 64; // 0b0100_0000
66
+ return bytes;
67
+ }
68
+ // sqrt(u/v)
69
+ function uvRatio(u, v) {
70
+ var P = ED25519_P;
71
+ var v3 = (0, modular_js_1.mod)(v * v * v, P); // v³
72
+ var v7 = (0, modular_js_1.mod)(v3 * v3 * v, P); // v⁷
73
+ // (p+3)/8 and (p-5)/8
74
+ var pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
75
+ var x = (0, modular_js_1.mod)(u * v3 * pow, P); // (uv³)(uv⁷)^(p-5)/8
76
+ var vx2 = (0, modular_js_1.mod)(v * x * x, P); // vx²
77
+ var root1 = x; // First root candidate
78
+ var root2 = (0, modular_js_1.mod)(x * ED25519_SQRT_M1, P); // Second root candidate
79
+ var useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root
80
+ var useRoot2 = vx2 === (0, modular_js_1.mod)(-u, P); // If vx² = -u, set x <-- x * 2^((p-1)/4)
81
+ var noRoot = vx2 === (0, modular_js_1.mod)(-u * ED25519_SQRT_M1, P); // There is no valid root, vx² = -u√(-1)
82
+ if (useRoot1)
83
+ x = root1;
84
+ if (useRoot2 || noRoot)
85
+ x = root2; // We return root2 anyway, for const-time
86
+ if ((0, modular_js_1.isNegativeLE)(x, P))
87
+ x = (0, modular_js_1.mod)(-x, P);
88
+ return { isValid: useRoot1 || useRoot2, value: x };
89
+ }
90
+ // Just in case
91
+ exports.ED25519_TORSION_SUBGROUP = [
92
+ '0100000000000000000000000000000000000000000000000000000000000000',
93
+ 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a',
94
+ '0000000000000000000000000000000000000000000000000000000000000080',
95
+ '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05',
96
+ 'ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f',
97
+ '26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85',
98
+ '0000000000000000000000000000000000000000000000000000000000000000',
99
+ 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
100
+ ];
101
+ var Fp = /* @__PURE__ */ (function () { return (0, modular_js_1.Field)(ED25519_P, undefined, true); })();
102
+ var ed25519Defaults = /* @__PURE__ */ (function () {
103
+ return ({
104
+ // Param: a
105
+ a: BigInt(-1),
106
+ // d is equal to -121665/121666 over finite field.
107
+ // Negative number is P - number, and division is invert(number, P)
108
+ d: BigInt('37095705934669439343138083508754565189542113879843219016388785533085940283555'),
109
+ // Finite field 𝔽p over which we'll do calculations; 2n**255n - 19n
110
+ Fp: Fp,
111
+ // Subgroup order: how many points curve has
112
+ // 2n**252n + 27742317777372353535851937790883648493n;
113
+ n: BigInt('7237005577332262213973186563042994240857116359379907606001950938285454250989'),
114
+ // Cofactor
115
+ h: _8n,
116
+ // Base point (x, y) aka generator point
117
+ Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
118
+ Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
119
+ hash: sha512_1.sha512,
120
+ randomBytes: utils_1.randomBytes,
121
+ adjustScalarBytes: adjustScalarBytes,
122
+ // dom2
123
+ // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
124
+ // Constant-time, u/√v
125
+ uvRatio: uvRatio,
126
+ });
127
+ })();
128
+ /**
129
+ * ed25519 curve with EdDSA signatures.
130
+ * @example
131
+ * import { ed25519 } from '@noble/curves/ed25519';
132
+ * const priv = ed25519.utils.randomPrivateKey();
133
+ * const pub = ed25519.getPublicKey(priv);
134
+ * const msg = new TextEncoder().encode('hello');
135
+ * const sig = ed25519.sign(msg, priv);
136
+ * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
137
+ * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
138
+ */
139
+ exports.ed25519 = (function () { return (0, edwards_1.twistedEdwards)(ed25519Defaults); })();
140
+ function ed25519_domain(data, ctx, phflag) {
141
+ if (ctx.length > 255)
142
+ throw new Error('Context is too big');
143
+ return (0, utils_1.concatBytes)((0, uint8array_utils_1.fromUtf8)('SigEd25519 no Ed25519 collisions'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
144
+ }
145
+ exports.ed25519ctx = (function () {
146
+ return (0, edwards_1.twistedEdwards)(__assign(__assign({}, ed25519Defaults), { domain: ed25519_domain }));
147
+ })();
148
+ exports.ed25519ph = (function () {
149
+ return (0, edwards_1.twistedEdwards)(Object.assign({}, ed25519Defaults, {
150
+ domain: ed25519_domain,
151
+ prehash: sha512_1.sha512,
152
+ }));
153
+ })();
154
+ /**
155
+ * ECDH using curve25519 aka x25519.
156
+ * @example
157
+ * import { x25519 } from '@noble/curves/ed25519';
158
+ * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';
159
+ * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';
160
+ * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases
161
+ * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
162
+ * x25519.getPublicKey(x25519.utils.randomPrivateKey());
163
+ */
164
+ exports.x25519 = (function () {
165
+ return (0, montgomery_1.montgomery)({
166
+ P: ED25519_P,
167
+ a: BigInt(486662),
168
+ montgomeryBits: 255,
169
+ nByteLength: 32,
170
+ Gu: BigInt(9),
171
+ powPminus2: function (x) {
172
+ var P = ED25519_P;
173
+ // x^(p-2) aka x^(2^255-21)
174
+ var _a = ed25519_pow_2_252_3(x), pow_p_5_8 = _a.pow_p_5_8, b2 = _a.b2;
175
+ return (0, modular_js_1.mod)((0, modular_js_1.pow2)(pow_p_5_8, _3n, P) * b2, P);
176
+ },
177
+ adjustScalarBytes: adjustScalarBytes,
178
+ randomBytes: utils_1.randomBytes,
179
+ });
180
+ })();
181
+ /**
182
+ * Converts ed25519 public key to x25519 public key. Uses formula:
183
+ * * `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
184
+ * * `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
185
+ * @example
186
+ * const someonesPub = ed25519.getPublicKey(ed25519.utils.randomPrivateKey());
187
+ * const aPriv = x25519.utils.randomPrivateKey();
188
+ * x25519.getSharedSecret(aPriv, edwardsToMontgomeryPub(someonesPub))
189
+ */
190
+ function edwardsToMontgomeryPub(edwardsPub) {
191
+ var y = exports.ed25519.ExtendedPoint.fromHex(edwardsPub).y;
192
+ var _1n = BigInt(1);
193
+ return Fp.toBytes(Fp.create((_1n + y) * Fp.inv(_1n - y)));
194
+ }
195
+ exports.edwardsToMontgomeryPub = edwardsToMontgomeryPub;
196
+ exports.edwardsToMontgomery = edwardsToMontgomeryPub; // deprecated
197
+ /**
198
+ * Converts ed25519 secret key to x25519 secret key.
199
+ * @example
200
+ * const someonesPub = x25519.getPublicKey(x25519.utils.randomPrivateKey());
201
+ * const aPriv = ed25519.utils.randomPrivateKey();
202
+ * x25519.getSharedSecret(edwardsToMontgomeryPriv(aPriv), someonesPub)
203
+ */
204
+ function edwardsToMontgomeryPriv(edwardsPriv) {
205
+ var hashed = ed25519Defaults.hash(edwardsPriv.subarray(0, 32));
206
+ return ed25519Defaults.adjustScalarBytes(hashed).subarray(0, 32);
207
+ }
208
+ exports.edwardsToMontgomeryPriv = edwardsToMontgomeryPriv;
209
+ // Hash To Curve Elligator2 Map (NOTE: different from ristretto255 elligator)
210
+ // NOTE: very important part is usage of FpSqrtEven for ELL2_C1_EDWARDS, since
211
+ // SageMath returns different root first and everything falls apart
212
+ var ELL2_C1 = /* @__PURE__ */ (function () { return (Fp.ORDER + _3n) / _8n; })(); // 1. c1 = (q + 3) / 8 # Integer arithmetic
213
+ var ELL2_C2 = /* @__PURE__ */ (function () { return Fp.pow(_2n, ELL2_C1); })(); // 2. c2 = 2^c1
214
+ var ELL2_C3 = /* @__PURE__ */ (function () { return Fp.sqrt(Fp.neg(Fp.ONE)); })(); // 3. c3 = sqrt(-1)
215
+ // prettier-ignore
216
+ function map_to_curve_elligator2_curve25519(u) {
217
+ var ELL2_C4 = (Fp.ORDER - _5n) / _8n; // 4. c4 = (q - 5) / 8 # Integer arithmetic
218
+ var ELL2_J = BigInt(486662);
219
+ var tv1 = Fp.sqr(u); // 1. tv1 = u^2
220
+ tv1 = Fp.mul(tv1, _2n); // 2. tv1 = 2 * tv1
221
+ var xd = Fp.add(tv1, Fp.ONE); // 3. xd = tv1 + 1 # Nonzero: -1 is square (mod p), tv1 is not
222
+ var x1n = Fp.neg(ELL2_J); // 4. x1n = -J # x1 = x1n / xd = -J / (1 + 2 * u^2)
223
+ var tv2 = Fp.sqr(xd); // 5. tv2 = xd^2
224
+ var gxd = Fp.mul(tv2, xd); // 6. gxd = tv2 * xd # gxd = xd^3
225
+ var gx1 = Fp.mul(tv1, ELL2_J); // 7. gx1 = J * tv1 # x1n + J * xd
226
+ gx1 = Fp.mul(gx1, x1n); // 8. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
227
+ gx1 = Fp.add(gx1, tv2); // 9. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
228
+ gx1 = Fp.mul(gx1, x1n); // 10. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
229
+ var tv3 = Fp.sqr(gxd); // 11. tv3 = gxd^2
230
+ tv2 = Fp.sqr(tv3); // 12. tv2 = tv3^2 # gxd^4
231
+ tv3 = Fp.mul(tv3, gxd); // 13. tv3 = tv3 * gxd # gxd^3
232
+ tv3 = Fp.mul(tv3, gx1); // 14. tv3 = tv3 * gx1 # gx1 * gxd^3
233
+ tv2 = Fp.mul(tv2, tv3); // 15. tv2 = tv2 * tv3 # gx1 * gxd^7
234
+ var y11 = Fp.pow(tv2, ELL2_C4); // 16. y11 = tv2^c4 # (gx1 * gxd^7)^((p - 5) / 8)
235
+ y11 = Fp.mul(y11, tv3); // 17. y11 = y11 * tv3 # gx1*gxd^3*(gx1*gxd^7)^((p-5)/8)
236
+ var y12 = Fp.mul(y11, ELL2_C3); // 18. y12 = y11 * c3
237
+ tv2 = Fp.sqr(y11); // 19. tv2 = y11^2
238
+ tv2 = Fp.mul(tv2, gxd); // 20. tv2 = tv2 * gxd
239
+ var e1 = Fp.eql(tv2, gx1); // 21. e1 = tv2 == gx1
240
+ var y1 = Fp.cmov(y12, y11, e1); // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt
241
+ var x2n = Fp.mul(x1n, tv1); // 23. x2n = x1n * tv1 # x2 = x2n / xd = 2 * u^2 * x1n / xd
242
+ var y21 = Fp.mul(y11, u); // 24. y21 = y11 * u
243
+ y21 = Fp.mul(y21, ELL2_C2); // 25. y21 = y21 * c2
244
+ var y22 = Fp.mul(y21, ELL2_C3); // 26. y22 = y21 * c3
245
+ var gx2 = Fp.mul(gx1, tv1); // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1)
246
+ tv2 = Fp.sqr(y21); // 28. tv2 = y21^2
247
+ tv2 = Fp.mul(tv2, gxd); // 29. tv2 = tv2 * gxd
248
+ var e2 = Fp.eql(tv2, gx2); // 30. e2 = tv2 == gx2
249
+ var y2 = Fp.cmov(y22, y21, e2); // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt
250
+ tv2 = Fp.sqr(y1); // 32. tv2 = y1^2
251
+ tv2 = Fp.mul(tv2, gxd); // 33. tv2 = tv2 * gxd
252
+ var e3 = Fp.eql(tv2, gx1); // 34. e3 = tv2 == gx1
253
+ var xn = Fp.cmov(x2n, x1n, e3); // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2
254
+ var y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2
255
+ var e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y
256
+ y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)
257
+ return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1)
258
+ }
259
+ var ELL2_C1_EDWARDS = /* @__PURE__ */ (function () { return (0, modular_js_1.FpSqrtEven)(Fp, Fp.neg(BigInt(486664))); })(); // sgn0(c1) MUST equal 0
260
+ function map_to_curve_elligator2_edwards25519(u) {
261
+ var _a = map_to_curve_elligator2_curve25519(u), xMn = _a.xMn, xMd = _a.xMd, yMn = _a.yMn, yMd = _a.yMd; // 1. (xMn, xMd, yMn, yMd) =
262
+ // map_to_curve_elligator2_curve25519(u)
263
+ var xn = Fp.mul(xMn, yMd); // 2. xn = xMn * yMd
264
+ xn = Fp.mul(xn, ELL2_C1_EDWARDS); // 3. xn = xn * c1
265
+ var xd = Fp.mul(xMd, yMn); // 4. xd = xMd * yMn # xn / xd = c1 * xM / yM
266
+ var yn = Fp.sub(xMn, xMd); // 5. yn = xMn - xMd
267
+ var yd = Fp.add(xMn, xMd); // 6. yd = xMn + xMd # (n / d - 1) / (n / d + 1) = (n - d) / (n + d)
268
+ var tv1 = Fp.mul(xd, yd); // 7. tv1 = xd * yd
269
+ var e = Fp.eql(tv1, Fp.ZERO); // 8. e = tv1 == 0
270
+ xn = Fp.cmov(xn, Fp.ZERO, e); // 9. xn = CMOV(xn, 0, e)
271
+ xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)
272
+ yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)
273
+ yd = Fp.cmov(yd, Fp.ONE, e); // 12. yd = CMOV(yd, 1, e)
274
+ var inv = Fp.invertBatch([xd, yd]); // batch division
275
+ return { x: Fp.mul(xn, inv[0]), y: Fp.mul(yn, inv[1]) }; // 13. return (xn, xd, yn, yd)
276
+ }
277
+ var htf = /* @__PURE__ */ (function () {
278
+ return (0, hash_to_curve_js_1.createHasher)(exports.ed25519.ExtendedPoint, function (scalars) { return map_to_curve_elligator2_edwards25519(scalars[0]); }, {
279
+ DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
280
+ encodeDST: 'edwards25519_XMD:SHA-512_ELL2_NU_',
281
+ p: Fp.ORDER,
282
+ m: 1,
283
+ k: 128,
284
+ expand: 'xmd',
285
+ hash: sha512_1.sha512,
286
+ });
287
+ })();
288
+ exports.hashToCurve = (function () { return htf.hashToCurve; })();
289
+ exports.encodeToCurve = (function () { return htf.encodeToCurve; })();
290
+ function assertRstPoint(other) {
291
+ if (!(other instanceof RistPoint))
292
+ throw new Error('RistrettoPoint expected');
293
+ }
294
+ // √(-1) aka √(a) aka 2^((p-1)/4)
295
+ var SQRT_M1 = ED25519_SQRT_M1;
296
+ // √(ad - 1)
297
+ var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt('25063068953384623474111414158702152701244531502492656460079210482610430750235');
298
+ // 1 / √(a-d)
299
+ var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt('54469307008909316920995813868745141605393597292927456921205312896311721017578');
300
+ // 1-d²
301
+ var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt('1159843021668779879193775521855586647937357759715417654439879720876111806838');
302
+ // (d-1)²
303
+ var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt('40440834346308536858101042469323190826248399146238708352240133220865137265952');
304
+ // Calculates 1/√(number)
305
+ var invertSqrt = function (number) { return uvRatio(_1n, number); };
306
+ var MAX_255B = /* @__PURE__ */ BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
307
+ var bytes255ToNumberLE = function (bytes) {
308
+ return exports.ed25519.CURVE.Fp.create((0, utils_js_1.bytesToNumberLE)(bytes) & MAX_255B);
309
+ };
310
+ // Computes Elligator map for Ristretto
311
+ // https://ristretto.group/formulas/elligator.html
312
+ function calcElligatorRistrettoMap(r0) {
313
+ var d = exports.ed25519.CURVE.d;
314
+ var P = exports.ed25519.CURVE.Fp.ORDER;
315
+ var mod = exports.ed25519.CURVE.Fp.create;
316
+ var r = mod(SQRT_M1 * r0 * r0); // 1
317
+ var Ns = mod((r + _1n) * ONE_MINUS_D_SQ); // 2
318
+ var c = BigInt(-1); // 3
319
+ var D = mod((c - d * r) * mod(r + d)); // 4
320
+ var _a = uvRatio(Ns, D), Ns_D_is_sq = _a.isValid, s = _a.value; // 5
321
+ var s_ = mod(s * r0); // 6
322
+ if (!(0, modular_js_1.isNegativeLE)(s_, P))
323
+ s_ = mod(-s_);
324
+ if (!Ns_D_is_sq)
325
+ s = s_; // 7
326
+ if (!Ns_D_is_sq)
327
+ c = r; // 8
328
+ var Nt = mod(c * (r - _1n) * D_MINUS_ONE_SQ - D); // 9
329
+ var s2 = s * s;
330
+ var W0 = mod((s + s) * D); // 10
331
+ var W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11
332
+ var W2 = mod(_1n - s2); // 12
333
+ var W3 = mod(_1n + s2); // 13
334
+ return new exports.ed25519.ExtendedPoint(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
335
+ }
336
+ /**
337
+ * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
338
+ * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
339
+ * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
340
+ * but it should work in its own namespace: do not combine those two.
341
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448
342
+ */
343
+ var RistPoint = /** @class */ (function () {
344
+ // Private property to discourage combining ExtendedPoint + RistrettoPoint
345
+ // Always use Ristretto encoding/decoding instead.
346
+ function RistPoint(ep) {
347
+ this.ep = ep;
348
+ }
349
+ RistPoint.fromAffine = function (ap) {
350
+ return new RistPoint(exports.ed25519.ExtendedPoint.fromAffine(ap));
351
+ };
352
+ /**
353
+ * Takes uniform output of 64-byte hash function like sha512 and converts it to `RistrettoPoint`.
354
+ * The hash-to-group operation applies Elligator twice and adds the results.
355
+ * **Note:** this is one-way map, there is no conversion from point to hash.
356
+ * https://ristretto.group/formulas/elligator.html
357
+ * @param hex 64-byte output of a hash function
358
+ */
359
+ RistPoint.hashToCurve = function (hex) {
360
+ hex = (0, utils_js_1.ensureBytes)('ristrettoHash', hex, 64);
361
+ var r1 = bytes255ToNumberLE(hex.slice(0, 32));
362
+ var R1 = calcElligatorRistrettoMap(r1);
363
+ var r2 = bytes255ToNumberLE(hex.slice(32, 64));
364
+ var R2 = calcElligatorRistrettoMap(r2);
365
+ return new RistPoint(R1.add(R2));
366
+ };
367
+ /**
368
+ * Converts ristretto-encoded string to ristretto point.
369
+ * https://ristretto.group/formulas/decoding.html
370
+ * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
371
+ */
372
+ RistPoint.fromHex = function (hex) {
373
+ hex = (0, utils_js_1.ensureBytes)('ristrettoHex', hex, 32);
374
+ var _a = exports.ed25519.CURVE, a = _a.a, d = _a.d;
375
+ var P = exports.ed25519.CURVE.Fp.ORDER;
376
+ var mod = exports.ed25519.CURVE.Fp.create;
377
+ var emsg = 'RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint';
378
+ var s = bytes255ToNumberLE(hex);
379
+ // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
380
+ // 3. Check that s is non-negative, or else abort
381
+ if (!(0, utils_js_1.equalBytes)((0, utils_js_1.numberToBytesLE)(s, 32), hex) || (0, modular_js_1.isNegativeLE)(s, P))
382
+ throw new Error(emsg);
383
+ var s2 = mod(s * s);
384
+ var u1 = mod(_1n + a * s2); // 4 (a is -1)
385
+ var u2 = mod(_1n - a * s2); // 5
386
+ var u1_2 = mod(u1 * u1);
387
+ var u2_2 = mod(u2 * u2);
388
+ var v = mod(a * d * u1_2 - u2_2); // 6
389
+ var _b = invertSqrt(mod(v * u2_2)), isValid = _b.isValid, I = _b.value; // 7
390
+ var Dx = mod(I * u2); // 8
391
+ var Dy = mod(I * Dx * v); // 9
392
+ var x = mod((s + s) * Dx); // 10
393
+ if ((0, modular_js_1.isNegativeLE)(x, P))
394
+ x = mod(-x); // 10
395
+ var y = mod(u1 * Dy); // 11
396
+ var t = mod(x * y); // 12
397
+ if (!isValid || (0, modular_js_1.isNegativeLE)(t, P) || y === _0n)
398
+ throw new Error(emsg);
399
+ return new RistPoint(new exports.ed25519.ExtendedPoint(x, y, _1n, t));
400
+ };
401
+ RistPoint.msm = function (points, scalars) {
402
+ var Fn = (0, modular_js_1.Field)(exports.ed25519.CURVE.n, exports.ed25519.CURVE.nBitLength);
403
+ return (0, curve_js_1.pippenger)(RistPoint, Fn, points, scalars);
404
+ };
405
+ /**
406
+ * Encodes ristretto point to Uint8Array.
407
+ * https://ristretto.group/formulas/encoding.html
408
+ */
409
+ RistPoint.prototype.toRawBytes = function () {
410
+ var _a = this.ep, x = _a.ex, y = _a.ey, z = _a.ez, t = _a.et;
411
+ var P = exports.ed25519.CURVE.Fp.ORDER;
412
+ var mod = exports.ed25519.CURVE.Fp.create;
413
+ var u1 = mod(mod(z + y) * mod(z - y)); // 1
414
+ var u2 = mod(x * y); // 2
415
+ // Square root always exists
416
+ var u2sq = mod(u2 * u2);
417
+ var invsqrt = invertSqrt(mod(u1 * u2sq)).value; // 3
418
+ var D1 = mod(invsqrt * u1); // 4
419
+ var D2 = mod(invsqrt * u2); // 5
420
+ var zInv = mod(D1 * D2 * t); // 6
421
+ var D; // 7
422
+ if ((0, modular_js_1.isNegativeLE)(t * zInv, P)) {
423
+ var _x = mod(y * SQRT_M1);
424
+ var _y = mod(x * SQRT_M1);
425
+ x = _x;
426
+ y = _y;
427
+ D = mod(D1 * INVSQRT_A_MINUS_D);
428
+ }
429
+ else {
430
+ D = D2; // 8
431
+ }
432
+ if ((0, modular_js_1.isNegativeLE)(x * zInv, P))
433
+ y = mod(-y); // 9
434
+ var s = mod((z - y) * D); // 10 (check footer's note, no sqrt(-a))
435
+ if ((0, modular_js_1.isNegativeLE)(s, P))
436
+ s = mod(-s);
437
+ return (0, utils_js_1.numberToBytesLE)(s, 32); // 11
438
+ };
439
+ RistPoint.prototype.toHex = function () {
440
+ return (0, utils_js_1.bytesToHex)(this.toRawBytes());
441
+ };
442
+ RistPoint.prototype.toString = function () {
443
+ return this.toHex();
444
+ };
445
+ // Compare one point to another.
446
+ RistPoint.prototype.equals = function (other) {
447
+ assertRstPoint(other);
448
+ var _a = this.ep, X1 = _a.ex, Y1 = _a.ey;
449
+ var _b = other.ep, X2 = _b.ex, Y2 = _b.ey;
450
+ var mod = exports.ed25519.CURVE.Fp.create;
451
+ // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
452
+ var one = mod(X1 * Y2) === mod(Y1 * X2);
453
+ var two = mod(Y1 * Y2) === mod(X1 * X2);
454
+ return one || two;
455
+ };
456
+ RistPoint.prototype.add = function (other) {
457
+ assertRstPoint(other);
458
+ return new RistPoint(this.ep.add(other.ep));
459
+ };
460
+ RistPoint.prototype.subtract = function (other) {
461
+ assertRstPoint(other);
462
+ return new RistPoint(this.ep.subtract(other.ep));
463
+ };
464
+ RistPoint.prototype.multiply = function (scalar) {
465
+ return new RistPoint(this.ep.multiply(scalar));
466
+ };
467
+ RistPoint.prototype.multiplyUnsafe = function (scalar) {
468
+ return new RistPoint(this.ep.multiplyUnsafe(scalar));
469
+ };
470
+ RistPoint.prototype.double = function () {
471
+ return new RistPoint(this.ep.double());
472
+ };
473
+ RistPoint.prototype.negate = function () {
474
+ return new RistPoint(this.ep.negate());
475
+ };
476
+ return RistPoint;
477
+ }());
478
+ exports.RistrettoPoint = (function () {
479
+ if (!RistPoint.BASE)
480
+ RistPoint.BASE = new RistPoint(exports.ed25519.ExtendedPoint.BASE);
481
+ if (!RistPoint.ZERO)
482
+ RistPoint.ZERO = new RistPoint(exports.ed25519.ExtendedPoint.ZERO);
483
+ return RistPoint;
484
+ })();
485
+ // Hashing to ristretto255. https://www.rfc-editor.org/rfc/rfc9380#appendix-B
486
+ var hashToRistretto255 = function (msg, options) {
487
+ var d = options.DST;
488
+ var DST = typeof d === 'string' ? (0, uint8array_utils_1.fromUtf8)(d) : d;
489
+ var uniform_bytes = (0, hash_to_curve_js_1.expand_message_xmd)(msg, DST, 64, sha512_1.sha512);
490
+ var P = RistPoint.hashToCurve(uniform_bytes);
491
+ return P;
492
+ };
493
+ exports.hashToRistretto255 = hashToRistretto255;
494
+ exports.hash_to_ristretto255 = exports.hashToRistretto255; // legacy
@@ -0,0 +1,25 @@
1
+ import { Hash, CHash, Input } from './utils.js';
2
+ export declare class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> {
3
+ oHash: T;
4
+ iHash: T;
5
+ blockLen: number;
6
+ outputLen: number;
7
+ private finished;
8
+ private destroyed;
9
+ constructor(hash: CHash, _key: Input);
10
+ update(buf: Input): this;
11
+ digestInto(out: Uint8Array): void;
12
+ digest(): Uint8Array;
13
+ _cloneInto(to?: HMAC<T>): HMAC<T>;
14
+ destroy(): void;
15
+ }
16
+ /**
17
+ * HMAC: RFC2104 message authentication code.
18
+ * @param hash - function that would be used e.g. sha256
19
+ * @param key - message key
20
+ * @param message - message data
21
+ */
22
+ export declare const hmac: {
23
+ (hash: CHash, key: Input, message: Input): Uint8Array;
24
+ create(hash: CHash, key: Input): HMAC<any>;
25
+ };