Reduce build latency and fix old cpu bugs

This commit is contained in:
Justine Tunney 2021-08-05 14:43:53 -07:00
parent df8ab0aa0c
commit 533f3d1ef1
69 changed files with 43069 additions and 43683 deletions

View file

@ -45,7 +45,7 @@ void mbedtls_mpi_mul_hlp1(size_t n, const uint64_t *s, uint64_t *d, uint64_t b)
uint128_t x;
uint64_t c, t, t1, t2;
i = c = 0;
#ifdef __x86_64__
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
if( X86_HAVE(BMI2) )
{
for( ; i + 8 <= n; i += 8 )
@ -120,7 +120,7 @@ void mbedtls_mpi_mul_hlp(size_t n, uint64_t *s, uint64_t *d, uint64_t b)
uint128_t x;
uint64_t c, l, h, t;
i = c = 0;
#ifdef __x86_64__
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
if (X86_HAVE(BMI2) && X86_HAVE(ADX))
{
for( ; i + 8 <= n; i += 8 )

View file

@ -1284,7 +1284,7 @@ forceinline mbedtls_mpi_uint mpi_sub_hlp(mbedtls_mpi_uint *d,
unsigned char cf;
mbedtls_mpi_uint c, x;
cf = c = i = 0;
#ifdef __x86_64__
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
if (!n) return 0;
asm volatile("xor\t%1,%1\n\t"
".align\t16\n1:\t"
@ -1553,7 +1553,7 @@ static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
mbedtls_mpi_uint d,
mbedtls_mpi_uint *r )
{
#ifdef __x86_64__
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
if (d && u1 < d)
{
mbedtls_mpi_uint quo, rem;

View file

@ -94,6 +94,10 @@ int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t k)
return 0;
}
void ShiftRightPure(mbedtls_mpi_uint *p, size_t n, unsigned char k) {
shrd(p, n, 0, n, k);
}
/**
* Performs right arithmetic shift on big number: X >>= k
*/

View file

@ -2,6 +2,11 @@
#define MBEDTLS_CONFIG_H_
#include "libc/dce.h"
/* #include "libc/nexgen32e/x86feature.h" */
/* #undef X86_HAVE */
/* #define X86_HAVE(x) 0 */
/* #undef __x86_64__ */
/* protocols */
#define MBEDTLS_SSL_PROTO_TLS1_2
#ifndef TINY

View file

@ -185,11 +185,11 @@ mbedtls_p256_plu( uint64_t A[5],
: "rax", "memory", "cc");
#else
uint64_t c;
ADC( X[0], A[0], B[0], 0, c );
ADC( X[1], A[1], B[1], c, c );
ADC( X[2], A[2], B[2], c, c );
ADC( X[3], A[3], B[3], c, c );
ADC( X[4], A[4], B[4], c, c );
ADC( A[0], A[0], B[0], 0, c );
ADC( A[1], A[1], B[1], c, c );
ADC( A[2], A[2], B[2], c, c );
ADC( A[3], A[3], B[3], c, c );
ADC( A[4], A[4], B[4], c, c );
#endif
}
@ -213,11 +213,11 @@ mbedtls_p256_slu( uint64_t A[5],
: "rax", "memory", "cc");
#else
uint64_t c;
SBB( X[0], A[0], B[0], 0, c );
SBB( X[1], A[1], B[1], c, c );
SBB( X[2], A[2], B[2], c, c );
SBB( X[3], A[3], B[3], c, c );
SBB( X[4], A[4], B[4], c, c );
SBB( A[0], A[0], B[0], 0, c );
SBB( A[1], A[1], B[1], c, c );
SBB( A[2], A[2], B[2], c, c );
SBB( A[3], A[3], B[3], c, c );
SBB( A[4], A[4], B[4], c, c );
#endif
}

View file

@ -35,8 +35,7 @@ mbedtls_p384_isz( uint64_t p[6] )
return( !p[0] & !p[1] & !p[2] & !p[3] & !p[4] & !p[5] );
}
static bool
mbedtls_p384_gte( uint64_t p[7] )
bool mbedtls_p384_gte( uint64_t p[7] )
{
return( (((int64_t)p[6] > 0) |
(!p[6] &
@ -129,15 +128,13 @@ mbedtls_p384_gro( uint64_t p[7] )
#endif
}
static inline void
mbedtls_p384_rum( uint64_t p[7] )
void mbedtls_p384_rum( uint64_t p[7] )
{
while( mbedtls_p384_gte( p ) )
mbedtls_p384_red( p );
}
static inline void
mbedtls_p384_mod( uint64_t X[12] )
void mbedtls_p384_mod( uint64_t X[12] )
{
secp384r1(X);
if( (int64_t)X[6] < 0 ){
@ -217,13 +214,13 @@ mbedtls_p384_plu( uint64_t A[7],
: "rax", "memory", "cc");
#else
uint64_t c;
ADC( X[0], A[0], B[0], 0, c );
ADC( X[1], A[1], B[1], c, c );
ADC( X[2], A[2], B[2], c, c );
ADC( X[3], A[3], B[3], c, c );
ADC( X[4], A[4], B[4], c, c );
ADC( X[5], A[5], B[5], c, c );
ADC( X[6], A[6], B[6], c, c );
ADC( A[0], A[0], B[0], 0, c );
ADC( A[1], A[1], B[1], c, c );
ADC( A[2], A[2], B[2], c, c );
ADC( A[3], A[3], B[3], c, c );
ADC( A[4], A[4], B[4], c, c );
ADC( A[5], A[5], B[5], c, c );
ADC( A[6], A[6], B[6], c, c );
#endif
}
@ -251,13 +248,13 @@ mbedtls_p384_slu( uint64_t A[7],
: "rax", "memory", "cc");
#else
uint64_t c;
SBB( X[0], A[0], B[0], 0, c );
SBB( X[1], A[1], B[1], c, c );
SBB( X[2], A[2], B[2], c, c );
SBB( X[3], A[3], B[3], c, c );
SBB( X[4], A[4], B[4], c, c );
SBB( X[5], A[5], B[5], c, c );
SBB( X[6], A[6], B[6], c, c );
SBB( A[0], A[0], B[0], 0, c );
SBB( A[1], A[1], B[1], c, c );
SBB( A[2], A[2], B[2], c, c );
SBB( A[3], A[3], B[3], c, c );
SBB( A[4], A[4], B[4], c, c );
SBB( A[5], A[5], B[5], c, c );
SBB( A[6], A[6], B[6], c, c );
#endif
}

View file

@ -261,4 +261,7 @@ int mbedtls_p384_normalize_jac( const mbedtls_ecp_group *,
int mbedtls_p384_normalize_jac_many( const mbedtls_ecp_group *,
mbedtls_ecp_point *[], size_t );
void mbedtls_p384_rum( uint64_t p[7] );
void mbedtls_p384_mod( uint64_t X[12] );
#endif /* COSMOPOLITAN_THIRD_PARTY_MBEDTLS_ECP_INTERNAL_H_ */

View file

@ -42,7 +42,7 @@
* @see FIPS 186-3 §D.2.4
*/
void secp384r1(uint64_t p[12]) {
uint64_t A, B, C, D, E, F, G, a, b;
uint64_t A, B, C, D, E, F, G, a, b, o;
A = Q(0);
B = Q(2);
C = Q(4);
@ -56,7 +56,7 @@ void secp384r1(uint64_t p[12]) {
ADC(C, C, a << 1, 0, o);
ADC(D, D, b << 1 | a >> 63, o, o);
ADC(E, E, b >> 63, o, o);
ADC(F, F, o, o, o);
ADC(F, F, 0, o, o);
G += o;
ADC(A, A, Q(12), 0, o);
ADC(B, B, Q(14), o, o);

View file

@ -1,39 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/mbedtls/bignum_internal.h"
#include "third_party/mbedtls/platform.h"
void ShiftRightPure(mbedtls_mpi_uint *p, size_t n, unsigned char k) {
mbedtls_mpi_uint x, y, *e, *f;
MBEDTLS_ASSERT(!(k & ~63));
f = p;
if (n) {
y = 0;
x = p[0];
e = p + n;
for (; ++p < e; x = y) {
y = p[0];
p[-1] = x >> 1 | y << (64 - 1);
}
p[-1] = x >> 1;
}
while (p < f) {
*p++ = 0;
}
}

View file

@ -22,7 +22,7 @@
void (*ShiftRight)(uint64_t *, size_t, unsigned char);
static textstartup void ShiftRightInit(void) {
ShiftRight = X86_HAVE(AVX) ? ShiftRightAvx : ShiftRightPure;
ShiftRight = 0 && X86_HAVE(AVX) ? ShiftRightAvx : ShiftRightPure;
}
const void *const ShiftRightCtor[] initarray = {ShiftRightInit};

View file

@ -48,96 +48,13 @@ TEST(secp384r1, testIsTheSame) {
for (i = 0; i < 12; ++i) {
printf("0x%016lx vs. 0x%016lx %d\n", A.p[i], B.p[i], A.p[i] == B.p[i]);
}
exit(1);
ASSERT_TRUE(false);
}
}
mbedtls_mpi_free(&B);
mbedtls_mpi_free(&A);
}
static inline bool mbedtls_p384_gte(uint64_t p[7]) {
return (((int64_t)p[6] > 0 ||
(p[5] > 0xffffffffffffffff ||
(p[5] == 0xffffffffffffffff &&
(p[4] > 0xffffffffffffffff ||
(p[4] == 0xffffffffffffffff &&
(p[3] > 0xffffffffffffffff ||
(p[3] == 0xffffffffffffffff &&
(p[2] > 0xfffffffffffffffe ||
(p[2] == 0xfffffffffffffffe &&
(p[1] > 0xffffffff00000000 ||
(p[1] == 0xffffffff00000000 &&
(p[0] > 0x00000000ffffffff ||
(p[0] == 0x00000000ffffffff))))))))))))));
}
static inline void mbedtls_p384_gro(uint64_t p[7]) {
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
asm("addq\t%1,%0\n\t"
"adcq\t%2,8+%0\n\t"
"adcq\t%3,16+%0\n\t"
"adcq\t%4,24+%0\n\t"
"adcq\t%4,32+%0\n\t"
"adcq\t%4,40+%0\n\t"
"adcq\t$0,48+%0"
: "+o"(*p)
: "r"(0x00000000ffffffffl), "r"(0xffffffff00000000),
"i"(0xfffffffffffffffel), "i"(0xffffffffffffffff)
: "memory", "cc");
#else
uint64_t c;
ADC(p[0], p[0], 0x00000000ffffffff, 0, c);
ADC(p[1], p[1], 0xffffffff00000000, c, c);
ADC(p[2], p[2], 0xfffffffffffffffe, c, c);
ADC(p[3], p[3], 0xffffffffffffffff, c, c);
ADC(p[4], p[4], 0xffffffffffffffff, c, c);
ADC(p[5], p[5], 0xffffffffffffffff, c, c);
ADC(p[6], p[6], 0, c, c);
#endif
}
static inline void mbedtls_p384_red(uint64_t p[7]) {
#if defined(__x86_64__) && !defined(__STRICT_ANSI__)
asm("subq\t%1,%0\n\t"
"sbbq\t%2,8+%0\n\t"
"sbbq\t%3,16+%0\n\t"
"sbbq\t%4,24+%0\n\t"
"sbbq\t%4,32+%0\n\t"
"sbbq\t%4,40+%0\n\t"
"sbbq\t$0,48+%0"
: "+o"(*p)
: "r"(0x00000000ffffffffl), "r"(0xffffffff00000000),
"i"(0xfffffffffffffffel), "i"(0xffffffffffffffff)
: "memory", "cc");
#else
uint64_t c;
SBB(p[0], p[0], 0x00000000ffffffff, 0, c);
SBB(p[1], p[1], 0xffffffff00000000, c, c);
SBB(p[2], p[2], 0xfffffffffffffffe, c, c);
SBB(p[3], p[3], 0xffffffffffffffff, c, c);
SBB(p[4], p[4], 0xffffffffffffffff, c, c);
SBB(p[5], p[5], 0xffffffffffffffff, c, c);
SBB(p[6], p[6], 0, c, c);
#endif
}
static inline void mbedtls_p384_rum(uint64_t p[7]) {
while (mbedtls_p384_gte(p)) mbedtls_p384_red(p);
}
static inline void mbedtls_p384_mod(uint64_t X[12]) {
secp384r1(X);
if ((int64_t)X[6] < 0) {
do {
mbedtls_p384_gro(X);
} while ((int64_t)X[6] < 0);
} else {
while (mbedtls_p384_gte(X)) {
mbedtls_p384_red(X);
}
}
}
TEST(secp384r1, needsDownwardCorrection) {
int i;
uint64_t P[6] = {
@ -174,7 +91,7 @@ TEST(secp384r1, needsDownwardCorrection) {
for (i = 0; i < 12; ++i) {
printf("0x%016lx vs. 0x%016lx %d\n", W[i], X[i], W[i] == X[i]);
}
exit(1);
ASSERT_TRUE(false);
}
}
@ -215,7 +132,7 @@ TEST(secp384r1, needsUpwardCorrection) {
for (i = 0; i < 12; ++i) {
printf("0x%016lx vs. 0x%016lx %d\n", W[i], X[i], W[i] == X[i]);
}
exit(1);
ASSERT_TRUE(false);
}
}
@ -229,66 +146,4 @@ BENCH(secp384r1, bench) {
mbedtls_mpi_free(&A);
}
void mbedtls_p384_shl_a(uint64_t p[7]) {
asm("shlq\t%0\n\t"
"rclq\t8+%0\n\t"
"rclq\t16+%0\n\t"
"rclq\t24+%0\n\t"
"rclq\t32+%0\n\t"
"rclq\t40+%0\n\t"
"rclq\t48+%0\n\t"
: "+o"(*p)
: /* no inputs */
: "memory", "cc");
mbedtls_p384_rum(p);
}
void mbedtls_p384_shl_b(uint64_t p[7]) {
p[6] = p[5] >> 63;
p[5] = p[5] << 1 | p[4] >> 63;
p[4] = p[4] << 1 | p[3] >> 63;
p[3] = p[3] << 1 | p[2] >> 63;
p[2] = p[2] << 1 | p[1] >> 63;
p[1] = p[1] << 1 | p[0] >> 63;
p[0] = p[0] << 1;
mbedtls_p384_rum(p);
}
BENCH(shl, bench) {
uint64_t A[7] = {0};
EZBENCH2("mbedtls_p384_shl_a", donothing, mbedtls_p384_shl_a(A));
EZBENCH2("mbedtls_p384_shl_b", donothing, mbedtls_p384_shl_b(A));
}
void mbedtls_p384_red_a(uint64_t p[7]) {
asm("subq\t%1,%0\n\t"
"sbbq\t%2,8+%0\n\t"
"sbbq\t%3,16+%0\n\t"
"sbbq\t%4,24+%0\n\t"
"sbbq\t%4,32+%0\n\t"
"sbbq\t%4,40+%0\n\t"
"sbbq\t$0,48+%0"
: "+o"(*p)
: "r"(0x00000000ffffffffl), "r"(0xffffffff00000000),
"i"(0xfffffffffffffffel), "i"(0xffffffffffffffff)
: "memory", "cc");
}
void mbedtls_p384_red_b(uint64_t p[7]) {
uint64_t c;
SBB(p[0], p[0], 0x00000000ffffffff, 0, c);
SBB(p[1], p[1], 0xffffffff00000000, c, c);
SBB(p[2], p[2], 0xfffffffffffffffe, c, c);
SBB(p[3], p[3], 0xffffffffffffffff, c, c);
SBB(p[4], p[4], 0xffffffffffffffff, c, c);
SBB(p[5], p[5], 0xffffffffffffffff, c, c);
SBB(p[6], p[6], 0, c, c);
}
BENCH(red, bench) {
uint64_t A[7] = {0};
EZBENCH2("mbedtls_p384_red_a", donothing, mbedtls_p384_red_a(A));
EZBENCH2("mbedtls_p384_red_b", donothing, mbedtls_p384_red_b(A));
}
#endif /* MBEDTLS_ECP_C */