From e95b355c41ea52539db929aa0a8db7f337eb4927 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Tue, 16 Jan 2024 13:54:50 +0000 Subject: [PATCH] utils/gcm-ref: Pull `poly64_mul' and `poly64_redc' out of `poly64_common'. Basically a refactoring, but there's some foreshadowing too -- most notably the UWHAT and VWHAT arguments. --- utils/gcm-ref | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/utils/gcm-ref b/utils/gcm-ref index 6a9c4c22..174e79e4 100755 --- a/utils/gcm-ref +++ b/utils/gcm-ref @@ -343,8 +343,7 @@ def poly64_mul_karatsuba(u, v, klimit, presfn, wd, presfn(TAG_PRODUCT, wd, x, 2*w, dispwd, '%s %s' % (uwhat, vwhat)) return x -def poly64_common(u, v, presfn, dispwd = 32, mulwd = 64, redcwd = 32, - klimit = 256): +def poly64_mul(u, v, presfn, dispwd, mulwd, klimit, uwhat, vwhat): """ Multiply U by V using a primitive 64-bit binary polynomial mutliplier. @@ -353,27 +352,27 @@ def poly64_common(u, v, presfn, dispwd = 32, mulwd = 64, redcwd = 32, Operands arrive in a `register format', which is a byte-swapped variant of the external format. Implementations differ on the precise details, - though. + though. Returns the double-precision product. """ - ## We work in two main phases: first, calculate the full double-width - ## product; and, second, reduce it modulo the field polynomial. - w = 8*len(u); assert(w == 8*len(v)) - p = poly(w) - presfn(TAG_INPUT_U, w, C.GF.loadb(u), w, dispwd, 'u') - presfn(TAG_INPUT_V, w, C.GF.loadb(v), w, dispwd, 'v') + x = poly64_mul_karatsuba(u, v, klimit, presfn, + w, dispwd, mulwd, uwhat, vwhat) - ## So, on to the first part: the multiplication. - x = poly64_mul_karatsuba(u, v, klimit, presfn, w, dispwd, mulwd, 'u', 'v') + return x.storeb(w/4) - ## Now we have to shift everything up one bit to account for GCM's crazy - ## bit ordering. - y = x << 1 - presfn(TAG_SHIFTED, w, y, 2*w, dispwd, 'y') +def poly64_redc(y, presfn, dispwd, redcwd): + """ + Reduce a double-precision product X modulo the appropriate polynomial. + + The operand arrives in a `register format', which is a byte-swapped variant + of the external format. Implementations differ on the precise details, + though. Returns the single-precision reduced value. + """ + + w = 4*len(y) + p = poly(w) - ## Now for the reduction. - ## ## Our polynomial has the form p = t^d + r where r = SUM_{0<=i