-/* $OpenBSD: bn_arch.h,v 1.3 2023/02/04 11:48:55 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.4 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#ifndef OPENSSL_NO_ASM
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/* Unsigned multiplication using a umulh/mul pair. */
__asm__ ("umulh %0, %2, %3; mul %1, %2, %3"
- : "=&r"(h), "=r"(l)
+ : "=&r"(r1), "=r"(r0)
: "r"(a), "r"(b));
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
-/* $OpenBSD: bn_arch.h,v 1.3 2023/02/04 11:48:55 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.4 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#if 0 /* Needs testing and enabling. */
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/* Unsigned multiplication using a umulh/mulq pair. */
__asm__ ("umulh %2, %3, %0; mulq %2, %3, %1"
- : "=&r"(h), "=r"(l)
+ : "=&r"(r1), "=r"(r0)
: "r"(a), "r"(b));
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
#endif
-/* $OpenBSD: bn_arch.h,v 1.11 2023/02/04 14:00:18 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.12 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#endif /* __GNUC__ */
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/*
* Unsigned multiplication of %rax, with the double word result being
* stored in %rdx:%rax.
*/
__asm__ ("mulq %3"
- : "=d"(h), "=a"(l)
+ : "=d"(r1), "=a"(r0)
: "a"(a), "rm"(b)
: "cc");
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
-/* $OpenBSD: bn_arch.h,v 1.8 2023/01/31 05:53:49 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.9 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#endif /* __GNUC__ */
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/*
* Unsigned multiplication of %eax, with the double word result being
* stored in %edx:%eax.
*/
__asm__ ("mull %3"
- : "=d"(h), "=a"(l)
+ : "=d"(r1), "=a"(r0)
: "a"(a), "rm"(b)
: "cc");
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
-/* $OpenBSD: bn_arch.h,v 1.3 2023/02/04 11:48:55 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.4 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#if 0 /* Needs testing and enabling. */
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/* Unsigned multiplication using a mulhdu/mul pair. */
__asm__ ("mulhdu %0, %2, %3; mul %1, %2, %3"
- : "=&r"(h), "=r"(l)
+ : "=&r"(r1), "=r"(r0)
: "r"(a), "r"(b));
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
#endif
-/* $OpenBSD: bn_arch.h,v 1.3 2023/02/04 11:48:55 jsing Exp $ */
+/* $OpenBSD: bn_arch.h,v 1.4 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
#if 0 /* Needs testing and enabling. */
#if defined(__GNUC__)
-#define HAVE_BN_UMUL_HILO
+#define HAVE_BN_MULW
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG h, l;
+ BN_ULONG r1, r0;
/*
* Unsigned multiplication using a mulh/mul pair. Note that the order
* into a single operation.
*/
__asm__ ("mulh %0, %2, %3; mul %1, %2, %3"
- : "=&r"(h), "=r"(l)
+ : "=&r"(r1), "=r"(r0)
: "r"(a), "r"(b));
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#endif /* __GNUC__ */
#endif
-/* $OpenBSD: bn_div.c,v 1.38 2023/02/14 18:19:27 jsing Exp $ */
+/* $OpenBSD: bn_div.c,v 1.39 2023/02/16 10:41:03 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
/* n0 < d0 */
bn_div_rem_words(n0, n1, d0, &q, &rem);
- bn_umul_hilo(d1, q, &t2h, &t2l);
+ bn_mulw(d1, q, &t2h, &t2l);
for (;;) {
if (t2h < rem || (t2h == rem && t2l <= m[-2]))
-/* $OpenBSD: bn_internal.h,v 1.6 2023/02/16 10:02:02 jsing Exp $ */
+/* $OpenBSD: bn_internal.h,v 1.7 2023/02/16 10:41:03 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
}
#endif
-#ifndef HAVE_BN_UMUL_HILO
+/*
+ * bn_mulw() computes (r1:r0) = a * b, where both inputs are single words,
+ * producing a double word result.
+ */
+#ifndef HAVE_BN_MULW
#ifdef BN_LLONG
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
BN_ULLONG r;
r = (BN_ULLONG)a * (BN_ULLONG)b;
- *out_h = r >> BN_BITS2;
- *out_l = r & BN_MASK2;
+ *out_r1 = r >> BN_BITS2;
+ *out_r0 = r & BN_MASK2;
}
#else /* !BN_LLONG */
*/
#if 1
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG ah, al, bh, bl, h, l, x, c1, c2;
+ BN_ULONG a1, a0, b1, b0, r1, r0, c1, c2, x;
- ah = a >> BN_BITS4;
- al = a & BN_MASK2l;
- bh = b >> BN_BITS4;
- bl = b & BN_MASK2l;
+ a1 = a >> BN_BITS4;
+ a0 = a & BN_MASK2l;
+ b1 = b >> BN_BITS4;
+ b0 = b & BN_MASK2l;
- h = ah * bh;
- l = al * bl;
+ r1 = a1 * b1;
+ r0 = a0 * b0;
- /* (ah * bl) << BN_BITS4, partition the result across h:l with carry. */
- x = ah * bl;
- h += x >> BN_BITS4;
+ /* (a1 * b0) << BN_BITS4, partition the result across r1:r0 with carry. */
+ x = a1 * b0;
+ r1 += x >> BN_BITS4;
x <<= BN_BITS4;
- c1 = l | x;
- c2 = l & x;
- l += x;
- h += ((c1 & ~l) | c2) >> (BN_BITS2 - 1); /* carry */
-
- /* (bh * al) << BN_BITS4, partition the result across h:l with carry. */
- x = bh * al;
- h += x >> BN_BITS4;
+ c1 = r0 | x;
+ c2 = r0 & x;
+ r0 += x;
+ r1 += ((c1 & ~r0) | c2) >> (BN_BITS2 - 1); /* carry */
+
+ /* (b1 * a0) << BN_BITS4, partition the result across r1:r0 with carry. */
+ x = b1 * a0;
+ r1 += x >> BN_BITS4;
x <<= BN_BITS4;
- c1 = l | x;
- c2 = l & x;
- l += x;
- h += ((c1 & ~l) | c2) >> (BN_BITS2 - 1); /* carry */
+ c1 = r0 | x;
+ c2 = r0 & x;
+ r0 += x;
+ r1 += ((c1 & ~r0) | c2) >> (BN_BITS2 - 1); /* carry */
- *out_h = h;
- *out_l = l;
+ *out_r1 = r1;
+ *out_r0 = r0;
}
#else
* implementations should eventually be removed.
*/
static inline void
-bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l)
+bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
{
- BN_ULONG ah, bh, al, bl, x, h, l;
+ BN_ULONG a1, a0, b1, b0, r1, r0, x;
BN_ULONG acc0, acc1, acc2, acc3;
- ah = a >> BN_BITS4;
- bh = b >> BN_BITS4;
- al = a & BN_MASK2l;
- bl = b & BN_MASK2l;
+ a1 = a >> BN_BITS4;
+ b1 = b >> BN_BITS4;
+ a0 = a & BN_MASK2l;
+ b0 = b & BN_MASK2l;
- h = ah * bh;
- l = al * bl;
+ r1 = a1 * b1;
+ r0 = a0 * b0;
- acc0 = l & BN_MASK2l;
- acc1 = l >> BN_BITS4;
- acc2 = h & BN_MASK2l;
- acc3 = h >> BN_BITS4;
+ acc0 = r0 & BN_MASK2l;
+ acc1 = r0 >> BN_BITS4;
+ acc2 = r1 & BN_MASK2l;
+ acc3 = r1 >> BN_BITS4;
- /* (ah * bl) << BN_BITS4, partition the result across h:l. */
- x = ah * bl;
+ /* (a1 * b0) << BN_BITS4, partition the result across r1:r0. */
+ x = a1 * b0;
acc1 += x & BN_MASK2l;
acc2 += (acc1 >> BN_BITS4) + (x >> BN_BITS4);
acc1 &= BN_MASK2l;
acc3 += acc2 >> BN_BITS4;
acc2 &= BN_MASK2l;
- /* (bh * al) << BN_BITS4, partition the result across h:l. */
- x = bh * al;
+ /* (b1 * a0) << BN_BITS4, partition the result across r1:r0. */
+ x = b1 * a0;
acc1 += x & BN_MASK2l;
acc2 += (acc1 >> BN_BITS4) + (x >> BN_BITS4);
acc1 &= BN_MASK2l;
acc3 += acc2 >> BN_BITS4;
acc2 &= BN_MASK2l;
- *out_h = (acc3 << BN_BITS4) | acc2;
- *out_l = (acc1 << BN_BITS4) | acc0;
+ *out_r1 = (acc3 << BN_BITS4) | acc2;
+ *out_r0 = (acc1 << BN_BITS4) | acc0;
}
#endif
#endif /* !BN_LLONG */
#endif
-#ifndef HAVE_BN_UMUL_LO
+#ifndef HAVE_BN_MULW_LO
static inline BN_ULONG
-bn_umul_lo(BN_ULONG a, BN_ULONG b)
+bn_mulw_lo(BN_ULONG a, BN_ULONG b)
{
return a * b;
}
#endif
-#ifndef HAVE_BN_UMUL_HI
+#ifndef HAVE_BN_MULW_HI
static inline BN_ULONG
-bn_umul_hi(BN_ULONG a, BN_ULONG b)
+bn_mulw_hi(BN_ULONG a, BN_ULONG b)
{
BN_ULONG h, l;
- bn_umul_hilo(a, b, &h, &l);
+ bn_mulw(a, b, &h, &l);
return h;
}
{
BN_ULONG carry, r1, r0;
- bn_umul_hilo(a, b, &r1, &r0);
+ bn_mulw(a, b, &r1, &r0);
bn_addw(r0, c, &carry, &r0);
r1 += carry;
{
BN_ULONG carry, r2, r1, r0, x1, x0;
- bn_umul_hilo(a, b, &x1, &x0);
+ bn_mulw(a, b, &x1, &x0);
bn_addw(c0, x0, &carry, &r0);
x1 += carry;
bn_addw(c1, x1, &carry, &r1);
-/* $OpenBSD: bn_sqr.c,v 1.25 2023/02/13 04:25:37 jsing Exp $ */
+/* $OpenBSD: bn_sqr.c,v 1.26 2023/02/16 10:41:03 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
#ifndef OPENSSL_SMALL_FOOTPRINT
while (n & ~3) {
- bn_umul_hilo(a[0], a[0], &r[1], &r[0]);
- bn_umul_hilo(a[1], a[1], &r[3], &r[2]);
- bn_umul_hilo(a[2], a[2], &r[5], &r[4]);
- bn_umul_hilo(a[3], a[3], &r[7], &r[6]);
+ bn_mulw(a[0], a[0], &r[1], &r[0]);
+ bn_mulw(a[1], a[1], &r[3], &r[2]);
+ bn_mulw(a[2], a[2], &r[5], &r[4]);
+ bn_mulw(a[3], a[3], &r[7], &r[6]);
a += 4;
r += 8;
n -= 4;
}
#endif
while (n) {
- bn_umul_hilo(a[0], a[0], &r[1], &r[0]);
+ bn_mulw(a[0], a[0], &r[1], &r[0]);
a++;
r += 2;
n--;