vlib: add `mut` for the first parameter of builtin.copy, arrays.copy and crypto (#13702)

pull/13705/head
Nick Treleaven 2022-03-09 18:26:00 +00:00 committed by GitHub
parent 4c33003f86
commit 7231a3f135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 84 additions and 88 deletions

View File

@ -544,7 +544,7 @@ fn swap_nonoverlapping<T>(x_ &T, y_ &T, count int) {
// copy copies the `src` array elements to the `dst` array.
// The number of the elements copied is the minimum of the length of both arrays.
// Returns the number of elements copied.
pub fn copy<T>(dst []T, src []T) int {
pub fn copy<T>(mut dst []T, src []T) int {
min := if dst.len < src.len { dst.len } else { src.len }
if min > 0 {
blen := min * int(sizeof(T))

View File

@ -268,16 +268,16 @@ fn test_rotate_left_string() {
fn test_copy() {
mut a := [1, 2, 3]
mut b := [4, 5, 6]
assert copy(b, a) == 3
assert copy(mut b, a) == 3
assert b == [1, 2, 3]
// check independent copies
b[0] = 99
assert a[0] == 1
// check longer src
b << 7
assert copy(a, b) == 3
assert copy(mut a, b) == 3
assert a == [99, 2, 3]
// check longer dst
assert copy(b, [8, 9]) == 2
assert copy(mut b, [8, 9]) == 2
assert b == [8, 9, 3, 7]
}

View File

@ -816,7 +816,7 @@ pub fn (b []byte) hex() string {
// Returns the number of elements copied.
// NOTE: This is not an `array` method. It is a function that takes two arrays of bytes.
// See also: `arrays.copy`.
pub fn copy(dst []byte, src []byte) int {
pub fn copy(mut dst []byte, src []byte) int {
min := if dst.len < src.len { dst.len } else { src.len }
if min > 0 {
unsafe { vmemmove(&byte(dst.data), src.data, min) }

View File

@ -151,13 +151,13 @@ fn (mut h Hashed) hash_byte() []byte {
}
arr[n] = `$`
n++
copy(arr[n..], '${int(h.cost):02}'.bytes())
copy(mut arr[n..], '${int(h.cost):02}'.bytes())
n += 2
arr[n] = `$`
n++
copy(arr[n..], h.salt)
copy(mut arr[n..], h.salt)
n += bcrypt.encoded_salt_size
copy(arr[n..], h.hash)
copy(mut arr[n..], h.hash)
n += bcrypt.encoded_hash_size
res := arr[..n].clone()
return res

View File

@ -21,7 +21,7 @@ fn test_aes_cbc() {
fn aes_cbc_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}

View File

@ -18,7 +18,7 @@ fn test_aes_cfb() {
fn aes_cfb_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@ -20,7 +20,7 @@ fn test_aes_ofb() {
fn aes_ofb_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@ -41,7 +41,7 @@ pub fn new_cbc(b Block, iv []byte) Cbc {
// encrypt_blocks encrypts the blocks in `src_` to `dst_`.
// Please note: `dst_` is mutable for performance reasons.
pub fn (x &Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
pub fn (mut x Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@ -69,7 +69,7 @@ pub fn (x &Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
dst = dst[x.block_size..]
}
// Save the iv for the next crypt_blocks call.
copy(x.iv, iv)
copy(mut x.iv, iv)
}
}
@ -94,7 +94,7 @@ pub fn (mut x Cbc) decrypt_blocks(mut dst []byte, src []byte) {
mut start := end - x.block_size
mut prev := start - x.block_size
// Copy the last block of ciphertext in preparation as the new iv.
copy(x.tmp, src[start..end])
copy(mut x.tmp, src[start..end])
// Loop over all but the first block.
for start > 0 {
src_chunk := src[start..end]
@ -113,9 +113,9 @@ pub fn (mut x Cbc) decrypt_blocks(mut dst []byte, src []byte) {
x.tmp = x.iv
}
fn (x &Cbc) set_iv(iv []byte) {
fn (mut x Cbc) set_iv(iv []byte) {
if iv.len != x.iv.len {
panic('cipher: incorrect length IV')
}
copy(x.iv, iv)
copy(mut x.iv, iv)
}

View File

@ -39,20 +39,18 @@ fn new_cfb(b Block, iv []byte, decrypt bool) Cfb {
if iv.len != block_size {
panic('cipher.new_cfb: IV length must be equal block size')
}
x := Cfb{
mut x := Cfb{
b: b
out: []byte{len: b.block_size}
next: []byte{len: b.block_size}
out_used: block_size
decrypt: decrypt
}
copy(x.next, iv)
copy(mut x.next, iv)
return x
}
pub fn (x &Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
pub fn (mut x Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@ -71,12 +69,12 @@ pub fn (x &Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
}
if x.decrypt {
copy(x.next[x.out_used..], src)
copy(mut x.next[x.out_used..], src)
}
n := xor_bytes(mut dst, src, x.out[x.out_used..])
if !x.decrypt {
copy(x.next[x.out_used..], dst)
copy(mut x.next[x.out_used..], dst)
}
dst = dst[n..]
src = src[n..]

View File

@ -50,6 +50,6 @@ interface BlockMode {
// fn dup(p []byte) []byte {
// q := make([]byte, p.len)
// copy(q, p)
// copy(mut q, p)
// return q
// }

View File

@ -31,7 +31,7 @@ fn test_des_cbc() {
fn des_cbc_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}
@ -43,7 +43,7 @@ fn des_cbc_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_cbc_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}

View File

@ -31,7 +31,7 @@ fn test_des_cfb() {
fn des_cfb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
@ -43,12 +43,12 @@ fn des_cfb_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_cfb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
fn triple_des_cfb_de(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cfb_decrypter(block, iv)
mut mode := cipher.new_cfb_decrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@ -31,7 +31,7 @@ fn test_des_ofb() {
fn des_ofb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
@ -43,7 +43,7 @@ fn des_ofb_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_ofb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@ -25,19 +25,17 @@ pub fn new_ofb(b Block, iv []byte) Ofb {
if iv.len != block_size {
panic('cipher.new_ofb: IV length must be equal block size')
}
x := Ofb{
mut x := Ofb{
b: b
out: []byte{len: b.block_size}
next: []byte{len: b.block_size}
out_used: block_size
}
copy(x.next, iv)
copy(mut x.next, iv)
return x
}
pub fn (x &Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
pub fn (mut x Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@ -55,7 +53,7 @@ pub fn (x &Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
x.out_used = 0
}
copy(x.next, x.out)
copy(mut x.next, x.out)
n := xor_bytes(mut dst, src, x.out)
dst = dst[n..]

View File

@ -32,7 +32,7 @@ pub type PrivateKey = []byte
// RFC 8032's private keys correspond to seeds in this module.
pub fn (priv PrivateKey) seed() []byte {
mut seed := []byte{len: ed25519.seed_size}
copy(seed, priv[..32])
copy(mut seed, priv[..32])
return seed
}
@ -40,7 +40,7 @@ pub fn (priv PrivateKey) seed() []byte {
pub fn (priv PrivateKey) public_key() PublicKey {
assert priv.len == ed25519.private_key_size
mut publickey := []byte{len: ed25519.public_key_size}
copy(publickey, priv[32..])
copy(mut publickey, priv[32..])
return PublicKey(publickey)
}
@ -62,11 +62,11 @@ pub fn (priv PrivateKey) sign(message []byte) ?[]byte {
// sign`signs the message with privatekey and returns a signature
pub fn sign(privatekey PrivateKey, message []byte) ?[]byte {
mut signature := []byte{len: ed25519.signature_size}
sign_generic(signature, privatekey, message) ?
sign_generic(mut signature, privatekey, message) ?
return signature
}
fn sign_generic(signature []byte, privatekey []byte, message []byte) ? {
fn sign_generic(mut signature []byte, privatekey []byte, message []byte) ? {
if privatekey.len != ed25519.private_key_size {
panic('ed25519: bad private key length: $privatekey.len')
}
@ -103,8 +103,8 @@ fn sign_generic(signature []byte, privatekey []byte, message []byte) ? {
mut ss := edwards25519.new_scalar()
ss.multiply_add(k, s, r)
copy(signature[..32], rr.bytes())
copy(signature[32..], ss.bytes())
copy(mut signature[..32], rr.bytes())
copy(mut signature[32..], ss.bytes())
}
// verify reports whether sig is a valid signature of message by publickey.
@ -148,8 +148,8 @@ pub fn generate_key() ?(PublicKey, PrivateKey) {
mut seed := rand.bytes(ed25519.seed_size) ?
privatekey := new_key_from_seed(seed)
publickey := []byte{len: ed25519.public_key_size}
copy(publickey, privatekey[32..])
mut publickey := []byte{len: ed25519.public_key_size}
copy(mut publickey, privatekey[32..])
return publickey, privatekey
}
@ -158,12 +158,12 @@ pub fn generate_key() ?(PublicKey, PrivateKey) {
// correspond to seeds in this module
pub fn new_key_from_seed(seed []byte) PrivateKey {
// Outline the function body so that the returned key can be stack-allocated.
privatekey := []byte{len: ed25519.private_key_size}
new_key_from_seed_generic(privatekey, seed)
mut privatekey := []byte{len: ed25519.private_key_size}
new_key_from_seed_generic(mut privatekey, seed)
return PrivateKey(privatekey)
}
fn new_key_from_seed_generic(privatekey []byte, seed []byte) {
fn new_key_from_seed_generic(mut privatekey []byte, seed []byte) {
if seed.len != ed25519.seed_size {
panic('ed25519: bad seed length: $seed.len')
}
@ -176,6 +176,6 @@ fn new_key_from_seed_generic(privatekey []byte, seed []byte) {
mut publickey := aa.bytes()
copy(privatekey, seed)
copy(privatekey[32..], publickey)
copy(mut privatekey, seed)
copy(mut privatekey[32..], publickey)
}

View File

@ -97,8 +97,8 @@ fn works_check_on_sign_input_string(item string) bool {
sig = sig[..ed25519.signature_size]
mut priv := []byte{len: ed25519.private_key_size}
copy(priv[..], privbytes)
copy(priv[32..], pubkey)
copy(mut priv[..], privbytes)
copy(mut priv[32..], pubkey)
sig2 := ed25519.sign(priv[..], msg) or { panic(err.msg) }
if sig != sig2[..] {
@ -182,8 +182,8 @@ fn test_input_from_djb_ed25519_crypto_sign_input_without_syncpool() ? {
sig = sig[..signature_size]
mut priv := []byte{len: ed25519.private_key_size}
copy(priv[..], privbytes)
copy(priv[32..], pubkey)
copy(mut priv[..], privbytes)
copy(mut priv[32..], pubkey)
sig2 := ed25519.sign(priv[..], msg) ?
assert sig == sig2[..]

View File

@ -398,7 +398,7 @@ fn test_bytes_big_equivalence() ? {
mut buf := []byte{len: 32} // pad with zeroes
fedtobig := fe1.to_big_integer()
mut fedbig_bytes, _ := fedtobig.bytes()
copy(buf, fedbig_bytes) // does not need to do swap_endianness
copy(mut buf, fedbig_bytes) // does not need to do swap_endianness
assert fe.bytes() == buf && is_in_bounds(fe) && is_in_bounds(fe1)
// assert big_equivalence(inp, fe, fe1) == true

View File

@ -229,7 +229,7 @@ fn (mut v Point) bytes_generic(mut buf [32]byte) []byte {
fn copy_field_element(mut buf [32]byte, mut v Element) []byte {
// this fail in test
/*
copy(buf[..], v.bytes())
copy(mut buf[..], v.bytes())
return buf[..]
*/

View File

@ -91,7 +91,7 @@ pub fn (mut s Scalar) set_uniform_bytes(x []byte) ?Scalar {
return error('edwards25519: invalid set_uniform_bytes input length')
}
mut wide_bytes := []byte{len: 64}
copy(wide_bytes, x)
copy(mut wide_bytes, x)
// for i, item in x {
// wide_bytes[i] = item
//}
@ -112,7 +112,7 @@ pub fn (mut s Scalar) set_canonical_bytes(x []byte) ?Scalar {
ss.s[i] = item
}
//_ := copy(ss.s[..], x) //its not working
//_ := copy(mut ss.s[..], x) //its not working
if !is_reduced(ss) {
return error('invalid scalar encoding')
}
@ -162,7 +162,7 @@ pub fn (mut s Scalar) set_bytes_with_clamping(x []byte) ?Scalar {
}
mut wide_bytes := []byte{len: 64, cap: 64}
copy(wide_bytes, x)
copy(mut wide_bytes, x)
// for i, item in x {
// wide_bytes[i] = item
//}
@ -176,7 +176,7 @@ pub fn (mut s Scalar) set_bytes_with_clamping(x []byte) ?Scalar {
// bytes returns the canonical 32-byte little-endian encoding of s.
pub fn (mut s Scalar) bytes() []byte {
mut buf := []byte{len: 32}
copy(buf, s.s[..])
copy(mut buf, s.s[..])
return buf
}
@ -1116,7 +1116,7 @@ fn generate_scalar(size int) ?Scalar {
// using builtin rand.read([]buf)
rand.read(mut s.s[..16])
// buf := rand.read(s.s[..16].len) ?
// copy(s.s[..16], buf)
// copy(mut s.s[..16], buf)
/*
for i, item in buf {
@ -1133,7 +1133,7 @@ fn generate_scalar(size int) ?Scalar {
// rand.Read(s.s[:16])
rand.read(mut s.s[..16])
// buf := rand.read(s.s[..16].len) ?
// copy(s.s[..16], buf)
// copy(mut s.s[..16], buf)
/*
for i, item in buf {
@ -1149,7 +1149,7 @@ fn generate_scalar(size int) ?Scalar {
// rand.Read(s.s[:])
rand.read(mut s.s[..])
// buf := crand.read(s.s.len) ?
// copy(s.s[..], buf)
// copy(mut s.s[..], buf)
/*
for i, item in buf {

View File

@ -58,7 +58,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
n := copy(mut d.x[d.nx..], p)
d.nx += n
if d.nx == md5.block_size {
block(mut d, d.x)
@ -80,7 +80,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
}
}
if p.len > 0 {
d.nx = copy(d.x, p)
d.nx = copy(mut d.x, p)
}
return nn
}

View File

@ -62,7 +62,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
mut p := p_
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
n := copy(mut d.x[d.nx..], p)
d.nx += n
if d.nx == sha1.chunk {
block(mut d, d.x)
@ -84,7 +84,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
}
}
if p.len > 0 {
d.nx = copy(d.x, p)
d.nx = copy(mut d.x, p)
}
}
return nn

View File

@ -96,7 +96,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
n := copy(mut d.x[d.nx..], p)
d.nx += n
if d.nx == sha256.chunk {
block(mut d, d.x)
@ -118,7 +118,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
}
}
if p.len > 0 {
d.nx = copy(d.x, p)
d.nx = copy(mut d.x, p)
}
return nn
}
@ -191,8 +191,8 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data) or { panic(err) }
sum := d.checksum()
sum224 := []byte{len: sha256.size224}
copy(sum224, sum[..sha256.size224])
mut sum224 := []byte{len: sha256.size224}
copy(mut sum224, sum[..sha256.size224])
return sum224
}

View File

@ -155,7 +155,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
n := copy(mut d.x[d.nx..], p)
d.nx += n
if d.nx == sha512.chunk {
block(mut d, d.x)
@ -177,7 +177,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
}
}
if p.len > 0 {
d.nx = copy(d.x, p)
d.nx = copy(mut d.x, p)
}
return nn
}
@ -258,8 +258,8 @@ pub fn sum384(data []byte) []byte {
mut d := new_digest(.sha384)
d.write(data) or { panic(err) }
sum := d.checksum()
sum384 := []byte{len: sha512.size384}
copy(sum384, sum[..sha512.size384])
mut sum384 := []byte{len: sha512.size384}
copy(mut sum384, sum[..sha512.size384])
return sum384
}
@ -268,8 +268,8 @@ pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224)
d.write(data) or { panic(err) }
sum := d.checksum()
sum224 := []byte{len: sha512.size224}
copy(sum224, sum[..sha512.size224])
mut sum224 := []byte{len: sha512.size224}
copy(mut sum224, sum[..sha512.size224])
return sum224
}
@ -278,8 +278,8 @@ pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256)
d.write(data) or { panic(err) }
sum := d.checksum()
sum256 := []byte{len: sha512.size256}
copy(sum256, sum[..sha512.size256])
mut sum256 := []byte{len: sha512.size256}
copy(mut sum256, sum[..sha512.size256])
return sum256
}

View File

@ -47,7 +47,7 @@ pub fn new_alphabet(str string) ?Alphabet {
}
mut ret := Alphabet{}
copy(ret.encode, str.bytes())
copy(mut ret.encode, str.bytes())
mut distinct := 0
for i, b in ret.encode {

View File

@ -47,7 +47,7 @@ pub fn (mut r BufferedReader) read(mut buf []byte) ?int {
return none
}
}
read := copy(buf, r.buf[r.offset..r.len])
read := copy(mut buf, r.buf[r.offset..r.len])
if read == 0 {
return none
}

View File

@ -19,7 +19,7 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
}
mut howmany := imin(buf.len, s.text.len - s.place)
xxx := s.text[s.place..s.place + howmany].bytes()
read := copy(buf, xxx)
read := copy(mut buf, xxx)
s.place += read
return read
}

View File

@ -16,7 +16,7 @@ fn (mut b Buf) read(mut buf []byte) ?int {
if !(b.i < b.bytes.len) {
return none
}
n := copy(buf, b.bytes[b.i..])
n := copy(mut buf, b.bytes[b.i..])
b.i += n
return n
}

View File

@ -11,7 +11,7 @@ fn (mut b Buf) read(mut buf []byte) ?int {
if !(b.i < b.bytes.len) {
return none
}
n := copy(buf, b.bytes[b.i..])
n := copy(mut buf, b.bytes[b.i..])
b.i += n
return n
}
@ -48,7 +48,7 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
if s.place >= s.text.len {
return none
}
read := copy(buf, s.text[s.place..].bytes())
read := copy(mut buf, s.text[s.place..].bytes())
s.place += read
return read
}

View File

@ -14,7 +14,7 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
}
max_bytes := 100
end := if s.place + max_bytes >= s.text.len { s.text.len } else { s.place + max_bytes }
n := copy(buf, s.text[s.place..end].bytes())
n := copy(mut buf, s.text[s.place..end].bytes())
s.place += n
return n
}

View File

@ -274,7 +274,7 @@ fn escape(s string, mode EncodingMode) string {
required := s.len + 2 * hex_count
mut t := []byte{len: required}
if hex_count == 0 {
copy(t, s.bytes())
copy(mut t, s.bytes())
for i in 0 .. s.len {
if s[i] == ` ` {
t[i] = `+`

View File

@ -39,7 +39,7 @@ fn (mut b TestReader) read(mut buf []byte) ?int {
if !(b.i < b.bytes.len) {
return none
}
n := copy(buf, b.bytes[b.i..])
n := copy(mut buf, b.bytes[b.i..])
b.i += n
return n
}