7
7
8
8
package poly1305
9
9
10
- import "encoding/binary"
10
+ import (
11
+ "encoding/binary"
12
+ "math/bits"
13
+ )
11
14
12
15
// Poly1305 [RFC 7539] is a relatively simple algorithm: the authentication tag
13
16
// for a 64 bytes message is approximately
@@ -114,13 +117,13 @@ type uint128 struct {
114
117
}
115
118
116
119
func mul64 (a , b uint64 ) uint128 {
117
- hi , lo := bitsMul64 (a , b )
120
+ hi , lo := bits . Mul64 (a , b )
118
121
return uint128 {lo , hi }
119
122
}
120
123
121
124
func 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 )
124
127
if c != 0 {
125
128
panic ("poly1305: unexpected overflow" )
126
129
}
@@ -155,8 +158,8 @@ func updateGeneric(state *macState, msg []byte) {
155
158
// hide leading zeroes. For full chunks, that's 1 << 128, so we can just
156
159
// add 1 to the most significant (2¹²⁸) limb, h2.
157
160
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 )
160
163
h2 += c + 1
161
164
162
165
msg = msg [TagSize :]
@@ -165,8 +168,8 @@ func updateGeneric(state *macState, msg []byte) {
165
168
copy (buf [:], msg )
166
169
buf [len (msg )] = 1
167
170
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 )
170
173
h2 += c
171
174
172
175
msg = nil
@@ -219,9 +222,9 @@ func updateGeneric(state *macState, msg []byte) {
219
222
m3 := h2r1
220
223
221
224
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 )
225
228
226
229
// Now we have the result as 4 64-bit limbs, and we need to reduce it
227
230
// modulo 2¹³⁰ - 5. The special shape of this Crandall prime lets us do
@@ -243,14 +246,14 @@ func updateGeneric(state *macState, msg []byte) {
243
246
244
247
// To add c * 5 to h, we first add cc = c * 4, and then add (cc >> 2) = c.
245
248
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 )
248
251
h2 += c
249
252
250
253
cc = shiftRightBy2 (cc )
251
254
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 )
254
257
h2 += c
255
258
256
259
// 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) {
287
290
// in constant time, we compute t = h - (2¹³⁰ - 5), and select h as the
288
291
// result if the subtraction underflows, and t otherwise.
289
292
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 )
293
296
294
297
// h = h if h < p else h - p
295
298
h0 = select64 (b , h0 , hMinusP0 )
@@ -301,8 +304,8 @@ func finalize(out *[TagSize]byte, h *[3]uint64, s *[2]uint64) {
301
304
//
302
305
// by just doing a wide addition with the 128 low bits of h and discarding
303
306
// 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 )
306
309
307
310
binary .LittleEndian .PutUint64 (out [0 :8 ], h0 )
308
311
binary .LittleEndian .PutUint64 (out [8 :16 ], h1 )
0 commit comments