-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftware_tv.py
More file actions
188 lines (132 loc) · 6.97 KB
/
software_tv.py
File metadata and controls
188 lines (132 loc) · 6.97 KB
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
import helpers
import sys
sys.setrecursionlimit(1500)
operation = 0
seed = "random"
print "===================================================================="
print "TEST VECTOR GENERATOR FOR DDP SW SESSIONS"
if len(sys.argv) == 3:
print "--> Seed is ", sys.argv[2]
seed = sys.argv[2]
helpers.setSeed(sys.argv[2])
else:
print "--> No seed specified"
if len(sys.argv) == 2 or len(sys.argv) == 3:
if str(sys.argv[1]) == "add": operation = 1;
if str(sys.argv[1]) == "sub": operation = 2;
if str(sys.argv[1]) == "mod_add": operation = 3;
if str(sys.argv[1]) == "mod_sub": operation = 4;
if str(sys.argv[1]) == "mul": operation = 5;
if str(sys.argv[1]) == "mont_mul": operation = 6;
print "===================================================================="
#####################################################
if operation == 0:
print "You should use this script by passing an argument like:"
print " $ python software_tv.py add"
print " $ python software_tv.py sub"
print " $ python software_tv.py mod_add"
print " $ python software_tv.py mod_sub"
print " $ python software_tv.py mul"
print " $ python software_tv.py mont_mul"
print ""
print "You can also set a seed for randomness to work"
print "with the same software_tv at each execution:"
print " $ python software_tv.py add 1"
print ""
#####################################################
if operation == 1:
print "Test Vector for Multi Precision Addition\n"
a = helpers.getRandomInt(1024)
b = helpers.getRandomInt(1024)
c = a + b
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "a + b = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "===================================================================="
#####################################################
if operation == 2:
print "Test Vector for Multi Precision Subtraction\n"
a = helpers.getRandomInt(1024)
b = helpers.getRandomMessage(1024,a)
c = a - b
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "a - b = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "===================================================================="
#####################################################
if operation == 3:
print "Test Vector for Multi Precision Modular Addition\n"
[p,q,n] = helpers.getModuli(512)
a = helpers.getRandomMessage(1024,n)
b = helpers.getRandomMessage(1024,n)
c = (a + b) % n
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "n = ", hex(n).rstrip("L") # 1024-bits
print "(a + b) mod n = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "uint32_t n[32] = {", helpers.WriteConstants(n), "};" # 1024-bits
print "===================================================================="
#####################################################
if operation == 4:
print "Test Vector for Multi Precision Modular Subtraction\n"
[p,q,n] = helpers.getModuli(512)
a = helpers.getRandomMessage(1024,n)
b = helpers.getRandomMessage(1024,n)
c = (a - b) % n
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "n = ", hex(n).rstrip("L") # 1024-bits
print "(a - b) mod n = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "uint32_t n[32] = {", helpers.WriteConstants(n), "};" # 1024-bits
print "===================================================================="
#####################################################
if operation == 5:
print "Test Vector for Multi Precision Multiplication\n"
a = helpers.getRandomInt(1024)
b = helpers.getRandomInt(1024)
c = a * b
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "a * b = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "===================================================================="
#####################################################
if operation == 6:
print "Test Vector for Multi Precision Montgomery Multiplication\n"
[p,q,n] = helpers.getModuli(512)
r = 2**1024
n_prime = helpers.Modinv(r, n)
a = helpers.getRandomMessage(1024,n)
b = helpers.getRandomMessage(1024,n)
c = (a * b * n_prime) % n
print "a = ", hex(a).rstrip("L") # 1024-bits
print "b = ", hex(b).rstrip("L") # 1024-bits
print "n = ", hex(n).rstrip("L") # 1024-bits
print "n_prime = ", hex(n_prime).rstrip("L") # 1024-bits
print "(a * b * n_prime) % n = ", hex(c).rstrip("L") # 1024-bits
print "===================================================================="
print "Input variable declaration in C language\n"
print "uint32_t a[32] = {", helpers.WriteConstants(a), "};" # 1024-bits
print "uint32_t b[32] = {", helpers.WriteConstants(b), "};" # 1024-bits
print "uint32_t n[32] = {", helpers.WriteConstants(n), "};" # 1024-bits
print "uint32_t n_prime[32] = {", helpers.WriteConstants(n_prime), "};" # 1024-bits
print "===================================================================="