[]: update all examples and tests

pull/2760/head
Alexander Medvednikov 2019-11-14 10:00:22 +03:00
parent 96b530cf85
commit 7d924679a6
10 changed files with 390 additions and 389 deletions

View File

@ -174,7 +174,7 @@ fn (g mut Game) init_game() {
g.parse_tetros() g.parse_tetros()
rand.seed(time.now().uni) rand.seed(time.now().uni)
g.generate_tetro() g.generate_tetro()
g.field = []array_int // TODO: g.field = [][]int g.field = [] // TODO: g.field = [][]int
// Generate the field, fill it with 0's, add -1's on each edge // Generate the field, fill it with 0's, add -1's on each edge
for i := 0; i < FieldHeight + 2; i++ { for i := 0; i < FieldHeight + 2; i++ {
mut row := [0].repeat(FieldWidth + 2) mut row := [0].repeat(FieldWidth + 2)

View File

@ -143,7 +143,7 @@ pub fn run_repl() []string {
os.write_file(file, source_code) os.write_file(file, source_code)
s := os.exec('"$vexe" run $file -repl') or { s := os.exec('"$vexe" run $file -repl') or {
rerror(err) rerror(err)
return []string return []
} }
print_output(s) print_output(s)
} }
@ -160,7 +160,7 @@ pub fn run_repl() []string {
s := os.exec('"$vexe" run $temp_file -repl') or { s := os.exec('"$vexe" run $temp_file -repl') or {
println("SDFSDF") println("SDFSDF")
rerror(err) rerror(err)
return []string return []
} }
if !func_call && s.exit_code == 0 && !temp_flag { if !func_call && s.exit_code == 0 && !temp_flag {
for r.temp_lines.len > 0 { for r.temp_lines.len > 0 {

View File

@ -65,7 +65,7 @@ struct K {
} }
fn test_empty() { fn test_empty() {
mut chunks := [] mut chunks := []Chunk
a := Chunk{} a := Chunk{}
assert chunks.len == 0 assert chunks.len == 0
chunks << a chunks << a
@ -346,7 +346,7 @@ fn (t Test) str() string {
fn test_struct_print() { fn test_struct_print() {
mut a := Test { mut a := Test {
a: 'Test', a: 'Test',
b: []Test2 b: []
} }
b := Test2 { b := Test2 {
one: 1, one: 1,

View File

@ -70,7 +70,7 @@ pub fn (x mut AesCbc) encrypt_blocks(dst mut []byte, src_ []byte) {
// Move to the next block with this block as the next iv. // Move to the next block with this block as the next iv.
iv = dst[..x.block_size] iv = dst[..x.block_size]
if x.block_size >= src.len { if x.block_size >= src.len {
src = []byte src = []
} else { } else {
src = src[x.block_size..] src = src[x.block_size..]
} }

View File

@ -109,7 +109,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
h4 += e h4 += e
if chunk >= p.len { if chunk >= p.len {
p = []byte p = []
} else { } else {
p = p[chunk..] p = p[chunk..]
} }

View File

@ -1,218 +1,218 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 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
const ( 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
) )
const ( const (
chunk = 64 chunk = 64
init0 = 0x6A09E667 init0 = 0x6A09E667
init1 = 0xBB67AE85 init1 = 0xBB67AE85
init2 = 0x3C6EF372 init2 = 0x3C6EF372
init3 = 0xA54FF53A init3 = 0xA54FF53A
init4 = 0x510E527F init4 = 0x510E527F
init5 = 0x9B05688C init5 = 0x9B05688C
init6 = 0x1F83D9AB init6 = 0x1F83D9AB
init7 = 0x5BE0CD19 init7 = 0x5BE0CD19
init0_224 = 0xC1059ED8 init0_224 = 0xC1059ED8
init1_224 = 0x367CD507 init1_224 = 0x367CD507
init2_224 = 0x3070DD17 init2_224 = 0x3070DD17
init3_224 = 0xF70E5939 init3_224 = 0xF70E5939
init4_224 = 0xFFC00B31 init4_224 = 0xFFC00B31
init5_224 = 0x68581511 init5_224 = 0x68581511
init6_224 = 0x64F98FA7 init6_224 = 0x64F98FA7
init7_224 = 0xBEFA4FA4 init7_224 = 0xBEFA4FA4
) )
// digest represents the partial evaluation of a checksum. // digest represents the partial evaluation of a checksum.
struct Digest { struct Digest {
mut: mut:
h []u32 h []u32
x []byte x []byte
nx int nx int
len u64 len u64
is224 bool // mark if this digest is SHA-224 is224 bool // mark if this digest is SHA-224
} }
fn (d mut Digest) reset() { fn (d mut Digest) reset() {
d.h = [u32(0)].repeat(8) d.h = [u32(0)].repeat(8)
d.x = [byte(0)].repeat(chunk) d.x = [byte(0)].repeat(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)
d.h[2] = u32(init2) d.h[2] = u32(init2)
d.h[3] = u32(init3) d.h[3] = u32(init3)
d.h[4] = u32(init4) d.h[4] = u32(init4)
d.h[5] = u32(init5) d.h[5] = u32(init5)
d.h[6] = u32(init6) d.h[6] = u32(init6)
d.h[7] = u32(init7) d.h[7] = u32(init7)
} else { } else {
d.h[0] = u32(init0_224) d.h[0] = u32(init0_224)
d.h[1] = u32(init1_224) d.h[1] = u32(init1_224)
d.h[2] = u32(init2_224) d.h[2] = u32(init2_224)
d.h[3] = u32(init3_224) d.h[3] = u32(init3_224)
d.h[4] = u32(init4_224) d.h[4] = u32(init4_224)
d.h[5] = u32(init5_224) d.h[5] = u32(init5_224)
d.h[6] = u32(init6_224) d.h[6] = u32(init6_224)
d.h[7] = u32(init7_224) d.h[7] = u32(init7_224)
} }
d.nx = 0 d.nx = 0
d.len = 0 d.len = 0
} }
// new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum. // new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum.
pub fn new() &Digest { pub fn new() &Digest {
mut d := &Digest{} mut d := &Digest{}
d.reset() d.reset()
return d return d
} }
// new224 returns a new Digest (implementing hash.Hash) computing the SHA224 checksum. // new224 returns a new Digest (implementing hash.Hash) computing the SHA224 checksum.
pub fn new224() &Digest { pub fn new224() &Digest {
mut d := &Digest{} mut d := &Digest{}
d.is224 = true d.is224 = true
d.reset() d.reset()
return d return d
} }
fn (d mut Digest) write(p_ []byte) int { fn (d mut Digest) write(p_ []byte) int {
mut p := p_ mut p := p_
nn := p.len nn := p.len
d.len += u64(nn) d.len += u64(nn)
if d.nx > 0 { if d.nx > 0 {
n := copy(d.x[d.nx..], p) n := copy(d.x[d.nx..], p)
d.nx += n d.nx += n
if d.nx == chunk { if d.nx == chunk {
block(mut d, d.x) block(mut d, d.x)
d.nx = 0 d.nx = 0
} }
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }
} }
if p.len >= chunk { if p.len >= chunk {
n := p.len &~ (chunk - 1) n := p.len &~ (chunk - 1)
block(mut d, p[..n]) block(mut d, p[..n])
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }
} }
if p.len > 0 { if p.len > 0 {
d.nx = copy(d.x, p) d.nx = copy(d.x, p)
} }
return nn return nn
} }
fn (d &Digest) sum(b_in []byte) []byte { fn (d &Digest) sum(b_in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing. // Make a copy of d so that caller can keep writing and summing.
mut d0 := *d mut d0 := *d
hash := d0.checksum() hash := d0.checksum()
mut b_out := b_in.clone() mut b_out := b_in.clone()
if d0.is224 { if d0.is224 {
for b in hash[..size224] { for b in hash[..size224] {
b_out << b b_out << b
} }
} else { } else {
for b in hash { for b in hash {
b_out << b b_out << b
} }
} }
return b_out return b_out
} }
fn (d mut Digest) checksum() []byte { fn (d mut 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(0)].repeat(64) mut tmp := [byte(0)].repeat(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')
} }
digest := [byte(0)].repeat(size) digest := [byte(0)].repeat(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])
binary.big_endian_put_u32(mut digest[12..], d.h[3]) binary.big_endian_put_u32(mut digest[12..], d.h[3])
binary.big_endian_put_u32(mut digest[16..], d.h[4]) binary.big_endian_put_u32(mut digest[16..], d.h[4])
binary.big_endian_put_u32(mut digest[20..], d.h[5]) binary.big_endian_put_u32(mut digest[20..], d.h[5])
binary.big_endian_put_u32(mut digest[24..], d.h[6]) binary.big_endian_put_u32(mut digest[24..], d.h[6])
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
} }
// sum256 returns the SHA256 checksum of the data. // sum256 returns the SHA256 checksum of the data.
pub fn sum(data []byte) []byte { pub fn sum(data []byte) []byte {
return sum256(data) return sum256(data)
} }
// sum256 returns the SHA256 checksum of the data. // sum256 returns the SHA256 checksum of the data.
pub fn sum256(data []byte) []byte { pub fn sum256(data []byte) []byte {
mut d := new() mut d := new()
d.write(data) d.write(data)
return d.checksum() return d.checksum()
} }
// sum224 returns the SHA224 checksum of the data. // sum224 returns the SHA224 checksum of the data.
pub fn sum224(data []byte) []byte { pub fn sum224(data []byte) []byte {
mut d := new224() mut d := new224()
d.write(data) d.write(data)
sum := d.checksum() sum := d.checksum()
mut sum224 := [byte(0)].repeat(size224) mut sum224 := [byte(0)].repeat(size224)
copy(sum224, sum[..size224]) copy(sum224, sum[..size224])
return sum224 return sum224
} }
fn block(dig mut Digest, p []byte) { fn block(dig mut 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 { pub fn (d &Digest) size() int {
if !d.is224 { if !d.is224 {
return size return size
} }
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 { return sum256(s.bytes()).hex() }
pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() } pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() }

View File

@ -5,5 +5,6 @@
import crypto.sha256 import crypto.sha256
fn test_crypto_sha256() { fn test_crypto_sha256() {
assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() ==
'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'
} }

View File

@ -1,159 +1,159 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 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
const ( const (
_k = [ _k = [
0x428a2f98, 0x428a2f98,
0x71374491, 0x71374491,
0xb5c0fbcf, 0xb5c0fbcf,
0xe9b5dba5, 0xe9b5dba5,
0x3956c25b, 0x3956c25b,
0x59f111f1, 0x59f111f1,
0x923f82a4, 0x923f82a4,
0xab1c5ed5, 0xab1c5ed5,
0xd807aa98, 0xd807aa98,
0x12835b01, 0x12835b01,
0x243185be, 0x243185be,
0x550c7dc3, 0x550c7dc3,
0x72be5d74, 0x72be5d74,
0x80deb1fe, 0x80deb1fe,
0x9bdc06a7, 0x9bdc06a7,
0xc19bf174, 0xc19bf174,
0xe49b69c1, 0xe49b69c1,
0xefbe4786, 0xefbe4786,
0x0fc19dc6, 0x0fc19dc6,
0x240ca1cc, 0x240ca1cc,
0x2de92c6f, 0x2de92c6f,
0x4a7484aa, 0x4a7484aa,
0x5cb0a9dc, 0x5cb0a9dc,
0x76f988da, 0x76f988da,
0x983e5152, 0x983e5152,
0xa831c66d, 0xa831c66d,
0xb00327c8, 0xb00327c8,
0xbf597fc7, 0xbf597fc7,
0xc6e00bf3, 0xc6e00bf3,
0xd5a79147, 0xd5a79147,
0x06ca6351, 0x06ca6351,
0x14292967, 0x14292967,
0x27b70a85, 0x27b70a85,
0x2e1b2138, 0x2e1b2138,
0x4d2c6dfc, 0x4d2c6dfc,
0x53380d13, 0x53380d13,
0x650a7354, 0x650a7354,
0x766a0abb, 0x766a0abb,
0x81c2c92e, 0x81c2c92e,
0x92722c85, 0x92722c85,
0xa2bfe8a1, 0xa2bfe8a1,
0xa81a664b, 0xa81a664b,
0xc24b8b70, 0xc24b8b70,
0xc76c51a3, 0xc76c51a3,
0xd192e819, 0xd192e819,
0xd6990624, 0xd6990624,
0xf40e3585, 0xf40e3585,
0x106aa070, 0x106aa070,
0x19a4c116, 0x19a4c116,
0x1e376c08, 0x1e376c08,
0x2748774c, 0x2748774c,
0x34b0bcb5, 0x34b0bcb5,
0x391c0cb3, 0x391c0cb3,
0x4ed8aa4a, 0x4ed8aa4a,
0x5b9cca4f, 0x5b9cca4f,
0x682e6ff3, 0x682e6ff3,
0x748f82ee, 0x748f82ee,
0x78a5636f, 0x78a5636f,
0x84c87814, 0x84c87814,
0x8cc70208, 0x8cc70208,
0x90befffa, 0x90befffa,
0xa4506ceb, 0xa4506ceb,
0xbef9a3f7, 0xbef9a3f7,
0xc67178f2, 0xc67178f2,
] ]
) )
fn block_generic(dig mut Digest, p_ []byte) { fn block_generic(dig mut Digest, p_ []byte) {
mut p := p_ mut p := p_
mut w := [u32(0)].repeat(64) mut w := [u32(0)].repeat(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 { for p.len >= chunk {
// Can interlace the computation of w with the // Can interlace the computation of w with the
// rounds below if needed for speed. // rounds below if needed for speed.
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
j := i * 4 j := i * 4
w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3]) 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++ { for i := 16; i < 64; i++ {
v1 := w[i-2] v1 := w[i-2]
t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10) t1 := (bits.rotate_left_32(v1, -17)) ^ (bits.rotate_left_32(v1, -19)) ^ (v1 >> 10)
v2 := w[i-15] v2 := w[i-15]
t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3) 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] w[i] = t1 + w[i-7] + t2 + w[i-16]
} }
mut a := h0 mut a := h0
mut b := h1 mut b := h1
mut c := h2 mut c := h2
mut d := h3 mut d := h3
mut e := h4 mut e := h4
mut f := h5 mut f := h5
mut g := h6 mut g := h6
mut h := h7 mut h := h7
for i := 0; i < 64; i++ { for i := 0; i < 64; i++ {
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] 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)) 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 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 h0 += a
h1 += b h1 += b
h2 += c h2 += c
h3 += d h3 += d
h4 += e h4 += e
h5 += f h5 += f
h6 += g h6 += g
h7 += h h7 += h
if chunk >= p.len { if chunk >= p.len {
p = []byte p = []
} else { } else {
p = p[chunk..] p = p[chunk..]
} }
} }
dig.h[0] = h0 dig.h[0] = h0
dig.h[1] = h1 dig.h[1] = h1
dig.h[2] = h2 dig.h[2] = h2
dig.h[3] = h3 dig.h[3] = h3
dig.h[4] = h4 dig.h[4] = h4
dig.h[5] = h5 dig.h[5] = h5
dig.h[6] = h6 dig.h[6] = h6
dig.h[7] = h7 dig.h[7] = h7
} }

View File

@ -158,7 +158,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
h7 += h h7 += h
if Chunk >= p.len { if Chunk >= p.len {
p = []byte p = []
} else { } else {
p = p[Chunk..] p = p[Chunk..]
} }

View File

@ -144,7 +144,7 @@ fn test_finalize_returns_error_for_unknown_flags() {
} }
fn test_allow_to_build_usage_message() { fn test_allow_to_build_usage_message() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([])
fp.limit_free_args(1, 4) fp.limit_free_args(1, 4)
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
@ -177,7 +177,7 @@ fn test_allow_to_build_usage_message() {
} }
fn test_if_no_description_given_usage_message_does_not_contain_descpription() { fn test_if_no_description_given_usage_message_does_not_contain_descpription() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([])
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
@ -187,7 +187,7 @@ fn test_if_no_description_given_usage_message_does_not_contain_descpription() {
} }
fn test_if_no_options_given_usage_message_does_not_contain_options() { fn test_if_no_options_given_usage_message_does_not_contain_options() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([])
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')