-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzzymatch.py
220 lines (193 loc) · 7.11 KB
/
fuzzymatch.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import sqlite3
import subprocess
import random
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def checkDB():
try:
conn = get_db_connection()
isinit = conn.execute('SELECT * from is_init').fetchall()
conn.close()
except:
with open('schema.sql') as f:
conn.executescript(f.read())
conn.close()
isinit = 0
return isinit
def runScript(sql):
scriptRan = False
try:
conn = get_db_connection()
conn.executescript(sql)
conn.close()
scriptRan = True
except:
scriptRan = False
return scriptRan
def getTests():
conn = get_db_connection()
tests = conn.execute('SELECT * FROM tests').fetchall()
conn.close()
return tests
def getAllRuns():
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_runs').fetchall()
conn.close()
return tests
def getRun(id):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_runs where id=' + str(id)).fetchone()
conn.close()
return tests
def getTest(id):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM tests where id=' + str(id)).fetchone()
conn.close()
return tests
def getTestData(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_data where test_id=' + str(idd)).fetchall()
conn.close()
return tests
def getaTestData(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_data where test_id=' + str(idd)).fetchone()
conn.close()
return tests
def addTest(testname,npath,cpath,rpath):
retVal = True
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO tests (testname,nodepath,circompath,rustpath) VALUES (?,?,?,?)',(testname,npath,cpath,rpath))
## crete test data record
tests = conn.execute('INSERT INTO test_data (test_id,input_data,expected_output) VALUES (?,?,?)',(cursor.lastrowid,"",""))
conn.commit()
conn.close()
retVal = True
except Exception as ex:
print(ex)
retVal = False
return retVal
def createRun(id,rundescription):
retVal = 0
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO test_runs (test_id,rundescription) VALUES (?,?)',(id,rundescription))
retVal = cursor.lastrowid
conn.commit()
conn.close()
except Exception as ex:
print(ex)
retVal = 0
return retVal
def insertTestData(id,circomoutput,circomMatch,rustoutput,rustMatch,wereEqual):
retVal = False
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO test_outcomes (run_id,circomoutput,circommatch,rustoutput,rustmatch,wereEqual) VALUES (?,?,?,?,?,?)',(id,circomoutput,circomMatch,rustoutput,rustMatch,wereEqual))
conn.commit()
conn.close()
retVal = True
except Exception as ex:
print(ex)
retVal = False
return retVal
def getOutcomes(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_outcomes where run_id=' + str(idd)).fetchall()
conn.close()
return tests
def updateTest(testname,npath,cpath,rpath,id):
retVal = True
try:
conn = get_db_connection()
tests = conn.execute('UPDATE tests set testname=?,nodepath=?,circompath=?,rustpath=? where id=?',(testname,npath,cpath,rpath,id))
conn.commit()
conn.close()
retVal = True
except:
retVal = False
return retVal
def updateTestData(f1data,f2data,expectOutput,id):
retVal = True
try:
conn = get_db_connection()
tests = conn.execute('UPDATE test_data set field1_data=?,field2_data=?,expected_output=? where tesT_id=?',(f1data,f2data,expectOutput,id))
conn.commit()
conn.close()
retVal = True
except:
retVal = False
return retVal
def generateInputField():
fieldsize = int(round(random.uniform(1,78)))
genfield = ""
for i in range(fieldsize):
genfield += str(round(random.uniform(0,9)))
return genfield
def runFuzzer(test_id,number_of_fuzz_runs):
print("-----------------------------------------------------------------------")
print("Trying to create output to match: 46702443887670435486723478191273607819169644657419964658749776213559127696053")
print("-----------------------------------------------------------------------")
print("")
strTomatch = "46702443887670435486723478191273607819169644657419964658749776213559127696053"
numRuns= int(number_of_fuzz_runs)
testtorun = getTest(test_id)
testname = testtorun['testname']
nodepath = testtorun['nodepath']
circompath = testtorun['circompath']
rustpath = testtorun['rustpath']
runId = 0
try:
for i in range(numRuns):
inputfield1 = generateInputField()
inputfield2 = generateInputField()
print("-----------------------------------------------------------------------")
print("[+] Input 1 : ",inputfield1)
print("[+] Input 2 : ",inputfield2)
nodeCommand = "node " + nodepath + " '" + inputfield1 + "' '" + inputfield2 + "' " + circompath
rustCommand = "cd " + rustpath + " && cargo run -q -- '" + inputfield1 + "' '" + inputfield2 + "'"
didFail = 0
try:
circomResp = subprocess.check_output(nodeCommand,shell=True)
rustResp = subprocess.check_output(rustCommand,shell=True)
castCommand = "cast --to-base " + str(rustResp.decode()[1:67]) + " 10"
castResp = subprocess.check_output(castCommand,shell=True)
except Exception as exx:
rout = "Rust call failed"
didFail = 1
circomMatch = 0
rustMatch = 0
wereEqual = 0
cout = circomResp.decode()[0:len(circomResp.decode())-1]
if didFail==0:
rout = castResp.decode()[0:len(castResp.decode())-1]
print("[+] Circom output : ",cout)
print("[+] Rust output : ",rout)
if cout == strTomatch:
wereEqual = 1
if rout == strTomatch:
rustMatch = 1
print("[+] Did Circom match : ",wereEqual)
print("[+] Did Rust match : ",rustMatch)
print("-----------------------------------------------------------------------")
print("")
if cout == strTomatch:
break
if rout == strTomatch:
break
except Exception as e:
print("Fuzz function exception : ",e)
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("test_id", help="The test id in the Fuzzy database")
parser.add_argument("number_of_fuzz_runs", help="The number of fuzz runs to try")
args = parser.parse_args()
runFuzzer(args.test_id,args.number_of_fuzz_runs)
main()