From c1ab73250c18d79a6afc22c32711a32ccdc4fb92 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Mon, 6 Jan 2025 01:41:48 +0530 Subject: [PATCH 1/4] updating tests diffie-hellman --- .../practice/diffie-hellman/.meta/config.json | 1 + .../practice/diffie-hellman/.meta/tests.toml | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) 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/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" From ac1da54914c60de03c749399173b2e40a327457a Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Mon, 6 Jan 2025 15:27:52 +0530 Subject: [PATCH 2/4] Updating names in test file to match those in test.toml --- exercises/practice/diffie-hellman/diffie-hellman.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/diffie-hellman/diffie-hellman.spec.js b/exercises/practice/diffie-hellman/diffie-hellman.spec.js index c592080591..5b2f19f89f 100644 --- a/exercises/practice/diffie-hellman/diffie-hellman.spec.js +++ b/exercises/practice/diffie-hellman/diffie-hellman.spec.js @@ -13,8 +13,8 @@ describe('diffie-hellman', () => { new DiffieHellman(10, 13); }).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); From d71c63ddccf1d017845381307bbf4e0407400fc0 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Mon, 6 Jan 2025 15:29:48 +0530 Subject: [PATCH 3/4] fixing formatting issue --- exercises/practice/diffie-hellman/diffie-hellman.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/diffie-hellman/diffie-hellman.spec.js b/exercises/practice/diffie-hellman/diffie-hellman.spec.js index 5b2f19f89f..4388ed6a31 100644 --- a/exercises/practice/diffie-hellman/diffie-hellman.spec.js +++ b/exercises/practice/diffie-hellman/diffie-hellman.spec.js @@ -13,7 +13,7 @@ describe('diffie-hellman', () => { new DiffieHellman(10, 13); }).toThrow(); }); - + describe('private key is greater than 1 and less than p', () => { const p = 23; const g = 5; From dd072168a22d488e1e89f983f1ae9bfdaf2bee03 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 4 Feb 2025 21:43:32 +0530 Subject: [PATCH 4/4] Adding getPrivateKey implementation and tests --- .../practice/diffie-hellman/.meta/proof.ci.js | 4 ++++ .../practice/diffie-hellman/diffie-hellman.js | 4 ++++ .../diffie-hellman/diffie-hellman.spec.js | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+) 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/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..7e4a5b8cc6 100644 --- a/exercises/practice/diffie-hellman/diffie-hellman.spec.js +++ b/exercises/practice/diffie-hellman/diffie-hellman.spec.js @@ -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); + }); });