-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathida-genfield.py
executable file
·51 lines (42 loc) · 1.06 KB
/
ida-genfield.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
#!/usr/bin/env python
#
# This script requires a recent version of python that supports long
# integers (i.e. arbitrary precision integers)
inv = []
# Also see ida.C
field = 65537
def bitrev (v):
t = v & 1;
while v:
t = t << 1
v = v >> 1
t = t | (v & 1)
return t
# Fast modular exponentiation
def expmod (b, e, n):
a = 1L
e = bitrev (e)
while e > 0:
a = (a * a) % n
if (e & 1): a = (a * b) % n
e = e >> 1
return a
# Main program
import sys
fh = file ("ida-field.C", "w")
fh.write ("/* This file is autogenerated by %s. DO NOT EDIT! */\n" % sys.argv[0])
fh.write ("static const u_long invtable[] = { 0,\n")
buf = "\t"
for x in xrange(1,field):
# Fermat's little theorem for inverses
y = expmod (x, field - 2, field)
assert (((x*y) % field) == 1)
buf = buf + ("%d, " % y)
if (len(buf) > 60):
fh.write (buf)
fh.write ("\n\t")
buf = ""
fh.write (buf)
fh.write ("0\n};\n")
fh.write ("const u_long * const Ida::inv = (u_long *) &invtable;\n")
fh.close ()