@lib-q/prf 0.0.2

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/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # lib-q-prf
2
+
3
+ Legendre and Gold (power-residue) pseudorandom functions over prime fields \(\mathbb{F}_p\), using **safe-prime** pilot moduli and constant-time arithmetic via [`crypto_bigint`](https://github.com/RustCrypto/crypto-bigint) (`modular::FixedMontyForm` with `MontyParams<U256>` / `MontyParams<U512>`).
4
+
5
+ ## Scope
6
+
7
+ - **Legendre PRF:** \(L_K(x)=\left(\frac{x+K}{p}\right)\) with explicit [`PrfError::ZeroInput`](src/error.rs) when \(x+K\equiv 0 \pmod p\).
8
+ - **Gold PRF:** \(\mathrm{Gold}_k(x)=(k+x)^g \bmod p\) with pilot \(g=(p-1)/2\) for safe primes \(p=2q+1\).
9
+ - **Key derivation:** domain-separated `SHAKE256` expansion into \([1,p)\) (see [`shake.rs`](src/shake.rs)).
10
+
11
+ This crate does **not** implement full anonymous ring signatures; [`lib-q-ring-sig`](../lib-q-ring-sig/) may compose these PRFs behind the `pilot-insecure-prf-transcript` feature for **non-shipping** transcript experiments (see that crate’s module docs).
12
+
13
+ ## Features
14
+
15
+ | Feature | Purpose |
16
+ | ------- | ------- |
17
+ | `alloc` (default) | Enables `lib-q-sha3/alloc` for XOF buffering. |
18
+ | `std` | Pass-through to `alloc`. |
19
+
20
+ `crypto-bigint/alloc` is intentionally **not** enabled: field elements use fixed-width `U256` / `U512` only.
21
+
22
+ ## Tests and KATs
23
+
24
+ - [`tests/reference_vectors.txt`](tests/reference_vectors.txt) — hex KATs; regenerate with [`scripts/gen_prf_kat.py`](../scripts/gen_prf_kat.py) (requires SymPy).
25
+ - SHA-256 fingerprints of modulus encodings catch typos in `params.rs`.
26
+
27
+ ## Documentation
28
+
29
+ See [DESIGN.md](DESIGN.md) for assumptions, extension-field caveats, and safe-prime provenance.
30
+
31
+ ## Subresource integrity (SHA-384)
32
+ Paths in `integrity-manifest.json` are relative to the package root.
@@ -0,0 +1,6 @@
1
+ {
2
+ "integrity": {
3
+ "nodejs/lib_q_prf_bg.wasm": "sha384-3Y7hHhJrBrTDmyZRiudoa5awaMcPzfKiUA/fQMfMelBFiJMfhFKovkTBWRNqyXsf",
4
+ "web/lib_q_prf_bg.wasm": "sha384-3Y7hHhJrBrTDmyZRiudoa5awaMcPzfKiUA/fQMfMelBFiJMfhFKovkTBWRNqyXsf"
5
+ }
6
+ }
@@ -0,0 +1,29 @@
1
+ # lib-q-prf
2
+
3
+ Legendre and Gold (power-residue) pseudorandom functions over prime fields \(\mathbb{F}_p\), using **safe-prime** pilot moduli and constant-time arithmetic via [`crypto_bigint`](https://github.com/RustCrypto/crypto-bigint) (`modular::FixedMontyForm` with `MontyParams<U256>` / `MontyParams<U512>`).
4
+
5
+ ## Scope
6
+
7
+ - **Legendre PRF:** \(L_K(x)=\left(\frac{x+K}{p}\right)\) with explicit [`PrfError::ZeroInput`](src/error.rs) when \(x+K\equiv 0 \pmod p\).
8
+ - **Gold PRF:** \(\mathrm{Gold}_k(x)=(k+x)^g \bmod p\) with pilot \(g=(p-1)/2\) for safe primes \(p=2q+1\).
9
+ - **Key derivation:** domain-separated `SHAKE256` expansion into \([1,p)\) (see [`shake.rs`](src/shake.rs)).
10
+
11
+ This crate does **not** implement full anonymous ring signatures; [`lib-q-ring-sig`](../lib-q-ring-sig/) may compose these PRFs behind the `pilot-insecure-prf-transcript` feature for **non-shipping** transcript experiments (see that crate’s module docs).
12
+
13
+ ## Features
14
+
15
+ | Feature | Purpose |
16
+ | ------- | ------- |
17
+ | `alloc` (default) | Enables `lib-q-sha3/alloc` for XOF buffering. |
18
+ | `std` | Pass-through to `alloc`. |
19
+
20
+ `crypto-bigint/alloc` is intentionally **not** enabled: field elements use fixed-width `U256` / `U512` only.
21
+
22
+ ## Tests and KATs
23
+
24
+ - [`tests/reference_vectors.txt`](tests/reference_vectors.txt) — hex KATs; regenerate with [`scripts/gen_prf_kat.py`](../scripts/gen_prf_kat.py) (requires SymPy).
25
+ - SHA-256 fingerprints of modulus encodings catch typos in `params.rs`.
26
+
27
+ ## Documentation
28
+
29
+ See [DESIGN.md](DESIGN.md) for assumptions, extension-field caveats, and safe-prime provenance.
@@ -0,0 +1,552 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Algorithm identifiers for cryptographic operations
6
+ */
7
+ export enum Algorithm {
8
+ MlKem512 = 0,
9
+ MlKem768 = 1,
10
+ MlKem1024 = 2,
11
+ CbKem348864 = 3,
12
+ CbKem460896 = 4,
13
+ CbKem6688128 = 5,
14
+ CbKem6960119 = 6,
15
+ CbKem8192128 = 7,
16
+ Hqc128 = 8,
17
+ Hqc192 = 9,
18
+ Hqc256 = 10,
19
+ MlDsa44 = 11,
20
+ MlDsa65 = 12,
21
+ MlDsa87 = 13,
22
+ FnDsa = 14,
23
+ FnDsa512 = 15,
24
+ FnDsa1024 = 16,
25
+ SlhDsaSha256128fRobust = 17,
26
+ SlhDsaSha256192fRobust = 18,
27
+ SlhDsaSha256256fRobust = 19,
28
+ SlhDsaShake256128fRobust = 20,
29
+ SlhDsaShake256192fRobust = 21,
30
+ SlhDsaShake256256fRobust = 22,
31
+ Shake128 = 23,
32
+ Shake256 = 24,
33
+ CShake128 = 25,
34
+ CShake256 = 26,
35
+ Sha3_224 = 27,
36
+ Sha3_256 = 28,
37
+ Sha3_384 = 29,
38
+ Sha3_512 = 30,
39
+ Keccak224 = 31,
40
+ Keccak256 = 32,
41
+ Keccak384 = 33,
42
+ Keccak512 = 34,
43
+ Kt128 = 35,
44
+ Kt256 = 36,
45
+ TurboShake128 = 37,
46
+ TurboShake256 = 38,
47
+ Kmac128 = 39,
48
+ Kmac256 = 40,
49
+ TupleHash128 = 41,
50
+ TupleHash256 = 42,
51
+ ParallelHash128 = 43,
52
+ ParallelHash256 = 44,
53
+ Sha224 = 45,
54
+ Sha256 = 46,
55
+ Sha384 = 47,
56
+ Sha512 = 48,
57
+ Sha512_224 = 49,
58
+ Sha512_256 = 50,
59
+ Saturnin = 51,
60
+ Shake256Aead = 52,
61
+ DuplexSpongeAead = 53,
62
+ TweakAead = 54,
63
+ RomulusN = 55,
64
+ RomulusM = 56,
65
+ /**
66
+ * Privacy-protocol identifiers (not standalone KEM/sig/hash providers).
67
+ */
68
+ LatticeRingSignature = 57,
69
+ LatticeBlindIssuance = 58,
70
+ LatticeAnonymousToken = 59,
71
+ LatticeNullifierRegistry = 60,
72
+ /**
73
+ * Witness-derived nullifier mode (SHAKE256 over opening witness wire; see `lib-q-lattice-zkp`).
74
+ */
75
+ LatticeWitnessNullifier = 61,
76
+ /**
77
+ * DualRing-LB (CCS 2021 Alg. 3 aggregated verify on Ajtai openings, `lib-q-ring-sig`).
78
+ */
79
+ LatticeDualRingLb = 62,
80
+ /**
81
+ * ML-KEM-768 layered encapsulation with Saturnin AEAD per hop (mix-layer transport).
82
+ */
83
+ MixOnionRouting = 63,
84
+ /**
85
+ * SHAKE256 session token and stateless retry-cookie derivation for resumption handshakes.
86
+ */
87
+ SessionResumptionBinding = 64,
88
+ }
89
+
90
+ /**
91
+ * Algorithm categories
92
+ */
93
+ export enum AlgorithmCategory {
94
+ Kem = 0,
95
+ Signature = 1,
96
+ Hash = 2,
97
+ Aead = 3,
98
+ /**
99
+ * Anonymous credentials, mix-layer transport helpers, and related ZKP-adjacent protocols.
100
+ */
101
+ PrivacyProtocol = 4,
102
+ }
103
+
104
+ /**
105
+ * WASM-compatible hash result
106
+ */
107
+ export class HashResultWasm {
108
+ free(): void;
109
+ [Symbol.dispose](): void;
110
+ constructor(hash: Uint8Array, algorithm: string);
111
+ readonly algorithm: string;
112
+ readonly hash: Uint8Array;
113
+ }
114
+
115
+ /**
116
+ * KEM keypair with automatic memory zeroization
117
+ */
118
+ export class KemKeypair {
119
+ free(): void;
120
+ [Symbol.dispose](): void;
121
+ /**
122
+ * Create a new KEM keypair from bytes for WASM
123
+ */
124
+ constructor(public_key: Uint8Array, secret_key: Uint8Array);
125
+ /**
126
+ * Get the public key as bytes for WASM
127
+ */
128
+ public_key_bytes(): Uint8Array;
129
+ /**
130
+ * Copy the secret key into a new `Uint8Array` for WASM (avoids returning an owned non-zeroizing `Vec<u8>`).
131
+ */
132
+ secret_key_bytes(): Uint8Array;
133
+ }
134
+
135
+ /**
136
+ * KEM public key
137
+ */
138
+ export class KemPublicKey {
139
+ free(): void;
140
+ [Symbol.dispose](): void;
141
+ /**
142
+ * Get the key data as bytes for WASM
143
+ */
144
+ bytes(): Uint8Array;
145
+ /**
146
+ * Create a new KEM public key from bytes for WASM
147
+ */
148
+ constructor(data: Uint8Array);
149
+ }
150
+
151
+ /**
152
+ * KEM secret key with automatic memory zeroization
153
+ */
154
+ export class KemSecretKey {
155
+ free(): void;
156
+ [Symbol.dispose](): void;
157
+ /**
158
+ * Copy the key material into a new `Uint8Array` for WASM (avoids returning an owned non-zeroizing `Vec<u8>`).
159
+ */
160
+ bytes(): Uint8Array;
161
+ /**
162
+ * Create a new KEM secret key from bytes for WASM
163
+ */
164
+ constructor(data: Uint8Array);
165
+ }
166
+
167
+ /**
168
+ * Secure WASM AEAD Context
169
+ *
170
+ * This context provides secure AEAD operations with:
171
+ * - Consistent error handling using Result<T, JsValue>
172
+ * - Security validation for all inputs
173
+ * - Protection against timing attacks
174
+ * - Memory safety with automatic cleanup
175
+ */
176
+ export class SecureWasmAeadContext {
177
+ free(): void;
178
+ [Symbol.dispose](): void;
179
+ /**
180
+ * Decrypt data (Layer A `Result` ABI only; see [`crate::AeadDecryptSemantic`] for Layer B).
181
+ */
182
+ decrypt(algorithm: string, key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, associated_data?: Uint8Array | null): any;
183
+ /**
184
+ * Encrypt data
185
+ */
186
+ encrypt(algorithm: string, key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, associated_data?: Uint8Array | null): any;
187
+ /**
188
+ * Get supported algorithms
189
+ */
190
+ get_supported_algorithms(): any;
191
+ /**
192
+ * Create a new secure WASM AEAD context
193
+ */
194
+ constructor();
195
+ }
196
+
197
+ /**
198
+ * Secure WASM Hash Context
199
+ *
200
+ * This context provides secure hash operations with:
201
+ * - Consistent error handling using Result<T, JsValue>
202
+ * - Security validation for all inputs
203
+ * - Protection against timing attacks
204
+ * - Memory safety with automatic cleanup
205
+ */
206
+ export class SecureWasmHashContext {
207
+ free(): void;
208
+ [Symbol.dispose](): void;
209
+ /**
210
+ * Get supported algorithms
211
+ */
212
+ get_supported_algorithms(): any;
213
+ /**
214
+ * Hash data
215
+ */
216
+ hash(algorithm: string, data: Uint8Array): any;
217
+ /**
218
+ * Create a new secure WASM hash context
219
+ */
220
+ constructor();
221
+ }
222
+
223
+ /**
224
+ * Secure WASM KEM Context
225
+ *
226
+ * This context provides secure KEM operations with:
227
+ * - Consistent error handling using Result<T, JsValue>
228
+ * - Security validation for all inputs
229
+ * - Protection against timing attacks
230
+ * - Memory safety with automatic cleanup
231
+ */
232
+ export class SecureWasmKemContext {
233
+ free(): void;
234
+ [Symbol.dispose](): void;
235
+ /**
236
+ * Decapsulate a shared secret
237
+ */
238
+ decapsulate(algorithm: string, private_key: Uint8Array, ciphertext: Uint8Array): any;
239
+ /**
240
+ * Encapsulate a shared secret
241
+ */
242
+ encapsulate(algorithm: string, public_key: Uint8Array, randomness?: Uint8Array | null): any;
243
+ /**
244
+ * Generate a KEM keypair
245
+ */
246
+ generate_keypair(algorithm: string, randomness?: Uint8Array | null): any;
247
+ /**
248
+ * Get supported algorithms
249
+ */
250
+ get_supported_algorithms(): any;
251
+ /**
252
+ * Create a new secure WASM KEM context
253
+ */
254
+ constructor();
255
+ }
256
+
257
+ /**
258
+ * Secure WASM Signature Context
259
+ *
260
+ * This context provides secure signature operations with:
261
+ * - Consistent error handling using Result<T, JsValue>
262
+ * - Security validation for all inputs
263
+ * - Protection against timing attacks
264
+ * - Memory safety with automatic cleanup
265
+ */
266
+ export class SecureWasmSignatureContext {
267
+ free(): void;
268
+ [Symbol.dispose](): void;
269
+ /**
270
+ * Generate a signature keypair
271
+ */
272
+ generate_keypair(algorithm: string, randomness?: Uint8Array | null): any;
273
+ /**
274
+ * Get supported algorithms
275
+ */
276
+ get_supported_algorithms(): any;
277
+ /**
278
+ * Create a new secure WASM signature context
279
+ */
280
+ constructor();
281
+ /**
282
+ * Sign a message
283
+ */
284
+ sign(algorithm: string, private_key: Uint8Array, message: Uint8Array, randomness?: Uint8Array | null): any;
285
+ /**
286
+ * Verify a signature
287
+ */
288
+ verify(algorithm: string, public_key: Uint8Array, message: Uint8Array, signature: Uint8Array): any;
289
+ }
290
+
291
+ /**
292
+ * Security levels for cryptographic algorithms
293
+ */
294
+ export enum SecurityLevel {
295
+ Level1 = 1,
296
+ Level3 = 3,
297
+ Level4 = 4,
298
+ Level5 = 5,
299
+ }
300
+
301
+ /**
302
+ * Signature keypair with automatic memory zeroization
303
+ */
304
+ export class SigKeypair {
305
+ private constructor();
306
+ free(): void;
307
+ [Symbol.dispose](): void;
308
+ }
309
+
310
+ /**
311
+ * Signature public key
312
+ */
313
+ export class SigPublicKey {
314
+ private constructor();
315
+ free(): void;
316
+ [Symbol.dispose](): void;
317
+ }
318
+
319
+ /**
320
+ * Signature secret key with automatic memory zeroization
321
+ */
322
+ export class SigSecretKey {
323
+ private constructor();
324
+ free(): void;
325
+ [Symbol.dispose](): void;
326
+ }
327
+
328
+ /**
329
+ * WASM-compatible AEAD context wrapper
330
+ *
331
+ * This wrapper provides JavaScript-compatible bindings for AEAD operations:
332
+ * - Integrates with the new modular architecture
333
+ * - Includes security validation
334
+ * - Provides consistent error handling
335
+ * - Supports all AEAD algorithms
336
+ */
337
+ export class WasmAeadContext {
338
+ private constructor();
339
+ free(): void;
340
+ [Symbol.dispose](): void;
341
+ }
342
+
343
+ /**
344
+ * WASM-compatible CryptoProvider wrapper
345
+ *
346
+ * This wrapper provides JavaScript-compatible bindings for the crypto provider:
347
+ * - Integrates with the new modular architecture
348
+ * - Provides consistent error handling
349
+ * - Supports all cryptographic operations
350
+ */
351
+ export class WasmCryptoProvider {
352
+ private constructor();
353
+ free(): void;
354
+ [Symbol.dispose](): void;
355
+ }
356
+
357
+ /**
358
+ * WASM-compatible Hash context wrapper
359
+ *
360
+ * This wrapper provides JavaScript-compatible bindings for hash operations:
361
+ * - Integrates with the new modular architecture
362
+ * - Includes security validation
363
+ * - Provides consistent error handling
364
+ * - Supports all hash algorithms
365
+ */
366
+ export class WasmHashContext {
367
+ private constructor();
368
+ free(): void;
369
+ [Symbol.dispose](): void;
370
+ }
371
+
372
+ /**
373
+ * WASM-compatible KEM context wrapper
374
+ *
375
+ * This wrapper provides JavaScript-compatible bindings for KEM operations:
376
+ * - Integrates with the new modular architecture
377
+ * - Includes security validation
378
+ * - Provides consistent error handling
379
+ * - Supports all KEM algorithms
380
+ */
381
+ export class WasmKemContext {
382
+ private constructor();
383
+ free(): void;
384
+ [Symbol.dispose](): void;
385
+ }
386
+
387
+ /**
388
+ * Generic WASM key pair implementation
389
+ */
390
+ export class WasmKeyPairImpl {
391
+ free(): void;
392
+ [Symbol.dispose](): void;
393
+ constructor(public_key: Uint8Array, secret_key: Uint8Array);
394
+ readonly public_key: Uint8Array;
395
+ readonly secret_key: Uint8Array;
396
+ }
397
+
398
+ /**
399
+ * WASM-compatible provider factory
400
+ *
401
+ * This factory provides JavaScript-compatible bindings for creating providers:
402
+ * - Integrates with the new modular architecture
403
+ * - Provides consistent error handling
404
+ * - Supports all provider creation operations
405
+ */
406
+ export class WasmProviderFactory {
407
+ private constructor();
408
+ free(): void;
409
+ [Symbol.dispose](): void;
410
+ /**
411
+ * Create a new provider manager
412
+ */
413
+ static create_provider_manager(): WasmProviderManager;
414
+ /**
415
+ * Create a provider manager with specific configuration
416
+ */
417
+ static create_provider_manager_with_config(config: string): WasmProviderManager;
418
+ /**
419
+ * Get available provider types
420
+ */
421
+ static get_available_providers(): string;
422
+ /**
423
+ * Validate provider configuration
424
+ */
425
+ static validate_provider_config(config: string): boolean;
426
+ }
427
+
428
+ /**
429
+ * WASM-compatible provider manager
430
+ *
431
+ * This manager provides JavaScript-compatible bindings for provider operations:
432
+ * - Integrates with the new modular architecture
433
+ * - Includes security validation
434
+ * - Provides consistent error handling
435
+ * - Supports all provider operations
436
+ */
437
+ export class WasmProviderManager {
438
+ private constructor();
439
+ free(): void;
440
+ [Symbol.dispose](): void;
441
+ }
442
+
443
+ /**
444
+ * WASM-compatible Signature context wrapper
445
+ *
446
+ * This wrapper provides JavaScript-compatible bindings for signature operations:
447
+ * - Integrates with the new modular architecture
448
+ * - Includes security validation
449
+ * - Provides consistent error handling
450
+ * - Supports all signature algorithms
451
+ */
452
+ export class WasmSignatureContext {
453
+ private constructor();
454
+ free(): void;
455
+ [Symbol.dispose](): void;
456
+ /**
457
+ * Generate a keypair for the specified algorithm
458
+ */
459
+ generate_keypair(algorithm: string, randomness?: Uint8Array | null): any;
460
+ /**
461
+ * Check if an algorithm is supported
462
+ */
463
+ is_algorithm_supported(algorithm: string): boolean;
464
+ /**
465
+ * Get the security level of the context
466
+ */
467
+ security_level(): number;
468
+ /**
469
+ * Sign a message using the given secret key
470
+ */
471
+ sign(algorithm: string, secret_key_data: Uint8Array, message: Uint8Array, randomness?: Uint8Array | null): Uint8Array;
472
+ /**
473
+ * Get supported algorithms
474
+ */
475
+ supported_algorithms(): string;
476
+ /**
477
+ * Verify a signature using the given public key
478
+ */
479
+ verify(algorithm: string, public_key_data: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean;
480
+ }
481
+
482
+ /**
483
+ * Convert bytes to hexadecimal string
484
+ *
485
+ * This function provides secure hex encoding for WASM:
486
+ * - Uses constant-time operations where possible
487
+ * - Handles large data efficiently
488
+ * - Returns JavaScript string
489
+ */
490
+ export function bytes_to_hex(data: Uint8Array): string;
491
+
492
+ /**
493
+ * Get library information for WASM
494
+ *
495
+ * This function provides library metadata for JavaScript consumption
496
+ */
497
+ export function get_library_info(): string;
498
+
499
+ /**
500
+ * Get supported algorithms by category
501
+ *
502
+ * This function provides algorithm information for JavaScript
503
+ */
504
+ export function get_supported_algorithms(): string;
505
+
506
+ /**
507
+ * Gold PRF on the 256-bit pilot parameters; inputs are **big-endian** 32-byte field elements.
508
+ * Returns hex-encoded 32-byte little-endian output residue.
509
+ */
510
+ export function goldPrfU256BeHex(key_be_hex: string, x_be_hex: string): any;
511
+
512
+ /**
513
+ * Convert hexadecimal string to bytes
514
+ *
515
+ * This function provides secure hex decoding for WASM:
516
+ * - Validates hex string format
517
+ * - Handles errors gracefully
518
+ * - Returns Uint8Array
519
+ */
520
+ export function hex_to_bytes(hex: string): Uint8Array;
521
+
522
+ /**
523
+ * Check if a feature is available
524
+ *
525
+ * This function allows JavaScript to check feature availability
526
+ */
527
+ export function is_feature_available(feature: string): boolean;
528
+
529
+ /**
530
+ * Legendre PRF on the 256-bit pilot modulus; `key` and `x` are **big-endian** 32-byte field elements (64 hex chars each).
531
+ */
532
+ export function legendrePrfU256BeHex(key_be_hex: string, x_be_hex: string): number;
533
+
534
+ /**
535
+ * Generate cryptographically secure random bytes for WASM
536
+ *
537
+ * This function provides secure random generation in WASM environments:
538
+ * - Uses the browser's crypto.getRandomValues() API
539
+ * - Validates input size to prevent DoS attacks
540
+ * - Returns Uint8Array for direct JavaScript consumption
541
+ */
542
+ export function random_bytes(length: number): Uint8Array;
543
+
544
+ /**
545
+ * Validate input data for WASM operations
546
+ *
547
+ * This function provides comprehensive input validation:
548
+ * - Checks for null/undefined values
549
+ * - Validates data size limits
550
+ * - Ensures data is not empty when required
551
+ */
552
+ export function validate_input(data: Uint8Array, min_size?: number | null, max_size?: number | null): boolean;