Skip to content

Commit 0d8cb77

Browse files
committed
Fix Ed25519 private key generation to comply with RFC 8032 standard
1 parent a8519b6 commit 0d8cb77

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

index.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,13 @@ <h3>✓ Key Generated Successfully!</h3>
663663
}
664664
}
665665

666-
// Generate a MeshCore-compatible Ed25519 keypair using the CORRECT algorithm
667-
// This matches the Python implementation exactly:
666+
// Generate a MeshCore-compatible Ed25519 keypair using RFC 8032 standard
667+
// This follows the official Ed25519 specification:
668668
// 1. Generate 32-byte random seed
669-
// 2. SHA512 hash the seed
670-
// 3. Manually clamp the first 32 bytes (scalar clamping)
669+
// 2. SHA512 hash the seed to get 64 bytes
670+
// 3. Clamp the first 32 bytes (scalar clamping)
671671
// 4. Use crypto_scalarmult_ed25519_base_noclamp to get public key
672-
// 5. Private key = [clamped_scalar][random_filler]
672+
// 5. Private key = [clamped_scalar][sha512_second_half] (RFC 8032 compliant)
673673
async generateMeshCoreKeypair() {
674674
// Ensure library is loaded
675675
await this.initialize();
@@ -745,12 +745,11 @@ <h3>✓ Key Generated Successfully!</h3>
745745
}
746746
}
747747

748-
// Step 5: Create 64-byte private key: [clamped_scalar][random_filler]
749-
// This matches the Python implementation exactly
750-
const filler = crypto.getRandomValues(new Uint8Array(32));
748+
// Step 5: Create 64-byte private key: [clamped_scalar][sha512_second_half]
749+
// This follows RFC 8032 Ed25519 standard: use second half of SHA-512(seed)
751750
const meshcorePrivateKey = new Uint8Array(64);
752-
meshcorePrivateKey.set(clamped, 0); // First 32 bytes: clamped scalar
753-
meshcorePrivateKey.set(filler, 32); // Second 32 bytes: random filler
751+
meshcorePrivateKey.set(clamped, 0); // First 32 bytes: clamped scalar
752+
meshcorePrivateKey.set(digestArray.slice(32, 64), 32); // Second 32 bytes: SHA-512(seed)[32:64]
754753

755754
return {
756755
publicKey: publicKeyBytes,
@@ -1247,7 +1246,7 @@ <h3>✓ Key Generated Successfully!</h3>
12471246
validationStatus.innerHTML = `
12481247
<div class="key-label">Validation Status:</div>
12491248
<div class="key-value" style="color: #27ae60; font-weight: bold;">
1250-
Full Ed25519 validation passed - Scalar clamping, key consistency, and cryptographic verification confirmed
1249+
RFC 8032 Ed25519 compliant - Proper SHA-512 expansion, scalar clamping, and key consistency verified
12511250
</div>
12521251
`;
12531252

0 commit comments

Comments
 (0)