| author | |
| committer | |
| log | b811a99af9b5549fd0f0940dafddc63040af01ac |
| tree | b4babfc938b0405eb007c45679f7da463527097b |
| parent | 0da7c4b0c8a2a2fe0862f7757bc5976342d51dc8 |
| parent | 56c5b665a1f93fd16a711986e25e4d7c5dfed163 |
12 files changed, 692 insertions(+), 46 deletions(-)
lib/std/crypto.zig+2| ... | @@ -28,6 +28,8 @@ pub const aead = struct { | ... | @@ -28,6 +28,8 @@ pub const aead = struct { |
| 28 | pub const Gimli = @import("crypto/gimli.zig").Aead; | 28 | pub const Gimli = @import("crypto/gimli.zig").Aead; |
| 29 | pub const ChaCha20Poly1305 = chacha20.Chacha20Poly1305; | 29 | pub const ChaCha20Poly1305 = chacha20.Chacha20Poly1305; |
| 30 | pub const XChaCha20Poly1305 = chacha20.XChacha20Poly1305; | 30 | pub const XChaCha20Poly1305 = chacha20.XChacha20Poly1305; |
| 31 | pub const AEGIS128L = @import("crypto/aegis.zig").AEGIS128L; | ||
| 32 | pub const AEGIS256 = @import("crypto/aegis.zig").AEGIS256; | ||
| 31 | }; | 33 | }; |
| 32 | 34 | ||
| 33 | /// MAC functions requiring single-use secret keys. | 35 | /// MAC functions requiring single-use secret keys. |
lib/std/crypto/aegis.zig created+447| ... | @@ -0,0 +1,447 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const mem = std.mem; | ||
| 3 | const assert = std.debug.assert; | ||
| 4 | const AESBlock = std.crypto.core.aes.Block; | ||
| 5 | |||
| 6 | const State128L = struct { | ||
| 7 | blocks: [8]AESBlock, | ||
| 8 | |||
| 9 | fn init(key: [16]u8, nonce: [16]u8) State128L { | ||
| 10 | const c1 = AESBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd }); | ||
| 11 | const c2 = AESBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 }); | ||
| 12 | const key_block = AESBlock.fromBytes(&key); | ||
| 13 | const nonce_block = AESBlock.fromBytes(&nonce); | ||
| 14 | const blocks = [8]AESBlock{ | ||
| 15 | key_block.xorBlocks(nonce_block), | ||
| 16 | c1, | ||
| 17 | c2, | ||
| 18 | c1, | ||
| 19 | key_block.xorBlocks(nonce_block), | ||
| 20 | key_block.xorBlocks(c2), | ||
| 21 | key_block.xorBlocks(c1), | ||
| 22 | key_block.xorBlocks(c2), | ||
| 23 | }; | ||
| 24 | var state = State128L{ .blocks = blocks }; | ||
| 25 | var i: usize = 0; | ||
| 26 | while (i < 10) : (i += 1) { | ||
| 27 | state.update(nonce_block, key_block); | ||
| 28 | } | ||
| 29 | return state; | ||
| 30 | } | ||
| 31 | |||
| 32 | inline fn update(state: *State128L, d1: AESBlock, d2: AESBlock) void { | ||
| 33 | const blocks = &state.blocks; | ||
| 34 | const tmp = blocks[7]; | ||
| 35 | comptime var i: usize = 7; | ||
| 36 | inline while (i > 0) : (i -= 1) { | ||
| 37 | blocks[i] = blocks[i - 1].encrypt(blocks[i]); | ||
| 38 | } | ||
| 39 | blocks[0] = tmp.encrypt(blocks[0]); | ||
| 40 | blocks[0] = blocks[0].xorBlocks(d1); | ||
| 41 | blocks[4] = blocks[4].xorBlocks(d2); | ||
| 42 | } | ||
| 43 | |||
| 44 | fn enc(state: *State128L, dst: *[32]u8, src: *const [32]u8) void { | ||
| 45 | const blocks = &state.blocks; | ||
| 46 | const msg0 = AESBlock.fromBytes(src[0..16]); | ||
| 47 | const msg1 = AESBlock.fromBytes(src[16..32]); | ||
| 48 | var tmp0 = msg0.xorBlocks(blocks[6]).xorBlocks(blocks[1]); | ||
| 49 | var tmp1 = msg1.xorBlocks(blocks[2]).xorBlocks(blocks[5]); | ||
| 50 | tmp0 = tmp0.xorBlocks(blocks[2].andBlocks(blocks[3])); | ||
| 51 | tmp1 = tmp1.xorBlocks(blocks[6].andBlocks(blocks[7])); | ||
| 52 | dst[0..16].* = tmp0.toBytes(); | ||
| 53 | dst[16..32].* = tmp1.toBytes(); | ||
| 54 | state.update(msg0, msg1); | ||
| 55 | } | ||
| 56 | |||
| 57 | fn dec(state: *State128L, dst: *[32]u8, src: *const [32]u8) void { | ||
| 58 | const blocks = &state.blocks; | ||
| 59 | var msg0 = AESBlock.fromBytes(src[0..16]).xorBlocks(blocks[6]).xorBlocks(blocks[1]); | ||
| 60 | var msg1 = AESBlock.fromBytes(src[16..32]).xorBlocks(blocks[2]).xorBlocks(blocks[5]); | ||
| 61 | msg0 = msg0.xorBlocks(blocks[2].andBlocks(blocks[3])); | ||
| 62 | msg1 = msg1.xorBlocks(blocks[6].andBlocks(blocks[7])); | ||
| 63 | dst[0..16].* = msg0.toBytes(); | ||
| 64 | dst[16..32].* = msg1.toBytes(); | ||
| 65 | state.update(msg0, msg1); | ||
| 66 | } | ||
| 67 | |||
| 68 | fn mac(state: *State128L, adlen: usize, mlen: usize) [16]u8 { | ||
| 69 | const blocks = &state.blocks; | ||
| 70 | var sizes: [16]u8 = undefined; | ||
| 71 | mem.writeIntLittle(u64, sizes[0..8], adlen * 8); | ||
| 72 | mem.writeIntLittle(u64, sizes[8..16], mlen * 8); | ||
| 73 | const tmp = AESBlock.fromBytes(&sizes).xorBlocks(blocks[2]); | ||
| 74 | var i: usize = 0; | ||
| 75 | while (i < 7) : (i += 1) { | ||
| 76 | state.update(tmp, tmp); | ||
| 77 | } | ||
| 78 | return blocks[0].xorBlocks(blocks[1]).xorBlocks(blocks[2]).xorBlocks(blocks[3]).xorBlocks(blocks[4]). | ||
| 79 | xorBlocks(blocks[5]).xorBlocks(blocks[6]).toBytes(); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | /// AEGIS is a very fast authenticated encryption system built on top of the core AES function. | ||
| 84 | /// | ||
| 85 | /// The 128L variant of AEGIS has a 128 bit key, a 128 bit nonce, and processes 256 bit message blocks. | ||
| 86 | /// It was designed to fully exploit the parallelism and built-in AES support of recent Intel and ARM CPUs. | ||
| 87 | /// | ||
| 88 | /// https://competitions.cr.yp.to/round3/aegisv11.pdf | ||
| 89 | pub const AEGIS128L = struct { | ||
| 90 | pub const tag_length = 16; | ||
| 91 | pub const nonce_length = 16; | ||
| 92 | pub const key_length = 16; | ||
| 93 | |||
| 94 | /// c: ciphertext: output buffer should be of size m.len | ||
| 95 | /// tag: authentication tag: output MAC | ||
| 96 | /// m: message | ||
| 97 | /// ad: Associated Data | ||
| 98 | /// npub: public nonce | ||
| 99 | /// k: private key | ||
| 100 | pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void { | ||
| 101 | assert(c.len == m.len); | ||
| 102 | var state = State128L.init(key, npub); | ||
| 103 | var src: [32]u8 align(16) = undefined; | ||
| 104 | var dst: [32]u8 align(16) = undefined; | ||
| 105 | var i: usize = 0; | ||
| 106 | while (i + 32 <= ad.len) : (i += 32) { | ||
| 107 | state.enc(&dst, ad[i..][0..32]); | ||
| 108 | } | ||
| 109 | if (ad.len % 32 != 0) { | ||
| 110 | mem.set(u8, src[0..], 0); | ||
| 111 | mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); | ||
| 112 | state.enc(&dst, &src); | ||
| 113 | } | ||
| 114 | i = 0; | ||
| 115 | while (i + 32 <= m.len) : (i += 32) { | ||
| 116 | state.enc(c[i..][0..32], m[i..][0..32]); | ||
| 117 | } | ||
| 118 | if (m.len % 32 != 0) { | ||
| 119 | mem.set(u8, src[0..], 0); | ||
| 120 | mem.copy(u8, src[0 .. m.len % 32], m[i .. i + m.len % 32]); | ||
| 121 | state.enc(&dst, &src); | ||
| 122 | mem.copy(u8, c[i .. i + m.len % 32], dst[0 .. m.len % 32]); | ||
| 123 | } | ||
| 124 | tag.* = state.mac(ad.len, m.len); | ||
| 125 | } | ||
| 126 | |||
| 127 | /// m: message: output buffer should be of size c.len | ||
| 128 | /// c: ciphertext | ||
| 129 | /// tag: authentication tag | ||
| 130 | /// ad: Associated Data | ||
| 131 | /// npub: public nonce | ||
| 132 | /// k: private key | ||
| 133 | pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) !void { | ||
| 134 | assert(c.len == m.len); | ||
| 135 | var state = State128L.init(key, npub); | ||
| 136 | var src: [32]u8 align(16) = undefined; | ||
| 137 | var dst: [32]u8 align(16) = undefined; | ||
| 138 | var i: usize = 0; | ||
| 139 | while (i + 32 <= ad.len) : (i += 32) { | ||
| 140 | state.enc(&dst, ad[i..][0..32]); | ||
| 141 | } | ||
| 142 | if (ad.len % 32 != 0) { | ||
| 143 | mem.set(u8, src[0..], 0); | ||
| 144 | mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); | ||
| 145 | state.enc(&dst, &src); | ||
| 146 | } | ||
| 147 | i = 0; | ||
| 148 | while (i + 32 <= m.len) : (i += 32) { | ||
| 149 | state.dec(m[i..][0..32], c[i..][0..32]); | ||
| 150 | } | ||
| 151 | if (m.len % 32 != 0) { | ||
| 152 | mem.set(u8, src[0..], 0); | ||
| 153 | mem.copy(u8, src[0 .. m.len % 32], c[i .. i + m.len % 32]); | ||
| 154 | state.dec(&dst, &src); | ||
| 155 | mem.copy(u8, m[i .. i + m.len % 32], dst[0 .. m.len % 32]); | ||
| 156 | mem.set(u8, dst[0 .. m.len % 32], 0); | ||
| 157 | const blocks = &state.blocks; | ||
| 158 | blocks[0] = blocks[0].xorBlocks(AESBlock.fromBytes(dst[0..16])); | ||
| 159 | blocks[4] = blocks[4].xorBlocks(AESBlock.fromBytes(dst[16..32])); | ||
| 160 | } | ||
| 161 | const computed_tag = state.mac(ad.len, m.len); | ||
| 162 | var acc: u8 = 0; | ||
| 163 | for (computed_tag) |_, j| { | ||
| 164 | acc |= (computed_tag[j] ^ tag[j]); | ||
| 165 | } | ||
| 166 | if (acc != 0) { | ||
| 167 | mem.set(u8, m, 0xaa); | ||
| 168 | return error.AuthenticationFailed; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | }; | ||
| 172 | |||
| 173 | const State256 = struct { | ||
| 174 | blocks: [6]AESBlock, | ||
| 175 | |||
| 176 | fn init(key: [32]u8, nonce: [32]u8) State256 { | ||
| 177 | const c1 = AESBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd }); | ||
| 178 | const c2 = AESBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 }); | ||
| 179 | const key_block1 = AESBlock.fromBytes(key[0..16]); | ||
| 180 | const key_block2 = AESBlock.fromBytes(key[16..32]); | ||
| 181 | const nonce_block1 = AESBlock.fromBytes(nonce[0..16]); | ||
| 182 | const nonce_block2 = AESBlock.fromBytes(nonce[16..32]); | ||
| 183 | const kxn1 = key_block1.xorBlocks(nonce_block1); | ||
| 184 | const kxn2 = key_block2.xorBlocks(nonce_block2); | ||
| 185 | const blocks = [6]AESBlock{ | ||
| 186 | kxn1, | ||
| 187 | kxn2, | ||
| 188 | c1, | ||
| 189 | c2, | ||
| 190 | key_block1.xorBlocks(c2), | ||
| 191 | key_block2.xorBlocks(c1), | ||
| 192 | }; | ||
| 193 | var state = State256{ .blocks = blocks }; | ||
| 194 | var i: usize = 0; | ||
| 195 | while (i < 4) : (i += 1) { | ||
| 196 | state.update(key_block1); | ||
| 197 | state.update(key_block2); | ||
| 198 | state.update(kxn1); | ||
| 199 | state.update(kxn2); | ||
| 200 | } | ||
| 201 | return state; | ||
| 202 | } | ||
| 203 | |||
| 204 | inline fn update(state: *State256, d: AESBlock) void { | ||
| 205 | const blocks = &state.blocks; | ||
| 206 | const tmp = blocks[5].encrypt(blocks[0]); | ||
| 207 | comptime var i: usize = 5; | ||
| 208 | inline while (i > 0) : (i -= 1) { | ||
| 209 | blocks[i] = blocks[i - 1].encrypt(blocks[i]); | ||
| 210 | } | ||
| 211 | blocks[0] = tmp.xorBlocks(d); | ||
| 212 | } | ||
| 213 | |||
| 214 | fn enc(state: *State256, dst: *[16]u8, src: *const [16]u8) void { | ||
| 215 | const blocks = &state.blocks; | ||
| 216 | const msg = AESBlock.fromBytes(src); | ||
| 217 | var tmp = msg.xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]); | ||
| 218 | tmp = tmp.xorBlocks(blocks[2].andBlocks(blocks[3])); | ||
| 219 | dst.* = tmp.toBytes(); | ||
| 220 | state.update(msg); | ||
| 221 | } | ||
| 222 | |||
| 223 | fn dec(state: *State256, dst: *[16]u8, src: *const [16]u8) void { | ||
| 224 | const blocks = &state.blocks; | ||
| 225 | var msg = AESBlock.fromBytes(src).xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]); | ||
| 226 | msg = msg.xorBlocks(blocks[2].andBlocks(blocks[3])); | ||
| 227 | dst.* = msg.toBytes(); | ||
| 228 | state.update(msg); | ||
| 229 | } | ||
| 230 | |||
| 231 | fn mac(state: *State256, adlen: usize, mlen: usize) [16]u8 { | ||
| 232 | const blocks = &state.blocks; | ||
| 233 | var sizes: [16]u8 = undefined; | ||
| 234 | mem.writeIntLittle(u64, sizes[0..8], adlen * 8); | ||
| 235 | mem.writeIntLittle(u64, sizes[8..16], mlen * 8); | ||
| 236 | const tmp = AESBlock.fromBytes(&sizes).xorBlocks(blocks[3]); | ||
| 237 | var i: usize = 0; | ||
| 238 | while (i < 7) : (i += 1) { | ||
| 239 | state.update(tmp); | ||
| 240 | } | ||
| 241 | return blocks[0].xorBlocks(blocks[1]).xorBlocks(blocks[2]).xorBlocks(blocks[3]).xorBlocks(blocks[4]). | ||
| 242 | xorBlocks(blocks[5]).toBytes(); | ||
| 243 | } | ||
| 244 | }; | ||
| 245 | |||
| 246 | /// AEGIS is a very fast authenticated encryption system built on top of the core AES function. | ||
| 247 | /// | ||
| 248 | /// The 256 bit variant of AEGIS has a 256 bit key, a 256 bit nonce, and processes 128 bit message blocks. | ||
| 249 | /// | ||
| 250 | /// https://competitions.cr.yp.to/round3/aegisv11.pdf | ||
| 251 | pub const AEGIS256 = struct { | ||
| 252 | pub const tag_length = 16; | ||
| 253 | pub const nonce_length = 32; | ||
| 254 | pub const key_length = 32; | ||
| 255 | |||
| 256 | /// c: ciphertext: output buffer should be of size m.len | ||
| 257 | /// tag: authentication tag: output MAC | ||
| 258 | /// m: message | ||
| 259 | /// ad: Associated Data | ||
| 260 | /// npub: public nonce | ||
| 261 | /// k: private key | ||
| 262 | pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void { | ||
| 263 | assert(c.len == m.len); | ||
| 264 | var state = State256.init(key, npub); | ||
| 265 | var src: [16]u8 align(16) = undefined; | ||
| 266 | var dst: [16]u8 align(16) = undefined; | ||
| 267 | var i: usize = 0; | ||
| 268 | while (i + 16 <= ad.len) : (i += 16) { | ||
| 269 | state.enc(&dst, ad[i..][0..16]); | ||
| 270 | } | ||
| 271 | if (ad.len % 16 != 0) { | ||
| 272 | mem.set(u8, src[0..], 0); | ||
| 273 | mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); | ||
| 274 | state.enc(&dst, &src); | ||
| 275 | } | ||
| 276 | i = 0; | ||
| 277 | while (i + 16 <= m.len) : (i += 16) { | ||
| 278 | state.enc(c[i..][0..16], m[i..][0..16]); | ||
| 279 | } | ||
| 280 | if (m.len % 16 != 0) { | ||
| 281 | mem.set(u8, src[0..], 0); | ||
| 282 | mem.copy(u8, src[0 .. m.len % 16], m[i .. i + m.len % 16]); | ||
| 283 | state.enc(&dst, &src); | ||
| 284 | mem.copy(u8, c[i .. i + m.len % 16], dst[0 .. m.len % 16]); | ||
| 285 | } | ||
| 286 | tag.* = state.mac(ad.len, m.len); | ||
| 287 | } | ||
| 288 | |||
| 289 | /// m: message: output buffer should be of size c.len | ||
| 290 | /// c: ciphertext | ||
| 291 | /// tag: authentication tag | ||
| 292 | /// ad: Associated Data | ||
| 293 | /// npub: public nonce | ||
| 294 | /// k: private key | ||
| 295 | pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) !void { | ||
| 296 | assert(c.len == m.len); | ||
| 297 | var state = State256.init(key, npub); | ||
| 298 | var src: [16]u8 align(16) = undefined; | ||
| 299 | var dst: [16]u8 align(16) = undefined; | ||
| 300 | var i: usize = 0; | ||
| 301 | while (i + 16 <= ad.len) : (i += 16) { | ||
| 302 | state.enc(&dst, ad[i..][0..16]); | ||
| 303 | } | ||
| 304 | if (ad.len % 16 != 0) { | ||
| 305 | mem.set(u8, src[0..], 0); | ||
| 306 | mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); | ||
| 307 | state.enc(&dst, &src); | ||
| 308 | } | ||
| 309 | i = 0; | ||
| 310 | while (i + 16 <= m.len) : (i += 16) { | ||
| 311 | state.dec(m[i..][0..16], c[i..][0..16]); | ||
| 312 | } | ||
| 313 | if (m.len % 16 != 0) { | ||
| 314 | mem.set(u8, src[0..], 0); | ||
| 315 | mem.copy(u8, src[0 .. m.len % 16], c[i .. i + m.len % 16]); | ||
| 316 | state.dec(&dst, &src); | ||
| 317 | mem.copy(u8, m[i .. i + m.len % 16], dst[0 .. m.len % 16]); | ||
| 318 | mem.set(u8, dst[0 .. m.len % 16], 0); | ||
| 319 | const blocks = &state.blocks; | ||
| 320 | blocks[0] = blocks[0].xorBlocks(AESBlock.fromBytes(&dst)); | ||
| 321 | } | ||
| 322 | const computed_tag = state.mac(ad.len, m.len); | ||
| 323 | var acc: u8 = 0; | ||
| 324 | for (computed_tag) |_, j| { | ||
| 325 | acc |= (computed_tag[j] ^ tag[j]); | ||
| 326 | } | ||
| 327 | if (acc != 0) { | ||
| 328 | mem.set(u8, m, 0xaa); | ||
| 329 | return error.AuthenticationFailed; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | }; | ||
| 333 | |||
| 334 | const htest = @import("test.zig"); | ||
| 335 | const testing = std.testing; | ||
| 336 | |||
| 337 | test "AEGIS128L test vector 1" { | ||
| 338 | const key: [AEGIS128L.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 14; | ||
| 339 | const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 13; | ||
| 340 | const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; | ||
| 341 | const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; | ||
| 342 | var c: [m.len]u8 = undefined; | ||
| 343 | var m2: [m.len]u8 = undefined; | ||
| 344 | var tag: [AEGIS128L.tag_length]u8 = undefined; | ||
| 345 | |||
| 346 | AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 347 | try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 348 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 349 | |||
| 350 | htest.assertEqual("79d94593d8c2119d7e8fd9b8fc77845c5c077a05b2528b6ac54b563aed8efe84", &c); | ||
| 351 | htest.assertEqual("cc6f3372f6aa1bb82388d695c3962d9a", &tag); | ||
| 352 | |||
| 353 | c[0] +%= 1; | ||
| 354 | testing.expectError(error.AuthenticationFailed, AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key)); | ||
| 355 | c[0] -%= 1; | ||
| 356 | tag[0] +%= 1; | ||
| 357 | testing.expectError(error.AuthenticationFailed, AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key)); | ||
| 358 | } | ||
| 359 | |||
| 360 | test "AEGIS128L test vector 2" { | ||
| 361 | const key: [AEGIS128L.key_length]u8 = [_]u8{0x00} ** 16; | ||
| 362 | const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{0x00} ** 16; | ||
| 363 | const ad = [_]u8{}; | ||
| 364 | const m = [_]u8{0x00} ** 16; | ||
| 365 | var c: [m.len]u8 = undefined; | ||
| 366 | var m2: [m.len]u8 = undefined; | ||
| 367 | var tag: [AEGIS128L.tag_length]u8 = undefined; | ||
| 368 | |||
| 369 | AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 370 | try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 371 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 372 | |||
| 373 | htest.assertEqual("41de9000a7b5e40e2d68bb64d99ebb19", &c); | ||
| 374 | htest.assertEqual("f4d997cc9b94227ada4fe4165422b1c8", &tag); | ||
| 375 | } | ||
| 376 | |||
| 377 | test "AEGIS128L test vector 3" { | ||
| 378 | const key: [AEGIS128L.key_length]u8 = [_]u8{0x00} ** 16; | ||
| 379 | const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{0x00} ** 16; | ||
| 380 | const ad = [_]u8{}; | ||
| 381 | const m = [_]u8{}; | ||
| 382 | var c: [m.len]u8 = undefined; | ||
| 383 | var m2: [m.len]u8 = undefined; | ||
| 384 | var tag: [AEGIS128L.tag_length]u8 = undefined; | ||
| 385 | |||
| 386 | AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 387 | try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 388 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 389 | |||
| 390 | htest.assertEqual("83cc600dc4e3e7e62d4055826174f149", &tag); | ||
| 391 | } | ||
| 392 | |||
| 393 | test "AEGIS256 test vector 1" { | ||
| 394 | const key: [AEGIS256.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 30; | ||
| 395 | const nonce: [AEGIS256.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 29; | ||
| 396 | const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; | ||
| 397 | const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; | ||
| 398 | var c: [m.len]u8 = undefined; | ||
| 399 | var m2: [m.len]u8 = undefined; | ||
| 400 | var tag: [AEGIS256.tag_length]u8 = undefined; | ||
| 401 | |||
| 402 | AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 403 | try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 404 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 405 | |||
| 406 | htest.assertEqual("f373079ed84b2709faee373584585d60accd191db310ef5d8b11833df9dec711", &c); | ||
| 407 | htest.assertEqual("8d86f91ee606e9ff26a01b64ccbdd91d", &tag); | ||
| 408 | |||
| 409 | c[0] +%= 1; | ||
| 410 | testing.expectError(error.AuthenticationFailed, AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key)); | ||
| 411 | c[0] -%= 1; | ||
| 412 | tag[0] +%= 1; | ||
| 413 | testing.expectError(error.AuthenticationFailed, AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key)); | ||
| 414 | } | ||
| 415 | |||
| 416 | test "AEGIS256 test vector 2" { | ||
| 417 | const key: [AEGIS256.key_length]u8 = [_]u8{0x00} ** 32; | ||
| 418 | const nonce: [AEGIS256.nonce_length]u8 = [_]u8{0x00} ** 32; | ||
| 419 | const ad = [_]u8{}; | ||
| 420 | const m = [_]u8{0x00} ** 16; | ||
| 421 | var c: [m.len]u8 = undefined; | ||
| 422 | var m2: [m.len]u8 = undefined; | ||
| 423 | var tag: [AEGIS256.tag_length]u8 = undefined; | ||
| 424 | |||
| 425 | AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 426 | try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 427 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 428 | |||
| 429 | htest.assertEqual("b98f03a947807713d75a4fff9fc277a6", &c); | ||
| 430 | htest.assertEqual("478f3b50dc478ef7d5cf2d0f7cc13180", &tag); | ||
| 431 | } | ||
| 432 | |||
| 433 | test "AEGIS256 test vector 3" { | ||
| 434 | const key: [AEGIS256.key_length]u8 = [_]u8{0x00} ** 32; | ||
| 435 | const nonce: [AEGIS256.nonce_length]u8 = [_]u8{0x00} ** 32; | ||
| 436 | const ad = [_]u8{}; | ||
| 437 | const m = [_]u8{}; | ||
| 438 | var c: [m.len]u8 = undefined; | ||
| 439 | var m2: [m.len]u8 = undefined; | ||
| 440 | var tag: [AEGIS256.tag_length]u8 = undefined; | ||
| 441 | |||
| 442 | AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key); | ||
| 443 | try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key); | ||
| 444 | testing.expectEqualSlices(u8, &m, &m2); | ||
| 445 | |||
| 446 | htest.assertEqual("f7a0878f68bd083e8065354071fc27c3", &tag); | ||
| 447 | } | ||
lib/std/crypto/aes/aesni.zig+18-8| ... | @@ -84,11 +84,21 @@ pub const Block = struct { | ... | @@ -84,11 +84,21 @@ pub const Block = struct { |
| 84 | }; | 84 | }; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | /// XOR the content of two blocks. | 87 | /// Apply the bitwise XOR operation to the content of two blocks. |
| 88 | pub inline fn xor(block1: Block, block2: Block) Block { | 88 | pub inline fn xorBlocks(block1: Block, block2: Block) Block { |
| 89 | return Block{ .repr = block1.repr ^ block2.repr }; | 89 | return Block{ .repr = block1.repr ^ block2.repr }; |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | /// Apply the bitwise AND operation to the content of two blocks. | ||
| 93 | pub inline fn andBlocks(block1: Block, block2: Block) Block { | ||
| 94 | return Block{ .repr = block1.repr & block2.repr }; | ||
| 95 | } | ||
| 96 | |||
| 97 | /// Apply the bitwise OR operation to the content of two blocks. | ||
| 98 | pub inline fn orBlocks(block1: Block, block2: Block) Block { | ||
| 99 | return Block{ .repr = block1.repr | block2.repr }; | ||
| 100 | } | ||
| 101 | |||
| 92 | /// Perform operations on multiple blocks in parallel. | 102 | /// Perform operations on multiple blocks in parallel. |
| 93 | pub const parallel = struct { | 103 | pub const parallel = struct { |
| 94 | /// The recommended number of AES encryption/decryption to perform in parallel for the chosen implementation. | 104 | /// The recommended number of AES encryption/decryption to perform in parallel for the chosen implementation. |
| ... | @@ -261,7 +271,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -261,7 +271,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 261 | /// Encrypt a single block. | 271 | /// Encrypt a single block. |
| 262 | pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { | 272 | pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { |
| 263 | const round_keys = ctx.key_schedule.round_keys; | 273 | const round_keys = ctx.key_schedule.round_keys; |
| 264 | var t = Block.fromBytes(src).xor(round_keys[0]); | 274 | var t = Block.fromBytes(src).xorBlocks(round_keys[0]); |
| 265 | comptime var i = 1; | 275 | comptime var i = 1; |
| 266 | inline while (i < rounds) : (i += 1) { | 276 | inline while (i < rounds) : (i += 1) { |
| 267 | t = t.encrypt(round_keys[i]); | 277 | t = t.encrypt(round_keys[i]); |
| ... | @@ -273,7 +283,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -273,7 +283,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 273 | /// Encrypt+XOR a single block. | 283 | /// Encrypt+XOR a single block. |
| 274 | pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void { | 284 | pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void { |
| 275 | const round_keys = ctx.key_schedule.round_keys; | 285 | const round_keys = ctx.key_schedule.round_keys; |
| 276 | var t = Block.fromBytes(&counter).xor(round_keys[0]); | 286 | var t = Block.fromBytes(&counter).xorBlocks(round_keys[0]); |
| 277 | comptime var i = 1; | 287 | comptime var i = 1; |
| 278 | inline while (i < rounds) : (i += 1) { | 288 | inline while (i < rounds) : (i += 1) { |
| 279 | t = t.encrypt(round_keys[i]); | 289 | t = t.encrypt(round_keys[i]); |
| ... | @@ -288,7 +298,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -288,7 +298,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 288 | var ts: [count]Block = undefined; | 298 | var ts: [count]Block = undefined; |
| 289 | comptime var j = 0; | 299 | comptime var j = 0; |
| 290 | inline while (j < count) : (j += 1) { | 300 | inline while (j < count) : (j += 1) { |
| 291 | ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xor(round_keys[0]); | 301 | ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xorBlocks(round_keys[0]); |
| 292 | } | 302 | } |
| 293 | comptime var i = 1; | 303 | comptime var i = 1; |
| 294 | inline while (i < rounds) : (i += 1) { | 304 | inline while (i < rounds) : (i += 1) { |
| ... | @@ -310,7 +320,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -310,7 +320,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 310 | var ts: [count]Block = undefined; | 320 | var ts: [count]Block = undefined; |
| 311 | comptime var j = 0; | 321 | comptime var j = 0; |
| 312 | inline while (j < count) : (j += 1) { | 322 | inline while (j < count) : (j += 1) { |
| 313 | ts[j] = Block.fromBytes(counters[j * 16 .. j * 16 + 16][0..16]).xor(round_keys[0]); | 323 | ts[j] = Block.fromBytes(counters[j * 16 .. j * 16 + 16][0..16]).xorBlocks(round_keys[0]); |
| 314 | } | 324 | } |
| 315 | comptime var i = 1; | 325 | comptime var i = 1; |
| 316 | inline while (i < rounds) : (i += 1) { | 326 | inline while (i < rounds) : (i += 1) { |
| ... | @@ -352,7 +362,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { | ... | @@ -352,7 +362,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { |
| 352 | /// Decrypt a single block. | 362 | /// Decrypt a single block. |
| 353 | pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { | 363 | pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { |
| 354 | const inv_round_keys = ctx.key_schedule.round_keys; | 364 | const inv_round_keys = ctx.key_schedule.round_keys; |
| 355 | var t = Block.fromBytes(src).xor(inv_round_keys[0]); | 365 | var t = Block.fromBytes(src).xorBlocks(inv_round_keys[0]); |
| 356 | comptime var i = 1; | 366 | comptime var i = 1; |
| 357 | inline while (i < rounds) : (i += 1) { | 367 | inline while (i < rounds) : (i += 1) { |
| 358 | t = t.decrypt(inv_round_keys[i]); | 368 | t = t.decrypt(inv_round_keys[i]); |
| ... | @@ -367,7 +377,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { | ... | @@ -367,7 +377,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { |
| 367 | var ts: [count]Block = undefined; | 377 | var ts: [count]Block = undefined; |
| 368 | comptime var j = 0; | 378 | comptime var j = 0; |
| 369 | inline while (j < count) : (j += 1) { | 379 | inline while (j < count) : (j += 1) { |
| 370 | ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xor(inv_round_keys[0]); | 380 | ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xorBlocks(inv_round_keys[0]); |
| 371 | } | 381 | } |
| 372 | comptime var i = 1; | 382 | comptime var i = 1; |
| 373 | inline while (i < rounds) : (i += 1) { | 383 | inline while (i < rounds) : (i += 1) { |
lib/std/crypto/aes/soft.zig+25-5| ... | @@ -125,8 +125,8 @@ pub const Block = struct { | ... | @@ -125,8 +125,8 @@ pub const Block = struct { |
| 125 | return Block{ .repr = BlockVec{ s0, s1, s2, s3 } }; | 125 | return Block{ .repr = BlockVec{ s0, s1, s2, s3 } }; |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | /// XOR the content of two blocks. | 128 | /// Apply the bitwise XOR operation to the content of two blocks. |
| 129 | pub inline fn xor(block1: Block, block2: Block) Block { | 129 | pub inline fn xorBlocks(block1: Block, block2: Block) Block { |
| 130 | var x: BlockVec = undefined; | 130 | var x: BlockVec = undefined; |
| 131 | comptime var i = 0; | 131 | comptime var i = 0; |
| 132 | inline while (i < 4) : (i += 1) { | 132 | inline while (i < 4) : (i += 1) { |
| ... | @@ -135,6 +135,26 @@ pub const Block = struct { | ... | @@ -135,6 +135,26 @@ pub const Block = struct { |
| 135 | return Block{ .repr = x }; | 135 | return Block{ .repr = x }; |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | /// Apply the bitwise AND operation to the content of two blocks. | ||
| 139 | pub inline fn andBlocks(block1: Block, block2: Block) Block { | ||
| 140 | var x: BlockVec = undefined; | ||
| 141 | comptime var i = 0; | ||
| 142 | inline while (i < 4) : (i += 1) { | ||
| 143 | x[i] = block1.repr[i] & block2.repr[i]; | ||
| 144 | } | ||
| 145 | return Block{ .repr = x }; | ||
| 146 | } | ||
| 147 | |||
| 148 | /// Apply the bitwise OR operation to the content of two blocks. | ||
| 149 | pub inline fn orBlocks(block1: Block, block2: Block) Block { | ||
| 150 | var x: BlockVec = undefined; | ||
| 151 | comptime var i = 0; | ||
| 152 | inline while (i < 4) : (i += 1) { | ||
| 153 | x[i] = block1.repr[i] | block2.repr[i]; | ||
| 154 | } | ||
| 155 | return Block{ .repr = x }; | ||
| 156 | } | ||
| 157 | |||
| 138 | /// Perform operations on multiple blocks in parallel. | 158 | /// Perform operations on multiple blocks in parallel. |
| 139 | pub const parallel = struct { | 159 | pub const parallel = struct { |
| 140 | /// The recommended number of AES encryption/decryption to perform in parallel for the chosen implementation. | 160 | /// The recommended number of AES encryption/decryption to perform in parallel for the chosen implementation. |
| ... | @@ -283,7 +303,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -283,7 +303,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 283 | /// Encrypt a single block. | 303 | /// Encrypt a single block. |
| 284 | pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { | 304 | pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { |
| 285 | const round_keys = ctx.key_schedule.round_keys; | 305 | const round_keys = ctx.key_schedule.round_keys; |
| 286 | var t = Block.fromBytes(src).xor(round_keys[0]); | 306 | var t = Block.fromBytes(src).xorBlocks(round_keys[0]); |
| 287 | comptime var i = 1; | 307 | comptime var i = 1; |
| 288 | inline while (i < rounds) : (i += 1) { | 308 | inline while (i < rounds) : (i += 1) { |
| 289 | t = t.encrypt(round_keys[i]); | 309 | t = t.encrypt(round_keys[i]); |
| ... | @@ -295,7 +315,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { | ... | @@ -295,7 +315,7 @@ pub fn AESEncryptCtx(comptime AES: type) type { |
| 295 | /// Encrypt+XOR a single block. | 315 | /// Encrypt+XOR a single block. |
| 296 | pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void { | 316 | pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void { |
| 297 | const round_keys = ctx.key_schedule.round_keys; | 317 | const round_keys = ctx.key_schedule.round_keys; |
| 298 | var t = Block.fromBytes(&counter).xor(round_keys[0]); | 318 | var t = Block.fromBytes(&counter).xorBlocks(round_keys[0]); |
| 299 | comptime var i = 1; | 319 | comptime var i = 1; |
| 300 | inline while (i < rounds) : (i += 1) { | 320 | inline while (i < rounds) : (i += 1) { |
| 301 | t = t.encrypt(round_keys[i]); | 321 | t = t.encrypt(round_keys[i]); |
| ... | @@ -349,7 +369,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { | ... | @@ -349,7 +369,7 @@ pub fn AESDecryptCtx(comptime AES: type) type { |
| 349 | /// Decrypt a single block. | 369 | /// Decrypt a single block. |
| 350 | pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { | 370 | pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void { |
| 351 | const inv_round_keys = ctx.key_schedule.round_keys; | 371 | const inv_round_keys = ctx.key_schedule.round_keys; |
| 352 | var t = Block.fromBytes(src).xor(inv_round_keys[0]); | 372 | var t = Block.fromBytes(src).xorBlocks(inv_round_keys[0]); |
| 353 | comptime var i = 1; | 373 | comptime var i = 1; |
| 354 | inline while (i < rounds) : (i += 1) { | 374 | inline while (i < rounds) : (i += 1) { |
| 355 | t = t.decrypt(inv_round_keys[i]); | 375 | t = t.decrypt(inv_round_keys[i]); |
lib/std/crypto/benchmark.zig+3-1| ... | @@ -149,6 +149,8 @@ const aeads = [_]Crypto{ | ... | @@ -149,6 +149,8 @@ const aeads = [_]Crypto{ |
| 149 | Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" }, | 149 | Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" }, |
| 150 | Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" }, | 150 | Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" }, |
| 151 | Crypto{ .ty = crypto.aead.Gimli, .name = "gimli-aead" }, | 151 | Crypto{ .ty = crypto.aead.Gimli, .name = "gimli-aead" }, |
| 152 | Crypto{ .ty = crypto.aead.AEGIS128L, .name = "aegis-128l" }, | ||
| 153 | Crypto{ .ty = crypto.aead.AEGIS256, .name = "aegis-256" }, | ||
| 152 | }; | 154 | }; |
| 153 | 155 | ||
| 154 | pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 { | 156 | pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 { |
| ... | @@ -168,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 | ... | @@ -168,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 |
| 168 | const start = timer.lap(); | 170 | const start = timer.lap(); |
| 169 | while (offset < bytes) : (offset += in.len) { | 171 | while (offset < bytes) : (offset += in.len) { |
| 170 | Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key); | 172 | Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key); |
| 171 | Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable; | 173 | try Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key); |
| 172 | } | 174 | } |
| 173 | mem.doNotOptimizeAway(&in); | 175 | mem.doNotOptimizeAway(&in); |
| 174 | const end = timer.read(); | 176 | const end = timer.read(); |
lib/std/crypto/gimli.zig+44-12| ... | @@ -38,7 +38,35 @@ pub const State = struct { | ... | @@ -38,7 +38,35 @@ pub const State = struct { |
| 38 | return mem.sliceAsBytes(self.data[0..]); | 38 | return mem.sliceAsBytes(self.data[0..]); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | pub fn permute(self: *Self) void { | 41 | fn permute_unrolled(self: *Self) void { |
| 42 | const state = &self.data; | ||
| 43 | comptime var round = @as(u32, 24); | ||
| 44 | inline while (round > 0) : (round -= 1) { | ||
| 45 | var column = @as(usize, 0); | ||
| 46 | while (column < 4) : (column += 1) { | ||
| 47 | const x = math.rotl(u32, state[column], 24); | ||
| 48 | const y = math.rotl(u32, state[4 + column], 9); | ||
| 49 | const z = state[8 + column]; | ||
| 50 | state[8 + column] = ((x ^ (z << 1)) ^ ((y & z) << 2)); | ||
| 51 | state[4 + column] = ((y ^ x) ^ ((x | z) << 1)); | ||
| 52 | state[column] = ((z ^ y) ^ ((x & y) << 3)); | ||
| 53 | } | ||
| 54 | switch (round & 3) { | ||
| 55 | 0 => { | ||
| 56 | mem.swap(u32, &state[0], &state[1]); | ||
| 57 | mem.swap(u32, &state[2], &state[3]); | ||
| 58 | state[0] ^= round | 0x9e377900; | ||
| 59 | }, | ||
| 60 | 2 => { | ||
| 61 | mem.swap(u32, &state[0], &state[2]); | ||
| 62 | mem.swap(u32, &state[1], &state[3]); | ||
| 63 | }, | ||
| 64 | else => {}, | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | fn permute_small(self: *Self) void { | ||
| 42 | const state = &self.data; | 70 | const state = &self.data; |
| 43 | var round = @as(u32, 24); | 71 | var round = @as(u32, 24); |
| 44 | while (round > 0) : (round -= 1) { | 72 | while (round > 0) : (round -= 1) { |
| ... | @@ -66,6 +94,8 @@ pub const State = struct { | ... | @@ -66,6 +94,8 @@ pub const State = struct { |
| 66 | } | 94 | } |
| 67 | } | 95 | } |
| 68 | 96 | ||
| 97 | pub const permute = if (std.builtin.mode == .ReleaseSmall) permute_small else permute_unrolled; | ||
| 98 | |||
| 69 | pub fn squeeze(self: *Self, out: []u8) void { | 99 | pub fn squeeze(self: *Self, out: []u8) void { |
| 70 | var i = @as(usize, 0); | 100 | var i = @as(usize, 0); |
| 71 | while (i + RATE <= out.len) : (i += RATE) { | 101 | while (i + RATE <= out.len) : (i += RATE) { |
| ... | @@ -249,15 +279,15 @@ pub const Aead = struct { | ... | @@ -249,15 +279,15 @@ pub const Aead = struct { |
| 249 | in = in[State.RATE..]; | 279 | in = in[State.RATE..]; |
| 250 | out = out[State.RATE..]; | 280 | out = out[State.RATE..]; |
| 251 | }) { | 281 | }) { |
| 252 | for (buf[0..State.RATE]) |*p, i| { | 282 | for (in[0..State.RATE]) |v, i| { |
| 253 | p.* ^= in[i]; | 283 | buf[i] ^= v; |
| 254 | out[i] = p.*; | ||
| 255 | } | 284 | } |
| 285 | mem.copy(u8, out[0..State.RATE], buf[0..State.RATE]); | ||
| 256 | state.permute(); | 286 | state.permute(); |
| 257 | } | 287 | } |
| 258 | for (buf[0..in.len]) |*p, i| { | 288 | for (in[0..]) |v, i| { |
| 259 | p.* ^= in[i]; | 289 | buf[i] ^= v; |
| 260 | out[i] = p.*; | 290 | out[i] = buf[i]; |
| 261 | } | 291 | } |
| 262 | 292 | ||
| 263 | // XOR 1 into the next byte of the state | 293 | // XOR 1 into the next byte of the state |
| ... | @@ -291,15 +321,17 @@ pub const Aead = struct { | ... | @@ -291,15 +321,17 @@ pub const Aead = struct { |
| 291 | in = in[State.RATE..]; | 321 | in = in[State.RATE..]; |
| 292 | out = out[State.RATE..]; | 322 | out = out[State.RATE..]; |
| 293 | }) { | 323 | }) { |
| 294 | for (buf[0..State.RATE]) |*p, i| { | 324 | const d = in[0..State.RATE].*; |
| 295 | out[i] = p.* ^ in[i]; | 325 | for (d) |v, i| { |
| 296 | p.* = in[i]; | 326 | out[i] = buf[i] ^ v; |
| 297 | } | 327 | } |
| 328 | mem.copy(u8, buf[0..State.RATE], d[0..State.RATE]); | ||
| 298 | state.permute(); | 329 | state.permute(); |
| 299 | } | 330 | } |
| 300 | for (buf[0..in.len]) |*p, i| { | 331 | for (buf[0..in.len]) |*p, i| { |
| 301 | out[i] = p.* ^ in[i]; | 332 | const d = in[i]; |
| 302 | p.* = in[i]; | 333 | out[i] = p.* ^ d; |
| 334 | p.* = d; | ||
| 303 | } | 335 | } |
| 304 | 336 | ||
| 305 | // XOR 1 into the next byte of the state | 337 | // XOR 1 into the next byte of the state |
lib/std/event/future.zig+1-1| ... | @@ -95,7 +95,7 @@ test "std.event.Future" { | ... | @@ -95,7 +95,7 @@ test "std.event.Future" { |
| 95 | // TODO provide a way to run tests in evented I/O mode | 95 | // TODO provide a way to run tests in evented I/O mode |
| 96 | if (!std.io.is_async) return error.SkipZigTest; | 96 | if (!std.io.is_async) return error.SkipZigTest; |
| 97 | 97 | ||
| 98 | const handle = async testFuture(); | 98 | testFuture(); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | fn testFuture() void { | 101 | fn testFuture() void { |
lib/std/event/lock.zig+11-7| ... | @@ -27,20 +27,24 @@ pub const Lock = struct { | ... | @@ -27,20 +27,24 @@ pub const Lock = struct { |
| 27 | 27 | ||
| 28 | const Waiter = struct { | 28 | const Waiter = struct { |
| 29 | // forced Waiter alignment to ensure it doesn't clash with LOCKED | 29 | // forced Waiter alignment to ensure it doesn't clash with LOCKED |
| 30 | next: ?*Waiter align(2), | 30 | next: ?*Waiter align(2), |
| 31 | tail: *Waiter, | 31 | tail: *Waiter, |
| 32 | node: Loop.NextTickNode, | 32 | node: Loop.NextTickNode, |
| 33 | }; | 33 | }; |
| 34 | 34 | ||
| 35 | pub fn initLocked() Lock { | ||
| 36 | return Lock{ .head = LOCKED }; | ||
| 37 | } | ||
| 38 | |||
| 35 | pub fn acquire(self: *Lock) Held { | 39 | pub fn acquire(self: *Lock) Held { |
| 36 | const held = self.mutex.acquire(); | 40 | const held = self.mutex.acquire(); |
| 37 | 41 | ||
| 38 | // self.head transitions from multiple stages depending on the value: | 42 | // self.head transitions from multiple stages depending on the value: |
| 39 | // UNLOCKED -> LOCKED: | 43 | // UNLOCKED -> LOCKED: |
| 40 | // acquire Lock ownership when theres no waiters | 44 | // acquire Lock ownership when theres no waiters |
| 41 | // LOCKED -> <Waiter head ptr>: | 45 | // LOCKED -> <Waiter head ptr>: |
| 42 | // Lock is already owned, enqueue first Waiter | 46 | // Lock is already owned, enqueue first Waiter |
| 43 | // <head ptr> -> <head ptr>: | 47 | // <head ptr> -> <head ptr>: |
| 44 | // Lock is owned with pending waiters. Push our waiter to the queue. | 48 | // Lock is owned with pending waiters. Push our waiter to the queue. |
| 45 | 49 | ||
| 46 | if (self.head == UNLOCKED) { | 50 | if (self.head == UNLOCKED) { |
| ... | @@ -51,7 +55,7 @@ pub const Lock = struct { | ... | @@ -51,7 +55,7 @@ pub const Lock = struct { |
| 51 | 55 | ||
| 52 | var waiter: Waiter = undefined; | 56 | var waiter: Waiter = undefined; |
| 53 | waiter.next = null; | 57 | waiter.next = null; |
| 54 | waiter.tail = &waiter; | 58 | waiter.tail = &waiter; |
| 55 | 59 | ||
| 56 | const head = switch (self.head) { | 60 | const head = switch (self.head) { |
| 57 | UNLOCKED => unreachable, | 61 | UNLOCKED => unreachable, |
| ... | @@ -79,15 +83,15 @@ pub const Lock = struct { | ... | @@ -79,15 +83,15 @@ pub const Lock = struct { |
| 79 | } | 83 | } |
| 80 | 84 | ||
| 81 | pub const Held = struct { | 85 | pub const Held = struct { |
| 82 | lock: *Lock, | 86 | lock: *Lock, |
| 83 | 87 | ||
| 84 | pub fn release(self: Held) void { | 88 | pub fn release(self: Held) void { |
| 85 | const waiter = blk: { | 89 | const waiter = blk: { |
| 86 | const held = self.lock.mutex.acquire(); | 90 | const held = self.lock.mutex.acquire(); |
| 87 | defer held.release(); | 91 | defer held.release(); |
| 88 | 92 | ||
| 89 | // self.head goes through the reverse transition from acquire(): | 93 | // self.head goes through the reverse transition from acquire(): |
| 90 | // <head ptr> -> <new head ptr>: | 94 | // <head ptr> -> <new head ptr>: |
| 91 | // pop a waiter from the queue to give Lock ownership when theres still others pending | 95 | // pop a waiter from the queue to give Lock ownership when theres still others pending |
| 92 | // <head ptr> -> LOCKED: | 96 | // <head ptr> -> LOCKED: |
| 93 | // pop the laster waiter from the queue, while also giving it lock ownership when awaken | 97 | // pop the laster waiter from the queue, while also giving it lock ownership when awaken |
lib/std/meta.zig+110-1| ... | @@ -807,7 +807,7 @@ pub fn sizeof(target: anytype) usize { | ... | @@ -807,7 +807,7 @@ pub fn sizeof(target: anytype) usize { |
| 807 | // TODO to get the correct result we have to translate | 807 | // TODO to get the correct result we have to translate |
| 808 | // `1073741824 * 4` as `int(1073741824) *% int(4)` since | 808 | // `1073741824 * 4` as `int(1073741824) *% int(4)` since |
| 809 | // sizeof(1073741824 * 4) != sizeof(4294967296). | 809 | // sizeof(1073741824 * 4) != sizeof(4294967296). |
| 810 | 810 | ||
| 811 | // TODO test if target fits in int, long or long long | 811 | // TODO test if target fits in int, long or long long |
| 812 | return @sizeOf(c_int); | 812 | return @sizeOf(c_int); |
| 813 | }, | 813 | }, |
| ... | @@ -826,3 +826,112 @@ test "sizeof" { | ... | @@ -826,3 +826,112 @@ test "sizeof" { |
| 826 | testing.expect(sizeof(E.One) == @sizeOf(c_int)); | 826 | testing.expect(sizeof(E.One) == @sizeOf(c_int)); |
| 827 | testing.expect(sizeof(S) == 4); | 827 | testing.expect(sizeof(S) == 4); |
| 828 | } | 828 | } |
| 829 | |||
| 830 | /// For a given function type, returns a tuple type which fields will | ||
| 831 | /// correspond to the argument types. | ||
| 832 | /// | ||
| 833 | /// Examples: | ||
| 834 | /// - `ArgsTuple(fn() void)` ⇒ `tuple { }` | ||
| 835 | /// - `ArgsTuple(fn(a: u32) u32)` ⇒ `tuple { u32 }` | ||
| 836 | /// - `ArgsTuple(fn(a: u32, b: f16) noreturn)` ⇒ `tuple { u32, f16 }` | ||
| 837 | pub fn ArgsTuple(comptime Function: type) type { | ||
| 838 | const info = @typeInfo(Function); | ||
| 839 | if (info != .Fn) | ||
| 840 | @compileError("ArgsTuple expects a function type"); | ||
| 841 | |||
| 842 | const function_info = info.Fn; | ||
| 843 | if (function_info.is_generic) | ||
| 844 | @compileError("Cannot create ArgsTuple for generic function"); | ||
| 845 | if (function_info.is_var_args) | ||
| 846 | @compileError("Cannot create ArgsTuple for variadic function"); | ||
| 847 | |||
| 848 | var argument_field_list: [function_info.args.len]std.builtin.TypeInfo.StructField = undefined; | ||
| 849 | inline for (function_info.args) |arg, i| { | ||
| 850 | @setEvalBranchQuota(10_000); | ||
| 851 | var num_buf: [128]u8 = undefined; | ||
| 852 | argument_field_list[i] = std.builtin.TypeInfo.StructField{ | ||
| 853 | .name = std.fmt.bufPrint(&num_buf, "{d}", .{i}) catch unreachable, | ||
| 854 | .field_type = arg.arg_type.?, | ||
| 855 | .default_value = @as(?(arg.arg_type.?), null), | ||
| 856 | .is_comptime = false, | ||
| 857 | }; | ||
| 858 | } | ||
| 859 | |||
| 860 | return @Type(std.builtin.TypeInfo{ | ||
| 861 | .Struct = std.builtin.TypeInfo.Struct{ | ||
| 862 | .is_tuple = true, | ||
| 863 | .layout = .Auto, | ||
| 864 | .decls = &[_]std.builtin.TypeInfo.Declaration{}, | ||
| 865 | .fields = &argument_field_list, | ||
| 866 | }, | ||
| 867 | }); | ||
| 868 | } | ||
| 869 | |||
| 870 | /// For a given anonymous list of types, returns a new tuple type | ||
| 871 | /// with those types as fields. | ||
| 872 | /// | ||
| 873 | /// Examples: | ||
| 874 | /// - `Tuple(&[_]type {})` ⇒ `tuple { }` | ||
| 875 | /// - `Tuple(&[_]type {f32})` ⇒ `tuple { f32 }` | ||
| 876 | /// - `Tuple(&[_]type {f32,u32})` ⇒ `tuple { f32, u32 }` | ||
| 877 | pub fn Tuple(comptime types: []const type) type { | ||
| 878 | var tuple_fields: [types.len]std.builtin.TypeInfo.StructField = undefined; | ||
| 879 | inline for (types) |T, i| { | ||
| 880 | @setEvalBranchQuota(10_000); | ||
| 881 | var num_buf: [128]u8 = undefined; | ||
| 882 | tuple_fields[i] = std.builtin.TypeInfo.StructField{ | ||
| 883 | .name = std.fmt.bufPrint(&num_buf, "{d}", .{i}) catch unreachable, | ||
| 884 | .field_type = T, | ||
| 885 | .default_value = @as(?T, null), | ||
| 886 | .is_comptime = false, | ||
| 887 | }; | ||
| 888 | } | ||
| 889 | |||
| 890 | return @Type(std.builtin.TypeInfo{ | ||
| 891 | .Struct = std.builtin.TypeInfo.Struct{ | ||
| 892 | .is_tuple = true, | ||
| 893 | .layout = .Auto, | ||
| 894 | .decls = &[_]std.builtin.TypeInfo.Declaration{}, | ||
| 895 | .fields = &tuple_fields, | ||
| 896 | }, | ||
| 897 | }); | ||
| 898 | } | ||
| 899 | |||
| 900 | const TupleTester = struct { | ||
| 901 | fn assertTypeEqual(comptime Expected: type, comptime Actual: type) void { | ||
| 902 | if (Expected != Actual) | ||
| 903 | @compileError("Expected type " ++ @typeName(Expected) ++ ", but got type " ++ @typeName(Actual)); | ||
| 904 | } | ||
| 905 | |||
| 906 | fn assertTuple(comptime expected: anytype, comptime Actual: type) void { | ||
| 907 | const info = @typeInfo(Actual); | ||
| 908 | if (info != .Struct) | ||
| 909 | @compileError("Expected struct type"); | ||
| 910 | if (!info.Struct.is_tuple) | ||
| 911 | @compileError("Struct type must be a tuple type"); | ||
| 912 | |||
| 913 | const fields_list = std.meta.fields(Actual); | ||
| 914 | if (expected.len != fields_list.len) | ||
| 915 | @compileError("Argument count mismatch"); | ||
| 916 | |||
| 917 | inline for (fields_list) |fld, i| { | ||
| 918 | if (expected[i] != fld.field_type) { | ||
| 919 | @compileError("Field " ++ fld.name ++ " expected to be type " ++ @typeName(expected[i]) ++ ", but was type " ++ @typeName(fld.field_type)); | ||
| 920 | } | ||
| 921 | } | ||
| 922 | } | ||
| 923 | }; | ||
| 924 | |||
| 925 | test "ArgsTuple" { | ||
| 926 | TupleTester.assertTuple(.{}, ArgsTuple(fn () void)); | ||
| 927 | TupleTester.assertTuple(.{u32}, ArgsTuple(fn (a: u32) []const u8)); | ||
| 928 | TupleTester.assertTuple(.{ u32, f16 }, ArgsTuple(fn (a: u32, b: f16) noreturn)); | ||
| 929 | TupleTester.assertTuple(.{ u32, f16, []const u8 }, ArgsTuple(fn (a: u32, b: f16, c: []const u8) noreturn)); | ||
| 930 | } | ||
| 931 | |||
| 932 | test "Tuple" { | ||
| 933 | TupleTester.assertTuple(.{}, Tuple(&[_]type{})); | ||
| 934 | TupleTester.assertTuple(.{u32}, Tuple(&[_]type{u32})); | ||
| 935 | TupleTester.assertTuple(.{ u32, f16 }, Tuple(&[_]type{ u32, f16 })); | ||
| 936 | TupleTester.assertTuple(.{ u32, f16, []const u8 }, Tuple(&[_]type{ u32, f16, []const u8 })); | ||
| 937 | } |
src/stage1/ir.cpp+14-10| ... | @@ -16715,16 +16715,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i | ... | @@ -16715,16 +16715,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i |
| 16715 | } | 16715 | } |
| 16716 | ZigType *dest_float_type = nullptr; | 16716 | ZigType *dest_float_type = nullptr; |
| 16717 | uint32_t op1_bits; | 16717 | uint32_t op1_bits; |
| 16718 | if (instr_is_comptime(op1)) { | 16718 | if (instr_is_comptime(op1) && result_type->id != ZigTypeIdVector) { |
| 16719 | ZigValue *op1_val = ir_resolve_const(ira, op1, UndefOk); | 16719 | ZigValue *op1_val = ir_resolve_const(ira, op1, UndefOk); |
| 16720 | if (op1_val == nullptr) | 16720 | if (op1_val == nullptr) |
| 16721 | return ira->codegen->invalid_inst_gen; | 16721 | return ira->codegen->invalid_inst_gen; |
| 16722 | if (op1_val->special == ConstValSpecialUndef) | 16722 | if (op1_val->special == ConstValSpecialUndef) |
| 16723 | return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool); | 16723 | return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool); |
| 16724 | if (result_type->id == ZigTypeIdVector) { | ||
| 16725 | ir_add_error(ira, &op1->base, buf_sprintf("compiler bug: TODO: support comptime vector here")); | ||
| 16726 | return ira->codegen->invalid_inst_gen; | ||
| 16727 | } | ||
| 16728 | bool is_unsigned; | 16724 | bool is_unsigned; |
| 16729 | if (op1_is_float) { | 16725 | if (op1_is_float) { |
| 16730 | BigInt bigint = {}; | 16726 | BigInt bigint = {}; |
| ... | @@ -16750,6 +16746,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i | ... | @@ -16750,6 +16746,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i |
| 16750 | op1_bits += 1; | 16746 | op1_bits += 1; |
| 16751 | } | 16747 | } |
| 16752 | } else if (op1_is_float) { | 16748 | } else if (op1_is_float) { |
| 16749 | ir_assert(op1_scalar_type->id == ZigTypeIdFloat, source_instr); | ||
| 16753 | dest_float_type = op1_scalar_type; | 16750 | dest_float_type = op1_scalar_type; |
| 16754 | } else { | 16751 | } else { |
| 16755 | ir_assert(op1_scalar_type->id == ZigTypeIdInt, source_instr); | 16752 | ir_assert(op1_scalar_type->id == ZigTypeIdInt, source_instr); |
| ... | @@ -16759,16 +16756,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i | ... | @@ -16759,16 +16756,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i |
| 16759 | } | 16756 | } |
| 16760 | } | 16757 | } |
| 16761 | uint32_t op2_bits; | 16758 | uint32_t op2_bits; |
| 16762 | if (instr_is_comptime(op2)) { | 16759 | if (instr_is_comptime(op2) && result_type->id != ZigTypeIdVector) { |
| 16763 | ZigValue *op2_val = ir_resolve_const(ira, op2, UndefOk); | 16760 | ZigValue *op2_val = ir_resolve_const(ira, op2, UndefOk); |
| 16764 | if (op2_val == nullptr) | 16761 | if (op2_val == nullptr) |
| 16765 | return ira->codegen->invalid_inst_gen; | 16762 | return ira->codegen->invalid_inst_gen; |
| 16766 | if (op2_val->special == ConstValSpecialUndef) | 16763 | if (op2_val->special == ConstValSpecialUndef) |
| 16767 | return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool); | 16764 | return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool); |
| 16768 | if (result_type->id == ZigTypeIdVector) { | ||
| 16769 | ir_add_error(ira, &op2->base, buf_sprintf("compiler bug: TODO: support comptime vector here")); | ||
| 16770 | return ira->codegen->invalid_inst_gen; | ||
| 16771 | } | ||
| 16772 | bool is_unsigned; | 16765 | bool is_unsigned; |
| 16773 | if (op2_is_float) { | 16766 | if (op2_is_float) { |
| 16774 | BigInt bigint = {}; | 16767 | BigInt bigint = {}; |
| ... | @@ -16794,6 +16787,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i | ... | @@ -16794,6 +16787,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i |
| 16794 | op2_bits += 1; | 16787 | op2_bits += 1; |
| 16795 | } | 16788 | } |
| 16796 | } else if (op2_is_float) { | 16789 | } else if (op2_is_float) { |
| 16790 | ir_assert(op2_scalar_type->id == ZigTypeIdFloat, source_instr); | ||
| 16797 | dest_float_type = op2_scalar_type; | 16791 | dest_float_type = op2_scalar_type; |
| 16798 | } else { | 16792 | } else { |
| 16799 | ir_assert(op2_scalar_type->id == ZigTypeIdInt, source_instr); | 16793 | ir_assert(op2_scalar_type->id == ZigTypeIdInt, source_instr); |
| ... | @@ -21934,7 +21928,17 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP | ... | @@ -21934,7 +21928,17 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP |
| 21934 | return ira->codegen->invalid_inst_gen; | 21928 | return ira->codegen->invalid_inst_gen; |
| 21935 | } | 21929 | } |
| 21936 | safety_check_on = false; | 21930 | safety_check_on = false; |
| 21931 | } else if (array_type->id == ZigTypeIdVector) { | ||
| 21932 | uint64_t vector_len = array_type->data.vector.len; | ||
| 21933 | if (index >= vector_len) { | ||
| 21934 | ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node, | ||
| 21935 | buf_sprintf("index %" ZIG_PRI_u64 " outside vector of size %" ZIG_PRI_u64, | ||
| 21936 | index, vector_len)); | ||
| 21937 | return ira->codegen->invalid_inst_gen; | ||
| 21938 | } | ||
| 21939 | safety_check_on = false; | ||
| 21937 | } | 21940 | } |
| 21941 | |||
| 21938 | if (array_type->id == ZigTypeIdVector) { | 21942 | if (array_type->id == ZigTypeIdVector) { |
| 21939 | ZigType *elem_type = array_type->data.vector.elem_type; | 21943 | ZigType *elem_type = array_type->data.vector.elem_type; |
| 21940 | uint32_t host_vec_len = array_type->data.vector.len; | 21944 | uint32_t host_vec_len = array_type->data.vector.len; |
test/compile_errors.zig+9-1| ... | @@ -2,6 +2,14 @@ const tests = @import("tests.zig"); | ... | @@ -2,6 +2,14 @@ const tests = @import("tests.zig"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 5 | cases.add("slice sentinel mismatch", | ||
| 6 | \\export fn entry() void { | ||
| 7 | \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 }; | ||
| 8 | \\} | ||
| 9 | , &[_][]const u8{ | ||
| 10 | "tmp.zig:2:62: error: index 3 outside vector of size 3", | ||
| 11 | }); | ||
| 12 | |||
| 5 | cases.add("slice sentinel mismatch", | 13 | cases.add("slice sentinel mismatch", |
| 6 | \\export fn entry() void { | 14 | \\export fn entry() void { |
| 7 | \\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 }; | 15 | \\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 }; |
| ... | @@ -7548,7 +7556,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { | ... | @@ -7548,7 +7556,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 7548 | }); | 7556 | }); |
| 7549 | 7557 | ||
| 7550 | cases.add( // fixed bug #2032 | 7558 | cases.add( // fixed bug #2032 |
| 7551 | "compile diagnostic string for top level decl type", | 7559 | "compile diagnostic string for top level decl type", |
| 7552 | \\export fn entry() void { | 7560 | \\export fn entry() void { |
| 7553 | \\ var foo: u32 = @This(){}; | 7561 | \\ var foo: u32 = @This(){}; |
| 7554 | \\} | 7562 | \\} |
test/stage1/behavior/vector.zig+8| ... | @@ -274,6 +274,14 @@ test "vector comparison operators" { | ... | @@ -274,6 +274,14 @@ test "vector comparison operators" { |
| 274 | expectEqual(@splat(4, true), v1 != v3); | 274 | expectEqual(@splat(4, true), v1 != v3); |
| 275 | expectEqual(@splat(4, false), v1 != v2); | 275 | expectEqual(@splat(4, false), v1 != v2); |
| 276 | } | 276 | } |
| 277 | { | ||
| 278 | // Comptime-known LHS/RHS | ||
| 279 | var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 }; | ||
| 280 | const v2 = @splat(4, @as(u32, 2)); | ||
| 281 | const v3: @Vector(4, bool) = [_]bool{ true, false, true, false }; | ||
| 282 | expectEqual(v3, v1 == v2); | ||
| 283 | expectEqual(v3, v2 == v1); | ||
| 284 | } | ||
| 277 | } | 285 | } |
| 278 | }; | 286 | }; |
| 279 | S.doTheTest(); | 287 | S.doTheTest(); |