Debian Codegraph @

src/crypto/crypto_ops_builder/ref10CommentedCombined/MakeCryptoOps.py

raw symbols SCIP
1 #assumes you have gnu sed, osx sed might need slight syntax changeo
2 #c.f. https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files
3
4 #written by shen-noether monero research labs
5
6 import os #for copying and sed etc.
7 import glob #for copy files
8 import textwrap #for comments etc
9
10 print("make sure you have cat and grep installed")
11 print("also assumes gnu sed syntax, c.f. :https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files")
12 print("I believe osx may have slightly different version of sed")
13 print("maybe someone smart can replace the sed with perl..")
14
15 a = ""
16
17 license = textwrap.dedent("""\
18 // Copyright (c) 2014-2022, The Monero Project
19 //
20 // All rights reserved.
21 //
22 // Redistribution and use in source and binary forms, with or without modification, are
23 // permitted provided that the following conditions are met:
24 //
25 // 1. Redistributions of source code must retain the above copyright notice, this list of
26 // conditions and the following disclaimer.
27 //
28 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
29 // of conditions and the following disclaimer in the documentation and/or other
30 // materials provided with the distribution.
31 //
32 // 3. Neither the name of the copyright holder nor the names of its contributors may be
33 // used to endorse or promote products derived from this software without specific
34 // prior written permission.
35 //
36 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
37 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
44 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 //
46 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
47 """)
48
49 crypto_ops_includes = textwrap.dedent("""\
50 #include <assert.h>
51 #include <stdint.h>
52
53 #include "warnings.h"
54 #include "crypto-ops.h"
55
56 DISABLE_VS_WARNINGS(4146 4244)
57 """)
58
59 predeclarations = textwrap.dedent("""\
60 /* Predeclarations */
61
62 static void fe_mul(fe, const fe, const fe);
63 static void fe_sq(fe, const fe);
64 static void fe_tobytes(unsigned char *, const fe);
65 static int fe_isnonzero(const fe);
66 static void ge_madd(ge_p1p1 *, const ge_p3 *, const ge_precomp *);
67 static void ge_msub(ge_p1p1 *, const ge_p3 *, const ge_precomp *);
68 static void ge_p2_0(ge_p2 *);
69 static void ge_p3_dbl(ge_p1p1 *, const ge_p3 *);
70 static void ge_sub(ge_p1p1 *, const ge_p3 *, const ge_cached *);
71 static void fe_divpowm1(fe, const fe, const fe);
72 """)
73
74
75
76 fe_comments = textwrap.dedent("""\
77 /*
78 fe means field element.
79 Here the field is \Z/(2^255-19).
80 An element t, entries t[0]...t[9], represents the integer
81 t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
82 Bounds on each t[i] vary depending on context.
83 */
84 """)
85
86 sc_comments = textwrap.dedent("""\
87 /*
88 *
89 * sc code
90 *
91 *
92 The set of scalars is \Z/l
93 where l = 2^252 + 27742317777372353535851937790883648493.
94
95 This is the order of the curve ed25519.
96 The point is that if a is a scalar and P is a point,
97 and b is congruent to a mod l, then aP = bP.
98 Thus, reducing mod l can possibly give you a smaller scalar,
99 so your elliptic curve operations take less time
100 */
101 """)
102
103 ge_comments = textwrap.dedent("""\
104 /*
105 *
106 * ge code
107 *
108 *
109 ge means group element.
110
111 Here the group is the set of pairs (x,y) of field elements (see fe.h)
112 satisfying -x^2 + y^2 = 1 + d x^2y^2
113 where d = -121665/121666.
114
115 Representations:
116 ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
117 ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
118 ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
119 ge_precomp (Duif): (y+x,y-x,2dxy)
120 */
121 """)
122
123 xmr_comments = textwrap.dedent("""\
124 /*
125 *
126 * xmr specific code
127 *
128 *
129 This code is from the original CryptoNote.
130 Some additional functions were needed to compute ring signatures
131 which are not needed for signing.
132 Note that sc_sub and sc_mulsub have been given their own file
133 since these have been rewritten
134
135 */
136 """)
137
138
139
140
141 def qhasmToC(fi, header, out):
142 #replaces mentiones of "header" in "fi" with output in "out"
143 #also removes qhasm comments
144 out1 = out+".tmp"
145 rem_qhasm = " |grep -v 'qhasm' |grep -v ' asm'"
146 com = "sed -e '/#include \""+header+"\"/ {' -e 'r "+header+"' -e 'd' -e '}' "+fi+rem_qhasm+" > "+out1
147 com2 = "awk 'NF' "+out1+" > "+out
148 print(com)
149 os.system(com)
150 print(com2)
151 os.system(com2)
152 os.remove(out1) #temporary
153
154 while (a != "m") and (a != "m") and (a != "c"):
155 a = raw_input("Make / Clean/ Quit m / c / q?")
156
157 if a == "m":
158 print("making crypto-ops.c and crypto-ops.h")
159
160
161 #ref10 header files
162
163 #ref10 c files
164
165 #fe things
166 #still need to do d2, d, sqrtm1
167 print("making fe.c")
168 print(fe_comments)
169 fe = glob.glob("fe*.c")
170 for g in fe:
171 os.system("cp "+g+" "+g.replace("fe", "fe.monero."))
172 qhasmToC("fe_pow22523.c", "pow22523.h", "fe.monero._pow22523.c")
173 qhasmToC("fe_invert.c", "pow225521.h", "fe.monero._invert.c")
174 os.system("rm fe.monero._isnonzero.c") #since it's modified, it's in xmrSpecificOld
175 os.system("cat fe.monero.*.c | grep -v '^#include' > fe.monero.c")
176
177 #sc things
178 print("\nmaking sc.c")
179 print(sc_comments)
180 #so you don't get multiple "loads"
181 os.system("tail -n +24 sc_reduce.c > sc.monero._reduce.c") #also good on linux
182 os.system("tail -n +24 sc_muladd.c > sc.monero._muladd.c")
183 os.system("tail -n +31 sc_sub.xmr.c > sc.monero._sub.xmr.c") #careful with the tails if you change these files!
184 os.system("cat sc.monero.*.c | grep -v '^#include' > sc.monero.c")
185
186 #ge stuff
187 print("making ge.c")
188 ge = glob.glob("ge*.c")
189 for g in ge:
190 os.system("cp "+g+" "+g.replace("ge", "ge.monero."))
191 print(ge_comments)
192 #need to substitute the below lines for their .h files in the appropriate places
193 qhasmToC("ge_add.c", "ge_add.h", "ge.monero._add.c")
194 qhasmToC("ge_madd.c", "ge_madd.h", "ge.monero._madd.c")
195 qhasmToC("ge_sub.c", "ge_sub.h", "ge.monero._sub.c")
196 qhasmToC("ge_msub.c", "ge_msub.h", "ge.monero._msub.c")
197 qhasmToC("ge_p2_dbl.c", "ge_p2_dbl.h", "ge.monero._p2_dbl.c")
198 qhasmToC("ge_frombytes.c", "d.h", "ge.monero._frombytes.c")
199 qhasmToC("ge.monero._frombytes.c", "sqrtm1.h", "ge.monero._frombytes.c")
200 qhasmToC("ge_p3_to_cached.c", "d2.h", "ge.monero._p3_to_cached.c")
201
202
203
204 #also ge_double_scalarmult needs base2.h for ge_precomp Bi
205 #note, base2.h is a large file!
206 #also in ge_scalarmult_base ge_precomp base needs base.h included
207
208 qhasmToC("ge_double_scalarmult.c", "base2.h", "ge.monero._double_scalarmult.c")
209 qhasmToC("ge_scalarmult_base.c", "base.h", "ge.monero._scalarmult_base.c")
210 #qhasmToC("ge.monero._scalarmult_base.c", "base.h", "ge.monero._scalarmult_base.c")
211 os.system("sed -i 's/ cmov/ ge_precomp_cmov/g' ge.monero._scalarmult_base.c")
212 os.system("cat ge.monero.*.c | grep -v '^#include' > ge.monero.c")
213
214
215 print("making crypto-ops.c")
216
217 #sqrtm1 things
218
219 #comments
220 with open("fe.monero.comments", "w") as text_file:
221 text_file.write(fe_comments)
222 with open("ge.monero.comments", "w") as text_file:
223 text_file.write(ge_comments)
224 with open("sc.monero.comments", "w") as text_file:
225 text_file.write(sc_comments)
226 with open("xmr.monero.comments", "w") as text_file:
227 text_file.write(xmr_comments)
228 with open("xmr.monero.predeclarations", "w") as text_file:
229 text_file.write(predeclarations)
230
231
232 #license
233 with open("monero.license", "w") as text_file:
234 text_file.write(license)
235
236 #crypto-ops.c includes
237 with open("crypto-ops.monero.includes", "w") as text_file:
238 text_file.write(crypto_ops_includes)
239
240 #note you may have duplicates of load_3, load_4 and possibly some other functions ...
241 os.system("cat monero.license crypto-ops.monero.includes xmr.monero.predeclarations fe.monero.comments fe.monero.c sc.monero.comments sc.monero.c ge.monero.comments ge.monero.c xmr.monero.comments xmrSpecificOld.c > crypto-ops.c")
242
243 #monero specific header files
244 #print("making crypto-ops-tmp.h")
245 #os.system("cat fe.h ge.h sc.h |grep -v crypto_sign_ed25519 |grep -v fe.h > crypto-ops-tmp.h")
246 #we'll just use the old header crypto-ops.h
247
248 #replace crypto_ints
249 os.system("sed -i 's/crypto_int32/int32_t/g' crypto-ops.c")
250 os.system("sed -i 's/crypto_int64/int64_t/g' crypto-ops.c")
251 os.system("sed -i 's/crypto_uint32/uint32_t/g' crypto-ops.c")
252 os.system("sed -i 's/crypto_uint64/uint64_t/g' crypto-ops.c")
253
254 #cleaning up
255 os.system("rm *monero*")
256
257 #monero specific c files
258 if a == "c":
259 #turn the directory back into ref10
260 os.system("rm *monero*")
261 os.system("rm crypto-ops.c")