use index assignment in existing code

This commit is contained in:
2026-03-11 15:40:05 +01:00
parent 930d7b56cc
commit 88915bbc8a
6 changed files with 67 additions and 74 deletions

View File

@@ -85,9 +85,9 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
if keylen > 0
for i in 0..keylen
mem.write8(block + i, key[i])
block[i] = key[i]
for i in (keylen)..128
mem.write8(block + i, 0)
block[i] = 0
c = 128
else
c = 0
@@ -99,7 +99,7 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
t1 = t1 + 1
crypto.blake2b._compress(h, block, t0, t1, 0, iv, sigma)
c = 0
mem.write8(block + c, input[i])
block[c] = input[i]
c = c + 1
t0 = t0 + c
@@ -108,11 +108,11 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
if c < 128
for i in (c)..128
mem.write8(block + i, 0)
block[i] = 0
crypto.blake2b._compress(h, block, t0, t1, 1, iv, sigma)
for i in 0..outlen
mem.write8(out + i, ((mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff))
out[i] = (mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff
mem.zero_and_free(h, 8 * 8)
mem.zero_and_free(block, 128)
@@ -210,10 +210,9 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
crypto.xchacha20._hchacha20(key, nonce, subkey)
let nonce12: ptr = mem.alloc(12)
for i in 0..12
mem.write8(nonce12 + i, 0)
mem.zero(nonce12, 12)
for i in 0..8
mem.write8(nonce12 + 4 + i, nonce[16 + i])
nonce12[i + 4] = nonce[16 + i]
let blocknum = 0
let remaining: i64 = len
@@ -225,7 +224,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
if remaining < 64
take = remaining
for i in 0..take
mem.write8(out + (len - remaining) + i, block[i])
out[len - remaining + i] = block[i]
remaining = remaining - take
blocknum = blocknum + 1
mem.zero_and_free(block, 64)
@@ -237,13 +236,13 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
func crypto.xchacha20.xor_no_auth[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr
if len <= 0
let out: ptr = mem.alloc(1)
mem.write8(out, 0)
out[0] = 0
return out
let out: ptr = mem.alloc(len)
let ks: ptr = mem.alloc(len)
crypto.xchacha20._stream(key, nonce, ks, len)
for i in 0..len
mem.write8(out + i, input[i] ^ ks[i])
out[i] = input[i] ^ ks[i]
mem.zero_and_free(ks, len)
return out
@@ -328,8 +327,8 @@ func crypto.x25519.pack[out: ptr, input: ptr] : void
for i in 0..16
let v: i64 = mem.read64(t + i * 8)
mem.write8(out + i * 2, v & 0xff)
mem.write8(out + i * 2 + 1, (v >> 8) & 0xff)
out[i * 2] = v & 0xff
out[i * 2 + 1] = (v >> 8) & 0xff
mem.zero_and_free(t, 16 * 8)
mem.zero_and_free(m, 16 * 8)
@@ -353,9 +352,9 @@ func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr
// copy and clamp scalar
for i in 0..32
mem.write8(clamped + i, scalar[i])
mem.write8(clamped, clamped[0] & 0xf8)
mem.write8(clamped + 31, (clamped[31] & 0x7f) | 0x40)
clamped[i] = scalar[i]
clamped[0] = clamped[0] & 0xf8
clamped[31] = (clamped[31] & 0x7f) | 0x40
// load point
crypto.x25519.unpack(x, point)

View File

@@ -21,7 +21,7 @@ func mem.zero_and_free[x: ptr, size: i64] : void
func mem.zero[x: ptr, size: i64] : void
for i in 0..size
mem.write8(x + i, 0)
x[i] = 0
func mem.read8[x: ptr] : u8
return x[0]
@@ -35,14 +35,11 @@ func mem.read32[x: ptr] : i64
func mem.read64[x: ptr] : i64
return _builtin_read64(x)
func mem.write8[x: ptr, d: u8] : void
_builtin_set8(x, d)
func mem.write32[x: ptr, d: i64] : void
mem.write8(x, d & 0xff)
mem.write8(x + 1, (d >> 8) & 0xff)
mem.write8(x + 2, (d >> 16) & 0xff)
mem.write8(x + 3, (d >> 24) & 0xff)
x[0] = d & 0xff
x[1] = (d >> 8) & 0xff
x[2] = (d >> 16) & 0xff
x[3] = (d >> 24) & 0xff
func mem.write64[x: ptr, d: i64] : void
_builtin_set64(x, d)
@@ -81,7 +78,7 @@ func io.read_line[]: str
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
if n < 0
return ""
str.set(buffer, n, 0)
buffer[n] = 0
return buffer
func io.read_file[path: str]: str
@@ -94,7 +91,7 @@ func io.read_file[path: str]: str
let buffer: str = mem.alloc(size + 1)
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
str.set(buffer, n, 0)
buffer[n] = 0
_builtin_syscall(SYS_close, fd)
return buffer
@@ -116,12 +113,9 @@ func str.copy[s: str] : str
let size: i64 = str.len(s) + 1
let dup: str = mem.alloc(size)
for i in 0..size
str.set(dup, i, s[i])
dup[i] = s[i]
return dup
func str.set[s: str, n: i64, c: u8] : void
mem.write8(s + n, c)
func str.equal[a: str, b: str] : bool
let i = 0
while a[i] != 0 && b[i] != 0
@@ -156,10 +150,10 @@ func str.concat[a: str, b: str] : str
let b_len: i64 = str.len(b)
let out: str = mem.alloc(a_len + b_len + 1)
for i in 0..a_len
str.set(out, i, a[i])
out[i] = a[i]
for i in 0..b_len
str.set(out, a_len + i, b[i])
str.set(out, a_len + b_len, 0)
out[a_len + i] = b[i]
out[a_len + b_len] = 0
return out
func str.find[haystack: str, needle: str] : i64
@@ -185,8 +179,8 @@ func str.substr[s: str, start: i64, length: i64] : str
let out: str = mem.alloc(length + 1)
for i in 0..length
str.set(out, i, s[start + i])
str.set(out, length, 0)
out[i] = s[start + i]
out[length] = 0
return out
func str.trim[s: str] : str
@@ -242,8 +236,8 @@ func str.reverse[s: str] : str
let out: str = mem.alloc(len + 1)
for i in 0..len
str.set(out, i, s[len - i - 1])
str.set(out, len, 0)
out[i] = s[len - i - 1]
out[len] = 0
return out
// not sure this covers all wacky edge cases
@@ -258,21 +252,21 @@ func str.from_i64[n: i64] : str
let i = 0
while n > 0
let d: u8 = n % 10
str.set(buf, i, '0' + d)
buf[i] = '0' + d
n = n / 10
i = i + 1
if neg
str.set(buf, i, '-')
buf[i] = '-'
i = i + 1
str.set(buf, i, 0)
buf[i] = 0
let s: str = str.reverse(buf)
mem.free(buf)
return s
func str.from_char[c: u8] : str
let s: str = mem.alloc(2)
str.set(s, 0, c)
str.set(s, 1, 0)
s[0] = c
s[1] = 0
return s
func str.parse_i64[s: str] : i64
@@ -301,11 +295,11 @@ func str.hex_encode[s: str, s_len: i64] : str
for i in 0..s_len
let high: u8 = (s[i] >> 4) & 15
let low: u8 = s[i] & 15
str.set(out, j, hex_chars[high])
str.set(out, j + 1, hex_chars[low])
out[j] = hex_chars[high]
out[j + 1] = hex_chars[low]
j = j + 2
str.set(out, j, 0)
out[j] = 0
return out
func str._hex_digit_to_int[d: u8] : i64
@@ -322,11 +316,11 @@ func str.hex_decode[s: str] : str
let out: str = mem.alloc(s_len / 2 + 1)
while i < s_len
str.set(out, j, str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1]))
out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])
i = i + 2
j = j + 1
str.set(out, j, 0)
out[j] = 0
return out
func math.gcd[a: i64, b: i64] : i64
@@ -606,14 +600,14 @@ func net.listen[packed_host: i64, port: i64] : i64
let sa: ptr = mem.alloc(16)
mem.zero(sa, 16)
mem.write8(sa + 0, 2)
mem.write8(sa + 1, 0)
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
mem.write8(sa + 4, (packed_host >> 24) & 255)
mem.write8(sa + 5, (packed_host >> 16) & 255)
mem.write8(sa + 6, (packed_host >> 8) & 255)
mem.write8(sa + 7, packed_host & 255)
sa[0] = 2
sa[1] = 0
sa[2] = (port >> 8) & 255
sa[3] = port & 255
sa[4] = (packed_host >> 24) & 255
sa[5] = (packed_host >> 16) & 255
sa[6] = (packed_host >> 8) & 255
sa[7] = packed_host & 255
if _builtin_syscall(SYS_bind, s, sa, 16) < 0
_builtin_syscall(SYS_close, s)
@@ -640,13 +634,13 @@ func net.connect[host: str, port: i64] : i64
let sa: ptr = mem.alloc(16)
mem.zero(sa, 16)
mem.write8(sa + 0, 2)
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
mem.write8(sa + 4, ip_ptr[0])
mem.write8(sa + 5, ip_ptr[1])
mem.write8(sa + 6, ip_ptr[2])
mem.write8(sa + 7, ip_ptr[3])
sa[0] = 2
sa[2] = (port >> 8) & 255
sa[3] = port & 255
sa[4] = ip_ptr[0]
sa[5] = ip_ptr[1]
sa[6] = ip_ptr[2]
sa[7] = ip_ptr[3]
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
mem.free(sa)