77
88package poly1305
99
10- import "encoding/binary"
10+ import (
11+ "encoding/binary"
12+ "math/bits"
13+ )
1114
1215// Poly1305 [RFC 7539] is a relatively simple algorithm: the authentication tag
1316// for a 64 bytes message is approximately
@@ -114,13 +117,13 @@ type uint128 struct {
114117}
115118
116119func mul64 (a , b uint64 ) uint128 {
117- hi , lo := bitsMul64 (a , b )
120+ hi , lo := bits . Mul64 (a , b )
118121 return uint128 {lo , hi }
119122}
120123
121124func add128 (a , b uint128 ) uint128 {
122- lo , c := bitsAdd64 (a .lo , b .lo , 0 )
123- hi , c := bitsAdd64 (a .hi , b .hi , c )
125+ lo , c := bits . Add64 (a .lo , b .lo , 0 )
126+ hi , c := bits . Add64 (a .hi , b .hi , c )
124127 if c != 0 {
125128 panic ("poly1305: unexpected overflow" )
126129 }
@@ -155,8 +158,8 @@ func updateGeneric(state *macState, msg []byte) {
155158 // hide leading zeroes. For full chunks, that's 1 << 128, so we can just
156159 // add 1 to the most significant (2¹²⁸) limb, h2.
157160 if len (msg ) >= TagSize {
158- h0 , c = bitsAdd64 (h0 , binary .LittleEndian .Uint64 (msg [0 :8 ]), 0 )
159- h1 , c = bitsAdd64 (h1 , binary .LittleEndian .Uint64 (msg [8 :16 ]), c )
161+ h0 , c = bits . Add64 (h0 , binary .LittleEndian .Uint64 (msg [0 :8 ]), 0 )
162+ h1 , c = bits . Add64 (h1 , binary .LittleEndian .Uint64 (msg [8 :16 ]), c )
160163 h2 += c + 1
161164
162165 msg = msg [TagSize :]
@@ -165,8 +168,8 @@ func updateGeneric(state *macState, msg []byte) {
165168 copy (buf [:], msg )
166169 buf [len (msg )] = 1
167170
168- h0 , c = bitsAdd64 (h0 , binary .LittleEndian .Uint64 (buf [0 :8 ]), 0 )
169- h1 , c = bitsAdd64 (h1 , binary .LittleEndian .Uint64 (buf [8 :16 ]), c )
171+ h0 , c = bits . Add64 (h0 , binary .LittleEndian .Uint64 (buf [0 :8 ]), 0 )
172+ h1 , c = bits . Add64 (h1 , binary .LittleEndian .Uint64 (buf [8 :16 ]), c )
170173 h2 += c
171174
172175 msg = nil
@@ -219,9 +222,9 @@ func updateGeneric(state *macState, msg []byte) {
219222 m3 := h2r1
220223
221224 t0 := m0 .lo
222- t1 , c := bitsAdd64 (m1 .lo , m0 .hi , 0 )
223- t2 , c := bitsAdd64 (m2 .lo , m1 .hi , c )
224- t3 , _ := bitsAdd64 (m3 .lo , m2 .hi , c )
225+ t1 , c := bits . Add64 (m1 .lo , m0 .hi , 0 )
226+ t2 , c := bits . Add64 (m2 .lo , m1 .hi , c )
227+ t3 , _ := bits . Add64 (m3 .lo , m2 .hi , c )
225228
226229 // Now we have the result as 4 64-bit limbs, and we need to reduce it
227230 // modulo 2¹³⁰ - 5. The special shape of this Crandall prime lets us do
@@ -243,14 +246,14 @@ func updateGeneric(state *macState, msg []byte) {
243246
244247 // To add c * 5 to h, we first add cc = c * 4, and then add (cc >> 2) = c.
245248
246- h0 , c = bitsAdd64 (h0 , cc .lo , 0 )
247- h1 , c = bitsAdd64 (h1 , cc .hi , c )
249+ h0 , c = bits . Add64 (h0 , cc .lo , 0 )
250+ h1 , c = bits . Add64 (h1 , cc .hi , c )
248251 h2 += c
249252
250253 cc = shiftRightBy2 (cc )
251254
252- h0 , c = bitsAdd64 (h0 , cc .lo , 0 )
253- h1 , c = bitsAdd64 (h1 , cc .hi , c )
255+ h0 , c = bits . Add64 (h0 , cc .lo , 0 )
256+ h1 , c = bits . Add64 (h1 , cc .hi , c )
254257 h2 += c
255258
256259 // h2 is at most 3 + 1 + 1 = 5, making the whole of h at most
@@ -287,9 +290,9 @@ func finalize(out *[TagSize]byte, h *[3]uint64, s *[2]uint64) {
287290 // in constant time, we compute t = h - (2¹³⁰ - 5), and select h as the
288291 // result if the subtraction underflows, and t otherwise.
289292
290- hMinusP0 , b := bitsSub64 (h0 , p0 , 0 )
291- hMinusP1 , b := bitsSub64 (h1 , p1 , b )
292- _ , b = bitsSub64 (h2 , p2 , b )
293+ hMinusP0 , b := bits . Sub64 (h0 , p0 , 0 )
294+ hMinusP1 , b := bits . Sub64 (h1 , p1 , b )
295+ _ , b = bits . Sub64 (h2 , p2 , b )
293296
294297 // h = h if h < p else h - p
295298 h0 = select64 (b , h0 , hMinusP0 )
@@ -301,8 +304,8 @@ func finalize(out *[TagSize]byte, h *[3]uint64, s *[2]uint64) {
301304 //
302305 // by just doing a wide addition with the 128 low bits of h and discarding
303306 // the overflow.
304- h0 , c := bitsAdd64 (h0 , s [0 ], 0 )
305- h1 , _ = bitsAdd64 (h1 , s [1 ], c )
307+ h0 , c := bits . Add64 (h0 , s [0 ], 0 )
308+ h1 , _ = bits . Add64 (h1 , s [1 ], c )
306309
307310 binary .LittleEndian .PutUint64 (out [0 :8 ], h0 )
308311 binary .LittleEndian .PutUint64 (out [8 :16 ], h1 )
0 commit comments