all: minor array fixes

pull/7429/head
Alexander Medvednikov 2020-12-20 16:08:56 +01:00
parent 3c210a57f9
commit 50a6976b5e
12 changed files with 357 additions and 417 deletions

View File

@ -50,8 +50,8 @@ fn main() {
init_settings() init_settings()
// This tool is intended to be launched by the v frontend, // This tool is intended to be launched by the v frontend,
// which provides the path to V inside os.getenv('VEXE') // which provides the path to V inside os.getenv('VEXE')
args := os.args // args are: vpm [options] SUBCOMMAND module names // args are: vpm [options] SUBCOMMAND module names
params := cmdline.only_non_options(args[1..]) params := cmdline.only_non_options(os.args[1..])
verbose_println('cli params: $params') verbose_println('cli params: $params')
if params.len < 1 { if params.len < 1 {
vpm_help() vpm_help()
@ -217,7 +217,7 @@ fn vpm_install(module_names []string) {
} }
fn vpm_update(m []string) { fn vpm_update(m []string) {
mut module_names := m mut module_names := m.clone()
if settings.is_help { if settings.is_help {
vhelp.show_topic('update') vhelp.show_topic('update')
exit(0) exit(0)

View File

@ -26,8 +26,7 @@ const (
) )
fn main() { fn main() {
args := os.args args_string := os.args[1..].join(' ')
args_string := args[1..].join(' ')
v_test_formatting(args_string.all_before('test-fmt')) v_test_formatting(args_string.all_before('test-fmt'))
} }

View File

@ -5,14 +5,14 @@ const (
) )
fn test_sorting_simple() { fn test_sorting_simple() {
mut a := unsorted mut a := unsorted.clone()
a.sort() a.sort()
eprintln(' a: $a') eprintln(' a: $a')
assert a == sorted_asc assert a == sorted_asc
} }
fn test_sorting_with_condition_expression() { fn test_sorting_with_condition_expression() {
mut a := unsorted mut a := unsorted.clone()
a.sort(a > b) a.sort(a > b)
eprintln(' a: $a') eprintln(' a: $a')
assert a == sorted_desc assert a == sorted_desc
@ -23,7 +23,7 @@ fn mysort(mut a []int) {
} }
fn test_sorting_by_passing_a_mut_array_to_a_function() { fn test_sorting_by_passing_a_mut_array_to_a_function() {
mut a := unsorted mut a := unsorted.clone()
mysort(mut a) mysort(mut a)
eprintln(' a: $a') eprintln(' a: $a')
assert a == sorted_asc assert a == sorted_asc

View File

@ -44,33 +44,35 @@ pub fn (x &AesCbc) block_size() int {
} }
pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) { pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
mut dst := *dst_ unsafe {
mut src := src_ mut dst := *dst_
if src.len % x.block_size != 0 { mut src := src_
panic('crypto.cipher: input not full blocks') 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..]
} }
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) { pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) {

View File

@ -24,4 +24,5 @@ fn test_crypto_aes() {
mode.encrypt_blocks(mut ciphertext, cipher_clone) mode.encrypt_blocks(mut ciphertext, cipher_clone)
assert ciphertext.hex() == assert ciphertext.hex() ==
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1' 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
println('ok')
} }

View File

@ -1,22 +1,18 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
// Package md5 implements the MD5 hash algorithm as defined in RFC 1321. // Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
// MD5 is cryptographically broken and should not be used for secure // MD5 is cryptographically broken and should not be used for secure
// applications. // applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5 // Based off: https://github.com/golang/go/blob/master/src/crypto/md5
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17 // Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
module md5 module md5
import encoding.binary import encoding.binary
pub const ( pub const (
// The size of an MD5 checksum in bytes. // The size of an MD5 checksum in bytes.
size = 16 size = 16
// The blocksize of MD5 in bytes. // The blocksize of MD5 in bytes.
block_size = 64 block_size = 64
) )
@ -38,9 +34,9 @@ mut:
} }
fn (mut d Digest) reset() { fn (mut d Digest) reset() {
d.s = []u32{len:(4)} d.s = []u32{len: (4)}
d.x = []byte{len:(block_size)} d.x = []byte{len: (block_size)}
d.s[0] = u32(init0) d.s[0] = u32(init0)
d.s[1] = u32(init1) d.s[1] = u32(init1)
d.s[2] = u32(init2) d.s[2] = u32(init2)
d.s[3] = u32(init3) d.s[3] = u32(init3)
@ -56,35 +52,37 @@ pub fn new() &Digest {
} }
pub fn (mut d Digest) write(p_ []byte) int { pub fn (mut d Digest) write(p_ []byte) int {
mut p := p_ unsafe {
nn := p.len mut p := p_
d.len += u64(nn) nn := p.len
if d.nx > 0 { d.len += u64(nn)
n := copy(d.x[d.nx..], p) if d.nx > 0 {
d.nx += n n := copy(d.x[d.nx..], p)
if d.nx == block_size { d.nx += n
block(mut d, d.x) if d.nx == block_size {
d.nx = 0 block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
if n >= p.len { if p.len >= block_size {
p = [] n := p.len & ~(block_size - 1)
} else { block(mut d, p[..n])
p = p[n..] if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
} if p.len > 0 {
if p.len >= block_size { d.nx = copy(d.x, p)
n := p.len &~ (block_size - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
} }
return nn
} }
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
} }
pub fn (d &Digest) sum(b_in []byte) []byte { 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 // 1 byte end marker :: 0-63 padding bytes :: 8 byte length
// tmp := [1 + 63 + 8]byte{0x80} // tmp := [1 + 63 + 8]byte{0x80}
mut tmp := []byte{len:(1 + 63 + 8)} mut tmp := []byte{len: (1 + 63 + 8)}
tmp[0] = 0x80 tmp[0] = 0x80
pad := ((55 - d.len) % 64) // calculate number of padding bytes 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 binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
d.write(tmp[..1+pad+8]) d.write(tmp[..1 + pad + 8])
// The previous write ensures that a whole number of // The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed. // blocks (i.e. a multiple of 64 bytes) have been hashed.
if d.nx != 0 { if d.nx != 0 {
panic('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, d.s[0])
binary.little_endian_put_u32(mut digest[4..], d.s[1]) binary.little_endian_put_u32(mut digest[4..], d.s[1])
binary.little_endian_put_u32(mut digest[8..], d.s[2]) 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) { 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 // 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()
}

View File

@ -1,22 +1,19 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
// Package sha256 implements the SHA224 and SHA256 hash algorithms as defined // Package sha256 implements the SHA224 and SHA256 hash algorithms as defined
// in FIPS 180-4. // in FIPS 180-4.
// Based off: https://github.com/golang/go/tree/master/src/crypto/sha256 // Based off: https://github.com/golang/go/tree/master/src/crypto/sha256
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01 // Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
module sha256 module sha256
import encoding.binary import encoding.binary
pub const ( pub const (
// The size of a SHA256 checksum in bytes. // The size of a SHA256 checksum in bytes.
size = 32 size = 32
// The size of a SHA224 checksum in bytes. // The size of a SHA224 checksum in bytes.
size224 = 28 size224 = 28
// The blocksize of SHA256 and SHA224 in bytes. // The blocksize of SHA256 and SHA224 in bytes.
block_size = 64 block_size = 64
) )
@ -52,8 +49,8 @@ mut:
} }
fn (mut d Digest) reset() { fn (mut d Digest) reset() {
d.h = []u32{len:(8)} d.h = []u32{len: (8)}
d.x = []byte{len:(chunk)} d.x = []byte{len: (chunk)}
if !d.is224 { if !d.is224 {
d.h[0] = u32(init0) d.h[0] = u32(init0)
d.h[1] = u32(init1) d.h[1] = u32(init1)
@ -93,35 +90,37 @@ pub fn new224() &Digest {
} }
fn (mut d Digest) write(p_ []byte) int { fn (mut d Digest) write(p_ []byte) int {
mut p := p_ unsafe {
nn := p.len mut p := p_
d.len += u64(nn) nn := p.len
if d.nx > 0 { d.len += u64(nn)
n := copy(d.x[d.nx..], p) if d.nx > 0 {
d.nx += n n := copy(d.x[d.nx..], p)
if d.nx == chunk { d.nx += n
block(mut d, d.x) if d.nx == chunk {
d.nx = 0 block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
if n >= p.len { if p.len >= chunk {
p = [] n := p.len & ~(chunk - 1)
} else { block(mut d, p[..n])
p = p[n..] if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
} if p.len > 0 {
if p.len >= chunk { d.nx = copy(d.x, p)
n := p.len &~ (chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
} }
return nn
} }
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
} }
fn (d &Digest) sum(b_in []byte) []byte { 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 { fn (mut d Digest) checksum() []byte {
mut len := d.len mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. // 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 tmp[0] = 0x80
if int(len)%64 < 56 { if int(len) % 64 < 56 {
d.write(tmp[..56-int(len)%64]) d.write(tmp[..56 - int(len) % 64])
} else { } else {
d.write(tmp[..64+56-int(len)%64]) d.write(tmp[..64 + 56 - int(len) % 64])
} }
// Length in bits. // Length in bits.
len <<= u64(3) len <<= u64(3)
binary.big_endian_put_u64(mut tmp, len) binary.big_endian_put_u64(mut tmp, len)
d.write(tmp[..8]) d.write(tmp[..8])
if d.nx != 0 { if d.nx != 0 {
panic('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, d.h[0])
binary.big_endian_put_u32(mut digest[4..], d.h[1]) binary.big_endian_put_u32(mut digest[4..], d.h[1])
binary.big_endian_put_u32(mut digest[8..], d.h[2]) binary.big_endian_put_u32(mut digest[8..], d.h[2])
@ -173,7 +168,6 @@ fn (mut d Digest) checksum() []byte {
if !d.is224 { if !d.is224 {
binary.big_endian_put_u32(mut digest[28..], d.h[7]) binary.big_endian_put_u32(mut digest[28..], d.h[7])
} }
return digest return digest
} }
@ -194,7 +188,7 @@ pub fn sum224(data []byte) []byte {
mut d := new224() mut d := new224()
d.write(data) d.write(data)
sum := d.checksum() sum := d.checksum()
sum224 := []byte{len:(size224)} sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224]) copy(sum224, sum[..size224])
return sum224 return sum224
} }
@ -212,7 +206,14 @@ pub fn (d &Digest) size() int {
return size224 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(s string) string {
pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() } return sum256(s.bytes()).hex()
}
pub fn hexhash_224(s string) string {
return sum224(s.bytes()).hex()
}

View File

@ -1,12 +1,10 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
// SHA256 block step. // SHA256 block step.
// This is the generic version with no architecture optimizations. // This is the generic version with no architecture optimizations.
// In its own file so that an architecture // In its own file so that an architecture
// optimized verision can be substituted // optimized verision can be substituted
module sha256 module sha256
import math.bits import math.bits
@ -81,79 +79,76 @@ const (
) )
fn block_generic(mut dig Digest, p_ []byte) { fn block_generic(mut dig Digest, p_ []byte) {
mut p := p_ unsafe {
mut p := p_
mut w := []u32{len:(64)} mut w := []u32{len: (64)}
mut h0 := dig.h[0]
mut h0 := dig.h[0] mut h1 := dig.h[1]
mut h1 := dig.h[1] mut h2 := dig.h[2]
mut h2 := dig.h[2] mut h3 := dig.h[3]
mut h3 := dig.h[3] mut h4 := dig.h[4]
mut h4 := dig.h[4] mut h5 := dig.h[5]
mut h5 := dig.h[5] mut h6 := dig.h[6]
mut h6 := dig.h[6] mut h7 := dig.h[7]
mut h7 := dig.h[7] for p.len >= chunk {
// Can interlace the computation of w with the
for p.len >= chunk { // rounds below if needed for speed.
// Can interlace the computation of w with the for i in 0 .. 16 {
// rounds below if needed for speed. j := i * 4
for i in 0..16 { w[i] = u32(p[j] << 24) | u32(p[j + 1] << 16) | u32(p[j + 2] << 8) | u32(p[j + 3])
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]
for i := 16; i < 64; i++ { t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10)
v1 := w[i-2] v2 := w[i - 15]
t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10) t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3)
v2 := w[i-15] w[i] = t1 + w[i - 7] + t2 + w[i - 16]
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 a := h0 mut d := h3
mut b := h1 mut e := h4
mut c := h2 mut f := h5
mut d := h3 mut g := h6
mut e := h4 mut h := h7
mut f := h5 for i in 0 .. 64 {
mut g := h6 t1 := h +
mut h := h7 ((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]
for i in 0..64 { t2 := ((bits.rotate_left_32(a, -2)) ^
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] (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) +
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)) ((a & b) ^ (a & c) ^ (b & c))
h = g
h = g g = f
g = f f = e
f = e e = d + t1
e = d + t1 d = c
d = c c = b
c = b b = a
b = a a = t1 + t2
a = t1 + t2 }
} h0 += a
h1 += b
h0 += a h2 += c
h1 += b h3 += d
h2 += c h4 += e
h3 += d h5 += f
h4 += e h6 += g
h5 += f h7 += h
h6 += g if chunk >= p.len {
h7 += h p = []
} else {
if chunk >= p.len { p = p[chunk..]
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
} }

View File

@ -11,29 +11,29 @@ import crypto
import encoding.binary import encoding.binary
pub const ( pub const (
// size is the size, in bytes, of a SHA-512 checksum. // size is the size, in bytes, of a SHA-512 checksum.
size = 64 size = 64
// size224 is the size, in bytes, of a SHA-512/224 checksum. // 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 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 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, // block_size is the block size, in bytes, of the SHA-512/224,
// SHA-512/256, SHA-384 and SHA-512 hash functions. // SHA-512/256, SHA-384 and SHA-512 hash functions.
block_size = 128 block_size = 128
) )
const ( const (
chunk = 128 chunk = 128
init0 = u64(0x6a09e667f3bcc908) init0 = u64(0x6a09e667f3bcc908)
init1 = u64(0xbb67ae8584caa73b) init1 = u64(0xbb67ae8584caa73b)
init2 = u64(0x3c6ef372fe94f82b) init2 = u64(0x3c6ef372fe94f82b)
init3 = u64(0xa54ff53a5f1d36f1) init3 = u64(0xa54ff53a5f1d36f1)
init4 = u64(0x510e527fade682d1) init4 = u64(0x510e527fade682d1)
init5 = u64(0x9b05688c2b3e6c1f) init5 = u64(0x9b05688c2b3e6c1f)
init6 = u64(0x1f83d9abfb41bd6b) init6 = u64(0x1f83d9abfb41bd6b)
init7 = u64(0x5be0cd19137e2179) init7 = u64(0x5be0cd19137e2179)
init0_224 = u64(0x8c3d37c819544da2) init0_224 = u64(0x8c3d37c819544da2)
init1_224 = u64(0x73e1996689dcd4d6) init1_224 = u64(0x73e1996689dcd4d6)
init2_224 = u64(0x1dfab7ae32ff9c82) init2_224 = u64(0x1dfab7ae32ff9c82)
@ -59,6 +59,7 @@ const (
init6_384 = u64(0xdb0c2e0d64f98fa7) init6_384 = u64(0xdb0c2e0d64f98fa7)
init7_384 = u64(0x47b5481dbefa4fa4) init7_384 = u64(0x47b5481dbefa4fa4)
) )
// digest represents the partial evaluation of a checksum. // digest represents the partial evaluation of a checksum.
struct Digest { struct Digest {
mut: mut:
@ -70,8 +71,8 @@ mut:
} }
fn (mut d Digest) reset() { fn (mut d Digest) reset() {
d.h = []u64{len:(8)} d.h = []u64{len: (8)}
d.x = []byte{len:(chunk)} d.x = []byte{len: (chunk)}
match d.function { match d.function {
.sha384 { .sha384 {
d.h[0] = init0_384 d.h[0] = init0_384
@ -112,7 +113,8 @@ fn (mut d Digest) reset() {
d.h[5] = init5 d.h[5] = init5
d.h[6] = init6 d.h[6] = init6
d.h[7] = init7 d.h[7] = init7
}} }
}
d.nx = 0 d.nx = 0
d.len = 0 d.len = 0
} }
@ -147,37 +149,37 @@ fn new384() &Digest {
} }
fn (mut d Digest) write(p_ []byte) int { fn (mut d Digest) write(p_ []byte) int {
mut p := p_ unsafe {
nn := p.len mut p := p_
d.len += u64(nn) nn := p.len
if d.nx > 0 { d.len += u64(nn)
n := copy(d.x[d.nx..], p) if d.nx > 0 {
d.nx += n n := copy(d.x[d.nx..], p)
if d.nx == chunk{ d.nx += n
block(mut d, d.x) if d.nx == chunk {
d.nx = 0 block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
if n >= p.len { if p.len >= chunk {
p = [] n := p.len & ~(chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
}
} }
else { if p.len > 0 {
p = p[n..] 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 { fn (d &Digest) sum(b_in []byte) []byte {
@ -205,19 +207,19 @@ fn (d &Digest) sum(b_in []byte) []byte {
for b in hash { for b in hash {
b_out << b b_out << b
} }
}} }
}
return b_out return b_out
} }
fn (mut d Digest) checksum() []byte { fn (mut d Digest) checksum() []byte {
// Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
mut len := d.len mut len := d.len
mut tmp := []byte{len:(128)} mut tmp := []byte{len: (128)}
tmp[0] = 0x80 tmp[0] = 0x80
if int(len) % 128 < 112 { if int(len) % 128 < 112 {
d.write(tmp[..112 - int(len) % 128]) d.write(tmp[..112 - int(len) % 128])
} } else {
else {
d.write(tmp[..128 + 112 - int(len) % 128]) d.write(tmp[..128 + 112 - int(len) % 128])
} }
// Length in bits. // Length in bits.
@ -228,7 +230,7 @@ fn (mut d Digest) checksum() []byte {
if d.nx != 0 { if d.nx != 0 {
panic('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, d.h[0])
binary.big_endian_put_u64(mut digest[8..], d.h[1]) binary.big_endian_put_u64(mut digest[8..], d.h[1])
binary.big_endian_put_u64(mut digest[16..], d.h[2]) 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) mut d := new_digest(.sha384)
d.write(data) d.write(data)
sum := d.checksum() sum := d.checksum()
sum384 := []byte{len:(size384)} sum384 := []byte{len: (size384)}
copy(sum384, sum[..size384]) copy(sum384, sum[..size384])
return sum384 return sum384
} }
@ -264,7 +266,7 @@ pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224) mut d := new_digest(.sha512_224)
d.write(data) d.write(data)
sum := d.checksum() sum := d.checksum()
sum224 := []byte{len:(size224)} sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224]) copy(sum224, sum[..size224])
return sum224 return sum224
} }
@ -274,7 +276,7 @@ pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256) mut d := new_digest(.sha512_256)
d.write(data) d.write(data)
sum := d.checksum() sum := d.checksum()
sum256 := []byte{len:(size256)} sum256 := []byte{len: (size256)}
copy(sum256, sum[..size256]) copy(sum256, sum[..size256])
return sum256 return sum256
} }
@ -287,18 +289,11 @@ fn block(mut dig Digest, p []byte) {
pub fn (d &Digest) size() int { pub fn (d &Digest) size() int {
match d.function { match d.function {
.sha512_224 { .sha512_224 { return size224 }
return size224 .sha512_256 { return size256 }
} .sha384 { return size384 }
.sha512_256 { else { return size }
return size256 }
}
.sha384 {
return size384
}
else {
return size
}}
} }
pub fn (d &Digest) block_size() int { pub fn (d &Digest) block_size() int {

View File

@ -10,153 +10,99 @@ module sha512
import math.bits import math.bits
const ( const (
_k = [u64(0x428a2f98d728ae22), _k = [u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
u64(0x7137449123ef65cd), u64(0x3956c25bf348b538), u64(0x59f111f1b605d019), u64(0x923f82a4af194f9b), u64(0xab1c5ed5da6d8118),
u64(0xb5c0fbcfec4d3b2f), u64(0xd807aa98a3030242), u64(0x12835b0145706fbe), u64(0x243185be4ee4b28c), u64(0x550c7dc3d5ffb4e2),
u64(0xe9b5dba58189dbbc), u64(0x72be5d74f27b896f), u64(0x80deb1fe3b1696b1), u64(0x9bdc06a725c71235), u64(0xc19bf174cf692694),
u64(0x3956c25bf348b538), u64(0xe49b69c19ef14ad2), u64(0xefbe4786384f25e3), u64(0x0fc19dc68b8cd5b5), u64(0x240ca1cc77ac9c65),
u64(0x59f111f1b605d019), u64(0x2de92c6f592b0275), u64(0x4a7484aa6ea6e483), u64(0x5cb0a9dcbd41fbd4), u64(0x76f988da831153b5),
u64(0x923f82a4af194f9b), u64(0x983e5152ee66dfab), u64(0xa831c66d2db43210), u64(0xb00327c898fb213f), u64(0xbf597fc7beef0ee4),
u64(0xab1c5ed5da6d8118), u64(0xc6e00bf33da88fc2), u64(0xd5a79147930aa725), u64(0x06ca6351e003826f), u64(0x142929670a0e6e70),
u64(0xd807aa98a3030242), u64(0x27b70a8546d22ffc), u64(0x2e1b21385c26c926), u64(0x4d2c6dfc5ac42aed), u64(0x53380d139d95b3df),
u64(0x12835b0145706fbe), u64(0x650a73548baf63de), u64(0x766a0abb3c77b2a8), u64(0x81c2c92e47edaee6), u64(0x92722c851482353b),
u64(0x243185be4ee4b28c), u64(0xa2bfe8a14cf10364), u64(0xa81a664bbc423001), u64(0xc24b8b70d0f89791), u64(0xc76c51a30654be30),
u64(0x550c7dc3d5ffb4e2), u64(0xd192e819d6ef5218), u64(0xd69906245565a910), u64(0xf40e35855771202a), u64(0x106aa07032bbd1b8),
u64(0x72be5d74f27b896f), u64(0x19a4c116b8d2d0c8), u64(0x1e376c085141ab53), u64(0x2748774cdf8eeb99), u64(0x34b0bcb5e19b48a8),
u64(0x80deb1fe3b1696b1), u64(0x391c0cb3c5c95a63), u64(0x4ed8aa4ae3418acb), u64(0x5b9cca4f7763e373), u64(0x682e6ff3d6b2b8a3),
u64(0x9bdc06a725c71235), u64(0x748f82ee5defb2fc), u64(0x78a5636f43172f60), u64(0x84c87814a1f0ab72), u64(0x8cc702081a6439ec),
u64(0xc19bf174cf692694), u64(0x90befffa23631e28), u64(0xa4506cebde82bde9), u64(0xbef9a3f7b2c67915), u64(0xc67178f2e372532b),
u64(0xe49b69c19ef14ad2), u64(0xca273eceea26619c), u64(0xd186b8c721c0c207), u64(0xeada7dd6cde0eb1e), u64(0xf57d4f7fee6ed178),
u64(0xefbe4786384f25e3), u64(0x06f067aa72176fba), u64(0x0a637dc5a2c898a6), u64(0x113f9804bef90dae), u64(0x1b710b35131c471b),
u64(0x0fc19dc68b8cd5b5), u64(0x28db77f523047d84), u64(0x32caab7b40c72493), u64(0x3c9ebe0a15c9bebc), u64(0x431d67c49c100d4c),
u64(0x240ca1cc77ac9c65), u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817)]
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) { fn block_generic(mut dig Digest, p_ []byte) {
mut p := p_ unsafe {
mut w := []u64{len:(80)} mut p := p_
mut h0 := dig.h[0] mut w := []u64{len: (80)}
mut h1 := dig.h[1] mut h0 := dig.h[0]
mut h2 := dig.h[2] mut h1 := dig.h[1]
mut h3 := dig.h[3] mut h2 := dig.h[2]
mut h4 := dig.h[4] mut h3 := dig.h[3]
mut h5 := dig.h[5] mut h4 := dig.h[4]
mut h6 := dig.h[6] mut h5 := dig.h[5]
mut h7 := dig.h[7] mut h6 := dig.h[6]
for p.len >= chunk { mut h7 := dig.h[7]
for i in 0..16 { for p.len >= chunk {
j := i * 8 for i in 0 .. 16 {
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]) j := i * 8
} w[i] = (u64(p[j]) << 56) |
for i := 16; i < 80; i++ { (u64(p[j + 1]) << 48) | (u64(p[j + 2]) << 40) |
v1 := w[i - 2] (u64(p[j + 3]) << 32) | (u64(p[j + 4]) << 24) |
t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1>>6) (u64(p[j + 5]) << 16) | (u64(p[j + 6]) << 8) | u64(p[j + 7])
v2 := w[i - 15] }
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2>>7) for i := 16; i < 80; i++ {
w[i] = t1 + w[i - 7] + t2 + w[i - 16] v1 := w[i - 2]
} t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1 >> 6)
mut a := h0 v2 := w[i - 15]
mut b := h1 t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7)
mut c := h2 w[i] = t1 + w[i - 7] + t2 + w[i - 16]
mut d := h3 }
mut e := h4 mut a := h0
mut f := h5 mut b := h1
mut g := h6 mut c := h2
mut h := h7 mut d := h3
for i in 0..80 { mut e := h4
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] mut f := h5
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)) mut g := h6
h = g mut h := h7
g = f for i in 0 .. 80 {
f = e t1 := h +
e = d + t1 (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) +
d = c ((e & f) ^ (~e & g)) + _k[i] + w[i]
c = b t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) +
b = a ((a & b) ^ (a & c) ^ (b & c))
a = t1 + t2 h = g
} g = f
h0 += a f = e
h1 += b e = d + t1
h2 += c d = c
h3 += d c = b
h4 += e b = a
h5 += f a = t1 + t2
h6 += g }
h7 += h h0 += a
if chunk >= p.len { h1 += b
p = [] h2 += c
} h3 += d
else { h4 += e
p = p[chunk..] 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
} }

View File

@ -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 // TODO replace all c.pref.translated checks with `$if !translated` for performance
continue continue
} }
if left_sym.kind == .array && if left_sym.kind == .array && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign] &&
assign_stmt.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident && right_sym.kind == .array && left is ast.Ident && right is ast.Ident {
right is ast.Ident {
// Do not allow `a = b`, only `a = b.clone()` // 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() left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
if left_is_ptr { if left_is_ptr {

View File

@ -9,24 +9,24 @@ import crypto.sha512
import cli { Command } import cli { Command }
struct TestAliasInStruct { struct TestAliasInStruct {
time t.Time time Time
} }
fn test_import() { fn test_import() {
info := l.Level.info info := l.Level.info
assert info == .info assert info == .info
assert term.white('INFO') == white('INFO') assert white('INFO') == white('INFO')
assert os.o_rdonly == os.o_rdonly assert os.o_rdonly == os.o_rdonly
assert t.month_days[0] == t.month_days[0] assert t.month_days[0] == t.month_days[0]
assert sha256.size == sha256.size assert sha256.size == sha256.size
assert math.pi == math.pi assert math.pi == math.pi
assert sha512.size == sha512.size assert sha512.size == sha512.size
assert md5.sum('module'.bytes()).hex() == sum('module'.bytes()).hex() assert sum('module'.bytes()).hex() == sum('module'.bytes()).hex()
assert t.utc().unix_time() == utc().unix_time() assert utc().unix_time() == utc().unix_time()
} }
fn test_imports_array_as_fn_arg() { fn test_imports_array_as_fn_arg() {
mut cmd := Command { mut cmd := Command{
name: 'module test' name: 'module test'
} }
c1 := Command{} c1 := Command{}
@ -38,7 +38,7 @@ fn test_imports_array_as_fn_arg() {
fn test_alias_in_struct_field() { fn test_alias_in_struct_field() {
a := TestAliasInStruct{ a := TestAliasInStruct{
time: t.Time{ time: Time{
year: 2020 year: 2020
} }
} }