mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
[]: update all examples and tests
This commit is contained in:
parent
96b530cf85
commit
7d924679a6
@ -174,7 +174,7 @@ fn (g mut Game) init_game() {
|
||||
g.parse_tetros()
|
||||
rand.seed(time.now().uni)
|
||||
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
|
||||
for i := 0; i < FieldHeight + 2; i++ {
|
||||
mut row := [0].repeat(FieldWidth + 2)
|
||||
|
@ -143,7 +143,7 @@ pub fn run_repl() []string {
|
||||
os.write_file(file, source_code)
|
||||
s := os.exec('"$vexe" run $file -repl') or {
|
||||
rerror(err)
|
||||
return []string
|
||||
return []
|
||||
}
|
||||
print_output(s)
|
||||
}
|
||||
@ -160,7 +160,7 @@ pub fn run_repl() []string {
|
||||
s := os.exec('"$vexe" run $temp_file -repl') or {
|
||||
println("SDFSDF")
|
||||
rerror(err)
|
||||
return []string
|
||||
return []
|
||||
}
|
||||
if !func_call && s.exit_code == 0 && !temp_flag {
|
||||
for r.temp_lines.len > 0 {
|
||||
|
@ -65,7 +65,7 @@ struct K {
|
||||
}
|
||||
|
||||
fn test_empty() {
|
||||
mut chunks := []
|
||||
mut chunks := []Chunk
|
||||
a := Chunk{}
|
||||
assert chunks.len == 0
|
||||
chunks << a
|
||||
@ -346,7 +346,7 @@ fn (t Test) str() string {
|
||||
fn test_struct_print() {
|
||||
mut a := Test {
|
||||
a: 'Test',
|
||||
b: []Test2
|
||||
b: []
|
||||
}
|
||||
b := Test2 {
|
||||
one: 1,
|
||||
|
@ -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.
|
||||
iv = dst[..x.block_size]
|
||||
if x.block_size >= src.len {
|
||||
src = []byte
|
||||
src = []
|
||||
} else {
|
||||
src = src[x.block_size..]
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
h4 += e
|
||||
|
||||
if chunk >= p.len {
|
||||
p = []byte
|
||||
p = []
|
||||
} else {
|
||||
p = p[chunk..]
|
||||
}
|
||||
|
@ -1,218 +1,218 @@
|
||||
// Copyright (c) 2019 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
|
||||
|
||||
const (
|
||||
// The size of a SHA256 checksum in bytes.
|
||||
size = 32
|
||||
// The size of a SHA224 checksum in bytes.
|
||||
size224 = 28
|
||||
// The blocksize of SHA256 and SHA224 in bytes.
|
||||
block_size = 64
|
||||
)
|
||||
|
||||
const (
|
||||
chunk = 64
|
||||
init0 = 0x6A09E667
|
||||
init1 = 0xBB67AE85
|
||||
init2 = 0x3C6EF372
|
||||
init3 = 0xA54FF53A
|
||||
init4 = 0x510E527F
|
||||
init5 = 0x9B05688C
|
||||
init6 = 0x1F83D9AB
|
||||
init7 = 0x5BE0CD19
|
||||
init0_224 = 0xC1059ED8
|
||||
init1_224 = 0x367CD507
|
||||
init2_224 = 0x3070DD17
|
||||
init3_224 = 0xF70E5939
|
||||
init4_224 = 0xFFC00B31
|
||||
init5_224 = 0x68581511
|
||||
init6_224 = 0x64F98FA7
|
||||
init7_224 = 0xBEFA4FA4
|
||||
)
|
||||
|
||||
// digest represents the partial evaluation of a checksum.
|
||||
struct Digest {
|
||||
mut:
|
||||
h []u32
|
||||
x []byte
|
||||
nx int
|
||||
len u64
|
||||
is224 bool // mark if this digest is SHA-224
|
||||
}
|
||||
|
||||
fn (d mut Digest) reset() {
|
||||
d.h = [u32(0)].repeat(8)
|
||||
d.x = [byte(0)].repeat(chunk)
|
||||
if !d.is224 {
|
||||
d.h[0] = u32(init0)
|
||||
d.h[1] = u32(init1)
|
||||
d.h[2] = u32(init2)
|
||||
d.h[3] = u32(init3)
|
||||
d.h[4] = u32(init4)
|
||||
d.h[5] = u32(init5)
|
||||
d.h[6] = u32(init6)
|
||||
d.h[7] = u32(init7)
|
||||
} else {
|
||||
d.h[0] = u32(init0_224)
|
||||
d.h[1] = u32(init1_224)
|
||||
d.h[2] = u32(init2_224)
|
||||
d.h[3] = u32(init3_224)
|
||||
d.h[4] = u32(init4_224)
|
||||
d.h[5] = u32(init5_224)
|
||||
d.h[6] = u32(init6_224)
|
||||
d.h[7] = u32(init7_224)
|
||||
}
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
}
|
||||
|
||||
// new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum.
|
||||
pub fn new() &Digest {
|
||||
mut d := &Digest{}
|
||||
d.reset()
|
||||
return d
|
||||
}
|
||||
|
||||
// new224 returns a new Digest (implementing hash.Hash) computing the SHA224 checksum.
|
||||
pub fn new224() &Digest {
|
||||
mut d := &Digest{}
|
||||
d.is224 = true
|
||||
d.reset()
|
||||
return d
|
||||
}
|
||||
|
||||
fn (d mut 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
|
||||
}
|
||||
if n >= p.len {
|
||||
p = []byte
|
||||
} else {
|
||||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= chunk {
|
||||
n := p.len &~ (chunk - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []byte
|
||||
} else {
|
||||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len > 0 {
|
||||
d.nx = copy(d.x, p)
|
||||
}
|
||||
return nn
|
||||
}
|
||||
|
||||
fn (d &Digest) sum(b_in []byte) []byte {
|
||||
// Make a copy of d so that caller can keep writing and summing.
|
||||
mut d0 := *d
|
||||
hash := d0.checksum()
|
||||
mut b_out := b_in.clone()
|
||||
if d0.is224 {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
}
|
||||
} else {
|
||||
for b in hash {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
return b_out
|
||||
}
|
||||
|
||||
fn (d mut Digest) checksum() []byte {
|
||||
mut len := d.len
|
||||
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||
mut tmp := [byte(0)].repeat(64)
|
||||
tmp[0] = 0x80
|
||||
if int(len)%64 < 56 {
|
||||
d.write(tmp[..56-int(len)%64])
|
||||
} else {
|
||||
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')
|
||||
}
|
||||
|
||||
digest := [byte(0)].repeat(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])
|
||||
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[20..], d.h[5])
|
||||
binary.big_endian_put_u32(mut digest[24..], d.h[6])
|
||||
if !d.is224 {
|
||||
binary.big_endian_put_u32(mut digest[28..], d.h[7])
|
||||
}
|
||||
|
||||
return digest
|
||||
}
|
||||
|
||||
// sum256 returns the SHA256 checksum of the data.
|
||||
pub fn sum(data []byte) []byte {
|
||||
return sum256(data)
|
||||
}
|
||||
|
||||
// sum256 returns the SHA256 checksum of the data.
|
||||
pub fn sum256(data []byte) []byte {
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
// sum224 returns the SHA224 checksum of the data.
|
||||
pub fn sum224(data []byte) []byte {
|
||||
mut d := new224()
|
||||
d.write(data)
|
||||
sum := d.checksum()
|
||||
mut sum224 := [byte(0)].repeat(size224)
|
||||
copy(sum224, sum[..size224])
|
||||
return sum224
|
||||
}
|
||||
|
||||
fn block(dig mut Digest, p []byte) {
|
||||
// For now just use block_generic until we have specific
|
||||
// architecture optimized versions
|
||||
block_generic(mut dig, p)
|
||||
}
|
||||
|
||||
pub fn (d &Digest) size() int {
|
||||
if !d.is224 {
|
||||
return size
|
||||
}
|
||||
return size224
|
||||
}
|
||||
|
||||
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() }
|
||||
// Copyright (c) 2019 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
|
||||
|
||||
const (
|
||||
// The size of a SHA256 checksum in bytes.
|
||||
size = 32
|
||||
// The size of a SHA224 checksum in bytes.
|
||||
size224 = 28
|
||||
// The blocksize of SHA256 and SHA224 in bytes.
|
||||
block_size = 64
|
||||
)
|
||||
|
||||
const (
|
||||
chunk = 64
|
||||
init0 = 0x6A09E667
|
||||
init1 = 0xBB67AE85
|
||||
init2 = 0x3C6EF372
|
||||
init3 = 0xA54FF53A
|
||||
init4 = 0x510E527F
|
||||
init5 = 0x9B05688C
|
||||
init6 = 0x1F83D9AB
|
||||
init7 = 0x5BE0CD19
|
||||
init0_224 = 0xC1059ED8
|
||||
init1_224 = 0x367CD507
|
||||
init2_224 = 0x3070DD17
|
||||
init3_224 = 0xF70E5939
|
||||
init4_224 = 0xFFC00B31
|
||||
init5_224 = 0x68581511
|
||||
init6_224 = 0x64F98FA7
|
||||
init7_224 = 0xBEFA4FA4
|
||||
)
|
||||
|
||||
// digest represents the partial evaluation of a checksum.
|
||||
struct Digest {
|
||||
mut:
|
||||
h []u32
|
||||
x []byte
|
||||
nx int
|
||||
len u64
|
||||
is224 bool // mark if this digest is SHA-224
|
||||
}
|
||||
|
||||
fn (d mut Digest) reset() {
|
||||
d.h = [u32(0)].repeat(8)
|
||||
d.x = [byte(0)].repeat(chunk)
|
||||
if !d.is224 {
|
||||
d.h[0] = u32(init0)
|
||||
d.h[1] = u32(init1)
|
||||
d.h[2] = u32(init2)
|
||||
d.h[3] = u32(init3)
|
||||
d.h[4] = u32(init4)
|
||||
d.h[5] = u32(init5)
|
||||
d.h[6] = u32(init6)
|
||||
d.h[7] = u32(init7)
|
||||
} else {
|
||||
d.h[0] = u32(init0_224)
|
||||
d.h[1] = u32(init1_224)
|
||||
d.h[2] = u32(init2_224)
|
||||
d.h[3] = u32(init3_224)
|
||||
d.h[4] = u32(init4_224)
|
||||
d.h[5] = u32(init5_224)
|
||||
d.h[6] = u32(init6_224)
|
||||
d.h[7] = u32(init7_224)
|
||||
}
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
}
|
||||
|
||||
// new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum.
|
||||
pub fn new() &Digest {
|
||||
mut d := &Digest{}
|
||||
d.reset()
|
||||
return d
|
||||
}
|
||||
|
||||
// new224 returns a new Digest (implementing hash.Hash) computing the SHA224 checksum.
|
||||
pub fn new224() &Digest {
|
||||
mut d := &Digest{}
|
||||
d.is224 = true
|
||||
d.reset()
|
||||
return d
|
||||
}
|
||||
|
||||
fn (d mut 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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
fn (d &Digest) sum(b_in []byte) []byte {
|
||||
// Make a copy of d so that caller can keep writing and summing.
|
||||
mut d0 := *d
|
||||
hash := d0.checksum()
|
||||
mut b_out := b_in.clone()
|
||||
if d0.is224 {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
}
|
||||
} else {
|
||||
for b in hash {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
return b_out
|
||||
}
|
||||
|
||||
fn (d mut Digest) checksum() []byte {
|
||||
mut len := d.len
|
||||
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||
mut tmp := [byte(0)].repeat(64)
|
||||
tmp[0] = 0x80
|
||||
if int(len)%64 < 56 {
|
||||
d.write(tmp[..56-int(len)%64])
|
||||
} else {
|
||||
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')
|
||||
}
|
||||
|
||||
digest := [byte(0)].repeat(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])
|
||||
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[20..], d.h[5])
|
||||
binary.big_endian_put_u32(mut digest[24..], d.h[6])
|
||||
if !d.is224 {
|
||||
binary.big_endian_put_u32(mut digest[28..], d.h[7])
|
||||
}
|
||||
|
||||
return digest
|
||||
}
|
||||
|
||||
// sum256 returns the SHA256 checksum of the data.
|
||||
pub fn sum(data []byte) []byte {
|
||||
return sum256(data)
|
||||
}
|
||||
|
||||
// sum256 returns the SHA256 checksum of the data.
|
||||
pub fn sum256(data []byte) []byte {
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
// sum224 returns the SHA224 checksum of the data.
|
||||
pub fn sum224(data []byte) []byte {
|
||||
mut d := new224()
|
||||
d.write(data)
|
||||
sum := d.checksum()
|
||||
mut sum224 := [byte(0)].repeat(size224)
|
||||
copy(sum224, sum[..size224])
|
||||
return sum224
|
||||
}
|
||||
|
||||
fn block(dig mut Digest, p []byte) {
|
||||
// For now just use block_generic until we have specific
|
||||
// architecture optimized versions
|
||||
block_generic(mut dig, p)
|
||||
}
|
||||
|
||||
pub fn (d &Digest) size() int {
|
||||
if !d.is224 {
|
||||
return size
|
||||
}
|
||||
return size224
|
||||
}
|
||||
|
||||
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() }
|
||||
|
@ -5,5 +5,6 @@
|
||||
import 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'
|
||||
}
|
||||
|
@ -1,159 +1,159 @@
|
||||
// Copyright (c) 2019 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
|
||||
|
||||
const (
|
||||
_k = [
|
||||
0x428a2f98,
|
||||
0x71374491,
|
||||
0xb5c0fbcf,
|
||||
0xe9b5dba5,
|
||||
0x3956c25b,
|
||||
0x59f111f1,
|
||||
0x923f82a4,
|
||||
0xab1c5ed5,
|
||||
0xd807aa98,
|
||||
0x12835b01,
|
||||
0x243185be,
|
||||
0x550c7dc3,
|
||||
0x72be5d74,
|
||||
0x80deb1fe,
|
||||
0x9bdc06a7,
|
||||
0xc19bf174,
|
||||
0xe49b69c1,
|
||||
0xefbe4786,
|
||||
0x0fc19dc6,
|
||||
0x240ca1cc,
|
||||
0x2de92c6f,
|
||||
0x4a7484aa,
|
||||
0x5cb0a9dc,
|
||||
0x76f988da,
|
||||
0x983e5152,
|
||||
0xa831c66d,
|
||||
0xb00327c8,
|
||||
0xbf597fc7,
|
||||
0xc6e00bf3,
|
||||
0xd5a79147,
|
||||
0x06ca6351,
|
||||
0x14292967,
|
||||
0x27b70a85,
|
||||
0x2e1b2138,
|
||||
0x4d2c6dfc,
|
||||
0x53380d13,
|
||||
0x650a7354,
|
||||
0x766a0abb,
|
||||
0x81c2c92e,
|
||||
0x92722c85,
|
||||
0xa2bfe8a1,
|
||||
0xa81a664b,
|
||||
0xc24b8b70,
|
||||
0xc76c51a3,
|
||||
0xd192e819,
|
||||
0xd6990624,
|
||||
0xf40e3585,
|
||||
0x106aa070,
|
||||
0x19a4c116,
|
||||
0x1e376c08,
|
||||
0x2748774c,
|
||||
0x34b0bcb5,
|
||||
0x391c0cb3,
|
||||
0x4ed8aa4a,
|
||||
0x5b9cca4f,
|
||||
0x682e6ff3,
|
||||
0x748f82ee,
|
||||
0x78a5636f,
|
||||
0x84c87814,
|
||||
0x8cc70208,
|
||||
0x90befffa,
|
||||
0xa4506ceb,
|
||||
0xbef9a3f7,
|
||||
0xc67178f2,
|
||||
]
|
||||
)
|
||||
|
||||
fn block_generic(dig mut Digest, p_ []byte) {
|
||||
mut p := p_
|
||||
|
||||
mut w := [u32(0)].repeat(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 := 0; i < 16; i++ {
|
||||
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 := 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]
|
||||
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 = []byte
|
||||
} 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
|
||||
}
|
||||
// Copyright (c) 2019 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
|
||||
|
||||
const (
|
||||
_k = [
|
||||
0x428a2f98,
|
||||
0x71374491,
|
||||
0xb5c0fbcf,
|
||||
0xe9b5dba5,
|
||||
0x3956c25b,
|
||||
0x59f111f1,
|
||||
0x923f82a4,
|
||||
0xab1c5ed5,
|
||||
0xd807aa98,
|
||||
0x12835b01,
|
||||
0x243185be,
|
||||
0x550c7dc3,
|
||||
0x72be5d74,
|
||||
0x80deb1fe,
|
||||
0x9bdc06a7,
|
||||
0xc19bf174,
|
||||
0xe49b69c1,
|
||||
0xefbe4786,
|
||||
0x0fc19dc6,
|
||||
0x240ca1cc,
|
||||
0x2de92c6f,
|
||||
0x4a7484aa,
|
||||
0x5cb0a9dc,
|
||||
0x76f988da,
|
||||
0x983e5152,
|
||||
0xa831c66d,
|
||||
0xb00327c8,
|
||||
0xbf597fc7,
|
||||
0xc6e00bf3,
|
||||
0xd5a79147,
|
||||
0x06ca6351,
|
||||
0x14292967,
|
||||
0x27b70a85,
|
||||
0x2e1b2138,
|
||||
0x4d2c6dfc,
|
||||
0x53380d13,
|
||||
0x650a7354,
|
||||
0x766a0abb,
|
||||
0x81c2c92e,
|
||||
0x92722c85,
|
||||
0xa2bfe8a1,
|
||||
0xa81a664b,
|
||||
0xc24b8b70,
|
||||
0xc76c51a3,
|
||||
0xd192e819,
|
||||
0xd6990624,
|
||||
0xf40e3585,
|
||||
0x106aa070,
|
||||
0x19a4c116,
|
||||
0x1e376c08,
|
||||
0x2748774c,
|
||||
0x34b0bcb5,
|
||||
0x391c0cb3,
|
||||
0x4ed8aa4a,
|
||||
0x5b9cca4f,
|
||||
0x682e6ff3,
|
||||
0x748f82ee,
|
||||
0x78a5636f,
|
||||
0x84c87814,
|
||||
0x8cc70208,
|
||||
0x90befffa,
|
||||
0xa4506ceb,
|
||||
0xbef9a3f7,
|
||||
0xc67178f2,
|
||||
]
|
||||
)
|
||||
|
||||
fn block_generic(dig mut Digest, p_ []byte) {
|
||||
mut p := p_
|
||||
|
||||
mut w := [u32(0)].repeat(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 := 0; i < 16; i++ {
|
||||
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 := 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]
|
||||
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
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
h7 += h
|
||||
|
||||
if Chunk >= p.len {
|
||||
p = []byte
|
||||
p = []
|
||||
} else {
|
||||
p = p[Chunk..]
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ fn test_finalize_returns_error_for_unknown_flags() {
|
||||
}
|
||||
|
||||
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.application('flag_tool')
|
||||
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() {
|
||||
mut fp := flag.new_flag_parser([]string)
|
||||
mut fp := flag.new_flag_parser([])
|
||||
fp.application('flag_tool')
|
||||
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() {
|
||||
mut fp := flag.new_flag_parser([]string)
|
||||
mut fp := flag.new_flag_parser([])
|
||||
fp.application('flag_tool')
|
||||
fp.version('v0.0.0')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user