@ac-kit/crypto-rsa 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +79 -0
- package/LICENSE +28 -0
- package/README.md +113 -2
- package/dist/_bytes.d.ts +18 -0
- package/dist/_bytes.d.ts.map +1 -0
- package/dist/_bytes.js +42 -0
- package/dist/_bytes.js.map +1 -0
- package/dist/encoding/_der.d.ts +51 -0
- package/dist/encoding/_der.d.ts.map +1 -0
- package/dist/encoding/_der.js +173 -0
- package/dist/encoding/_der.js.map +1 -0
- package/dist/encoding/rsa-algorithm.d.ts +132 -0
- package/dist/encoding/rsa-algorithm.d.ts.map +1 -0
- package/dist/encoding/rsa-algorithm.js +475 -0
- package/dist/encoding/rsa-algorithm.js.map +1 -0
- package/dist/hash/rsa-hash.d.ts +68 -0
- package/dist/hash/rsa-hash.d.ts.map +1 -0
- package/dist/hash/rsa-hash.js +86 -0
- package/dist/hash/rsa-hash.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/key/_generate.d.ts +39 -0
- package/dist/key/_generate.d.ts.map +1 -0
- package/dist/key/_generate.js +124 -0
- package/dist/key/_generate.js.map +1 -0
- package/dist/key/_key-state.d.ts +53 -0
- package/dist/key/_key-state.d.ts.map +1 -0
- package/dist/key/_key-state.js +26 -0
- package/dist/key/_key-state.js.map +1 -0
- package/dist/key/_primality.d.ts +22 -0
- package/dist/key/_primality.d.ts.map +1 -0
- package/dist/key/_primality.js +156 -0
- package/dist/key/_primality.js.map +1 -0
- package/dist/key/_primitives.d.ts +27 -0
- package/dist/key/_primitives.d.ts.map +1 -0
- package/dist/key/_primitives.js +247 -0
- package/dist/key/_primitives.js.map +1 -0
- package/dist/key/_validate.d.ts +60 -0
- package/dist/key/_validate.d.ts.map +1 -0
- package/dist/key/_validate.js +328 -0
- package/dist/key/_validate.js.map +1 -0
- package/dist/key/rsa-private-key.d.ts +180 -0
- package/dist/key/rsa-private-key.d.ts.map +1 -0
- package/dist/key/rsa-private-key.js +479 -0
- package/dist/key/rsa-private-key.js.map +1 -0
- package/dist/key/rsa-public-key.d.ts +117 -0
- package/dist/key/rsa-public-key.d.ts.map +1 -0
- package/dist/key/rsa-public-key.js +196 -0
- package/dist/key/rsa-public-key.js.map +1 -0
- package/dist/rsa-error.d.ts +54 -0
- package/dist/rsa-error.d.ts.map +1 -0
- package/dist/rsa-error.js +14 -0
- package/dist/rsa-error.js.map +1 -0
- package/dist/scheme/_scheme-checks.d.ts +50 -0
- package/dist/scheme/_scheme-checks.d.ts.map +1 -0
- package/dist/scheme/_scheme-checks.js +162 -0
- package/dist/scheme/_scheme-checks.js.map +1 -0
- package/dist/scheme/_signature.d.ts +8 -0
- package/dist/scheme/_signature.d.ts.map +1 -0
- package/dist/scheme/_signature.js +13 -0
- package/dist/scheme/_signature.js.map +1 -0
- package/dist/scheme/rsaes-oaep.d.ts +75 -0
- package/dist/scheme/rsaes-oaep.d.ts.map +1 -0
- package/dist/scheme/rsaes-oaep.js +191 -0
- package/dist/scheme/rsaes-oaep.js.map +1 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.d.ts +73 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.d.ts.map +1 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.js +133 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.js.map +1 -0
- package/dist/scheme/rsassa-pss.d.ts +88 -0
- package/dist/scheme/rsassa-pss.d.ts.map +1 -0
- package/dist/scheme/rsassa-pss.js +175 -0
- package/dist/scheme/rsassa-pss.js.map +1 -0
- package/package.json +75 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A hash for the RSA schemes: a synchronous one-shot function, its digest
|
|
4
|
+
* length and the object identifier that names it in a PKCS #1 `DigestInfo` and
|
|
5
|
+
* in the `RSASSA-PSS-params` and `RSAES-OAEP-params` of RFC 8017 Appendix A.
|
|
6
|
+
*/
|
|
7
|
+
export interface RsaHash {
|
|
8
|
+
/** The name the standards and the test corpora use, e.g. `"SHA-256"`. */
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/** The dotted object identifier, e.g. `"2.16.840.1.101.3.4.2.1"`. */
|
|
11
|
+
readonly oid: string;
|
|
12
|
+
/** Digest length in bytes. */
|
|
13
|
+
readonly digestLength: number;
|
|
14
|
+
/** The digest of `data`, `digestLength` bytes. */
|
|
15
|
+
readonly hash: Callable<[data: Uint8Array], Uint8Array>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The mask generation function of RSASSA-PSS and RSAES-OAEP: MGF1 over a hash
|
|
19
|
+
* (RFC 8017 §B.2.1), or SHAKE128 or SHAKE256 used directly as the mask
|
|
20
|
+
* generator, as RFC 8702 §3.2 does for RSASSA-PSS with a SHAKE hash.
|
|
21
|
+
*/
|
|
22
|
+
export type RsaMaskGeneration = {
|
|
23
|
+
readonly name: "MGF1";
|
|
24
|
+
readonly hash: RsaHash;
|
|
25
|
+
} | {
|
|
26
|
+
readonly name: "SHAKE128";
|
|
27
|
+
} | {
|
|
28
|
+
readonly name: "SHAKE256";
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The hashes the RSA schemes take, by the names RFC 8017, FIPS 186-5, Web
|
|
32
|
+
* Crypto and Wycheproof use. SHA-1 is kept for verifying legacy signatures and
|
|
33
|
+
* for MGF1, which RFC 8017 still lists; SHAKE128 and SHAKE256 have the fixed 32.
|
|
34
|
+
* and 64-byte outputs of RFC 8702. SHA-512/224 and SHA-512/256 wait for
|
|
35
|
+
* `@ac-kit/crypto-hash`.
|
|
36
|
+
*/
|
|
37
|
+
export declare const RSA_HASHES: Readonly<{
|
|
38
|
+
"SHA-1": RsaHash;
|
|
39
|
+
"SHA-224": RsaHash;
|
|
40
|
+
"SHA-256": RsaHash;
|
|
41
|
+
"SHA-384": RsaHash;
|
|
42
|
+
"SHA-512": RsaHash;
|
|
43
|
+
"SHA3-224": RsaHash;
|
|
44
|
+
"SHA3-256": RsaHash;
|
|
45
|
+
"SHA3-384": RsaHash;
|
|
46
|
+
"SHA3-512": RsaHash;
|
|
47
|
+
SHAKE128: RsaHash;
|
|
48
|
+
SHAKE256: RsaHash;
|
|
49
|
+
}>;
|
|
50
|
+
/** The name of a hash of {@link RSA_HASHES}. */
|
|
51
|
+
export type RsaHashName = keyof typeof RSA_HASHES;
|
|
52
|
+
/** A hash given by value or by its {@link RSA_HASHES} name. */
|
|
53
|
+
export type RsaHashInput = RsaHash | RsaHashName;
|
|
54
|
+
/** MGF1 over `hash` (RFC 8017 §B.2.1). */
|
|
55
|
+
export declare function mgf1(hash: RsaHash): RsaMaskGeneration;
|
|
56
|
+
/**
|
|
57
|
+
* The mask generation a scheme over `hash` uses by default: SHAKE itself for a
|
|
58
|
+
* SHAKE hash (RFC 8702), MGF1 over the same hash otherwise (RFC 8017 §9.1 and
|
|
59
|
+
* §7.1 recommend the same hash).
|
|
60
|
+
*/
|
|
61
|
+
export declare function defaultMaskGeneration(hash: RsaHash): RsaMaskGeneration;
|
|
62
|
+
/**
|
|
63
|
+
* XORs the `out.length`-byte mask of `seed` into `out` (RFC 8017 §B.2.1, RFC
|
|
64
|
+
* 8702 §3.2). The mask's bytes never exist apart from `out`, except inside the
|
|
65
|
+
* hash.
|
|
66
|
+
*/
|
|
67
|
+
export declare function applyMask(out: Uint8Array, seed: Uint8Array, maskGeneration: RsaMaskGeneration): void;
|
|
68
|
+
//# sourceMappingURL=rsa-hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsa-hash.d.ts","sourceRoot":"","sources":["../../src/hash/rsa-hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAe7C;;;;GAIG;AACH,MAAM,WAAW,OAA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{
|
|
3
|
+
return Object.freeze({ name, oid, digestLength, hash });
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The hashes the RSA schemes take, by the names RFC 8017, FIPS 186-5, Web
|
|
7
|
+
* Crypto and Wycheproof use. SHA-1 is kept for verifying legacy signatures and
|
|
8
|
+
* for MGF1, which RFC 8017 still lists; SHAKE128 and SHAKE256 have the fixed 32.
|
|
9
|
+
* and 64-byte outputs of RFC 8702. SHA-512/224 and SHA-512/256 wait for
|
|
10
|
+
* `@ac-kit/crypto-hash`.
|
|
11
|
+
*/
|
|
12
|
+
export const RSA_HASHES = Object.freeze({
|
|
13
|
+
"SHA-1": rsaHash_("SHA-1", "1.3.14.3.2.26", 20, sha1Js),
|
|
14
|
+
"SHA-224": rsaHash_("SHA-224", "2.16.840.1.101.3.4.2.4", 28, sha224),
|
|
15
|
+
"SHA-256": rsaHash_("SHA-256", "2.16.840.1.101.3.4.2.1", 32, sha256Ts),
|
|
16
|
+
"SHA-384": rsaHash_("SHA-384", "2.16.840.1.101.3.4.2.2", 48, sha384Ts),
|
|
17
|
+
"SHA-512": rsaHash_("SHA-512", "2.16.840.1.101.3.4.2.3", 64, sha512Ts),
|
|
18
|
+
"SHA3-224": rsaHash_("SHA3-224", "2.16.840.1.101.3.4.2.7", 28, sha3_224),
|
|
19
|
+
"SHA3-256": rsaHash_("SHA3-256", "2.16.840.1.101.3.4.2.8", 32, sha3_256),
|
|
20
|
+
"SHA3-384": rsaHash_("SHA3-384", "2.16.840.1.101.3.4.2.9", 48, sha3_384),
|
|
21
|
+
"SHA3-512": rsaHash_("SHA3-512", "2.16.840.1.101.3.4.2.10", 64, sha3_512),
|
|
22
|
+
SHAKE128: rsaHash_("SHAKE128", "2.16.840.1.101.3.4.2.11", 32, (data) => shake128(data, 32)),
|
|
23
|
+
SHAKE256: rsaHash_("SHAKE256", "2.16.840.1.101.3.4.2.12", 64, (data) => shake256(data, 64)),
|
|
24
|
+
});
|
|
25
|
+
/** MGF1 over `hash` (RFC 8017 §B.2.1). */
|
|
26
|
+
export function mgf1(hash) {
|
|
27
|
+
return Object.freeze({ name: "MGF1", hash });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The mask generation a scheme over `hash` uses by default: SHAKE itself for a
|
|
31
|
+
* SHAKE hash (RFC 8702), MGF1 over the same hash otherwise (RFC 8017 §9.1 and
|
|
32
|
+
* §7.1 recommend the same hash).
|
|
33
|
+
*/
|
|
34
|
+
export function defaultMaskGeneration(hash) {
|
|
35
|
+
if (hash.oid === RSA_HASHES.SHAKE128.oid) {
|
|
36
|
+
return Object.freeze({ name: "SHAKE128" });
|
|
37
|
+
}
|
|
38
|
+
if (hash.oid === RSA_HASHES.SHAKE256.oid) {
|
|
39
|
+
return Object.freeze({ name: "SHAKE256" });
|
|
40
|
+
}
|
|
41
|
+
return mgf1(hash);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* XORs the `out.length`-byte mask of `seed` into `out` (RFC 8017 §B.2.1, RFC
|
|
45
|
+
* 8702 §3.2). The mask's bytes never exist apart from `out`, except inside the
|
|
46
|
+
* hash.
|
|
47
|
+
*/
|
|
48
|
+
export function applyMask(out, seed, maskGeneration) {
|
|
49
|
+
if (maskGeneration.name !== "MGF1") {
|
|
50
|
+
const mask = maskGeneration.name === "SHAKE128"
|
|
51
|
+
? shake128(seed, out.length)
|
|
52
|
+
: shake256(seed, out.length);
|
|
53
|
+
try {
|
|
54
|
+
xorInto_(out, mask, 0);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
mask.fill(0);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const { hash } = maskGeneration;
|
|
62
|
+
const input = new Uint8Array(seed.length + 4);
|
|
63
|
+
try {
|
|
64
|
+
input.set(seed);
|
|
65
|
+
for (let counter = 0, offset = 0; offset < out.length; counter++, offset += hash.digestLength) {
|
|
66
|
+
new DataView(input.buffer).setUint32(seed.length, counter);
|
|
67
|
+
const block = hash.hash(input);
|
|
68
|
+
try {
|
|
69
|
+
xorInto_(out, block, offset);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
block.fill(0);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
input.fill(0);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function xorInto_(out, mask, offset) {
|
|
81
|
+
const end = Math.min(out.length, offset + mask.length);
|
|
82
|
+
for (let index = offset; index < end; index++) {
|
|
83
|
+
out[index] = out[index] ^ mask[index - offset];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=rsa-hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsa-hash.js","sourceRoot":"","sources":["../../src/hash/rsa-hash.ts"],"names":[],"mappings":"AACA,OAAO,EACN,MAAM,EACN,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,GACR,MAAM,qBAAqB,CAAC;AA4B7B,SAAS,QAAQ,CAChB,IAAY,EACZ,GAAW,EACX,YAAoB,EACpB,IAA8C;IAE9C,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC;IACvC,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,CAAC;IACvD,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,wBAAwB,EAAE,EAAE,EAAE,MAAM,CAAC;IACpE,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,wBAAwB,EAAE,EAAE,EAAE,QAAQ,CAAC;IACtE,SAAS,EA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,QAAQ,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;YAC9B,CAAC;oBAAS,CAAC;gBACV,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACf,CAAC;QACF,CAAC;IACF,CAAC;YAAS,CAAC;QACV,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACf,CAAC;AACF,CAAC;AAED,SAAS,QAAQ,CAAC,GAAe,EAAE,IAAgB,EAAE,MAAc;IAClE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAEvD,KAAK,IAAI,KAAK,GAAG,MAAM,EAAE,KAAK,GAAG,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,KAAK,CAAE,GAAG,IAAI,CAAC,KAAK,GAAG,MAAM,CAAE,CAAC;IAClD,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { decodeRsaKeyAlgorithm, decodeRsaOaepAlgorithm, decodeRsaSignatureAlgorithm, encodeRsaKeyAlgorithm, encodeRsaOaepAlgorithm, encodeRsaSignatureAlgorithm, maskGenerationEqual, pssParametersEqual, type RsaKeyAlgorithm, type RsaOaepParameters, type RsaPssParameters, type RsaSignatureAlgorithm, } from "./encoding/rsa-algorithm.js";
|
|
2
|
+
export { defaultMaskGeneration, mgf1, RSA_HASHES, type RsaHash, type RsaHashInput, type RsaHashName, type RsaMaskGeneration, } from "./hash/rsa-hash.js";
|
|
3
|
+
export { type RsaAsyncKeyGenerationOptions, type RsaCrtComponents, type RsaKeyGenerationOptions, type RsaOtherPrimeComponents, RsaPrivateKey, type RsaPrivateKeyComponents, type RsaPrivateKeyImportOptions, type RsaPrivateKeyOptions, type RsaPrimesInput, } from "./key/rsa-private-key.js";
|
|
4
|
+
export { RSA_ENCRYPTION_ALGORITHM, RSA_MAXIMUM_MODULUS_BITS, RSA_MINIMUM_MODULUS_BITS, type RsaKeyOptions, RsaPublicKey, type RsaPublicKeyComponents, } from "./key/rsa-public-key.js";
|
|
5
|
+
export { RsaError, type RsaErrorReason } from "./rsa-error.js";
|
|
6
|
+
export { type RsaesOaepCallOptions, type RsaesOaepEncryptOptions, rsaesOaep, type RsaesOaepOptions, RsaesOaepScheme, } from "./scheme/rsaes-oaep.js";
|
|
7
|
+
export { rsassaPkcs1V15, RsassaPkcs1V15Scheme, } from "./scheme/rsassa-pkcs1-v1_5.js";
|
|
8
|
+
export { rsassaPss, type RsassaPssOptions, RsassaPssScheme, type RsassaPssSignOptions, } from "./scheme/rsassa-pss.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,EAC3B,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,GAC1B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACN,qBAAqB,EACrB,IAAI,EACJ,UAAU,EACV,KAAK,OAAO,EACZ,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,iBAAiB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACN,KAAK,4BAA4B,EACjC,KAAK,gBAAgB,EACrB,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC5B,aAAa,EACb,KAAK,uBAAuB,EAC5B,KAAK,0BAA0B,EAC/B,KAAK,oBAAoB,EACzB,KAAK,cAAc,GACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACN,wBAAwB,EACxB,wBAAwB,EACxB,wBAAwB,EACxB,KAAK,aAAa,EAClB,YAAY,EACZ,KAAK,sBAAsB,GAC3B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EACN,KAAK,oBAAoB,EACzB,KAAK,uBAAuB,EAC5B,SAAS,EACT,KAAK,gBAAgB,EACrB,eAAe,GACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACN,cAAc,EACd,oBAAoB,GACpB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACN,SAAS,EACT,KAAK,gBAAgB,EACrB,eAAe,EACf,KAAK,oBAAoB,GACzB,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { decodeRsaKeyAlgorithm, decodeRsaOaepAlgorithm, decodeRsaSignatureAlgorithm, encodeRsaKeyAlgorithm, encodeRsaOaepAlgorithm, encodeRsaSignatureAlgorithm, maskGenerationEqual, pssParametersEqual, } from "./encoding/rsa-algorithm.js";
|
|
2
|
+
export { defaultMaskGeneration, mgf1, RSA_HASHES, } from "./hash/rsa-hash.js";
|
|
3
|
+
export { RsaPrivateKey, } from "./key/rsa-private-key.js";
|
|
4
|
+
export { RSA_ENCRYPTION_ALGORITHM, RSA_MAXIMUM_MODULUS_BITS, RSA_MINIMUM_MODULUS_BITS, RsaPublicKey, } from "./key/rsa-public-key.js";
|
|
5
|
+
export { RsaError } from "./rsa-error.js";
|
|
6
|
+
export { rsaesOaep, RsaesOaepScheme, } from "./scheme/rsaes-oaep.js";
|
|
7
|
+
export { rsassaPkcs1V15, RsassaPkcs1V15Scheme, } from "./scheme/rsassa-pkcs1-v1_5.js";
|
|
8
|
+
export { rsassaPss, RsassaPssScheme, } from "./scheme/rsassa-pss.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,EAC3B,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,GAKlB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACN,qBAAqB,EACrB,IAAI,EACJ,UAAU,GAKV,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAKN,aAAa,GAKb,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACN,wBAAwB,EACxB,wBAAwB,EACxB,wBAAwB,EAExB,YAAY,GAEZ,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAuB,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAGN,SAAS,EAET,eAAe,GACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACN,cAAc,EACd,oBAAoB,GACpB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACN,SAAS,EAET,eAAe,GAEf,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Miller–Rabin rounds of FIPS 186-5 Table B.1 for p and q, at the error
|
|
3
|
+
* probability that matches each modulus size's security strength (2⁻¹¹², 2⁻¹²⁸,
|
|
4
|
+
* 2⁻¹⁴⁴): 5 below 3072-bit moduli, 4 from there on.
|
|
5
|
+
*/
|
|
6
|
+
export declare function millerRabinRoundsFor(modulusBits: number): number;
|
|
7
|
+
/**
|
|
8
|
+
* ⌈√2 · 2^(bits − 1)⌉ over ⌈bits / 8⌉ bytes: the smallest prime FIPS 186-5
|
|
9
|
+
* A.1.1 2(b) admits. Public; cached by size, never to be written.
|
|
10
|
+
*/
|
|
11
|
+
export declare function primeLowerBound(bits: number): Uint8Array<ArrayBuffer>;
|
|
12
|
+
/** Why FIPS 186-5 A.1.3 discards a candidate, or `"accepted"`. */
|
|
13
|
+
export type CandidateVerdict = "accepted"
|
|
14
|
+
/** Step 4.4 or 5.4: below ⌈√2 · 2^(nlen/2 − 1)⌉. */
|
|
15
|
+
| "tooSmall"
|
|
16
|
+
/** Step 5.5: |p − q| ≤ 2^(nlen/2 − 100). */
|
|
17
|
+
| "tooClose"
|
|
18
|
+
/** Step 4.5 or 5.6: GCD(candidate − 1, e) ≠ 1. */
|
|
19
|
+
| "notCoprime"
|
|
20
|
+
/** Step 4.5.1 or 5.6.1: composite. */
|
|
21
|
+
| "composite";
|
|
22
|
+
/**
|
|
23
|
+
* Steps 4.3 to 4.5 (p) or 5.3 to 5.6 (q, with `other` = p) of FIPS 186-5 A.1.3
|
|
24
|
+
* for the random `bits`-bit string `candidate`, made odd here as step 4.3 says.
|
|
25
|
+
* On acceptance `inverse` holds e⁻¹ mod (candidate − 1), the CRT exponent.
|
|
26
|
+
* Constant time in an accepted candidate.
|
|
27
|
+
*/
|
|
28
|
+
export declare function checkPrimeCandidate(candidate: Uint8Array, publicExponent: Uint8Array, bits: number, rounds: number, other: Uint8Array | undefined, inverse: Uint8Array): CandidateVerdict;
|
|
29
|
+
/**
|
|
30
|
+
* A prime of `bits` bits by FIPS 186-5 A.1.3 step 4 (p) or 5 (q, `other` = p),
|
|
31
|
+
* one candidate a step. Only candidates that reach the coprimality test count
|
|
32
|
+
* towards the bound i, as in the standard: a candidate refused at step 4.4, 5.4
|
|
33
|
+
* or 5.5 is redrawn at once.
|
|
34
|
+
*
|
|
35
|
+
* @throws {RsaError} `keyGenerationFailed` after 5·nlen (p) or 10·nlen (q)
|
|
36
|
+
* counted candidates (steps 4.7 and 5.8).
|
|
37
|
+
*/
|
|
38
|
+
export declare function generatePrimeSteps(bits: number, publicExponent: Uint8Array, other: Uint8Array | undefined): Generator<undefined, Uint8Array<ArrayBuffer>, undefined>;
|
|
39
|
+
//# sourceMappingURL=_generate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_generate.d.ts","sourceRoot":"","sources":["../../src/key/_generate.ts"],"names":[],"mappings":"AAWA;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAEhE;AAID;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CA8BrE;AAED,kEAAkE;AAClE,MAAM,MAAM,gBAAgB,GACzB,UAAU;AACZ,oDAAoD;GAClD,UAAU;AACZ,4CAA4C;GAC1C,UAAU;AACZ,kDAAkD;GAChD,YAAY;AACd,sCAAsC;GACpC,WAAW,CAAC;AAEf;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAClC,SAAS,EAAE,UAAU,EACrB,cAAc,EAAE,UAAU,EAC1B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,GAAG,SAAS,EAC7B,OAAO,EAAE,UAAU,GACjB,gBAAgB,CAgClB;AAED;;;;;;;;GAQG;AACH,wBAAiB,kBAAkB,CAClC,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,UAAU,EAC1B,KAAK,EAAE,UAAU,GAAG,SAAS,GAC3B,SAAS,CAAC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,EAAE,SAAS,CAAC,CAyC1D"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { getRandomBytes } from "@ac-kit/crypto-random";
|
|
2
|
+
import { constantTimeUnsignedCompare, constantTimeUnsignedModInverse, constantTimeUnsignedSubtract, } from "@ac-kit/crypto-safe";
|
|
3
|
+
import { bytesOfUnsigned } from "../_bytes.js";
|
|
4
|
+
import { RsaError } from "../rsa-error.js";
|
|
5
|
+
import { passesMillerRabin, passesTrialDivision } from "./_primality.js";
|
|
6
|
+
/**
|
|
7
|
+
* The Miller–Rabin rounds of FIPS 186-5 Table B.1 for p and q, at the error
|
|
8
|
+
* probability that matches each modulus size's security strength (2⁻¹¹², 2⁻¹²⁸,
|
|
9
|
+
* 2⁻¹⁴⁴): 5 below 3072-bit moduli, 4 from there on.
|
|
10
|
+
*/
|
|
11
|
+
export function millerRabinRoundsFor(modulusBits) {
|
|
12
|
+
return modulusBits < 3072 ? 5 : 4;
|
|
13
|
+
}
|
|
14
|
+
const LOWER_BOUNDS_ = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* ⌈√2 · 2^(bits − 1)⌉ over ⌈bits / 8⌉ bytes: the smallest prime FIPS 186-5
|
|
17
|
+
* A.1.1 2(b) admits. Public; cached by size, never to be written.
|
|
18
|
+
*/
|
|
19
|
+
export function primeLowerBound(bits) {
|
|
20
|
+
const cached = LOWER_BOUNDS_.get(bits);
|
|
21
|
+
if (cached !== undefined) {
|
|
22
|
+
return cached;
|
|
23
|
+
}
|
|
24
|
+
// √(2^(2·bits − 1)), rounded up.
|
|
25
|
+
const square = 1n << BigInt(2 * bits - 1);
|
|
26
|
+
let root = 1n << BigInt(bits);
|
|
27
|
+
for (;;) {
|
|
28
|
+
const next = (root + square / root) >> 1n;
|
|
29
|
+
if (next >= root) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
root = next;
|
|
33
|
+
}
|
|
34
|
+
if (root * root < square) {
|
|
35
|
+
root++;
|
|
36
|
+
}
|
|
37
|
+
const bound = bytesOfUnsigned(root, Math.ceil(bits / 8));
|
|
38
|
+
LOWER_BOUNDS_.set(bits, bound);
|
|
39
|
+
return bound;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Steps 4.3 to 4.5 (p) or 5.3 to 5.6 (q, with `other` = p) of FIPS 186-5 A.1.3
|
|
43
|
+
* for the random `bits`-bit string `candidate`, made odd here as step 4.3 says.
|
|
44
|
+
* On acceptance `inverse` holds e⁻¹ mod (candidate − 1), the CRT exponent.
|
|
45
|
+
* Constant time in an accepted candidate.
|
|
46
|
+
*/
|
|
47
|
+
export function checkPrimeCandidate(candidate, publicExponent, bits, rounds, other, inverse) {
|
|
48
|
+
const length = candidate.length;
|
|
49
|
+
candidate[length - 1] = candidate[length - 1] | 1;
|
|
50
|
+
if (constantTimeUnsignedCompare(candidate, primeLowerBound(bits)) < 0) {
|
|
51
|
+
return "tooSmall";
|
|
52
|
+
}
|
|
53
|
+
if (other !== undefined && !farEnough_(candidate, other, bits)) {
|
|
54
|
+
return "tooClose";
|
|
55
|
+
}
|
|
56
|
+
if (passesTrialDivision(candidate) === 0) {
|
|
57
|
+
return "composite";
|
|
58
|
+
}
|
|
59
|
+
const minusOne = candidate.slice();
|
|
60
|
+
try {
|
|
61
|
+
minusOne[length - 1] = minusOne[length - 1] & 0xfe;
|
|
62
|
+
if (constantTimeUnsignedModInverse(inverse, publicExponent, minusOne) === 0) {
|
|
63
|
+
return "notCoprime";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
minusOne.fill(0);
|
|
68
|
+
}
|
|
69
|
+
return passesMillerRabin(candidate, rounds) === 1 ? "accepted" : "composite";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A prime of `bits` bits by FIPS 186-5 A.1.3 step 4 (p) or 5 (q, `other` = p),
|
|
73
|
+
* one candidate a step. Only candidates that reach the coprimality test count
|
|
74
|
+
* towards the bound i, as in the standard: a candidate refused at step 4.4, 5.4
|
|
75
|
+
* or 5.5 is redrawn at once.
|
|
76
|
+
*
|
|
77
|
+
* @throws {RsaError} `keyGenerationFailed` after 5·nlen (p) or 10·nlen (q)
|
|
78
|
+
* counted candidates (steps 4.7 and 5.8).
|
|
79
|
+
*/
|
|
80
|
+
export function* generatePrimeSteps(bits, publicExponent, other) {
|
|
81
|
+
const length = Math.ceil(bits / 8);
|
|
82
|
+
const limit = (other === undefined ? 5 : 10) * 2 * bits;
|
|
83
|
+
const rounds = millerRabinRoundsFor(2 * bits);
|
|
84
|
+
const inverse = new Uint8Array(length);
|
|
85
|
+
try {
|
|
86
|
+
for (let counted = 0; counted < limit;) {
|
|
87
|
+
const candidate = getRandomBytes(length);
|
|
88
|
+
candidate[0] = candidate[0] & (0xff >>> (8 * length - bits));
|
|
89
|
+
const verdict = checkPrimeCandidate(candidate, publicExponent, bits, rounds, other, inverse);
|
|
90
|
+
if (verdict === "accepted") {
|
|
91
|
+
return candidate;
|
|
92
|
+
}
|
|
93
|
+
candidate.fill(0);
|
|
94
|
+
if (verdict === "notCoprime" || verdict === "composite") {
|
|
95
|
+
counted++;
|
|
96
|
+
}
|
|
97
|
+
yield;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
inverse.fill(0);
|
|
102
|
+
}
|
|
103
|
+
throw new RsaError("keyGenerationFailed", "No RSA prime was found within the FIPS 186-5 iteration bound");
|
|
104
|
+
}
|
|
105
|
+
/** |a − b| > 2^(bits − 100), compared in constant time. */
|
|
106
|
+
function farEnough_(a, b, bits) {
|
|
107
|
+
const difference = new Uint8Array(a.length);
|
|
108
|
+
const reverse = new Uint8Array(a.length);
|
|
109
|
+
try {
|
|
110
|
+
const borrow = constantTimeUnsignedSubtract(difference, a, b);
|
|
111
|
+
constantTimeUnsignedSubtract(reverse, b, a);
|
|
112
|
+
// |a − b|: the forward difference, or the reverse one on a borrow.
|
|
113
|
+
for (let index = 0; index < a.length; index++) {
|
|
114
|
+
const forward = difference[index];
|
|
115
|
+
difference[index] = forward ^ (-borrow & (forward ^ reverse[index]));
|
|
116
|
+
}
|
|
117
|
+
return (constantTimeUnsignedCompare(difference, bytesOfUnsigned(1n << BigInt(bits - 100), a.length)) > 0);
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
difference.fill(0);
|
|
121
|
+
reverse.fill(0);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=_generate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_generate.js","sourceRoot":"","sources":["../../src/key/_generate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACN,2BAA2B,EAC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C;AAED,2DAA2D;AAC3D,SAAS,UAAU,CAAC,CAAa,EAAE,CAAa,EAAE,IAAY;IAC7D,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAEzC,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,4BAA4B,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAE9D,4BAA4B,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAE5C,mEAAmE;QACnE,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YAC/C,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAE,CAAC;YAEnC,UAAU,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC;QACvE,CAAC;QAED,OAAO,CACN,2BAA2B,CAC1B,UAAU,EACV,eAAe,CAAC,EAAE,IAAI,MAAM,CAAC,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CACnD,GAAG,CAAC,CACL,CAAC;IACH,CAAC;YAAS,CAAC;QACV,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACjB,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type ConstantTimeModulus } from "@ac-kit/crypto-safe";
|
|
2
|
+
import type { RsaKeyAlgorithm } from "../encoding/rsa-algorithm.js";
|
|
3
|
+
import type { RsaPrivateKey } from "./rsa-private-key.js";
|
|
4
|
+
import type { RsaPublicKey } from "./rsa-public-key.js";
|
|
5
|
+
/** The fields of a public key, kept out of the instance. */
|
|
6
|
+
export type PublicKeyState = {
|
|
7
|
+
/** N, minimal big-endian: `k` bytes. */
|
|
8
|
+
readonly modulus: Uint8Array;
|
|
9
|
+
readonly modulusBitLength: number;
|
|
10
|
+
/** E, minimal big-endian. */
|
|
11
|
+
readonly publicExponent: Uint8Array;
|
|
12
|
+
/** Arithmetic modulo n. */
|
|
13
|
+
readonly ring: ConstantTimeModulus;
|
|
14
|
+
/** What the key may be used for, from its `AlgorithmIdentifier`. */
|
|
15
|
+
readonly algorithm: RsaKeyAlgorithm;
|
|
16
|
+
};
|
|
17
|
+
/** One prime of a private key in CRT form (RFC 8017 §3.2, second form). */
|
|
18
|
+
export type PrimeState = {
|
|
19
|
+
/** R_i, minimal big-endian. */
|
|
20
|
+
readonly prime: Uint8Array;
|
|
21
|
+
/** R_i − 1, as long as `prime`. */
|
|
22
|
+
readonly primeMinusOne: Uint8Array;
|
|
23
|
+
/** D_i = d mod (r_i − 1), as long as `prime`. */
|
|
24
|
+
readonly exponent: Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* The CRT coefficient, as long as `prime`: q⁻¹ mod p for the first prime,
|
|
27
|
+
* empty for the second, t_i = (r_1 ⋯ r_(i−1))⁻¹ mod r_i for the others.
|
|
28
|
+
*/
|
|
29
|
+
readonly coefficient: Uint8Array;
|
|
30
|
+
/** R_1 ⋯ r_(i−1) over `k` bytes for the third prime on, else empty. */
|
|
31
|
+
readonly productBefore: Uint8Array;
|
|
32
|
+
/** Arithmetic modulo r_i. */
|
|
33
|
+
readonly ring: ConstantTimeModulus;
|
|
34
|
+
};
|
|
35
|
+
/** The fields of a private key, kept out of the instance. */
|
|
36
|
+
export type PrivateKeyState = {
|
|
37
|
+
readonly publicKey: RsaPublicKey;
|
|
38
|
+
/** D over `k` bytes. */
|
|
39
|
+
readonly privateExponent: Uint8Array;
|
|
40
|
+
/** The primes, or `undefined` for a key of the first form, (n, d) only. */
|
|
41
|
+
readonly primes: readonly PrimeState[] | undefined;
|
|
42
|
+
/** E·d − 1, a multiple of λ(n), for blinding d in the first form; else empty. */
|
|
43
|
+
readonly multipleOfLambda: Uint8Array;
|
|
44
|
+
disposed: boolean;
|
|
45
|
+
};
|
|
46
|
+
export declare function setPublicState(key: RsaPublicKey, state: PublicKeyState): void;
|
|
47
|
+
export declare function setPrivateState(key: RsaPrivateKey, state: PrivateKeyState): void;
|
|
48
|
+
export declare function publicStateOf(key: RsaPublicKey): PublicKeyState;
|
|
49
|
+
/** The state of `key`, disposed or not. */
|
|
50
|
+
export declare function privateStateOf(key: RsaPrivateKey): PrivateKeyState;
|
|
51
|
+
/** @throws {RsaError} `disposed` once the key has been disposed. */
|
|
52
|
+
export declare function livePrivateStateOf(key: RsaPrivateKey): PrivateKeyState;
|
|
53
|
+
//# sourceMappingURL=_key-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_key-state.d.ts","sourceRoot":"","sources":["../../src/key/_key-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAiB,MAAM,qBAAqB,CAAC;AAE9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,4DAA4D;AAC5D,MAAM,MAAM,cAAc,GAAG;IAC5B,wCAAwC;IACxC,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;IAC7B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,6BAA6B;IAC7B,QAAQ,CAAC,cAAc,EAAE,UAAU,CAAC;IACpC,2BAA2B;IAC3B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,oEAAoE;IACpE,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC;CACpC,CAAC;AAEF,2EAA2E;AAC3E,MAA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
const PUBLIC_KEYS_ = new WeakMap();
|
|
4
|
+
const PRIVATE_KEYS_ = new WeakMap();
|
|
5
|
+
export function setPublicState(key, state) {
|
|
6
|
+
PUBLIC_KEYS_.set(key, state);
|
|
7
|
+
}
|
|
8
|
+
export function setPrivateState(key, state) {
|
|
9
|
+
PRIVATE_KEYS_.set(key, state);
|
|
10
|
+
}
|
|
11
|
+
export function publicStateOf(key) {
|
|
12
|
+
return secretStateOf(PUBLIC_KEYS_, key, "This RSA public key");
|
|
13
|
+
}
|
|
14
|
+
/** The state of `key`, disposed or not. */
|
|
15
|
+
export function privateStateOf(key) {
|
|
16
|
+
return secretStateOf(PRIVATE_KEYS_, key, "This RSA private key");
|
|
17
|
+
}
|
|
18
|
+
/** @throws {RsaError} `disposed` once the key has been disposed. */
|
|
19
|
+
export function livePrivateStateOf(key) {
|
|
20
|
+
const state = privateStateOf(key);
|
|
21
|
+
if (state.disposed) {
|
|
22
|
+
throw new RsaError("disposed", "This RSA private key has been disposed");
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=_key-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_key-state.js","sourceRoot":"","sources":["../../src/key/_key-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAG9E,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAgD3C,MAAM,YAAY,GAAG,IAAI,OAAO,EAAgC,CAAC;AACjE,MAAM,aAAa,GAAG,IAAI,OAAO,EAAkC,CAAC;AAEpE,MAAM,UAAU,cAAc,CAAC,GAAiB,EAAE,KAAqB;IACtE,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,eAAe,CAC9B,GAAkB,EAClB,KAAsB;IAEtB,aAAa,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAiB;IAC9C,OAAO,aAAa,CAAC,YAAY,EAAE,GAAG,EAAE,qBAAqB,CAAC,CAAC;AAChE,CAAC;AAED,2CAA2C;AAC3C,MAAM,UAAU,cAAc,CAAC,GAAkB;IAChD,OAAO,aAAa,CAAC,aAAa,EAAE,GAAG,EAAE,sBAAsB,CAAC,CAAC;AAClE,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,kBAAkB,CAAC,GAAkB;IACpD,MAAM,KAAK,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;IAElC,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;QACpB,MAAM,IAAI,QAAQ,CAAC,UAAU,EAAE,wCAAwC,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1 when no odd prime below 2¹⁴ and below the odd `candidate` divides it, else
|
|
3
|
+
* 0: a small prime is not refused for dividing itself. Each residue is a Horner
|
|
4
|
+
* sum over the candidate's bytes reduced with a floating reciprocal and an
|
|
5
|
+
* arithmetic correction, so no division instruction sees the candidate. Stops
|
|
6
|
+
* at the first divisor found (that candidate is discarded) unless `exhaustive`,
|
|
7
|
+
* which runs every division whatever the candidate, for checks that must not
|
|
8
|
+
* reveal why a value was refused.
|
|
9
|
+
*/
|
|
10
|
+
export declare function passesTrialDivision(candidate: Uint8Array, exhaustive?: boolean): number;
|
|
11
|
+
/**
|
|
12
|
+
* 1 when the odd `candidate` (w > 3) passes `rounds` Miller–Rabin rounds with
|
|
13
|
+
* bases drawn uniformly from [2, w − 2] (FIPS 186-5 B.3.1), else 0. Constant
|
|
14
|
+
* time in w: the decomposition w − 1 = 2^a·m is computed with a constant-time
|
|
15
|
+
* count and shift, z = b^m by the fixed-window exponentiation of
|
|
16
|
+
* `ConstantTimeModulus` over `w.length` bytes, and the chain of squarings masks
|
|
17
|
+
* its steps. `candidate` is minimal (its first byte non-zero); the caller
|
|
18
|
+
* ensures w is odd and above 3, for which a base exists. Stops at the first
|
|
19
|
+
* failing round unless `exhaustive`, which runs every round whatever w.
|
|
20
|
+
*/
|
|
21
|
+
export declare function passesMillerRabin(candidate: Uint8Array, rounds: number, exhaustive?: boolean): number;
|
|
22
|
+
//# sourceMappingURL=_primality.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_primality.d.ts","sourceRoot":"","sources":["../../src/key/_primality.ts"],"names":[],"mappings":"AAoDA;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAClC,SAAS,EAAE,UAAU,EACrB,UAAU,UAAQ,GAChB,MAAM,CA6BR;AAaD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAChC,SAAS,EAAE,UAAU,EACrB,MAAM,EAAE,MAAM,EACd,UAAU,UAAQ,GAChB,MAAM,CAwDR"}
|