-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
438 lines (393 loc) · 15.1 KB
/
app.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
from typing import Counter
from flask import Flask, render_template, session, redirect, url_for,flash,request
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from numpy.lib.polynomial import poly
from wtforms import StringField, SubmitField, IntegerField, RadioField
from wtforms.validators import DataRequired
import galois
import numpy as np
import numba
import pickle
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
bootstrap = Bootstrap(app)
#################################Check#########################################
valid_character = ['0','1','2','3','4','5','6','7','8','9','^']
def check_str(GF):
if GF[0] == '^' or GF[-1] == '^':
return False
for character in GF:
if character not in valid_character:
return False
return True
def cal_order(GF):
pos = GF.find("^");
if pos == -1:
#"^" not exists
p = int(GF)
if not galois.is_prime(p):
return -1
else:
return p
else:
p = int(GF[:pos])
if not galois.is_prime(p):
return -1
m = int(GF[pos+1:])
if m == 0:
return -2
return p**m
##########################################################################
###########################Galois#########################################
def primitive_polys_str(order,degree,limit,method):
counter = 0
field = galois.GF(order)
# Only search monic polynomials of degree m over GF(p)
#min_ = order**degree
file_start=open('./var/start.pkl','rb+')
dic_start= pickle.load(file_start)
file_start.close()
min_=dic_start['min_']
max_=dic_start['max_']
#max_ = 2*order**degree
polys_str = []
if method == 'order' and limit == -1:
for element in range(min_, max_):
poly = galois.Poly.Integer(element, field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
return polys_str
if method == 'reverse' and limit == -1:
for element in range(max_-1, min_-1,-1):
poly = galois.Poly.Integer(element, field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
return polys_str
if method == 'random' and limit == -1:
for element in range(min_, max_):
poly = galois.Poly.Integer(element, field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
return polys_str
if method == 'order' and limit != -1:
for element in range(min_, max_):
poly = galois.Poly.Integer(element, field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
counter += 1
if counter == limit:
dic_start['min_'] = element+1
file_start=open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
break
return polys_str
if method == 'reverse' and limit != -1:
for element in range(max_-1, min_-1,-1):
poly = galois.Poly.Integer(element, field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
counter += 1
if counter == limit:
dic_start['max_'] = element
file_start = open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
break
return polys_str
else:
for element in range(max_-1, min_-1,-1):
poly = galois.primitive_poly(order,degree,method='random')
poly_str = poly.string
if poly_str not in polys_str:
polys_str.append(poly_str)
counter += 1
if counter == limit:
break
return polys_str
def num_term_primitive_polys_str(order,degree,num_term,limit,method):
counter = 0
#min_ = order**degree
#max_ = 2*order**degree
file_start=open('./var/start.pkl','rb+')
dic_start= pickle.load(file_start)
file_start.close()
min_=dic_start['min_']
max_=dic_start['max_']
field = galois.GF(order)
polys_str = []
if num_term <1 or num_term>degree+1:
return polys_str
if num_term == 1:
poly = galois.Poly.Integer(min_,field=field)
if galois.is_primitive(poly):
polys_str.append(poly.string)
return polys_str
#for i in range(num_term-1):
# min_ += order**i
#max_ -= order**(degree-num_term+1)
#max_ += 1
if method == 'order' and limit == -1:
for element in range(min_, max_):
poly = galois.Poly.Integer(element, field=field)
poly_str = poly.string
if poly_str.count('+') == num_term-1:
if galois.is_primitive(poly):
polys_str.append(poly_str)
return polys_str
if method == 'reverse' and limit == -1:
for element in range(max_-1, min_-1,-1):
poly = galois.Poly.Integer(element, field=field)
poly_str = poly.string
if poly_str.count('+') == num_term-1:
if galois.is_primitive(poly):
polys_str.append(poly_str)
return polys_str
if method == 'order' and limit != -1:
for element in range(min_,max_):
poly = galois.Poly.Integer(element,field=field)
poly_str = poly.string
if poly_str.count('+') == num_term-1:
if galois.is_primitive(poly):
polys_str.append(poly_str)
counter += 1
if counter == limit:
dic_start['min_'] = element+1
file_start=open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
break;
return polys_str
else:
for element in range(max_-1,min_-1,-1):
poly = galois.Poly.Integer(element,field=field)
poly_str = poly.string
if poly_str.count('+') == num_term-1:
if galois.is_primitive(poly):
polys_str.append(poly_str)
counter += 1
if counter == limit:
dic_start['max_'] = element
file_start = open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
break;
return polys_str
#####################################################################
################################FlaskForm################################
class index_form(FlaskForm):
GF = StringField('GF', validators=[DataRequired()])
degree = IntegerField('Degree', validators=[DataRequired()])
limit = IntegerField('Limit',validators=[DataRequired()])
method = RadioField('Method',choices = ['order','reverse','random'])
submit = SubmitField('Submit')
class find_one_form(FlaskForm):
GF = StringField('GF',validators=[DataRequired()])
degree = IntegerField('Degree',validators=[DataRequired()])
method = RadioField('Method',choices = ['min','max','random'])
submit = SubmitField('Submit')
class num_term_form(FlaskForm):
GF = StringField('GF', validators=[DataRequired()])
degree = IntegerField('Degree',validators=[DataRequired()])
num_term = IntegerField('Number Of Term',validators=[DataRequired()])
limit = IntegerField('Limit',validators=[DataRequired()])
method = RadioField('Method',choices = ['order','reverse'])
submit = SubmitField('Submit')
class conway_form(FlaskForm):
characteristic = IntegerField('Characteristic',validators=[DataRequired()])
degree = IntegerField('Degree',validators=[DataRequired()])
Submit = SubmitField('Submit')
class judge_form(FlaskForm):
GF = StringField('GF',validators=[DataRequired()])
expression = StringField('Expression',validators=[DataRequired()])
submit = SubmitField('Submit')
########################################################################
############################flask route##################################
@app.route('/', methods=['GET', 'POST'])
def index():
form = index_form()
GF = form.GF.data
if form.validate_on_submit():
if check_str(GF) == False:
flash("'GF' is invalid")
return render_template('index.html',form = form)
order = cal_order(GF)
if order == -1:
flash("'characteristic' must be a prime")
return render_template('index.html',form = form)
if order == -2:
flash("'dimension' can't be 0")
return render_template('index.html',form = form)
if form.degree.data < 0:
flash("'degree' can't be a negtive number")
return render_template('index.html',form = form)
else:
#初始化搜索起点
min_ = order**(form.degree.data)
max_ = 2*min_
dic_start = {'min_':min_,'max_':max_}
file_start=open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
session['primitive_polys_str'] = primitive_polys_str(order,form.degree.data,form.limit.data,form.method.data)
dic_index = {'form_GF':form.GF.data,'form_degree':form.degree.data,'form_limit':form.limit.data,'form_method':form.method.data,'order':order}
file_index = open('./var/index.pkl','wb+')
pickle.dump(dic_index,file_index)
file_index.close()
return render_template('result_index.html',form=form,primitive_polys_str = session.get('primitive_polys_str'))
else:
return render_template('index.html',form = form)
@app.route('/result_index',methods=['GET','POST'])
def result_index():
form = index_form()
file_index = open('./var/index.pkl','rb')
dic_index = pickle.load(file_index)
file_index.close()
form.GF.data = dic_index['form_GF']
form.degree.data = dic_index['form_degree']
form.limit.data = dic_index['form_limit']
form.method.data = dic_index['form_method']
order = dic_index['order']
session['primitive_polys_str'] = primitive_polys_str(order,form.degree.data,form.limit.data,form.method.data)
return render_template('/result_index.html',form=form,primitive_polys_str=session.get('primitive_polys_str'))
@app.route('/find_one', methods=['GET', 'POST'])
def find_one():
form = find_one_form()
if form.validate_on_submit():
GF = form.GF.data
if check_str(GF) == False:
flash("'GF' is invalid")
render_template('find_one.html',form = form)
order = cal_order(GF)
if order == -1:
flash("'characteristic' must be a prime")
return render_template('find_one.html',form = form)
if order == -2:
flash("'dimension' can't be 0")
return render_template('find_one.html',form = form)
if form.degree.data < 0:
flash("'degree' can't be a negtive number")
return render_template('find_one.html',form = form)
else:
session['primitive_poly_str'] = [galois.primitive_poly(order,form.degree.data,form.method.data).string]
dic_find_one = {'form_GF':form.GF.data,'form_degree':form.degree.data,'form_method':form.method.data,'order':order}
file_find_one = open('./var/find_one.pkl','wb+')
pickle.dump(dic_find_one,file_find_one)
file_find_one.close()
return render_template('result_find_one.html',form=form,primitive_polys_str = session.get('primitive_poly_str'))
else:
return render_template('find_one.html',form=form)
@app.route('/result_find_one',methods=['GET','POST'])
def result_find_one():
form = find_one_form()
file_find_one = open('./var/find_one.pkl','rb')
dic_find_one = pickle.load(file_find_one)
file_find_one.close()
form.GF.data = dic_find_one['form_GF']
form.degree.data = dic_find_one['form_degree']
form.method.data = dic_find_one['form_method']
order = dic_find_one['order']
session['primitive_poly_str'] = [galois.primitive_poly(order,form.degree.data,form.method.data).string]
return render_template('result_find_one.html',form=form,primitive_polys_str = session.get('primitive_poly_str'))
@app.route('/num_term', methods=['GET', 'POST'])
def num_term():
form = num_term_form()
GF = form.GF.data
if form.validate_on_submit():
if check_str(GF) == False:
flash("'GF' is invalid")
return render_template('num_term.html',form = form)
order = cal_order(GF)
if order == -1:
flash("'characteristic' must be a prime")
return render_template('num_term.html',form = form)
if order == -2:
flash("'dimension' can't be 0")
return render_template('num_term.html',form = form)
if form.degree.data < 0:
flash("'degree' can't be a negtive number")
return render_template('num_term.html',form = form)
if form.num_term.data <= 0:
flash("'num_term' must be a positive number")
return render_template('num_term.html',form = form)
else:
#初始化搜索起点
min_ = order**(form.degree.data)
max_ = 2*min_
for i in range(form.num_term.data-1):
min_ += order**i
max_ -= order**(form.degree.data-form.num_term.data+1)
max_ += 1
dic_start = {'min_':min_,'max_':max_}
file_start=open('./var/start.pkl','wb+')
pickle.dump(dic_start,file_start)
file_start.close()
session['num_term_primitive_polys_str'] = num_term_primitive_polys_str(order,form.degree.data,form.num_term.data,form.limit.data,form.method.data)#Pistache
dic_num_term = {'form_GF':form.GF.data,'form_degree':form.degree.data,'form_num_term':form.num_term.data,'form_limit':form.limit.data,'form_method':form.method.data,'order':order}
file_num_term = open('./var/num_term.pkl','wb+')
pickle.dump(dic_num_term,file_num_term)
file_num_term.close()
return render_template('result_num_term.html',form = form,num_term_primitive_polys_str = session.get('num_term_primitive_polys_str'))
else:
return render_template('num_term.html',form = form)
@app.route('/result_num_term',methods=['GET','POST'])
def result_num_term():
form = num_term_form()
file_num_term = open('./var/num_term.pkl','rb')
dic_num_term = pickle.load(file_num_term)
file_num_term.close()
form.GF.data = dic_num_term['form_GF']
form.degree.data = dic_num_term['form_degree']
form.num_term.data = dic_num_term['form_num_term']
form.limit.data = dic_num_term['form_limit']
form.method.data = dic_num_term['form_method']
order = dic_num_term['order']
session['num_term_primitive_polys_str'] = num_term_primitive_polys_str(order,form.degree.data,form.num_term.data,form.limit.data,form.method.data)
return render_template('result_num_term.html',form=form,num_term_primitive_polys_str=session.get('num_term_primitive_polys_str'))
@app.route('/conway', methods=['GET', 'POST'])
def conway():
form = conway_form()
session['show'] = False
if form.validate_on_submit():
if not galois.is_prime(form.characteristic.data):
flash("'characteristic' must be a prime")
return render_template('conway.html',form = form,show = session.get('show'))
if form.degree.data <=0:
flash("'degree' must be a positive number")
return render_template('conway.html',form = form,show = session.get('show'))
session['show'] = True
session['primitive_poly_str'] = (galois.conway_poly(form.characteristic.data,form.degree.data)).string
return render_template('conway.html',form = form, primitive_poly_str = session.get('primitive_poly_str') ,show = session.get('show'))
@app.route('/judge',methods=['GET','POST'])
def judge():
form = judge_form()
GF = form.GF.data
session['show'] = False
if form.validate_on_submit():
if check_str(GF) == False:
flash("'GF' is invalid")
render_template('judge.html',form = form,show = session.get('show'))
order = cal_order(GF)
if order == -1:
flash("'characteristic' must be a prime")
return render_template('judge.html',form = form,show = session.get('show'))
if order == -2:
flash("'dimension' can't be 0")
return render_template('judge.html',form = form,show = session.get('show'))
session['show'] = True
session['is_primitive'] = galois.is_primitive(galois.Poly.String(form.expression.data,field=galois.GF(order)))#Pistache
return render_template('judge.html',form=form,is_primitive = session.get('is_primitive'),show = session.get('show'))
@app.route('/polynomial',methods=['GET','POST'])
def polynomial():
return render_template('polynomial.html')
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'),404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'),500
###########################################################################################
if __name__ == '__main__':
app.run("0.0.0.0",5050,debug=True)