@darkbio/crypto 0.17.3 → 0.19.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/dist/rand.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  /**
2
- * Generate cryptographically secure random bytes.
2
+ * Generates cryptographically secure random bytes.
3
3
  *
4
- * @param bytes - Number of random bytes to generate (max 64 MiB)
5
- * @returns A Uint8Array containing the random bytes
4
+ * @param bytes - Number of random bytes to generate, at most 64 MiB
5
+ * @returns A new array filled with random bytes
6
+ * @throws If `bytes` is not a whole number between 0 and 64 MiB
6
7
  */
7
8
  export declare function generate(bytes: number): Promise<Uint8Array>;
8
9
  //# sourceMappingURL=rand.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"rand.d.ts","sourceRoot":"","sources":["../src/rand.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAGjE"}
1
+ {"version":3,"file":"rand.d.ts","sourceRoot":"","sources":["../src/rand.ts"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAGjE"}
package/dist/xdsa.d.ts CHANGED
@@ -1,14 +1,24 @@
1
1
  import { Codec } from './cbor.js';
2
- /** Size of the secret key in bytes (64). */
2
+ /**
3
+ * Size of the secret key seed in bytes, the 32-byte ML-DSA-65 seed followed by
4
+ * the 32-byte Ed25519 seed.
5
+ */
3
6
  export declare const SECRET_KEY_SIZE = 64;
4
- /** Size of the public key in bytes (1984). */
7
+ /**
8
+ * Size of the public key in bytes, the 1952-byte ML-DSA-65 key followed by the
9
+ * 32-byte Ed25519 key.
10
+ */
5
11
  export declare const PUBLIC_KEY_SIZE = 1984;
6
- /** Size of a signature in bytes (3373). */
12
+ /**
13
+ * Size of a signature in bytes, the 3309-byte ML-DSA-65 signature followed by
14
+ * the 64-byte Ed25519 signature.
15
+ */
7
16
  export declare const SIGNATURE_SIZE = 3373;
8
- /** Size of a fingerprint in bytes (32). */
17
+ /** Size of a key fingerprint in bytes. */
9
18
  export declare const FINGERPRINT_SIZE = 32;
10
19
  /**
11
- * Get the size constants (requires WASM initialization).
20
+ * Returns the sizes in bytes as the Rust library defines them, the same values
21
+ * as the constants of this module.
12
22
  */
13
23
  export declare function sizes(): Promise<{
14
24
  secretKey: number;
@@ -16,12 +26,14 @@ export declare function sizes(): Promise<{
16
26
  signature: number;
17
27
  fingerprint: number;
18
28
  }>;
19
- /**
20
- * Fingerprint is a 32-byte unique identifier for an xDSA key.
21
- * Backed by an opaque WASM handle.
22
- */
29
+ /** A 256-bit unique identifier for an xDSA key. */
23
30
  export declare class Fingerprint {
24
- /** Creates a fingerprint from a 32-byte array. */
31
+ private constructor();
32
+ /**
33
+ * Creates a fingerprint from a 32-byte array.
34
+ *
35
+ * @throws If `bytes` is not 32 bytes long
36
+ */
25
37
  static fromBytes(bytes: Uint8Array): Promise<Fingerprint>;
26
38
  /** Converts a fingerprint into a 32-byte array. */
27
39
  toBytes(): Uint8Array;
@@ -30,74 +42,118 @@ export declare class Fingerprint {
30
42
  /** Reports whether another fingerprint is the same. */
31
43
  equals(other: Fingerprint): boolean;
32
44
  }
33
- /**
34
- * Signature is a 3373-byte xDSA signature.
35
- * Backed by an opaque WASM handle.
36
- */
45
+ /** An xDSA signature, an ML-DSA-65 signature paired with an Ed25519 one. */
37
46
  export declare class Signature {
38
- /** Creates a signature from a 3373-byte array. */
47
+ private constructor();
48
+ /**
49
+ * Creates a signature from a 3373-byte array. The halves are only checked
50
+ * when the signature is verified.
51
+ *
52
+ * @throws If `bytes` is not 3373 bytes long
53
+ */
39
54
  static fromBytes(bytes: Uint8Array): Promise<Signature>;
40
55
  /** Converts a signature into a 3373-byte array. */
41
56
  toBytes(): Uint8Array;
42
57
  }
43
58
  /**
44
- * PublicKey contains a composite ML-DSA-65 + Ed25519 public key for
59
+ * An xDSA public key, an ML-DSA-65 key paired with an Ed25519 key, for
45
60
  * verifying quantum resistant digital signatures.
46
- * Backed by an opaque WASM handle — key material stays in WASM memory.
47
61
  */
48
62
  export declare class PublicKey {
49
- /** Creates a public key from a 1984-byte array. */
63
+ private constructor();
64
+ /**
65
+ * Creates a public key from a 1984-byte array.
66
+ *
67
+ * @throws If `bytes` is not 1984 bytes long or not a valid composite key
68
+ */
50
69
  static fromBytes(bytes: Uint8Array): Promise<PublicKey>;
51
- /** Parses a PEM string into a public key. */
70
+ /**
71
+ * Parses a PEM string into a public key. The input must be exactly one
72
+ * `PUBLIC KEY` block, with no leading whitespace, strict base64 and LF or
73
+ * CRLF line endings throughout.
74
+ *
75
+ * @throws If the PEM is malformed or holds another kind of key
76
+ */
52
77
  static fromPem(pem: string): Promise<PublicKey>;
53
78
  /** Converts a public key into a 1984-byte array. */
54
79
  toBytes(): Uint8Array;
55
- /** Serializes a public key into a PEM string. */
80
+ /** Serializes a public key into a `PUBLIC KEY` PEM block with LF line endings. */
56
81
  toPem(): string;
57
82
  /** Reports whether another public key is the same. */
58
83
  equals(other: PublicKey): boolean;
59
84
  /** Returns a 256-bit unique identifier for this key. */
60
85
  fingerprint(): Fingerprint;
61
86
  /**
62
- * Verifies a digital signature of the message.
87
+ * Verifies a digital signature of the message. Both the ML-DSA-65 and the
88
+ * Ed25519 halves must verify.
63
89
  *
64
- * @returns true if the signature is valid, false otherwise (never throws)
90
+ * @param message - The signed message
91
+ * @param signature - The signature to check
92
+ * @returns True if the signature is valid for this key and message, false
93
+ * otherwise
65
94
  */
66
95
  verify(message: Uint8Array, signature: Signature): boolean;
67
96
  }
68
97
  /**
69
- * SecretKey contains a composite ML-DSA-65 + Ed25519 private key for
70
- * creating quantum resistant digital signatures.
71
- * Backed by an opaque WASM handle — key material stays in WASM memory.
98
+ * An xDSA secret key, an ML-DSA-65 key paired with an Ed25519 key, for
99
+ * creating quantum resistant digital signatures. The key stays in WASM memory
100
+ * unless {@link SecretKey.toBytes} or {@link SecretKey.toPem} copies it out.
72
101
  */
73
102
  export declare class SecretKey {
74
- /** Creates a new, random private key. */
103
+ private inner;
104
+ private constructor();
105
+ /** Generates a new, random secret key. */
75
106
  static generate(): Promise<SecretKey>;
76
- /** Creates a private key from a 64-byte seed. */
107
+ /**
108
+ * Creates a secret key from a 64-byte seed.
109
+ *
110
+ * @throws If `bytes` is not 64 bytes long
111
+ */
77
112
  static fromBytes(bytes: Uint8Array): Promise<SecretKey>;
78
- /** Parses a PEM string into a private key. */
113
+ /**
114
+ * Parses a PEM string into a secret key. The input must be exactly one
115
+ * `PRIVATE KEY` block, with no leading whitespace, strict base64 and LF or
116
+ * CRLF line endings throughout.
117
+ *
118
+ * @throws If the PEM is malformed or holds another kind of key
119
+ */
79
120
  static fromPem(pem: string): Promise<SecretKey>;
80
- /** Converts a secret key into a 64-byte array. */
121
+ /** Converts a secret key into its 64-byte seed. */
81
122
  toBytes(): Uint8Array;
82
- /** Serializes a private key into a PEM string. */
123
+ /** Serializes a secret key into a `PRIVATE KEY` PEM block with LF line endings. */
83
124
  toPem(): string;
84
125
  /** Retrieves the public counterpart of the secret key. */
85
126
  publicKey(): PublicKey;
86
- /** Returns a 256-bit unique identifier for this key. */
127
+ /** Returns a 256-bit unique identifier for this key, the same as its public key's. */
87
128
  fingerprint(): Fingerprint;
88
- /** Creates a digital signature of the message. */
129
+ /**
130
+ * Creates a digital signature of the message.
131
+ *
132
+ * @param message - The message to sign
133
+ * @returns The signature
134
+ */
89
135
  sign(message: Uint8Array): Signature;
136
+ /**
137
+ * Wipes the secret key held in WASM memory. Every later use of this key
138
+ * throws, and disposing it again does nothing. Copies already exported, such
139
+ * as by {@link SecretKey.toBytes}, are not affected. Without a dispose, the
140
+ * key is only wiped if the garbage collector finalizes it, which is not
141
+ * guaranteed.
142
+ */
143
+ dispose(): void;
90
144
  }
91
- /** The COSE algorithm identifier of the key type. */
145
+ /** Private COSE algorithm identifier of composite ML-DSA-65 + Ed25519 signatures. */
92
146
  export declare const ALGORITHM_ID = -70000;
93
147
  /**
94
- * Codec of a public key as its bytes. Only the CBOR type is checked here, the
95
- * size and the key material are the Rust key's call.
148
+ * Codec of a public key as its 1984 bytes. Decoding throws a
149
+ * {@link CodecError} unless the bytes are a valid key. Calling its decode
150
+ * directly needs any async function of this package to have run first.
96
151
  */
97
152
  export declare const publicKey: Codec<PublicKey>;
98
153
  /**
99
- * Codec of a fingerprint as its bytes. Only the CBOR type is checked here,
100
- * the size is the Rust fingerprint's call.
154
+ * Codec of a fingerprint as its 32 bytes. Decoding throws a {@link CodecError}
155
+ * unless the value is 32 bytes. Calling its decode directly needs any async
156
+ * function of this package to have run first.
101
157
  */
102
158
  export declare const fingerprint: Codec<Fingerprint>;
103
159
  //# sourceMappingURL=xdsa.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"xdsa.d.ts","sourceRoot":"","sources":["../src/xdsa.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,WAAW,CAAC;AAG1D,4CAA4C;AAC5C,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,8CAA8C;AAC9C,eAAO,MAAM,eAAe,OAAO,CAAC;AAEpC,2CAA2C;AAC3C,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC,2CAA2C;AAC3C,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC;;GAEG;AACH,wBAAsB,KAAK,IAAI,OAAO,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC,CAQD;AAED;;;GAGG;AACH,qBAAa,WAAW;IAStB,kDAAkD;WACrC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAK/D,mDAAmD;IACnD,OAAO,IAAI,UAAU;IAIrB,8CAA8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1,18 @@
1
1
  import { Codec } from './cbor.js';
2
- /** Size of the secret key seed in bytes (32). */
2
+ /** Size of the secret key seed in bytes. */
3
3
  export declare const SECRET_KEY_SIZE = 32;
4
- /** Size of the public key in bytes (1216). */
4
+ /** Size of the public key in bytes. */
5
5
  export declare const PUBLIC_KEY_SIZE = 1216;
6
- /** Size of the encapsulated key in bytes (1120). */
6
+ /**
7
+ * Size of the encapsulated key in bytes, the prefix of every
8
+ * {@link PublicKey.seal} output.
9
+ */
7
10
  export declare const ENCAP_KEY_SIZE = 1120;
8
- /** Size of a fingerprint in bytes (32). */
11
+ /** Size of a key fingerprint in bytes. */
9
12
  export declare const FINGERPRINT_SIZE = 32;
10
13
  /**
11
- * Get the size constants (requires WASM initialization).
14
+ * Returns the sizes in bytes as the Rust library defines them, the same values
15
+ * as the constants of this module.
12
16
  */
13
17
  export declare function sizes(): Promise<{
14
18
  secretKey: number;
@@ -16,12 +20,14 @@ export declare function sizes(): Promise<{
16
20
  encapKey: number;
17
21
  fingerprint: number;
18
22
  }>;
19
- /**
20
- * Fingerprint is a 32-byte unique identifier for an xHPKE key.
21
- * Backed by an opaque WASM handle.
22
- */
23
+ /** A 256-bit unique identifier for an xHPKE key. */
23
24
  export declare class Fingerprint {
24
- /** Creates a fingerprint from a 32-byte array. */
25
+ private constructor();
26
+ /**
27
+ * Creates a fingerprint from a 32-byte array.
28
+ *
29
+ * @throws If `bytes` is not 32 bytes long
30
+ */
25
31
  static fromBytes(bytes: Uint8Array): Promise<Fingerprint>;
26
32
  /** Converts a fingerprint into a 32-byte array. */
27
33
  toBytes(): Uint8Array;
@@ -30,103 +36,203 @@ export declare class Fingerprint {
30
36
  /** Reports whether another fingerprint is the same. */
31
37
  equals(other: Fingerprint): boolean;
32
38
  }
33
- /**
34
- * PublicKey contains an X-Wing public key for hybrid post-quantum encryption.
35
- * Backed by an opaque WASM handle — key material stays in WASM memory.
36
- */
39
+ /** An X-Wing public key for encrypting HPKE messages. */
37
40
  export declare class PublicKey {
38
- /** Creates a public key from a 1216-byte array. */
41
+ private constructor();
42
+ /**
43
+ * Creates a public key from a 1216-byte array.
44
+ *
45
+ * @throws If `bytes` is not 1216 bytes long or not a valid X-Wing key
46
+ */
39
47
  static fromBytes(bytes: Uint8Array): Promise<PublicKey>;
40
- /** Parses a PEM string into a public key. */
48
+ /**
49
+ * Parses a PEM string into a public key. The input must be exactly one
50
+ * `PUBLIC KEY` block, with no leading whitespace, strict base64 and LF or
51
+ * CRLF line endings throughout.
52
+ *
53
+ * @throws If the PEM is malformed or holds another kind of key
54
+ */
41
55
  static fromPem(pem: string): Promise<PublicKey>;
42
56
  /** Converts a public key into a 1216-byte array. */
43
57
  toBytes(): Uint8Array;
44
- /** Serializes a public key into a PEM string. */
58
+ /** Serializes a public key into a `PUBLIC KEY` PEM block with LF line endings. */
45
59
  toPem(): string;
46
60
  /** Reports whether another public key is the same. */
47
61
  equals(other: PublicKey): boolean;
48
- /** Returns a 256-bit unique identifier for this key (SHA256 of raw public key). */
62
+ /**
63
+ * Returns a 256-bit unique identifier for this key, the SHA-256 hash of the
64
+ * public key bytes.
65
+ */
49
66
  fingerprint(): Fingerprint;
50
67
  /**
51
68
  * Creates an HPKE sender context for multi-message encryption to this
52
- * public key. Returns a stateful Sender and the 1120-byte encapsulated
53
- * key that must be transmitted to the recipient.
69
+ * public key. Messages it seals must be opened in order by a
70
+ * {@link Receiver} created from the returned encapsulated key.
71
+ *
72
+ * HPKE runs in base mode, which does not authenticate the sender. The
73
+ * recipient cannot verify who created the context.
74
+ *
75
+ * @param domain - Application domain, fixed for every message of the context
76
+ * @returns The stateful sender and the 1120-byte encapsulated key, which
77
+ * must be transmitted to the recipient
54
78
  */
55
79
  newSender(domain: Uint8Array): {
56
80
  sender: Sender;
57
81
  encapKey: Uint8Array;
58
82
  };
59
83
  /**
60
- * Seal (encrypt) a message to this public key.
84
+ * Encrypts a message to this public key, also authenticating a second
85
+ * message that is not included in the output. Opening it with
86
+ * {@link SecretKey.open} needs the same second message and domain.
87
+ *
88
+ * HPKE runs in base mode, which does not authenticate the sender. The
89
+ * recipient cannot verify who sealed the message.
61
90
  *
62
91
  * @param msgToSeal - The message to encrypt
63
- * @param msgToAuth - Additional data to authenticate (but not encrypt)
64
- * @param domain - Application domain for context separation
65
- * @returns Sealed data (encapsulated key + ciphertext)
92
+ * @param msgToAuth - The message to authenticate but not include
93
+ * @param domain - Application domain, which both sides must agree on
94
+ * @returns The 1120-byte encapsulated key followed by the ciphertext
66
95
  */
67
96
  seal(msgToSeal: Uint8Array, msgToAuth: Uint8Array, domain: Uint8Array): Uint8Array;
68
97
  }
69
98
  /**
70
- * SecretKey contains an X-Wing private key for hybrid post-quantum encryption.
71
- * Backed by an opaque WASM handle — key material stays in WASM memory.
99
+ * An X-Wing secret key for decrypting HPKE messages. The key stays in WASM
100
+ * memory unless {@link SecretKey.toBytes} or {@link SecretKey.toPem} copies it
101
+ * out.
72
102
  */
73
103
  export declare class SecretKey {
74
- /** Creates a new, random private key. */
104
+ private inner;
105
+ private constructor();
106
+ /** Generates a new, random secret key. */
75
107
  static generate(): Promise<SecretKey>;
76
- /** Creates a private key from a 32-byte seed. */
108
+ /**
109
+ * Creates a secret key from a 32-byte seed.
110
+ *
111
+ * @throws If `bytes` is not 32 bytes long
112
+ */
77
113
  static fromBytes(bytes: Uint8Array): Promise<SecretKey>;
78
- /** Parses a PEM string into a private key. */
114
+ /**
115
+ * Parses a PEM string into a secret key. The input must be exactly one
116
+ * `PRIVATE KEY` block, with no leading whitespace, strict base64 and LF or
117
+ * CRLF line endings throughout.
118
+ *
119
+ * @throws If the PEM is malformed or holds another kind of key
120
+ */
79
121
  static fromPem(pem: string): Promise<SecretKey>;
80
- /** Converts a private key into a 32-byte seed. */
122
+ /** Converts a secret key into its 32-byte seed. */
81
123
  toBytes(): Uint8Array;
82
- /** Serializes a private key into a PEM string. */
124
+ /** Serializes a secret key into a `PRIVATE KEY` PEM block with LF line endings. */
83
125
  toPem(): string;
84
126
  /** Retrieves the public counterpart of the secret key. */
85
127
  publicKey(): PublicKey;
86
- /** Returns a 256-bit unique identifier for this key (SHA256 of raw public key). */
128
+ /**
129
+ * Returns a 256-bit unique identifier for this key, the SHA-256 hash of the
130
+ * public key bytes.
131
+ */
87
132
  fingerprint(): Fingerprint;
88
133
  /**
89
- * Creates an HPKE receiver context for multi-message decryption.
134
+ * Creates an HPKE receiver context for multi-message decryption. Messages
135
+ * must be opened in the order the {@link Sender} sealed them.
136
+ *
137
+ * HPKE runs in base mode, which does not authenticate the sender. The
138
+ * recipient cannot verify who created the context.
139
+ *
140
+ * @param encapKey - The 1120-byte encapsulated key from {@link PublicKey.newSender}
141
+ * @param domain - The same application domain the sender uses
142
+ * @returns The stateful receiver
143
+ * @throws If `encapKey` is not 1120 bytes long or is malformed
90
144
  */
91
145
  newReceiver(encapKey: Uint8Array, domain: Uint8Array): Receiver;
92
146
  /**
93
- * Open (decrypt) a sealed message with this secret key.
147
+ * Decrypts a message sealed to this key and checks the second message it
148
+ * authenticates.
149
+ *
150
+ * HPKE runs in base mode, which does not authenticate the sender. The
151
+ * recipient cannot verify who sealed the message.
94
152
  *
95
- * @param sealed - The sealed data from `seal()`
96
- * @param msgToAuth - The same additional authenticated data used during sealing
153
+ * @param sealed - The 1120-byte encapsulated key followed by the
154
+ * ciphertext, as {@link PublicKey.seal} returns them
155
+ * @param msgToAuth - The same second message used during sealing
97
156
  * @param domain - The same application domain used during sealing
98
157
  * @returns The decrypted message
158
+ * @throws If the data is malformed, sealed to another key or tampered with,
159
+ * or `msgToAuth` or `domain` differ
99
160
  */
100
161
  open(sealed: Uint8Array, msgToAuth: Uint8Array, domain: Uint8Array): Uint8Array;
162
+ /**
163
+ * Wipes the secret key held in WASM memory. Every later use of this key
164
+ * throws, and disposing it again does nothing. Copies already exported, such
165
+ * as by {@link SecretKey.toBytes}, and receivers already created from it are
166
+ * not affected. Without a dispose, the key is only wiped if the garbage
167
+ * collector finalizes it, which is not guaranteed.
168
+ */
169
+ dispose(): void;
101
170
  }
102
171
  /**
103
- * Sender is a stateful HPKE encryption context for multi-message
104
- * communication. Created via `PublicKey.newSender()`.
172
+ * A stateful HPKE encryption context for multi-message communication, created
173
+ * by {@link PublicKey.newSender}. Each seal uses the next nonce in the
174
+ * sequence, so identical messages encrypt differently. The matching
175
+ * {@link Receiver} must open messages in the order they were sealed.
105
176
  */
106
177
  export declare class Sender {
107
- private readonly inner;
108
- /** Encrypts a message using the next nonce in the sequence. */
178
+ private inner;
179
+ private constructor();
180
+ private get wasm();
181
+ /**
182
+ * Encrypts a message using the next nonce in the sequence.
183
+ *
184
+ * @param msgToSeal - The message to encrypt
185
+ * @param msgToAuth - The message to authenticate but not include
186
+ * @returns The ciphertext
187
+ */
109
188
  seal(msgToSeal: Uint8Array, msgToAuth: Uint8Array): Uint8Array;
189
+ /**
190
+ * Wipes the keys of this context held in WASM memory. Every later use of the
191
+ * sender throws, and disposing it again does nothing. Without a dispose, the
192
+ * keys are only wiped if the garbage collector finalizes the sender, which is
193
+ * not guaranteed.
194
+ */
195
+ dispose(): void;
110
196
  }
111
197
  /**
112
- * Receiver is a stateful HPKE decryption context for multi-message
113
- * communication. Created via `SecretKey.newReceiver()`.
198
+ * A stateful HPKE decryption context for multi-message communication, created
199
+ * by {@link SecretKey.newReceiver}. Messages must be opened in the order the
200
+ * {@link Sender} sealed them.
114
201
  */
115
202
  export declare class Receiver {
116
- private readonly inner;
117
- /** Decrypts a message using the next nonce in the sequence. */
203
+ private inner;
204
+ private constructor();
205
+ private get wasm();
206
+ /**
207
+ * Decrypts a message using the next nonce in the sequence.
208
+ *
209
+ * @param msgToOpen - The ciphertext from {@link Sender.seal}
210
+ * @param msgToAuth - The same second message used during sealing
211
+ * @returns The decrypted message
212
+ * @throws If the message is out of order or was tampered with, or
213
+ * `msgToAuth` differs
214
+ */
118
215
  open(msgToOpen: Uint8Array, msgToAuth: Uint8Array): Uint8Array;
216
+ /**
217
+ * Wipes the keys of this context held in WASM memory. Every later use of the
218
+ * receiver throws, and disposing it again does nothing. Without a dispose,
219
+ * the keys are only wiped if the garbage collector finalizes the receiver,
220
+ * which is not guaranteed.
221
+ */
222
+ dispose(): void;
119
223
  }
120
- /** The COSE algorithm identifier of the key type. */
224
+ /** Private COSE algorithm identifier of X-Wing (ML-KEM-768 + X25519). */
121
225
  export declare const ALGORITHM_ID = -70001;
122
226
  /**
123
- * Codec of a public key as its bytes. Only the CBOR type is checked here, the
124
- * size and the key material are the Rust key's call.
227
+ * Codec of a public key as its 1216 bytes. Decoding throws a
228
+ * {@link CodecError} unless the bytes are a valid key. Calling its decode
229
+ * directly needs any async function of this package to have run first.
125
230
  */
126
231
  export declare const publicKey: Codec<PublicKey>;
127
232
  /**
128
- * Codec of a fingerprint as its bytes. Only the CBOR type is checked here,
129
- * the size is the Rust fingerprint's call.
233
+ * Codec of a fingerprint as its 32 bytes. Decoding throws a {@link CodecError}
234
+ * unless the value is 32 bytes. Calling its decode directly needs any async
235
+ * function of this package to have run first.
130
236
  */
131
237
  export declare const fingerprint: Codec<Fingerprint>;
132
238
  //# sourceMappingURL=xhpke.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"xhpke.d.ts","sourceRoot":"","sources":["../src/xhpke.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,WAAW,CAAC;AAG1D,iDAAiD;AACjD,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,8CAA8C;AAC9C,eAAO,MAAM,eAAe,OAAO,CAAC;AAEpC,oDAAoD;AACpD,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC,2CAA2C;AAC3C,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC;;GAEG;AACH,wBAAsB,KAAK,IAAI,OAAO,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC,CAQD;AA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.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkbio/crypto",
3
- "version": "0.17.3",
3
+ "version": "0.19.1",
4
4
  "author": "Péter Szilágyi <peter@dark.bio>",
5
5
  "description": "Cryptography primitives and wrappers",
6
6
  "repository": {
@@ -21,11 +21,12 @@
21
21
  "dist"
22
22
  ],
23
23
  "scripts": {
24
- "build:wasm": "wasm-pack build crates/wasm --target web --out-dir ../../src/wasm",
24
+ "build:wasm": "wasm-pack build crates/wasm --target web --out-dir ../../src/wasm -- --locked",
25
25
  "build:ts": "node node_modules/typescript/bin/tsc && vite build",
26
26
  "build": "npm run build:wasm && npm run build:ts",
27
27
  "test": "vitest",
28
28
  "test:browser": "vitest --browser.enabled --browser.name=chromium",
29
+ "test:types": "node node_modules/typescript/bin/tsc -p tests/types",
29
30
  "check": "node node_modules/typescript/bin/tsc --noEmit"
30
31
  },
31
32
  "dependencies": {
@@ -35,6 +36,7 @@
35
36
  "@typescript/typescript6": "^6.0.2",
36
37
  "@vitest/browser-playwright": "^5.0.0",
37
38
  "playwright": "^1.62.1",
39
+ "prettier": "^3.9.9",
38
40
  "typescript": "^7.0.2",
39
41
  "vite": "^8.2.2",
40
42
  "vite-plugin-dts": "^5.1.0",