From 50a6976b5e6584df2ff3e372cca92c9ec1b2aeb7 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 20 Dec 2020 16:08:56 +0100 Subject: [PATCH] all: minor array fixes --- cmd/tools/vpm.v | 6 +- cmd/tools/vtest-fmt.v | 3 +- vlib/builtin/sorting_test.v | 6 +- vlib/crypto/aes/aes_cbc.v | 52 ++--- vlib/crypto/aes/aes_test.v | 1 + vlib/crypto/md5/md5.v | 91 ++++----- vlib/crypto/sha256/sha256.v | 93 ++++----- vlib/crypto/sha256/sha256block_generic.v | 145 +++++++------- vlib/crypto/sha512/sha512.v | 123 ++++++------ vlib/crypto/sha512/sha512block_generic.v | 234 +++++++++-------------- vlib/v/checker/checker.v | 8 +- vlib/v/tests/module_test.v | 12 +- 12 files changed, 357 insertions(+), 417 deletions(-) diff --git a/cmd/tools/vpm.v b/cmd/tools/vpm.v index ee1e30a638..30c03009d6 100644 --- a/cmd/tools/vpm.v +++ b/cmd/tools/vpm.v @@ -50,8 +50,8 @@ fn main() { init_settings() // This tool is intended to be launched by the v frontend, // which provides the path to V inside os.getenv('VEXE') - args := os.args // args are: vpm [options] SUBCOMMAND module names - params := cmdline.only_non_options(args[1..]) + // args are: vpm [options] SUBCOMMAND module names + params := cmdline.only_non_options(os.args[1..]) verbose_println('cli params: $params') if params.len < 1 { vpm_help() @@ -217,7 +217,7 @@ fn vpm_install(module_names []string) { } fn vpm_update(m []string) { - mut module_names := m + mut module_names := m.clone() if settings.is_help { vhelp.show_topic('update') exit(0) diff --git a/cmd/tools/vtest-fmt.v b/cmd/tools/vtest-fmt.v index f987b68c80..0ac0e32152 100644 --- a/cmd/tools/vtest-fmt.v +++ b/cmd/tools/vtest-fmt.v @@ -26,8 +26,7 @@ const ( ) fn main() { - args := os.args - args_string := args[1..].join(' ') + args_string := os.args[1..].join(' ') v_test_formatting(args_string.all_before('test-fmt')) } diff --git a/vlib/builtin/sorting_test.v b/vlib/builtin/sorting_test.v index f47f1c35ef..3a40bc6d95 100644 --- a/vlib/builtin/sorting_test.v +++ b/vlib/builtin/sorting_test.v @@ -5,14 +5,14 @@ const ( ) fn test_sorting_simple() { - mut a := unsorted + mut a := unsorted.clone() a.sort() eprintln(' a: $a') assert a == sorted_asc } fn test_sorting_with_condition_expression() { - mut a := unsorted + mut a := unsorted.clone() a.sort(a > b) eprintln(' a: $a') assert a == sorted_desc @@ -23,7 +23,7 @@ fn mysort(mut a []int) { } fn test_sorting_by_passing_a_mut_array_to_a_function() { - mut a := unsorted + mut a := unsorted.clone() mysort(mut a) eprintln(' a: $a') assert a == sorted_asc diff --git a/vlib/crypto/aes/aes_cbc.v b/vlib/crypto/aes/aes_cbc.v index 0c350514f4..c3f5fac84d 100644 --- a/vlib/crypto/aes/aes_cbc.v +++ b/vlib/crypto/aes/aes_cbc.v @@ -44,33 +44,35 @@ pub fn (x &AesCbc) block_size() int { } pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) { - mut dst := *dst_ - mut src := src_ - if src.len % x.block_size != 0 { - panic('crypto.cipher: input not full blocks') - } - if dst.len < src.len { - panic('crypto.cipher: output smaller than input') - } - if subtle.inexact_overlap(dst[..src.len], src_) { - panic('crypto.cipher: invalid buffer overlap') - } - mut iv := x.iv - for src.len > 0 { - // Write the xor to dst, then encrypt in place. - cipher.xor_bytes(mut dst[..x.block_size], src[..x.block_size], iv) - x.b.encrypt(mut dst[..x.block_size], mut dst[..x.block_size]) - // Move to the next block with this block as the next iv. - iv = dst[..x.block_size] - if x.block_size >= src.len { - src = [] - } else { - src = src[x.block_size..] + unsafe { + mut dst := *dst_ + mut src := src_ + if src.len % x.block_size != 0 { + panic('crypto.cipher: input not full blocks') } - dst = dst[x.block_size..] + if dst.len < src.len { + panic('crypto.cipher: output smaller than input') + } + if subtle.inexact_overlap(dst[..src.len], src_) { + panic('crypto.cipher: invalid buffer overlap') + } + mut iv := x.iv + for src.len > 0 { + // Write the xor to dst, then encrypt in place. + cipher.xor_bytes(mut dst[..x.block_size], src[..x.block_size], iv) + x.b.encrypt(mut dst[..x.block_size], mut dst[..x.block_size]) + // Move to the next block with this block as the next iv. + iv = dst[..x.block_size] + if x.block_size >= src.len { + src = [] + } else { + src = src[x.block_size..] + } + dst = dst[x.block_size..] + } + // Save the iv for the next crypt_blocks call. + copy(x.iv, iv) } - // Save the iv for the next crypt_blocks call. - copy(x.iv, iv) } pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) { diff --git a/vlib/crypto/aes/aes_test.v b/vlib/crypto/aes/aes_test.v index 01991ab535..069268b0c2 100644 --- a/vlib/crypto/aes/aes_test.v +++ b/vlib/crypto/aes/aes_test.v @@ -24,4 +24,5 @@ fn test_crypto_aes() { mode.encrypt_blocks(mut ciphertext, cipher_clone) assert ciphertext.hex() == 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1' + println('ok') } diff --git a/vlib/crypto/md5/md5.v b/vlib/crypto/md5/md5.v index 457081fb45..b742e28911 100644 --- a/vlib/crypto/md5/md5.v +++ b/vlib/crypto/md5/md5.v @@ -1,22 +1,18 @@ // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - // Package md5 implements the MD5 hash algorithm as defined in RFC 1321. - // MD5 is cryptographically broken and should not be used for secure // applications. - // Based off: https://github.com/golang/go/blob/master/src/crypto/md5 // Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17 - module md5 import encoding.binary pub const ( // The size of an MD5 checksum in bytes. - size = 16 + size = 16 // The blocksize of MD5 in bytes. block_size = 64 ) @@ -38,9 +34,9 @@ mut: } fn (mut d Digest) reset() { - d.s = []u32{len:(4)} - d.x = []byte{len:(block_size)} - d.s[0] = u32(init0) + d.s = []u32{len: (4)} + d.x = []byte{len: (block_size)} + d.s[0] = u32(init0) d.s[1] = u32(init1) d.s[2] = u32(init2) d.s[3] = u32(init3) @@ -56,35 +52,37 @@ pub fn new() &Digest { } pub fn (mut d Digest) write(p_ []byte) int { - mut p := p_ - nn := p.len - d.len += u64(nn) - if d.nx > 0 { - n := copy(d.x[d.nx..], p) - d.nx += n - if d.nx == block_size { - block(mut d, d.x) - d.nx = 0 + unsafe { + mut p := p_ + nn := p.len + d.len += u64(nn) + if d.nx > 0 { + n := copy(d.x[d.nx..], p) + d.nx += n + if d.nx == block_size { + block(mut d, d.x) + d.nx = 0 + } + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - if n >= p.len { - p = [] - } else { - p = p[n..] + if p.len >= block_size { + n := p.len & ~(block_size - 1) + block(mut d, p[..n]) + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - } - if p.len >= block_size { - n := p.len &~ (block_size - 1) - block(mut d, p[..n]) - if n >= p.len { - p = [] - } else { - p = p[n..] + if p.len > 0 { + d.nx = copy(d.x, p) } + return nn } - if p.len > 0 { - d.nx = copy(d.x, p) - } - return nn } pub fn (d &Digest) sum(b_in []byte) []byte { @@ -105,20 +103,17 @@ pub fn (mut d Digest) checksum() []byte { // // 1 byte end marker :: 0-63 padding bytes :: 8 byte length // tmp := [1 + 63 + 8]byte{0x80} - mut tmp := []byte{len:(1 + 63 + 8)} + mut tmp := []byte{len: (1 + 63 + 8)} tmp[0] = 0x80 pad := ((55 - d.len) % 64) // calculate number of padding bytes - binary.little_endian_put_u64(mut tmp[1+pad..], d.len<<3) // append length in bits - d.write(tmp[..1+pad+8]) - + binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits + d.write(tmp[..1 + pad + 8]) // The previous write ensures that a whole number of // blocks (i.e. a multiple of 64 bytes) have been hashed. if d.nx != 0 { panic('d.nx != 0') } - - mut digest := []byte{len:(size)} - + mut digest := []byte{len: (size)} binary.little_endian_put_u32(mut digest, d.s[0]) binary.little_endian_put_u32(mut digest[4..], d.s[1]) binary.little_endian_put_u32(mut digest[8..], d.s[2]) @@ -134,13 +129,19 @@ pub fn sum(data []byte) []byte { } fn block(mut dig Digest, p []byte) { - // For now just use block_generic until we have specific + // For now just use block_generic until we have specific // architecture optimized versions - block_generic(mut dig, p) + block_generic(mut dig, p) } -pub fn (d &Digest) size() int { return size } +pub fn (d &Digest) size() int { + return size +} -pub fn (d &Digest) block_size() int { return block_size } +pub fn (d &Digest) block_size() int { + return block_size +} -pub fn hexhash(s string) string { return sum(s.bytes()).hex() } +pub fn hexhash(s string) string { + return sum(s.bytes()).hex() +} diff --git a/vlib/crypto/sha256/sha256.v b/vlib/crypto/sha256/sha256.v index 88b080ceff..52d987d71d 100644 --- a/vlib/crypto/sha256/sha256.v +++ b/vlib/crypto/sha256/sha256.v @@ -1,22 +1,19 @@ // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - // Package sha256 implements the SHA224 and SHA256 hash algorithms as defined // in FIPS 180-4. - // Based off: https://github.com/golang/go/tree/master/src/crypto/sha256 // Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01 - module sha256 import encoding.binary pub const ( // The size of a SHA256 checksum in bytes. - size = 32 + size = 32 // The size of a SHA224 checksum in bytes. - size224 = 28 + size224 = 28 // The blocksize of SHA256 and SHA224 in bytes. block_size = 64 ) @@ -52,8 +49,8 @@ mut: } fn (mut d Digest) reset() { - d.h = []u32{len:(8)} - d.x = []byte{len:(chunk)} + d.h = []u32{len: (8)} + d.x = []byte{len: (chunk)} if !d.is224 { d.h[0] = u32(init0) d.h[1] = u32(init1) @@ -93,35 +90,37 @@ pub fn new224() &Digest { } fn (mut d Digest) write(p_ []byte) int { - mut p := p_ - nn := p.len - d.len += u64(nn) - if d.nx > 0 { - n := copy(d.x[d.nx..], p) - d.nx += n - if d.nx == chunk { - block(mut d, d.x) - d.nx = 0 + unsafe { + mut p := p_ + nn := p.len + d.len += u64(nn) + if d.nx > 0 { + n := copy(d.x[d.nx..], p) + d.nx += n + if d.nx == chunk { + block(mut d, d.x) + d.nx = 0 + } + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - if n >= p.len { - p = [] - } else { - p = p[n..] + if p.len >= chunk { + n := p.len & ~(chunk - 1) + block(mut d, p[..n]) + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - } - if p.len >= chunk { - n := p.len &~ (chunk - 1) - block(mut d, p[..n]) - if n >= p.len { - p = [] - } else { - p = p[n..] + if p.len > 0 { + d.nx = copy(d.x, p) } + return nn } - if p.len > 0 { - d.nx = copy(d.x, p) - } - return nn } fn (d &Digest) sum(b_in []byte) []byte { @@ -144,25 +143,21 @@ fn (d &Digest) sum(b_in []byte) []byte { fn (mut d Digest) checksum() []byte { mut len := d.len // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. - mut tmp := []byte{len:(64)} + mut tmp := []byte{len: (64)} tmp[0] = 0x80 - if int(len)%64 < 56 { - d.write(tmp[..56-int(len)%64]) + if int(len) % 64 < 56 { + d.write(tmp[..56 - int(len) % 64]) } else { - d.write(tmp[..64+56-int(len)%64]) + d.write(tmp[..64 + 56 - int(len) % 64]) } - // Length in bits. len <<= u64(3) binary.big_endian_put_u64(mut tmp, len) d.write(tmp[..8]) - if d.nx != 0 { panic('d.nx != 0') } - - mut digest := []byte{len:(size)} - + mut digest := []byte{len: (size)} binary.big_endian_put_u32(mut digest, d.h[0]) binary.big_endian_put_u32(mut digest[4..], d.h[1]) binary.big_endian_put_u32(mut digest[8..], d.h[2]) @@ -173,7 +168,6 @@ fn (mut d Digest) checksum() []byte { if !d.is224 { binary.big_endian_put_u32(mut digest[28..], d.h[7]) } - return digest } @@ -194,7 +188,7 @@ pub fn sum224(data []byte) []byte { mut d := new224() d.write(data) sum := d.checksum() - sum224 := []byte{len:(size224)} + sum224 := []byte{len: (size224)} copy(sum224, sum[..size224]) return sum224 } @@ -212,7 +206,14 @@ pub fn (d &Digest) size() int { return size224 } -pub fn (d &Digest) block_size() int { return block_size } +pub fn (d &Digest) block_size() int { + return block_size +} -pub fn hexhash(s string) string { return sum256(s.bytes()).hex() } -pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() } +pub fn hexhash(s string) string { + return sum256(s.bytes()).hex() +} + +pub fn hexhash_224(s string) string { + return sum224(s.bytes()).hex() +} diff --git a/vlib/crypto/sha256/sha256block_generic.v b/vlib/crypto/sha256/sha256block_generic.v index 55e57b40b9..a64990efb6 100644 --- a/vlib/crypto/sha256/sha256block_generic.v +++ b/vlib/crypto/sha256/sha256block_generic.v @@ -1,12 +1,10 @@ // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - // SHA256 block step. // This is the generic version with no architecture optimizations. // In its own file so that an architecture // optimized verision can be substituted - module sha256 import math.bits @@ -81,79 +79,76 @@ const ( ) fn block_generic(mut dig Digest, p_ []byte) { - mut p := p_ - - mut w := []u32{len:(64)} - - mut h0 := dig.h[0] - mut h1 := dig.h[1] - mut h2 := dig.h[2] - mut h3 := dig.h[3] - mut h4 := dig.h[4] - mut h5 := dig.h[5] - mut h6 := dig.h[6] - mut h7 := dig.h[7] - - for p.len >= chunk { - // Can interlace the computation of w with the - // rounds below if needed for speed. - for i in 0..16 { - j := i * 4 - w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3]) - } - for i := 16; i < 64; i++ { - v1 := w[i-2] - t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10) - v2 := w[i-15] - t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3) - w[i] = t1 + w[i-7] + t2 + w[i-16] - } - - mut a := h0 - mut b := h1 - mut c := h2 - mut d := h3 - mut e := h4 - mut f := h5 - mut g := h6 - mut h := h7 - - for i in 0..64 { - t1 := h + ((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) + ((e & f) ^ (~e & g)) + u32(_k[i]) + w[i] - t2 := ((bits.rotate_left_32(a, -2)) ^ (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c)) - - h = g - g = f - f = e - e = d + t1 - d = c - c = b - b = a - a = t1 + t2 - } - - h0 += a - h1 += b - h2 += c - h3 += d - h4 += e - h5 += f - h6 += g - h7 += h - - if chunk >= p.len { - p = [] - } else { - p = p[chunk..] + unsafe { + mut p := p_ + mut w := []u32{len: (64)} + mut h0 := dig.h[0] + mut h1 := dig.h[1] + mut h2 := dig.h[2] + mut h3 := dig.h[3] + mut h4 := dig.h[4] + mut h5 := dig.h[5] + mut h6 := dig.h[6] + mut h7 := dig.h[7] + for p.len >= chunk { + // Can interlace the computation of w with the + // rounds below if needed for speed. + for i in 0 .. 16 { + j := i * 4 + w[i] = u32(p[j] << 24) | u32(p[j + 1] << 16) | u32(p[j + 2] << 8) | u32(p[j + 3]) + } + for i := 16; i < 64; i++ { + v1 := w[i - 2] + t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10) + v2 := w[i - 15] + t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3) + w[i] = t1 + w[i - 7] + t2 + w[i - 16] + } + mut a := h0 + mut b := h1 + mut c := h2 + mut d := h3 + mut e := h4 + mut f := h5 + mut g := h6 + mut h := h7 + for i in 0 .. 64 { + t1 := h + + ((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) + + ((e & f) ^ (~e & g)) + u32(_k[i]) + w[i] + t2 := ((bits.rotate_left_32(a, -2)) ^ + (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + + ((a & b) ^ (a & c) ^ (b & c)) + h = g + g = f + f = e + e = d + t1 + d = c + c = b + b = a + a = t1 + t2 + } + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + if chunk >= p.len { + p = [] + } else { + p = p[chunk..] + } } + dig.h[0] = h0 + dig.h[1] = h1 + dig.h[2] = h2 + dig.h[3] = h3 + dig.h[4] = h4 + dig.h[5] = h5 + dig.h[6] = h6 + dig.h[7] = h7 } - - dig.h[0] = h0 - dig.h[1] = h1 - dig.h[2] = h2 - dig.h[3] = h3 - dig.h[4] = h4 - dig.h[5] = h5 - dig.h[6] = h6 - dig.h[7] = h7 } diff --git a/vlib/crypto/sha512/sha512.v b/vlib/crypto/sha512/sha512.v index 08538648f7..28cb1deb69 100644 --- a/vlib/crypto/sha512/sha512.v +++ b/vlib/crypto/sha512/sha512.v @@ -11,29 +11,29 @@ import crypto import encoding.binary pub const ( -// size is the size, in bytes, of a SHA-512 checksum. - size = 64 + // size is the size, in bytes, of a SHA-512 checksum. + size = 64 // size224 is the size, in bytes, of a SHA-512/224 checksum. - size224 = 28 + size224 = 28 // size256 is the size, in bytes, of a SHA-512/256 checksum. - size256 = 32 + size256 = 32 // size384 is the size, in bytes, of a SHA-384 checksum. - size384 = 48 + size384 = 48 // block_size is the block size, in bytes, of the SHA-512/224, // SHA-512/256, SHA-384 and SHA-512 hash functions. block_size = 128 ) const ( - chunk = 128 - init0 = u64(0x6a09e667f3bcc908) - init1 = u64(0xbb67ae8584caa73b) - init2 = u64(0x3c6ef372fe94f82b) - init3 = u64(0xa54ff53a5f1d36f1) - init4 = u64(0x510e527fade682d1) - init5 = u64(0x9b05688c2b3e6c1f) - init6 = u64(0x1f83d9abfb41bd6b) - init7 = u64(0x5be0cd19137e2179) + chunk = 128 + init0 = u64(0x6a09e667f3bcc908) + init1 = u64(0xbb67ae8584caa73b) + init2 = u64(0x3c6ef372fe94f82b) + init3 = u64(0xa54ff53a5f1d36f1) + init4 = u64(0x510e527fade682d1) + init5 = u64(0x9b05688c2b3e6c1f) + init6 = u64(0x1f83d9abfb41bd6b) + init7 = u64(0x5be0cd19137e2179) init0_224 = u64(0x8c3d37c819544da2) init1_224 = u64(0x73e1996689dcd4d6) init2_224 = u64(0x1dfab7ae32ff9c82) @@ -59,6 +59,7 @@ const ( init6_384 = u64(0xdb0c2e0d64f98fa7) init7_384 = u64(0x47b5481dbefa4fa4) ) + // digest represents the partial evaluation of a checksum. struct Digest { mut: @@ -70,8 +71,8 @@ mut: } fn (mut d Digest) reset() { - d.h = []u64{len:(8)} - d.x = []byte{len:(chunk)} + d.h = []u64{len: (8)} + d.x = []byte{len: (chunk)} match d.function { .sha384 { d.h[0] = init0_384 @@ -112,7 +113,8 @@ fn (mut d Digest) reset() { d.h[5] = init5 d.h[6] = init6 d.h[7] = init7 - }} + } + } d.nx = 0 d.len = 0 } @@ -147,37 +149,37 @@ fn new384() &Digest { } fn (mut d Digest) write(p_ []byte) int { - mut p := p_ - nn := p.len - d.len += u64(nn) - if d.nx > 0 { - n := copy(d.x[d.nx..], p) - d.nx += n - if d.nx == chunk{ - block(mut d, d.x) - d.nx = 0 + unsafe { + mut p := p_ + nn := p.len + d.len += u64(nn) + if d.nx > 0 { + n := copy(d.x[d.nx..], p) + d.nx += n + if d.nx == chunk { + block(mut d, d.x) + d.nx = 0 + } + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - if n >= p.len { - p = [] + if p.len >= chunk { + n := p.len & ~(chunk - 1) + block(mut d, p[..n]) + if n >= p.len { + p = [] + } else { + p = p[n..] + } } - else { - p = p[n..] + if p.len > 0 { + d.nx = copy(d.x, p) } + return nn } - if p.len >= chunk{ - n := p.len & ~(chunk- 1) - block(mut d, p[..n]) - if n >= p.len { - p = [] - } - else { - p = p[n..] - } - } - if p.len > 0 { - d.nx = copy(d.x, p) - } - return nn } fn (d &Digest) sum(b_in []byte) []byte { @@ -205,19 +207,19 @@ fn (d &Digest) sum(b_in []byte) []byte { for b in hash { b_out << b } - }} + } + } return b_out } fn (mut d Digest) checksum() []byte { // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. mut len := d.len - mut tmp := []byte{len:(128)} + mut tmp := []byte{len: (128)} tmp[0] = 0x80 if int(len) % 128 < 112 { d.write(tmp[..112 - int(len) % 128]) - } - else { + } else { d.write(tmp[..128 + 112 - int(len) % 128]) } // Length in bits. @@ -228,7 +230,7 @@ fn (mut d Digest) checksum() []byte { if d.nx != 0 { panic('d.nx != 0') } - mut digest := []byte{len:(size)} + mut digest := []byte{len: (size)} binary.big_endian_put_u64(mut digest, d.h[0]) binary.big_endian_put_u64(mut digest[8..], d.h[1]) binary.big_endian_put_u64(mut digest[16..], d.h[2]) @@ -254,7 +256,7 @@ pub fn sum384(data []byte) []byte { mut d := new_digest(.sha384) d.write(data) sum := d.checksum() - sum384 := []byte{len:(size384)} + sum384 := []byte{len: (size384)} copy(sum384, sum[..size384]) return sum384 } @@ -264,7 +266,7 @@ pub fn sum512_224(data []byte) []byte { mut d := new_digest(.sha512_224) d.write(data) sum := d.checksum() - sum224 := []byte{len:(size224)} + sum224 := []byte{len: (size224)} copy(sum224, sum[..size224]) return sum224 } @@ -274,7 +276,7 @@ pub fn sum512_256(data []byte) []byte { mut d := new_digest(.sha512_256) d.write(data) sum := d.checksum() - sum256 := []byte{len:(size256)} + sum256 := []byte{len: (size256)} copy(sum256, sum[..size256]) return sum256 } @@ -287,18 +289,11 @@ fn block(mut dig Digest, p []byte) { pub fn (d &Digest) size() int { match d.function { - .sha512_224 { - return size224 - } - .sha512_256 { - return size256 - } - .sha384 { - return size384 - } - else { - return size - }} + .sha512_224 { return size224 } + .sha512_256 { return size256 } + .sha384 { return size384 } + else { return size } + } } pub fn (d &Digest) block_size() int { diff --git a/vlib/crypto/sha512/sha512block_generic.v b/vlib/crypto/sha512/sha512block_generic.v index 3f1f4818d5..0b4a8b4934 100644 --- a/vlib/crypto/sha512/sha512block_generic.v +++ b/vlib/crypto/sha512/sha512block_generic.v @@ -10,153 +10,99 @@ module sha512 import math.bits const ( - _k = [u64(0x428a2f98d728ae22), - u64(0x7137449123ef65cd), - u64(0xb5c0fbcfec4d3b2f), - u64(0xe9b5dba58189dbbc), - u64(0x3956c25bf348b538), - u64(0x59f111f1b605d019), - u64(0x923f82a4af194f9b), - u64(0xab1c5ed5da6d8118), - u64(0xd807aa98a3030242), - u64(0x12835b0145706fbe), - u64(0x243185be4ee4b28c), - u64(0x550c7dc3d5ffb4e2), - u64(0x72be5d74f27b896f), - u64(0x80deb1fe3b1696b1), - u64(0x9bdc06a725c71235), - u64(0xc19bf174cf692694), - u64(0xe49b69c19ef14ad2), - u64(0xefbe4786384f25e3), - u64(0x0fc19dc68b8cd5b5), - u64(0x240ca1cc77ac9c65), - u64(0x2de92c6f592b0275), - u64(0x4a7484aa6ea6e483), - u64(0x5cb0a9dcbd41fbd4), - u64(0x76f988da831153b5), - u64(0x983e5152ee66dfab), - u64(0xa831c66d2db43210), - u64(0xb00327c898fb213f), - u64(0xbf597fc7beef0ee4), - u64(0xc6e00bf33da88fc2), - u64(0xd5a79147930aa725), - u64(0x06ca6351e003826f), - u64(0x142929670a0e6e70), - u64(0x27b70a8546d22ffc), - u64(0x2e1b21385c26c926), - u64(0x4d2c6dfc5ac42aed), - u64(0x53380d139d95b3df), - u64(0x650a73548baf63de), - u64(0x766a0abb3c77b2a8), - u64(0x81c2c92e47edaee6), - u64(0x92722c851482353b), - u64(0xa2bfe8a14cf10364), - u64(0xa81a664bbc423001), - u64(0xc24b8b70d0f89791), - u64(0xc76c51a30654be30), - u64(0xd192e819d6ef5218), - u64(0xd69906245565a910), - u64(0xf40e35855771202a), - u64(0x106aa07032bbd1b8), - u64(0x19a4c116b8d2d0c8), - u64(0x1e376c085141ab53), - u64(0x2748774cdf8eeb99), - u64(0x34b0bcb5e19b48a8), - u64(0x391c0cb3c5c95a63), - u64(0x4ed8aa4ae3418acb), - u64(0x5b9cca4f7763e373), - u64(0x682e6ff3d6b2b8a3), - u64(0x748f82ee5defb2fc), - u64(0x78a5636f43172f60), - u64(0x84c87814a1f0ab72), - u64(0x8cc702081a6439ec), - u64(0x90befffa23631e28), - u64(0xa4506cebde82bde9), - u64(0xbef9a3f7b2c67915), - u64(0xc67178f2e372532b), - u64(0xca273eceea26619c), - u64(0xd186b8c721c0c207), - u64(0xeada7dd6cde0eb1e), - u64(0xf57d4f7fee6ed178), - u64(0x06f067aa72176fba), - u64(0x0a637dc5a2c898a6), - u64(0x113f9804bef90dae), - u64(0x1b710b35131c471b), - u64(0x28db77f523047d84), - u64(0x32caab7b40c72493), - u64(0x3c9ebe0a15c9bebc), - u64(0x431d67c49c100d4c), - u64(0x4cc5d4becb3e42b6), - u64(0x597f299cfc657e2a), - u64(0x5fcb6fab3ad6faec), - u64(0x6c44198c4a475817), - ] + _k = [u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc), + u64(0x3956c25bf348b538), u64(0x59f111f1b605d019), u64(0x923f82a4af194f9b), u64(0xab1c5ed5da6d8118), + u64(0xd807aa98a3030242), u64(0x12835b0145706fbe), u64(0x243185be4ee4b28c), u64(0x550c7dc3d5ffb4e2), + u64(0x72be5d74f27b896f), u64(0x80deb1fe3b1696b1), u64(0x9bdc06a725c71235), u64(0xc19bf174cf692694), + u64(0xe49b69c19ef14ad2), u64(0xefbe4786384f25e3), u64(0x0fc19dc68b8cd5b5), u64(0x240ca1cc77ac9c65), + u64(0x2de92c6f592b0275), u64(0x4a7484aa6ea6e483), u64(0x5cb0a9dcbd41fbd4), u64(0x76f988da831153b5), + u64(0x983e5152ee66dfab), u64(0xa831c66d2db43210), u64(0xb00327c898fb213f), u64(0xbf597fc7beef0ee4), + u64(0xc6e00bf33da88fc2), u64(0xd5a79147930aa725), u64(0x06ca6351e003826f), u64(0x142929670a0e6e70), + u64(0x27b70a8546d22ffc), u64(0x2e1b21385c26c926), u64(0x4d2c6dfc5ac42aed), u64(0x53380d139d95b3df), + u64(0x650a73548baf63de), u64(0x766a0abb3c77b2a8), u64(0x81c2c92e47edaee6), u64(0x92722c851482353b), + u64(0xa2bfe8a14cf10364), u64(0xa81a664bbc423001), u64(0xc24b8b70d0f89791), u64(0xc76c51a30654be30), + u64(0xd192e819d6ef5218), u64(0xd69906245565a910), u64(0xf40e35855771202a), u64(0x106aa07032bbd1b8), + u64(0x19a4c116b8d2d0c8), u64(0x1e376c085141ab53), u64(0x2748774cdf8eeb99), u64(0x34b0bcb5e19b48a8), + u64(0x391c0cb3c5c95a63), u64(0x4ed8aa4ae3418acb), u64(0x5b9cca4f7763e373), u64(0x682e6ff3d6b2b8a3), + u64(0x748f82ee5defb2fc), u64(0x78a5636f43172f60), u64(0x84c87814a1f0ab72), u64(0x8cc702081a6439ec), + u64(0x90befffa23631e28), u64(0xa4506cebde82bde9), u64(0xbef9a3f7b2c67915), u64(0xc67178f2e372532b), + u64(0xca273eceea26619c), u64(0xd186b8c721c0c207), u64(0xeada7dd6cde0eb1e), u64(0xf57d4f7fee6ed178), + u64(0x06f067aa72176fba), u64(0x0a637dc5a2c898a6), u64(0x113f9804bef90dae), u64(0x1b710b35131c471b), + u64(0x28db77f523047d84), u64(0x32caab7b40c72493), u64(0x3c9ebe0a15c9bebc), u64(0x431d67c49c100d4c), + u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817)] ) fn block_generic(mut dig Digest, p_ []byte) { - mut p := p_ - mut w := []u64{len:(80)} - mut h0 := dig.h[0] - mut h1 := dig.h[1] - mut h2 := dig.h[2] - mut h3 := dig.h[3] - mut h4 := dig.h[4] - mut h5 := dig.h[5] - mut h6 := dig.h[6] - mut h7 := dig.h[7] - for p.len >= chunk { - for i in 0..16 { - j := i * 8 - w[i] = (u64(p[j])<<56) | (u64(p[j + 1])<<48) | (u64(p[j + 2])<<40) | (u64(p[j + 3])<<32) | (u64(p[j + 4])<<24) | (u64(p[j + 5])<<16) | (u64(p[j + 6])<<8) | u64(p[j + 7]) - } - for i := 16; i < 80; i++ { - v1 := w[i - 2] - t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1>>6) - v2 := w[i - 15] - t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2>>7) - w[i] = t1 + w[i - 7] + t2 + w[i - 16] - } - mut a := h0 - mut b := h1 - mut c := h2 - mut d := h3 - mut e := h4 - mut f := h5 - mut g := h6 - mut h := h7 - for i in 0..80 { - t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i] - t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c)) - h = g - g = f - f = e - e = d + t1 - d = c - c = b - b = a - a = t1 + t2 - } - h0 += a - h1 += b - h2 += c - h3 += d - h4 += e - h5 += f - h6 += g - h7 += h - if chunk >= p.len { - p = [] - } - else { - p = p[chunk..] + unsafe { + mut p := p_ + mut w := []u64{len: (80)} + mut h0 := dig.h[0] + mut h1 := dig.h[1] + mut h2 := dig.h[2] + mut h3 := dig.h[3] + mut h4 := dig.h[4] + mut h5 := dig.h[5] + mut h6 := dig.h[6] + mut h7 := dig.h[7] + for p.len >= chunk { + for i in 0 .. 16 { + j := i * 8 + w[i] = (u64(p[j]) << 56) | + (u64(p[j + 1]) << 48) | (u64(p[j + 2]) << 40) | + (u64(p[j + 3]) << 32) | (u64(p[j + 4]) << 24) | + (u64(p[j + 5]) << 16) | (u64(p[j + 6]) << 8) | u64(p[j + 7]) + } + for i := 16; i < 80; i++ { + v1 := w[i - 2] + t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1 >> 6) + v2 := w[i - 15] + t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7) + w[i] = t1 + w[i - 7] + t2 + w[i - 16] + } + mut a := h0 + mut b := h1 + mut c := h2 + mut d := h3 + mut e := h4 + mut f := h5 + mut g := h6 + mut h := h7 + for i in 0 .. 80 { + t1 := h + + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + + ((e & f) ^ (~e & g)) + _k[i] + w[i] + t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + + ((a & b) ^ (a & c) ^ (b & c)) + h = g + g = f + f = e + e = d + t1 + d = c + c = b + b = a + a = t1 + t2 + } + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + if chunk >= p.len { + p = [] + } else { + p = p[chunk..] + } } + dig.h[0] = h0 + dig.h[1] = h1 + dig.h[2] = h2 + dig.h[3] = h3 + dig.h[4] = h4 + dig.h[5] = h5 + dig.h[6] = h6 + dig.h[7] = h7 } - dig.h[0] = h0 - dig.h[1] = h1 - dig.h[2] = h2 - dig.h[3] = h3 - dig.h[4] = h4 - dig.h[5] = h5 - dig.h[6] = h6 - dig.h[7] = h7 } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3a1c7ab5ed..79d10f046a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2209,11 +2209,11 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { // TODO replace all c.pref.translated checks with `$if !translated` for performance continue } - if left_sym.kind == .array && - assign_stmt.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident && - right is ast.Ident { + if left_sym.kind == .array && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign] && + right_sym.kind == .array && left is ast.Ident && right is ast.Ident { // Do not allow `a = b`, only `a = b.clone()` - c.error('use `array2 = array1.clone()` instead of `array2 = array1`', assign_stmt.pos) + c.error('use `array2 = array1.clone()` instead of `array2 = array1` (or use `unsafe`)', + assign_stmt.pos) } left_is_ptr := left_type.is_ptr() || left_sym.is_pointer() if left_is_ptr { diff --git a/vlib/v/tests/module_test.v b/vlib/v/tests/module_test.v index 5291a41952..419ca03e4f 100644 --- a/vlib/v/tests/module_test.v +++ b/vlib/v/tests/module_test.v @@ -9,24 +9,24 @@ import crypto.sha512 import cli { Command } struct TestAliasInStruct { - time t.Time + time Time } fn test_import() { info := l.Level.info assert info == .info - assert term.white('INFO') == white('INFO') + assert white('INFO') == white('INFO') assert os.o_rdonly == os.o_rdonly assert t.month_days[0] == t.month_days[0] assert sha256.size == sha256.size assert math.pi == math.pi assert sha512.size == sha512.size - assert md5.sum('module'.bytes()).hex() == sum('module'.bytes()).hex() - assert t.utc().unix_time() == utc().unix_time() + assert sum('module'.bytes()).hex() == sum('module'.bytes()).hex() + assert utc().unix_time() == utc().unix_time() } fn test_imports_array_as_fn_arg() { - mut cmd := Command { + mut cmd := Command{ name: 'module test' } c1 := Command{} @@ -38,7 +38,7 @@ fn test_imports_array_as_fn_arg() { fn test_alias_in_struct_field() { a := TestAliasInStruct{ - time: t.Time{ + time: Time{ year: 2020 } }