27 modp_sqrt_m1 = pow(2, (p-1) // 4, p)
42 x = pow(x2, (p+3) // 8, p)
43 if (x*x - x2) % p != 0:
44 x = x * modp_sqrt_m1 % p
45 if (x*x - x2) % p != 0:
64 x3 = ((x1*y2 + y1*x2) *
modp_inv(1 + d*x1*x2*y1*y2)) % p
65 y3 = ((y1*y2 + x1*x2) *
modp_inv(1 - d*x1*x2*y1*y2)) % p
93 return ((y + x) % p, (y - x) % p, (x * y * 2 * d) % p)
96 limbs = (26, 25, 26, 25, 26, 25, 26, 25, 26, 25)
99 ret.append(x & ((1<<l) - 1))
107 ret.append(x & ((1<<51) - 1))
113 ret =
"{{\n#if defined(BORINGSSL_CURVE25519_64BIT)\n"
117 ret +=
"\n#endif\n}}"
123 small_precomp = bytearray()
124 for i
in range(1, 16):
125 s = (i&1) | ((i&2) << (64-1)) | ((i&4) << (128-2)) | ((i&8) << (192-3))
132 large_precomp.append([])
134 P =
point_mul((j + 1) << (i * 8), (g_x, g_y))
143 buf = StringIO.StringIO()
144 buf.write(
"""/* Copyright (c) 2020, Google Inc.
146 * Permission to use, copy, modify, and/or distribute this software for any
147 * purpose with or without fee is hereby granted, provided that the above
148 * copyright notice and this permission notice appear in all copies.
150 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
151 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
152 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
153 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
154 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
155 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
156 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
158 // This file is generated from
159 // ./make_curve25519_tables.py > curve25519_tables.h
162 static const fe d = """)
166 static const fe sqrtm1 = """)
170 static const fe d2 = """)
174 #if defined(OPENSSL_SMALL)
176 // This block of code replaces the standard base-point table with a much smaller
177 // one. The standard table is 30,720 bytes while this one is just 960.
179 // This table contains 15 pairs of group elements, (x, y), where each field
180 // element is serialised with |fe_tobytes|. If |i| is the index of the group
181 // element then consider i+1 as a four-bit number: (i₀, i₁, i₂, i₃) (where i₀
182 // is the most significant bit). The value of the group element is then:
183 // (i₀×2^192 + i₁×2^128 + i₂×2^64 + i₃)G, where G is the generator.
184 static const uint8_t k25519SmallPrecomp[15 * 2 * 32] = {""")
185 for i, b
in enumerate(small_precomp):
186 buf.write(
"0x%02x, " % b)
192 // k25519Precomp[i][j] = (j+1)*256^i*B
193 static const ge_precomp k25519Precomp[32][8] = {
195 for child
in large_precomp:
205 #endif // OPENSSL_SMALL
208 static const ge_precomp Bi[8] = {
210 for val
in bi_precomp:
218 proc = subprocess.Popen([
"clang-format"], stdin=subprocess.PIPE)
219 proc.communicate(buf.getvalue())
221 if __name__ ==
"__main__":