forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffie-hellman.spec.js
More file actions
111 lines (89 loc) · 3.13 KB
/
Copy pathdiffie-hellman.spec.js
File metadata and controls
111 lines (89 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { describe, expect, test, xtest } from '@jest/globals';
import { DiffieHellman } from './diffie-hellman';
describe('diffie-hellman', () => {
test('throws an error if the constructor arguments are out of range', () => {
expect(() => {
new DiffieHellman(0, 9999);
}).toThrow();
});
xtest('throws an error if the constructor arguments are not prime', () => {
expect(() => {
new DiffieHellman(10, 13);
}).toThrow();
});
describe('private key is greater than 1 and less than p', () => {
const p = 23;
const g = 5;
const diffieHellman = new DiffieHellman(p, g);
xtest('throws an error if private key is negative', () => {
expect(() => {
diffieHellman.getPublicKey(-1);
}).toThrow();
});
xtest('throws an error if private key is zero', () => {
expect(() => {
diffieHellman.getPublicKey(0);
}).toThrow();
});
xtest('throws an error if private key is one', () => {
expect(() => {
diffieHellman.getPublicKey(1);
}).toThrow();
});
xtest('throws an error if private key equals the modulus parameter p', () => {
expect(() => {
diffieHellman.getPublicKey(p);
}).toThrow();
});
xtest('throws an error if private key is greater than the modulus parameter p', () => {
expect(() => {
diffieHellman.getPublicKey(p + 1);
}).toThrow();
});
});
describe('stateless calculation', () => {
const diffieHellman = new DiffieHellman(23, 5);
const alicePrivateKey = 6;
const alicePublicKey = 8;
const bobPrivateKey = 15;
const bobPublicKey = 19;
xtest('can calculate public key using private key', () => {
expect(diffieHellman.getPublicKey(alicePrivateKey)).toEqual(
alicePublicKey,
);
});
xtest('can calculate public key when given a different private key', () => {
expect(diffieHellman.getPublicKey(bobPrivateKey)).toEqual(bobPublicKey);
});
});
xtest("can calculate secret using other party's public key", () => {
expect(new DiffieHellman(23, 5).getSecret(19, 6)).toEqual(2);
});
xtest('key exchange', () => {
const diffieHellman = new DiffieHellman(23, 5);
const alicePrivateKey = 6;
const bobPrivateKey = 15;
const alicePublicKey = diffieHellman.getPublicKey(alicePrivateKey);
const bobPublicKey = diffieHellman.getPublicKey(bobPrivateKey);
const secretA = diffieHellman.getSecret(bobPublicKey, alicePrivateKey);
const secretB = diffieHellman.getSecret(alicePublicKey, bobPrivateKey);
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);
});
});