| 5 | 5 | |
| 6 | 6 | export class DiffieHellman { |
| 7 | 7 | constructor(p, g) { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Edge Case: Check if p and g are valid positive integers |
| 9 | if (!Number.isInteger(p) || !Number.isInteger(g) || p <= 0 || g <= 0) { |
| 10 | throw new Error('Arguments must be positive integers'); |
| 11 | } |
| 12 | |
| 13 | // Edge Case: Check if p is a prime number (basic check) |
| 14 | if (p < 2 || !this.isPrime(p)) { |
| 15 | throw new Error('First argument must be a prime number'); |
| 16 | } |
| 17 | |
| 18 | this.p = p; |
| 19 | this.g = g; |
| 20 | } |
| 21 | |
| 22 | isPrime(num) { |
| 23 | if (num <= 1) return false; |
| 24 | if (num <= 3) return true; |
| 25 | if (num % 2 === 0 || num % 3 === 0) return false; |
| 26 | |
| 27 | for (let i = 5; i * i <= num; i += 6) { |
| 28 | if (num % i === 0 || num % (i + 2) === 0) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 9 | 33 | } |
| 10 | 34 | |
| 11 | 35 | getPublicKey(privateKey) { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 36 | // Edge Case: Check if privateKey is a valid integer |
| 37 | if (!Number.isInteger(privateKey)) { |
| 38 | throw new Error('Private key must be an integer'); |
| 39 | } |
| 40 | |
| 41 | // Edge Case: Check if privateKey is in valid range (1 < privateKey < p) |
| 42 | if (privateKey <= 1 || privateKey >= this.p) { |
| 43 | throw new Error('Private key must be greater than 1 and less than p'); |
| 44 | } |
| 45 | |
| 46 | // Calculate public key: g^privateKey mod p |
| 47 | return this.modularExponentiation(this.g, privateKey, this.p); |
| 13 | 48 | } |
| 14 | 49 | |
| 15 | 50 | getSecret(theirPublicKey, myPrivateKey) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 51 | // Edge Case: Check if theirPublicKey and myPrivateKey are valid integers |
| 52 | if (!Number.isInteger(theirPublicKey) || !Number.isInteger(myPrivateKey)) { |
| 53 | throw new Error('Arguments must be integers'); |
| 54 | } |
| 55 | |
| 56 | // Edge Case: Check if theirPublicKey is in valid range |
| 57 | if (theirPublicKey <= 0 || theirPublicKey >= this.p) { |
| 58 | throw new Error('Public key must be between 1 and p-1'); |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Check if myPrivateKey is in valid range |
| 62 | if (myPrivateKey <= 1 || myPrivateKey >= this.p) { |
| 63 | throw new Error('Private key must be greater than 1 and less than p'); |
| 64 | } |
| 65 | |
| 66 | // Calculate shared secret: theirPublicKey^myPrivateKey mod p |
| 67 | return this.modularExponentiation(theirPublicKey, myPrivateKey, this.p); |
| 17 | 68 | } |
| 18 | 69 | |
| 19 | | getPrivateKey() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 70 | static getPrivateKey(p) { |
| 71 | // Generate a random private key in the range [2, p-1] |
| 72 | const min = 2; |
| 73 | const max = p - 1; |
| 74 | |
| 75 | // Edge Case: Check if valid range exists |
| 76 | if (min > max) { |
| 77 | throw new Error('No valid private keys available for this prime'); |
| 78 | } |
| 79 | |
| 80 | return Math.floor(Math.random() * (max - min + 1)) + min; |
| 81 | } |
| 82 | |
| 83 | // Helper function for modular exponentiation to handle large numbers |
| 84 | modularExponentiation(base, exponent, modulus) { |
| 85 | if (modulus === 1) return 0; |
| 86 | |
| 87 | let result = 1; |
| 88 | base = base % modulus; |
| 89 | |
| 90 | while (exponent > 0) { |
| 91 | if (exponent % 2 === 1) { |
| 92 | result = (result * base) % modulus; |
| 93 | } |
| 94 | exponent = Math.floor(exponent / 2); |
| 95 | base = (base * base) % modulus; |
| 96 | } |
| 97 | |
| 98 | return result; |
| 21 | 99 | } |
| 22 | 100 | } |
| 101 | |
| 102 | // Handled Edge Cases: invalid arguments, non-prime p, privateKey out of range, invalid public key range, no valid private keys available, large number overflow |