| ... | @@ -166,14 +166,14 @@ pub const Poly1305 = struct { | ... | @@ -166,14 +166,14 @@ pub const Poly1305 = struct { |
| 166 | var h2 = st.h[2]; | 166 | var h2 = st.h[2]; |
| 167 | | 167 | |
| 168 | // H - (2^130 - 5) | 168 | // H - (2^130 - 5) |
| 169 | var v = sub(h0, 0xfffffffffffffffb, 0); | 169 | var v = @subWithOverflow(h0, 0xfffffffffffffffb); |
| 170 | const h_p0 = v[0]; | 170 | const h_p0 = v[0]; |
| 171 | v = sub(h1, 0xffffffffffffffff, v[1]); | 171 | v = sub(h1, 0xffffffffffffffff, v[1]); |
| 172 | const h_p1 = v[0]; | 172 | const h_p1 = v[0]; |
| 173 | v = sub(h2, 0x0000000000000003, v[1]); | 173 | v = sub(h2, 0x0000000000000003, v[1]); |
| 174 | | 174 | |
| 175 | // Final reduction, subtract 2^130-5 from H if H >= 2^130-5 | 175 | // Final reduction, subtract 2^130-5 from H if H >= 2^130-5 |
| 176 | const mask = v[1] -% 1; | 176 | const mask = @as(u64, v[1]) -% 1; |
| 177 | h0 ^= mask & (h0 ^ h_p0); | 177 | h0 ^= mask & (h0 ^ h_p0); |
| 178 | h1 ^= mask & (h1 ^ h_p1); | 178 | h1 ^= mask & (h1 ^ h_p1); |
| 179 | | 179 | |
| ... | @@ -207,3 +207,12 @@ test "poly1305 rfc7439 vector1" { | ... | @@ -207,3 +207,12 @@ test "poly1305 rfc7439 vector1" { |
| 207 | | 207 | |
| 208 | try std.testing.expectEqualSlices(u8, expected_mac, &mac); | 208 | try std.testing.expectEqualSlices(u8, expected_mac, &mac); |
| 209 | } | 209 | } |
| | 210 | |
| | 211 | test "poly1305 requiring a final reduction" { |
| | 212 | const expected_mac = [_]u8{ 25, 13, 249, 42, 164, 57, 99, 60, 149, 181, 74, 74, 13, 63, 121, 6 }; |
| | 213 | const msg = [_]u8{ 253, 193, 249, 146, 70, 6, 214, 226, 131, 213, 241, 116, 20, 24, 210, 224, 65, 151, 255, 104, 133 }; |
| | 214 | const key = [_]u8{ 190, 63, 95, 57, 155, 103, 77, 170, 7, 98, 106, 44, 117, 186, 90, 185, 109, 118, 184, 24, 69, 41, 166, 243, 119, 132, 151, 61, 52, 43, 64, 250 }; |
| | 215 | var mac: [16]u8 = undefined; |
| | 216 | Poly1305.create(mac[0..], &msg, &key); |
| | 217 | try std.testing.expectEqualSlices(u8, &expected_mac, &mac); |
| | 218 | } |