Skip to content

Commit 177987c

Browse files
committed
Adding getPrivateKey implementation and tests
1 parent 489f9eb commit 177987c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

exercises/practice/diffie-hellman/.meta/proof.ci.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ export class DiffieHellman {
113113
PRIMES.includes(g)
114114
);
115115
}
116+
117+
static getPrivateKey(p) {
118+
return Math.floor(Math.random() * (p - 1) + 2);
119+
}
116120
}

exercises/practice/diffie-hellman/diffie-hellman.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ export class DiffieHellman {
1515
getSecret(theirPublicKey, myPrivateKey) {
1616
throw new Error('Remove this statement and implement this function');
1717
}
18+
19+
getPrivateKey() {
20+
throw new Error('Remove this statement and implement this function');
21+
}
1822
}

exercises/practice/diffie-hellman/diffie-hellman.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,25 @@ describe('diffie-hellman', () => {
8787

8888
expect(secretA).toEqual(secretB);
8989
});
90+
91+
xtest('private key is greater than 1 and less than p', () => {
92+
let p = 23;
93+
for (let i = 0; i < 10; i++) {
94+
let privateKey = DiffieHellman.getPrivateKey(p);
95+
expect(privateKey).toBeGreaterThan(1);
96+
expect(privateKey).toBeLessThan(p);
97+
}
98+
});
99+
100+
xtest('private key is random', () => {
101+
let p = 7919;
102+
let uniqueKeys = new Set();
103+
let testIterations = 1000;
104+
105+
for (let i = 0; i < testIterations; i++) {
106+
uniqueKeys.add(DiffieHellman.getPrivateKey(p));
107+
}
108+
109+
expect(uniqueKeys.size).toBeGreaterThan(testIterations - 100);
110+
});
90111
});

0 commit comments

Comments
 (0)