File tree Expand file tree Collapse file tree 5 files changed +45
-5
lines changed
exercises/practice/diffie-hellman Expand file tree Collapse file tree 5 files changed +45
-5
lines changed Original file line number Diff line number Diff line change 44 ],
55 "contributors" : [
66 " ankorGH" ,
7+ " jagdish-15" ,
78 " rchavarria" ,
89 " serixscorpio" ,
910 " SleeplessByte" ,
Original file line number Diff line number Diff 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 - 2 ) + 2 ) ;
119+ }
116120}
Original file line number Diff line number Diff line change 1- # This is an auto-generated file. Regular comments will be removed when this
2- # file is regenerated. Regenerating will not touch any manually added keys,
3- # so comments can be added in a "comment" key.
1+ # This is an auto-generated file.
2+ #
3+ # Regenerating this file via `configlet sync` will:
4+ # - Recreate every `description` key/value pair
5+ # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+ # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+ # - Preserve any other key/value pair
8+ #
9+ # As user-added comments (using the # character) will be removed when this file
10+ # is regenerated, comments can be added via a `comment` key.
411
512[1b97bf38-4307-418e-bfd2-446ffc77588d ]
6- description = " private key is in range 1 .. p"
13+ description = " private key is greater than 1 and less than p"
714
815[68b2a5f7-7755-44c3-97b2-d28d21f014a9 ]
916description = " private key is random"
1017
1118[b4161d8e-53a1-4241-ae8f-48cc86527f22 ]
1219description = " can calculate public key using private key"
1320
21+ [0d25f8d7-4897-4338-a033-2d3d7a9af688 ]
22+ description = " can calculate public key when given a different private key"
23+
1424[cd02ad45-3f52-4510-99cc-5161dad948a8 ]
1525description = " can calculate secret using other party's public key"
1626
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ describe('diffie-hellman', () => {
1414 } ) . toThrow ( ) ;
1515 } ) ;
1616
17- describe ( 'input validation ' , ( ) => {
17+ describe ( 'private key is greater than 1 and less than p ' , ( ) => {
1818 const p = 23 ;
1919 const g = 5 ;
2020 const diffieHellman = new DiffieHellman ( p , g ) ;
@@ -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} ) ;
You can’t perform that action at this time.
0 commit comments