diff --git a/exercises/practice/diffie-hellman/.meta/config.json b/exercises/practice/diffie-hellman/.meta/config.json index e7ec64117f..cac727712a 100644 --- a/exercises/practice/diffie-hellman/.meta/config.json +++ b/exercises/practice/diffie-hellman/.meta/config.json @@ -4,6 +4,7 @@ ], "contributors": [ "ankorGH", + "jagdish-15", "rchavarria", "serixscorpio", "SleeplessByte", diff --git a/exercises/practice/diffie-hellman/.meta/proof.ci.js b/exercises/practice/diffie-hellman/.meta/proof.ci.js index a526e27604..0efdeda17e 100644 --- a/exercises/practice/diffie-hellman/.meta/proof.ci.js +++ b/exercises/practice/diffie-hellman/.meta/proof.ci.js @@ -113,4 +113,8 @@ export class DiffieHellman { PRIMES.includes(g) ); } + + static getPrivateKey(p) { + return Math.floor(Math.random() * (p - 1) + 2); + } } diff --git a/exercises/practice/diffie-hellman/.meta/tests.toml b/exercises/practice/diffie-hellman/.meta/tests.toml index e17d006ea7..a56c97fae2 100644 --- a/exercises/practice/diffie-hellman/.meta/tests.toml +++ b/exercises/practice/diffie-hellman/.meta/tests.toml @@ -1,9 +1,16 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [1b97bf38-4307-418e-bfd2-446ffc77588d] -description = "private key is in range 1 .. p" +description = "private key is greater than 1 and less than p" [68b2a5f7-7755-44c3-97b2-d28d21f014a9] description = "private key is random" @@ -11,6 +18,9 @@ description = "private key is random" [b4161d8e-53a1-4241-ae8f-48cc86527f22] description = "can calculate public key using private key" +[0d25f8d7-4897-4338-a033-2d3d7a9af688] +description = "can calculate public key when given a different private key" + [cd02ad45-3f52-4510-99cc-5161dad948a8] description = "can calculate secret using other party's public key" diff --git a/exercises/practice/diffie-hellman/diffie-hellman.js b/exercises/practice/diffie-hellman/diffie-hellman.js index d825928b50..c72e5dc1a2 100644 --- a/exercises/practice/diffie-hellman/diffie-hellman.js +++ b/exercises/practice/diffie-hellman/diffie-hellman.js @@ -15,4 +15,8 @@ export class DiffieHellman { getSecret(theirPublicKey, myPrivateKey) { throw new Error('Remove this statement and implement this function'); } + + getPrivateKey() { + throw new Error('Remove this statement and implement this function'); + } } diff --git a/exercises/practice/diffie-hellman/diffie-hellman.spec.js b/exercises/practice/diffie-hellman/diffie-hellman.spec.js index c592080591..213a5df723 100644 --- a/exercises/practice/diffie-hellman/diffie-hellman.spec.js +++ b/exercises/practice/diffie-hellman/diffie-hellman.spec.js @@ -14,7 +14,7 @@ describe('diffie-hellman', () => { }).toThrow(); }); - describe('input validation', () => { + describe('private key is greater than 1 and less than p', () => { const p = 23; const g = 5; const diffieHellman = new DiffieHellman(p, g); @@ -87,4 +87,25 @@ describe('diffie-hellman', () => { expect(secretA).toEqual(secretB); }); + + xtest('private key is greater than 1 and less than p', () => { + let p = 23; + for (let i = 0; i < 10; i++) { + let privateKey = DiffieHellman.getPrivateKey(p); + expect(privateKey).toBeGreaterThan(1); + expect(privateKey).toBeLessThan(p); + } + }); + + xtest('private key is random', () => { + let p = 7919; + let uniqueKeys = new Set(); + let testIterations = 1000; + + for (let i = 0; i < testIterations; i++) { + uniqueKeys.add(DiffieHellman.getPrivateKey(p)); + } + + expect(uniqueKeys.size).toBeGreaterThan(testIterations - 100); + }); });