From 9cae46f1647a224f9e5bd15e3c6336ca7f9801aa Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Mon, 18 Sep 2023 15:42:50 +0100 Subject: [PATCH] add python-based implementation of dsrd to poly1305-donna.py and also fix "5" bug. somehow managed to put a const "4" instead of 5 --- src/openpower/decoder/isa/poly1305-donna.py | 20 ++++++++++++++++++-- src/openpower/test/bigint/bigint_cases.py | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/openpower/decoder/isa/poly1305-donna.py b/src/openpower/decoder/isa/poly1305-donna.py index 66e4c4a1..9ff95335 100644 --- a/src/openpower/decoder/isa/poly1305-donna.py +++ b/src/openpower/decoder/isa/poly1305-donna.py @@ -17,6 +17,21 @@ def ADDLO(out, i): return (out + (i & mask64)) def SHR(i, shift): out = (i >> shift) & mask64; print("shr %x>>%d=%x mask %x" % (i,shift,out,mask64)); return out def LO(i): return i & mask64 + +# this function is extracted from bigint_cases.py (should be in a library) +# it is a python implementation of dsrd, see pseudocode in +# https://libre-soc.org/openpower/isa/svfixedarith/ +def dsrd(lo, hi, sh): + sh = sh % 64 + v = lo << 64 + v >>= sh + mask = ~((2 ** 64 - 1) >> sh) + v |= (hi & mask) << 64 + hi = (v >> 64) % (2 ** 64) + lo == v % (2 ** 64) + return lo, hi + + class Poly1305Donna(object): """Poly1305 authenticator""" @@ -180,7 +195,7 @@ class Poly1305Donna(object): idxconsts = [ # hN c* shf [1, 1, 44], [2, 1, 42], - [0, 4, 44] + [0, 5, 44] ] c = 0 # start with carry=0 for hidx, cmul, shf in idxconsts*2: # repeat the pattern twice @@ -194,7 +209,8 @@ class Poly1305Donna(object): print(" h0-2 %x %x %x" % (h0, h1, h2)) #/* compute h + -p */ - g0 = h0 + 5; c = (g0 >> 44); g0 &= ff; + c = 5 + g0 = h0 + c; c = (g0 >> 44); g0 &= ff; g1 = h1 + c; c = (g1 >> 44); g1 &= ff; g2 = (h2 + c - (1 << 42)) & mask64 diff --git a/src/openpower/test/bigint/bigint_cases.py b/src/openpower/test/bigint/bigint_cases.py index 38ad4e08..98ad5fda 100644 --- a/src/openpower/test/bigint/bigint_cases.py +++ b/src/openpower/test/bigint/bigint_cases.py @@ -114,6 +114,8 @@ class BigIntCases(TestAccumulatorBase): gprs[4] = 0xFEDCBA9876543210 gprs[5] = sh % 2 ** 64 e = ExpectedState(pc=4, int_regs=gprs) + # XXX the function here should be extracted to a library, + # see poly1305_donna.py v = (gprs[4] << 64) v >>= sh % 64 mask = ~((2 ** 64 - 1) >> (sh % 64)) -- 2.30.2