forked from cryptoquick/libbitcoinpqc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
92 lines (81 loc) · 2.82 KB
/
basic-usage.ts
File metadata and controls
92 lines (81 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
Algorithm,
generateKeyPair,
sign,
verify,
publicKeySize,
secretKeySize,
signatureSize,
} from "../src";
import crypto from "crypto";
/**
* This example demonstrates basic usage of the bitcoinpqc TypeScript bindings
* with SLH-DSA-SHAKE-128S (SPHINCS+).
*
* It shows:
* 1. Getting key and signature sizes for SLH-DSA
* 2. Key generation
* 3. Signing messages
* 4. Verifying signatures
* 5. Error handling
*/
// Print key sizes for SLH-DSA algorithm
console.log("===== SLH-DSA Key and Signature Sizes =====");
const algo = Algorithm.SLH_DSA_SHAKE_128S;
const algoName = Algorithm[algo];
console.log(`${algoName}:`);
console.log(` Public key size: ${publicKeySize(algo)} bytes`);
console.log(` Secret key size: ${secretKeySize(algo)} bytes`);
console.log(` Signature size: ${signatureSize(algo)} bytes`);
console.log();
// Generate a keypair and demonstrate SLH-DSA functionality
function demonstrateSlhDsa(): void {
console.log(`===== Working with ${Algorithm[Algorithm.SLH_DSA_SHAKE_128S]} =====`);
// Generate random data for key generation
console.log("Generating random data...");
const randomData = crypto.randomBytes(128);
try {
// Generate a keypair
console.log("Generating SLH-DSA keypair...");
const keypair = generateKeyPair(Algorithm.SLH_DSA_SHAKE_128S, randomData);
console.log(
`Generated keypair with public key size ${keypair.publicKey.bytes.length} bytes`
);
// Create a message to sign
const message = "Hello from SLH-DSA-SHAKE-128S (SPHINCS+)!";
const messageBytes = Buffer.from(message, "utf-8");
console.log(`Message to sign: "${message}"`);
// Sign the message
console.log("Signing message...");
const signature = sign(keypair.secretKey, messageBytes);
console.log(`Created signature of size ${signature.bytes.length} bytes`);
// Verify the signature
console.log("Verifying signature...");
try {
verify(keypair.publicKey, messageBytes, signature);
console.log("✅ Signature verified successfully!");
} catch (error) {
console.error("❌ Signature verification failed:", error);
}
// Try verifying with a different message (should fail)
const badMessage = Buffer.from(message + " (modified)", "utf-8");
console.log(
`\nAttempting to verify with modified message: "${message} (modified)"`
);
try {
verify(keypair.publicKey, badMessage, signature);
console.log("❌ Verification succeeded with modified message!");
} catch (error) {
console.log("✅ Verification correctly failed with modified message");
console.log(` Error: ${error}`);
}
} catch (error) {
console.error(
`❌ Error while working with ${Algorithm[Algorithm.SLH_DSA_SHAKE_128S]}:`,
error
);
}
console.log("\n");
}
// Run the SLH-DSA demonstration
demonstrateSlhDsa();