summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_internal.h
diff options
context:
space:
mode:
authorjsing <>2023-06-12 16:17:24 +0000
committerjsing <>2023-06-12 16:17:24 +0000
commite65682b76bcfaec43a218a52db723a341bec5b90 (patch)
treeb4e21a96adfc49bf3a4624865e60a6430dcf75ab /src/lib/libcrypto/bn/bn_internal.h
parent3fc29f2a9986e70c00a72c515bd13f00f6157fc0 (diff)
downloadopenbsd-e65682b76bcfaec43a218a52db723a341bec5b90.tar.gz
openbsd-e65682b76bcfaec43a218a52db723a341bec5b90.tar.bz2
openbsd-e65682b76bcfaec43a218a52db723a341bec5b90.zip
Provide and use various quad word primitives.
This includes bn_qwaddqw(), bn_qwsubqw(), bn_qwmulw_addw() and bn_qwmulw_addqw_addw(). These can typically be optimised on architectures that have a reasonable number of general purpose registers. ok tb@
Diffstat (limited to 'src/lib/libcrypto/bn/bn_internal.h')
-rw-r--r--src/lib/libcrypto/bn/bn_internal.h110
1 files changed, 109 insertions, 1 deletions
diff --git a/src/lib/libcrypto/bn/bn_internal.h b/src/lib/libcrypto/bn/bn_internal.h
index 8a729b8e44..5f86e21330 100644
--- a/src/lib/libcrypto/bn/bn_internal.h
+++ b/src/lib/libcrypto/bn/bn_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_internal.h,v 1.11 2023/03/07 09:35:55 jsing Exp $ */ 1/* $OpenBSD: bn_internal.h,v 1.12 2023/06/12 16:17:24 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -123,6 +123,33 @@ bn_addw_addw(BN_ULONG a, BN_ULONG b, BN_ULONG c, BN_ULONG *out_r1,
123#endif 123#endif
124 124
125/* 125/*
126 * bn_qwaddqw() computes
127 * (r4:r3:r2:r1:r0) = (a3:a2:a1:a0) + (b3:b2:b1:b0) + carry, where a is a quad word,
128 * b is a quad word, and carry is a single word with value 0 or 1, producing a four
129 * word result and carry.
130 */
131#ifndef HAVE_BN_QWADDQW
132static inline void
133bn_qwaddqw(BN_ULONG a3, BN_ULONG a2, BN_ULONG a1, BN_ULONG a0, BN_ULONG b3,
134 BN_ULONG b2, BN_ULONG b1, BN_ULONG b0, BN_ULONG carry, BN_ULONG *out_carry,
135 BN_ULONG *out_r3, BN_ULONG *out_r2, BN_ULONG *out_r1, BN_ULONG *out_r0)
136{
137 BN_ULONG r3, r2, r1, r0;
138
139 bn_addw_addw(a0, b0, carry, &carry, &r0);
140 bn_addw_addw(a1, b1, carry, &carry, &r1);
141 bn_addw_addw(a2, b2, carry, &carry, &r2);
142 bn_addw_addw(a3, b3, carry, &carry, &r3);
143
144 *out_carry = carry;
145 *out_r3 = r3;
146 *out_r2 = r2;
147 *out_r1 = r1;
148 *out_r0 = r0;
149}
150#endif
151
152/*
126 * bn_subw() computes r0 = a - b, where both inputs are single words, 153 * bn_subw() computes r0 = a - b, where both inputs are single words,
127 * producing a single word result and borrow. 154 * producing a single word result and borrow.
128 */ 155 */
@@ -160,6 +187,33 @@ bn_subw_subw(BN_ULONG a, BN_ULONG b, BN_ULONG c, BN_ULONG *out_borrow,
160#endif 187#endif
161 188
162/* 189/*
190 * bn_qwsubqw() computes
191 * (r3:r2:r1:r0) = (a3:a2:a1:a0) - (b3:b2:b1:b0) - borrow, where a is a quad word,
192 * b is a quad word, and borrow is a single word with value 0 or 1, producing a
193 * four word result and borrow.
194 */
195#ifndef HAVE_BN_QWSUBQW
196static inline void
197bn_qwsubqw(BN_ULONG a3, BN_ULONG a2, BN_ULONG a1, BN_ULONG a0, BN_ULONG b3,
198 BN_ULONG b2, BN_ULONG b1, BN_ULONG b0, BN_ULONG borrow, BN_ULONG *out_borrow,
199 BN_ULONG *out_r3, BN_ULONG *out_r2, BN_ULONG *out_r1, BN_ULONG *out_r0)
200{
201 BN_ULONG r3, r2, r1, r0;
202
203 bn_subw_subw(a0, b0, borrow, &borrow, &r0);
204 bn_subw_subw(a1, b1, borrow, &borrow, &r1);
205 bn_subw_subw(a2, b2, borrow, &borrow, &r2);
206 bn_subw_subw(a3, b3, borrow, &borrow, &r3);
207
208 *out_borrow = borrow;
209 *out_r3 = r3;
210 *out_r2 = r2;
211 *out_r1 = r1;
212 *out_r0 = r0;
213}
214#endif
215
216/*
163 * bn_mulw() computes (r1:r0) = a * b, where both inputs are single words, 217 * bn_mulw() computes (r1:r0) = a * b, where both inputs are single words,
164 * producing a double word result. 218 * producing a double word result.
165 */ 219 */
@@ -387,4 +441,58 @@ bn_mul2_mulw_addtw(BN_ULONG a, BN_ULONG b, BN_ULONG c2, BN_ULONG c1, BN_ULONG c0
387} 441}
388#endif 442#endif
389 443
444/*
445 * bn_qwmulw_addw() computes (r4:r3:r2:r1:r0) = (a3:a2:a1:a0) * b + c, where a
446 * is a quad word, b is a single word and c is a single word, producing a five
447 * word result.
448 */
449#ifndef HAVE_BN_QWMULW_ADDW
450static inline void
451bn_qwmulw_addw(BN_ULONG a3, BN_ULONG a2, BN_ULONG a1, BN_ULONG a0, BN_ULONG b,
452 BN_ULONG c, BN_ULONG *out_r4, BN_ULONG *out_r3, BN_ULONG *out_r2,
453 BN_ULONG *out_r1, BN_ULONG *out_r0)
454{
455 BN_ULONG r3, r2, r1, r0;
456
457 bn_mulw_addw(a0, b, c, &c, &r0);
458 bn_mulw_addw(a1, b, c, &c, &r1);
459 bn_mulw_addw(a2, b, c, &c, &r2);
460 bn_mulw_addw(a3, b, c, &c, &r3);
461
462 *out_r4 = c;
463 *out_r3 = r3;
464 *out_r2 = r2;
465 *out_r1 = r1;
466 *out_r0 = r0;
467}
468#endif
469
470/*
471 * bn_qwmulw_addqw_addw() computes
472 * (r4:r3:r2:r1:r0) = (a3:a2:a1:a0) * b + (c3:c2:c1:c0) + d, where a
473 * is a quad word, b is a single word, c is a quad word, and d is a single word,
474 * producing a five word result.
475 */
476#ifndef HAVE_BN_QWMULW_ADDQW_ADDW
477static inline void
478bn_qwmulw_addqw_addw(BN_ULONG a3, BN_ULONG a2, BN_ULONG a1, BN_ULONG a0,
479 BN_ULONG b, BN_ULONG c3, BN_ULONG c2, BN_ULONG c1, BN_ULONG c0, BN_ULONG d,
480 BN_ULONG *out_r4, BN_ULONG *out_r3, BN_ULONG *out_r2, BN_ULONG *out_r1,
481 BN_ULONG *out_r0)
482{
483 BN_ULONG r3, r2, r1, r0;
484
485 bn_mulw_addw_addw(a0, b, c0, d, &d, &r0);
486 bn_mulw_addw_addw(a1, b, c1, d, &d, &r1);
487 bn_mulw_addw_addw(a2, b, c2, d, &d, &r2);
488 bn_mulw_addw_addw(a3, b, c3, d, &d, &r3);
489
490 *out_r4 = d;
491 *out_r3 = r3;
492 *out_r2 = r2;
493 *out_r1 = r1;
494 *out_r0 = r0;
495}
496#endif
497
390#endif 498#endif