-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl-efficient.go
175 lines (123 loc) · 2.84 KB
/
dl-efficient.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"strings"
)
var giantMap = make(map[string]string)
var babyMap = make(map[string]string)
//Funciton to throw an error when the input CLI has missing/wrong parameters
func missingParametersError() {
fmt.Println("ERROR: Parameters missing!")
fmt.Println("HELP:")
fmt.Println("./dl-efficient <filename for input>")
}
//Funciton to setup the CLI
func setupCLI() string {
if len(os.Args) < 2 {
missingParametersError()
os.Exit(1)
}
inputFile := os.Args[1]
return inputFile
}
//Function to return the value from the given input file
func getInputText(inputText string) string {
file, err := os.Open(inputText)
if err != nil {
log.Fatal(err)
}
dataBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
text := string(dataBytes)
return text
}
// Function to return the values from the input string
func getParameters(inputFile string) (*big.Int, *big.Int, *big.Int) {
P := new(big.Int)
G := new(big.Int)
H := new(big.Int)
fileText := getInputText(inputFile)
fileText = fileText[2 : len(fileText)-2]
fileTextSplit := strings.Split(fileText, ",")
PText := fileTextSplit[0]
GText := fileTextSplit[1]
HText := fileTextSplit[2]
P.SetString(PText, 10)
G.SetString(GText, 10)
H.SetString(HText, 10)
return P, G, H
}
func getM(P *big.Int) *big.Int {
// one := new(big.Int).SetInt64(1)
M := new(big.Int)
// M.Sub(P, one)
M.Sqrt(P)
return M
}
func setupGiantMapping(G, M, P *big.Int) {
i := new(big.Int)
Gim := new(big.Int)
Gm := new(big.Int)
one := new(big.Int).SetInt64(1)
limit := new(big.Int)
limit.Div(P, M)
limit.Add(limit, one)
Gm.Exp(G, M, P)
for i.SetInt64(0); i.Cmp(limit) != 0; i.Add(i, one) {
Gim.Exp(Gm, i, P)
giantMap[i.String()] = Gim.String()
}
}
func setupBabyMapping(G, M, P, H *big.Int) {
i := new(big.Int)
Gi := new(big.Int)
HGi := new(big.Int)
one := new(big.Int).SetInt64(1)
limit := new(big.Int)
limit.Div(P, M)
limit.Add(limit, one)
for i.SetInt64(0); i.Cmp(limit) != 0; i.Add(i, one) {
Gi.Exp(G, i, P)
HGi.Mul(Gi, H)
HGi.Mod(HGi, P)
babyMap[HGi.String()] = i.String()
}
}
func babyGiant(G, H, M, P *big.Int) {
X := new(big.Int).SetInt64(0)
Gqm := new(big.Int)
Q := new(big.Int)
R := new(big.Int)
limit := new(big.Int)
one := new(big.Int).SetInt64(1)
limit.Div(P, M)
limit.Add(limit, one)
for Q.SetInt64(0); Q.Cmp(limit) != 0; Q.Add(Q, one) {
// fmt.Println(Q)
// fmt.Println(inverseMap[Q.String()])
Gqm.SetString(giantMap[Q.String()], 10)
if r, ok := babyMap[Gqm.String()]; ok {
R.SetString(r, 10)
X.Mul(Q, M)
X.Sub(X, R)
fmt.Println(X)
os.Exit(0)
}
}
}
//Main Function
func main() {
inputFile := setupCLI()
P, G, H := getParameters(inputFile)
M := getM(P)
setupGiantMapping(G, M, P)
setupBabyMapping(G, M, P, H)
// fmt.Println(H)
babyGiant(G, H, M, P)
}