|
1 |
| -/* |
2 |
| - Algorithm 1, implementing key generation for ML-DSA, uses an RBG to generate the 256-bit random |
3 |
| - value ξ . The seed ξ shall be freshly generated using an approved RBG, as prescribed in NIST SP 800-90A, |
4 |
| - SP 800-90B, and SP 800-90C [19, 20, 21]. Moreover, the RBG used shall have a security strength of at |
5 |
| - least 192 bits for ML-DSA-65 and 256 bits for ML-DSA-87. For ML-DSA-44, the RBG should have a |
6 |
| - security strength of at least 192 bits and shall have a security strength of at least 128 bits. (If an approved |
7 |
| - RBG with at least 128 bits of security but less than 192 bits of security is used, then the claimed security |
8 |
| - strength of ML-DSA-44 is reduced from category 2 to category 1.) |
9 |
| -*/ |
| 1 | +import { hash256, hash512, shake256 } from '../hash/shake256.js'; |
10 | 2 | import {
|
11 | 3 | randomBuffer,
|
12 | 4 | toBase64,
|
13 | 5 | toHex,
|
14 | 6 | } from '#crypto';
|
15 | 7 | import { ml_dsa44 } from '@noble/post-quantum/ml-dsa';
|
16 |
| -const generateKeypair = ml_dsa44; |
17 |
| -export async function signatureKeypair(seed) { |
18 |
| - const keypair = await generateKeypair.keygen(seed); |
| 8 | +import { signatureScheme } from './signatureScheme.js'; |
| 9 | +const seedSize = 64; |
| 10 | +const publicKeySize = 1312; |
| 11 | +const privateKeySize = 2560; |
| 12 | +const signatureSize = 2420; |
| 13 | +const generateKeypair = ml_dsa44.keygen; |
| 14 | +const verifyData = ml_dsa44.verify; |
| 15 | +const signData = ml_dsa44.sign; |
| 16 | +async function createKeypair(seed) { |
| 17 | + const keypair = await generateKeypair(); |
19 | 18 | return {
|
20 | 19 | publicKey: keypair.publicKey,
|
21 | 20 | privateKey: keypair.secretKey
|
22 | 21 | };
|
23 | 22 | }
|
24 |
| -export async function sign(message, privateKey) { |
25 |
| - const signedMessage = await generateKeypair.sign(privateKey?.privateKey || privateKey, message); |
26 |
| - return signedMessage; |
| 23 | +function signMethod(message, privateKey) { |
| 24 | + return signData(privateKey?.privateKey || privateKey, message); |
27 | 25 | }
|
28 |
| -export async function verifySignature(signedMessage, publicKey, message) { |
29 |
| - const isValid = await generateKeypair.verify(publicKey?.publicKey || publicKey, message, signedMessage); |
30 |
| - return isValid; |
| 26 | +function verifyMethod(signature, message, publicKey) { |
| 27 | + return verifyData(publicKey?.publicKey || publicKey, message, signature); |
31 | 28 | }
|
32 |
| -export const dilithium44 = { |
| 29 | +export const dilithium44 = signatureScheme({ |
33 | 30 | name: 'dilithium44',
|
34 |
| - alias: 'dilithium44', |
35 |
| - id: 2, |
36 |
| - signatureKeypair, |
37 |
| - sign, |
38 |
| - verifySignature |
39 |
| -}; |
| 31 | + alias: 'ml_dsa44', |
| 32 | + id: 1, |
| 33 | + security: 1, |
| 34 | + publicKeySize, |
| 35 | + privateKeySize, |
| 36 | + signatureSize, |
| 37 | + seedSize, |
| 38 | + createKeypair, |
| 39 | + verifyMethod, |
| 40 | + signMethod, |
| 41 | + hash256, |
| 42 | + hash512, |
| 43 | + hash: shake256, |
| 44 | + preferred: false |
| 45 | +}); |
40 | 46 | export default dilithium44;
|
41 |
| -// const kp = await signatureKeypair(); |
42 |
| -// console.log(kp.publicKey, kp.publicKey.length); |
| 47 | +// const key = await dilithium44.signatureKeypair(); |
| 48 | +// const msg = Buffer.from('hello world'); |
| 49 | +// console.log(key); |
| 50 | +// console.log(key.publicKey.length, key.privateKey.length); |
| 51 | +// const sig = await dilithium44.sign(msg, key); |
| 52 | +// console.log(sig.length); |
| 53 | +// console.log(await dilithium44.verify(sig, key, msg)); |
0 commit comments