@@ -4,19 +4,21 @@ import (
4
4
"fmt"
5
5
"math/rand"
6
6
)
7
- func main (){
8
- bit := 30
7
+
8
+ func main () {
9
+ bit := 30
9
10
/*
10
- p and g are pre-agreed constants
11
- that can be communicated over an insecure channel
12
- p should ideally be a large prime number but any integer works
13
- g should be a small integer, 2,3 works fine
11
+ p and g are pre-agreed constants
12
+ that can be communicated over an insecure channel
13
+ p should ideally be a large prime number but any integer works
14
+ g should be a small integer, 2,3 works fine
14
15
15
- PS: Note that the secret keys are never send over the network
16
+ PS: Note that the secret keys are never send over
17
+ the network
16
18
*/
17
19
18
- p := 2 + rand .Intn (1 << bit )
19
- g := 2 + rand .Intn (5 )
20
+ p := 2 + rand .Intn (1 << bit )
21
+ g := 2 + rand .Intn (5 )
20
22
21
23
//Both parties choose a secret key
22
24
@@ -29,14 +31,48 @@ func main(){
29
31
//Both parties send ((g^secret_key)%p)
30
32
//It's not possible to determine the secretkey from the value sent
31
33
32
- AliceSends := modularExponentiation (g ,AliceSecret ,p )
33
- BobSends := modularExponentiation (g ,BobSecret ,p )
34
+ AliceSends := modularExponentiation (g , AliceSecret , p )
35
+ BobSends := modularExponentiation (g , BobSecret , p )
34
36
35
- fmt .Printf ("Alice sends to Bob: %v\n " ,AliceSends )
36
- fmt .Printf ("Bob sends to Alice: %v\n " ,BobSends )
37
+ fmt .Printf ("Alice sends to Bob: %v \n " , AliceSends )
38
+ fmt .Printf ("Bob sends to Alice: %v \n " , BobSends )
37
39
38
40
//Both parties calculate the shared secret key from the value send
41
+ << << << < HEAD
42
+
43
+ == == == =
44
+ //(value_sent^secret_key)%p
45
+ //Both calculations end up with same value despite the different inputs
46
+ << << << < HEAD
47
+ AliceComputes := modularExponentiation (BobSends , AliceSecret , p )
48
+ BobComputes := modularExponentiation (AliceSends , BobSecret , p )
39
49
50
+ fmt .Printf ("Alice Computes the shared secret key as: %v \n " , AliceComputes )
51
+ fmt .Printf ("Bob Computes the shared secret key as: %v \n " , BobComputes )
52
+
53
+ // simply confirms that the values are equal
54
+ if AliceComputes == BobComputes {
55
+ sharedKey := AliceComputes
56
+ fmt .Println ("Tada, shared key is" , sharedKey )
57
+ }
58
+
59
+ }
60
+ func modularExponentiation (b int , e int , mod int ) int {
61
+ //runs in O(log(n)) where n = e
62
+ //uses exponentiation by squaring to speed up the process
63
+ if mod == 1 {
64
+ return 0
65
+ }
66
+ r := 1
67
+ b = b % mod
68
+ for e > 0 {
69
+ if e % 2 == 1 {
70
+ r = (r * b ) % mod
71
+ }
72
+ e = e >> 1
73
+ b = (b * b ) % mod
74
+ == == == =
75
+ >> >> >> > 8422 b48c4e335339d2173d32895e907f84510d05
40
76
AliceComputes := modularExponentiation (BobSends ,AliceSecret ,p )
41
77
BobComputes := modularExponentiation (AliceSends ,BobSecret ,p )
42
78
@@ -64,6 +100,7 @@ func modularExponentiation(b int, e int, mod int)int{
64
100
}
65
101
e = e >> 1
66
102
b = (b * b )% mod
103
+ >> >> >> > 8e01 f3d134dcddea9e8ab08b08e45ae06a476ede
67
104
}
68
105
return r
69
106
}
0 commit comments