-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib25519.BUILD
294 lines (251 loc) · 8.17 KB
/
lib25519.BUILD
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
# Copyright 2024 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_cc//cc:defs.bzl", "cc_library")
genrule(
name = "crypto_uint64_h",
srcs = ["inttypes/crypto_uintN.h"],
outs = ["include-build/crypto_uint64.h"],
cmd = "sed -e 's/N/64/g' $(<) > $(@)",
)
genrule(
name = "crypto_verify_c",
srcs = ["crypto_verify/32/ref/verify.c"],
outs = ["crypto_verify.c"],
cmd = "cat $(<) > $(@)",
)
genrule(
name = "crypto_verify_h",
outs = ["include-build/crypto_verify.h"],
cmd = """
cat > $@ << EOF
#ifndef crypto_verify_h
#define crypto_verify_h
#define crypto_verify CRYPTO_NAMESPACE(verify)
#define crypto_verify_32_BYTES 32
#define crypto_verify_BYTES 32
extern int crypto_verify(const unsigned char *,const unsigned char *);
#endif
EOF""",
)
genrule(
name = "ge25519_unpack_c",
srcs = ["crypto_multiscalar/ed25519/amd64-maax-p3/ge25519_unpack.c"],
outs = ["ge25519_unpack.c"],
cmd = "sed -e 's/crypto_verify_32/crypto_verify/g' $(<) > $(@)",
)
genrule(
name = "ge25519_sub_h",
outs = ["include-build/ge25519_sub.h"],
cmd = """
cat > $@ << EOF
#ifndef ge25519_sub_h
#define ge25519_sub_h
#include "ge25519.h"
#define ge25519_sub CRYPTO_NAMESPACE(ge25519_sub)
extern void ge25519_sub(ge25519 *r, const ge25519 *p, const ge25519 *q);
#endif
EOF""",
)
genrule(
name = "ge25519_sub_c",
outs = ["ge25519_sub.c"],
cmd = """
cat > $@ << EOF
#include "ge25519_sub.h"
void ge25519_sub(ge25519 *r, const ge25519 *p, const ge25519 *q)
{
ge25519_p3 qneg;
fe25519_neg(&qneg.x,&q->x);
fe25519_neg(&qneg.t,&q->t);
qneg.y = q->y;
qneg.z = q->z;
ge25519_add(r,p,&qneg);
}
EOF""",
)
genrule(
name = "ge25519_scalarmult_h",
outs = ["include-build/ge25519_scalarmult.h"],
cmd = """
cat > $@ << EOF
#ifndef ge25519_scalarmult_h
#define ge25519_scalarmult_h
#include "ge25519.h"
#define ge25519_scalarmult CRYPTO_NAMESPACE(ge25519_scalarmult)
extern void ge25519_scalarmult(ge25519 *r, const ge25519 *p, const sc25519 *s);
#endif
EOF""",
)
genrule(
name = "ge25519_scalarmult_c",
outs = ["ge25519_scalarmult.c"],
cmd = """
cat > $@ << EOF
#include "ge25519_scalarmult.h"
#include "ge25519_sub.h"
// warning: these constants are not encapsulated
#define P_WINDOWSIZE 5
#define P_MULTIPLES (1<<(P_WINDOWSIZE-2))
static void ge25519_p3_0(ge25519_p3 *r)
{
fe25519_setint(&r->x,0);
fe25519_setint(&r->y,1);
fe25519_setint(&r->z,1);
fe25519_setint(&r->t,0);
}
static void ge25519_multi_scalarmult_precompute(ge25519_p3 *cP, const ge25519_p3 *P, unsigned long long multiples)
{
__attribute__ ((aligned(32))) ge25519_p3 twoP;
ge25519_double(&twoP,P);
cP[0] = *P;
for (long long i = 0;i < multiples-1;++i)
ge25519_add(&cP[i+1],&twoP,&cP[i]);
}
static void ge25519_multi_scalarmult_process(ge25519_p3 *r, const signed char nslide[256], const ge25519_p3 cP[P_MULTIPLES])
{
int maybenonzero = 0;
ge25519_p3_0(r);
for (long long i = 255;i >= 0;--i) {
if (maybenonzero)
ge25519_double(r,r);
signed char c = nslide[i];
if (c != 0) {
maybenonzero = 1;
if (c > 0)
ge25519_add(r,r,&cP[c/2]);
else
ge25519_sub(r,r,&cP[-c/2]);
}
}
}
void ge25519_scalarmult(ge25519 *r, const ge25519 *p, const sc25519 *s) {
signed char nslide[256];
ge25519_p3 cP[P_MULTIPLES]; /* P,3P,5P,7P,9P,11P,13P,15P */
sc25519_slide(nslide,s,P_WINDOWSIZE);
ge25519_multi_scalarmult_precompute(cP,p,P_MULTIPLES);
ge25519_multi_scalarmult_process(r,nslide,cP);
}
EOF""",
)
genrule(
name = "ge25519_is_on_curve_h",
outs = ["include-build/ge25519_is_on_curve.h"],
cmd = """
cat > $@ << EOF
#ifndef ge25519_is_on_curve_h
#define ge25519_is_on_curve_h
#include "ge25519.h"
#define ge25519_is_on_curve CRYPTO_NAMESPACE(ge25519_is_on_curve)
extern int ge25519_is_on_curve(const ge25519 *p);
#endif
EOF""",
)
genrule(
name = "ge25519_is_on_curve_c",
outs = ["ge25519_is_on_curve.c"],
cmd = """
cat > $@ << EOF
#include "ge25519_is_on_curve.h"
/* d */
static const fe25519 ecd = {{0x75EB4DCA135978A3, 0x00700A4D4141D8AB, 0x8CC740797779E898, 0x52036CEE2B6FFE73}};
static const fe25519 zero = {{0,0,0,0}};
int ge25519_is_on_curve(const ge25519_p3 *p)
{
fe25519 x2;
fe25519 y2;
fe25519 z2;
fe25519 z4;
fe25519 t0;
fe25519 t1;
fe25519_square(&x2, &p->x);
fe25519_square(&y2, &p->y);
fe25519_square(&z2, &p->z);
fe25519_sub(&t0, &y2, &x2);
fe25519_mul(&t0, &t0, &z2);
fe25519_mul(&t1, &x2, &y2);
fe25519_mul(&t1, &t1, &ecd);
fe25519_square(&z4, &z2);
fe25519_add(&t1, &t1, &z4);
fe25519_sub(&t0, &t0, &t1);
return fe25519_iseq_vartime(&t0, &zero) != 0;
}
EOF""",
)
cc_library(
name = "25519",
srcs = [
"crypto_mGnP/ed25519/amd64-maax/ge25519_pack.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_add.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_freeze.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_getparity.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_iseq.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_mul.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_neg.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_pack.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_pow2523.c", # referenced by ge25519_unpack
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_setint.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_sub.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/fe25519_unpack.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/ge25519_add.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/ge25519_double.S",
"crypto_multiscalar/ed25519/amd64-maax-p3/sc25519_from32bytes.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/sc25519_slide.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/sc25519_to32bytes.c",
"crypto_multiscalar/ed25519/amd64-maax-p3/shared-consts.c",
"crypto_nG/merged25519/amd64-maax/fe25519_cmov.c",
"crypto_nG/merged25519/amd64-maax/ge25519_base.S",
"crypto_nG/merged25519/amd64-maax/ge25519_scalarmult_base.c",
"crypto_nG/merged25519/amd64-maax/sc25519_window4.c",
"crypto_nG/merged25519/amd64-maax/shared-base-data.c",
"crypto_pow/inv25519/amd64-maax/fe25519_invert.c",
"crypto_pow/inv25519/amd64-maax/fe25519_nsquare.S",
"crypto_pow/inv25519/amd64-maax/fe25519_square.S",
"crypto_verify.c",
"ge25519_is_on_curve.c",
"ge25519_scalarmult.c",
"ge25519_sub.c",
"ge25519_unpack.c",
"include-build/crypto_asm_hidden.h",
"include-build/crypto_uint64.h",
"include-build/crypto_verify.h",
],
hdrs = [
"crypto_multiscalar/ed25519/amd64-maax-p3/ge25519_unpack.h",
"crypto_multiscalar/ed25519/amd64-maax-p3/sc25519.h",
"crypto_nG/merged25519/amd64-maax/fe25519.h",
"crypto_nG/merged25519/amd64-maax/ge25519.h",
"crypto_nG/merged25519/amd64-maax/ge25519_base_niels.data",
"include-build/ge25519_is_on_curve.h",
"include-build/ge25519_scalarmult.h",
"include-build/ge25519_sub.h",
],
copts = ["-fvisibility=hidden"],
defines = [
"CRYPTO_NAMESPACE(name)=crypto_##name",
"_CRYPTO_NAMESPACE(name)=_crypto_##name",
"CRYPTO_SHARED_NAMESPACE(name)=crypto_shared_##name",
"_CRYPTO_SHARED_NAMESPACE(name)=_crypto_shared_##name",
],
includes = [
"crypto_multiscalar/ed25519/amd64-maax-p3",
"crypto_nG/merged25519/amd64-maax",
"include-build",
],
linkstatic = True,
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
visibility = ["//visibility:public"],
)