Compare commits
2 Commits
192cff8f8d
...
c67abf5a8e
| Author | SHA1 | Date | |
|---|---|---|---|
| c67abf5a8e | |||
| 1ac7401e50 |
@@ -84,10 +84,8 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
|
|||||||
mem.write64(h, mem.read64(h) ^ (0x01010000 ^ ((keylen << 8) ^ outlen)))
|
mem.write64(h, mem.read64(h) ^ (0x01010000 ^ ((keylen << 8) ^ outlen)))
|
||||||
|
|
||||||
if keylen > 0
|
if keylen > 0
|
||||||
for i in 0..keylen
|
mem.zero(block, 128)
|
||||||
block[i] = key[i]
|
mem.copy(key, block, keylen)
|
||||||
for i in (keylen)..128
|
|
||||||
block[i] = 0
|
|
||||||
c = 128
|
c = 128
|
||||||
else
|
else
|
||||||
c = 0
|
c = 0
|
||||||
@@ -211,8 +209,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
|
|||||||
|
|
||||||
let nonce12: ptr = mem.alloc(12)
|
let nonce12: ptr = mem.alloc(12)
|
||||||
mem.zero(nonce12, 12)
|
mem.zero(nonce12, 12)
|
||||||
for i in 0..8
|
mem.copy(nonce + 16, nonce12 + 4, 8)
|
||||||
nonce12[i + 4] = nonce[16 + i]
|
|
||||||
|
|
||||||
let blocknum = 0
|
let blocknum = 0
|
||||||
let remaining: i64 = len
|
let remaining: i64 = len
|
||||||
@@ -223,8 +220,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
|
|||||||
let take = 64
|
let take = 64
|
||||||
if remaining < 64
|
if remaining < 64
|
||||||
take = remaining
|
take = remaining
|
||||||
for i in 0..take
|
mem.copy(block, out + len - remaining, take)
|
||||||
out[len - remaining + i] = block[i]
|
|
||||||
remaining = remaining - take
|
remaining = remaining - take
|
||||||
blocknum = blocknum + 1
|
blocknum = blocknum + 1
|
||||||
mem.zero_and_free(block, 64)
|
mem.zero_and_free(block, 64)
|
||||||
@@ -351,8 +347,7 @@ func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr
|
|||||||
mem.write64(magic + 8, 1)
|
mem.write64(magic + 8, 1)
|
||||||
|
|
||||||
// copy and clamp scalar
|
// copy and clamp scalar
|
||||||
for i in 0..32
|
mem.copy(scalar, clamped, 32)
|
||||||
clamped[i] = scalar[i]
|
|
||||||
clamped[0] = clamped[0] & 0xf8
|
clamped[0] = clamped[0] & 0xf8
|
||||||
clamped[31] = (clamped[31] & 0x7f) | 0x40
|
clamped[31] = (clamped[31] & 0x7f) | 0x40
|
||||||
|
|
||||||
|
|||||||
@@ -75,8 +75,9 @@ func mem._coalesce[] : void
|
|||||||
if !cur->next
|
if !cur->next
|
||||||
mem.write64(_builtin_heap_tail(), 0)
|
mem.write64(_builtin_heap_tail(), 0)
|
||||||
|
|
||||||
_builtin_syscall(SYS_munmap, cur, block_total)
|
let saved_next: mem.Block = cur->next
|
||||||
cur = cur->next
|
_builtin_syscall(11, cur, block_total)
|
||||||
|
cur = saved_next
|
||||||
continue
|
continue
|
||||||
|
|
||||||
prev = cur
|
prev = cur
|
||||||
@@ -147,8 +148,7 @@ func mem.realloc[x: ptr, new_size: i64] : ptr
|
|||||||
if !new_ptr
|
if !new_ptr
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
for i in 0..blk->size
|
mem.copy(x, new_ptr, blk->size)
|
||||||
new_ptr[i] = x[i]
|
|
||||||
|
|
||||||
mem.free(x)
|
mem.free(x)
|
||||||
return new_ptr
|
return new_ptr
|
||||||
@@ -161,6 +161,10 @@ func mem.zero[x: ptr, size: i64] : void
|
|||||||
for i in 0..size
|
for i in 0..size
|
||||||
x[i] = 0
|
x[i] = 0
|
||||||
|
|
||||||
|
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
||||||
|
for i in 0..n
|
||||||
|
dst[i] = src[i]
|
||||||
|
|
||||||
func mem.read8[x: ptr] : u8
|
func mem.read8[x: ptr] : u8
|
||||||
return x[0]
|
return x[0]
|
||||||
|
|
||||||
@@ -195,6 +199,12 @@ func io.println[x: str] : void
|
|||||||
func io.print_char[x: u8] : void
|
func io.print_char[x: u8] : void
|
||||||
io.print_sized(^x, 1)
|
io.print_sized(^x, 1)
|
||||||
|
|
||||||
|
func io.print_bool[x: bool] : void
|
||||||
|
if x
|
||||||
|
io.print("true")
|
||||||
|
else
|
||||||
|
io.print("false")
|
||||||
|
|
||||||
func io.print_i64[x: i64] : void
|
func io.print_i64[x: i64] : void
|
||||||
let s: str = str.from_i64(x)
|
let s: str = str.from_i64(x)
|
||||||
io.print(s)
|
io.print(s)
|
||||||
@@ -225,6 +235,13 @@ func io.read_line[]: str
|
|||||||
buffer[n] = 0
|
buffer[n] = 0
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
|
func io.file_exists[path: str] : bool
|
||||||
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
|
if fd >= 0
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
func io.read_file[path: str] : str
|
func io.read_file[path: str] : str
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd <= 0
|
if fd <= 0
|
||||||
@@ -265,6 +282,14 @@ func io.write_file[path: str, content: str] : void
|
|||||||
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||||
_builtin_syscall(SYS_close, fd)
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
|
func io.write_binary_file[path: str, content: ptr, size: i64] : void
|
||||||
|
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||||
|
if fd < 0
|
||||||
|
panic("failed to open file") // TODO
|
||||||
|
|
||||||
|
_builtin_syscall(SYS_write, fd, content, size)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
func str.len[s: str] : i64
|
func str.len[s: str] : i64
|
||||||
let i = 0
|
let i = 0
|
||||||
while s[i]
|
while s[i]
|
||||||
@@ -274,8 +299,7 @@ func str.len[s: str] : i64
|
|||||||
func str.copy[s: str] : str
|
func str.copy[s: str] : str
|
||||||
let size: i64 = str.len(s) + 1
|
let size: i64 = str.len(s) + 1
|
||||||
let dup: str = mem.alloc(size)
|
let dup: str = mem.alloc(size)
|
||||||
for i in 0..size
|
mem.copy(s, dup, size)
|
||||||
dup[i] = s[i]
|
|
||||||
return dup
|
return dup
|
||||||
|
|
||||||
func str.equal[a: str, b: str] : bool
|
func str.equal[a: str, b: str] : bool
|
||||||
@@ -311,13 +335,14 @@ func str.concat[a: str, b: str] : str
|
|||||||
let a_len: i64 = str.len(a)
|
let a_len: i64 = str.len(a)
|
||||||
let b_len: i64 = str.len(b)
|
let b_len: i64 = str.len(b)
|
||||||
let out: str = mem.alloc(a_len + b_len + 1)
|
let out: str = mem.alloc(a_len + b_len + 1)
|
||||||
for i in 0..a_len
|
mem.copy(a, out, a_len)
|
||||||
out[i] = a[i]
|
mem.copy(b, out + a_len, b_len)
|
||||||
for i in 0..b_len
|
|
||||||
out[a_len + i] = b[i]
|
|
||||||
out[a_len + b_len] = 0
|
out[a_len + b_len] = 0
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
func str.contains[haystack: str, needle: str] : bool
|
||||||
|
return str.find(haystack, needle) != -1
|
||||||
|
|
||||||
func str.find[haystack: str, needle: str] : i64
|
func str.find[haystack: str, needle: str] : i64
|
||||||
let haystack_len: i64 = str.len(haystack)
|
let haystack_len: i64 = str.len(haystack)
|
||||||
let needle_len: i64 = str.len(needle)
|
let needle_len: i64 = str.len(needle)
|
||||||
@@ -340,8 +365,7 @@ func str.substr[s: str, start: i64, length: i64] : str
|
|||||||
panic("str.substr out of bounds")
|
panic("str.substr out of bounds")
|
||||||
|
|
||||||
let out: str = mem.alloc(length + 1)
|
let out: str = mem.alloc(length + 1)
|
||||||
for i in 0..length
|
mem.copy(s + start, out, length)
|
||||||
out[i] = s[start + i]
|
|
||||||
out[length] = 0
|
out[length] = 0
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -689,6 +713,17 @@ func alg.reduce[arr: Array, fn: ptr, acc: any] : any
|
|||||||
func os.exit[code: i64] : void
|
func os.exit[code: i64] : void
|
||||||
_builtin_syscall(SYS_exit, code)
|
_builtin_syscall(SYS_exit, code)
|
||||||
|
|
||||||
|
func os.getpid[] : i64
|
||||||
|
return _builtin_syscall(SYS_getpid)
|
||||||
|
|
||||||
|
func os.sleep[ms: i64] : void
|
||||||
|
// TODO: we really shouldnt need a heap allocation to sleep
|
||||||
|
let req: os.Timeval = new os.Timeval
|
||||||
|
req->tv_sec = ms / 1000
|
||||||
|
req->tv_usec = (ms % 1000) * 1000000
|
||||||
|
_builtin_syscall(SYS_nanosleep, req, 0)
|
||||||
|
mem.free(req)
|
||||||
|
|
||||||
func os.urandom[n: i64]: ptr
|
func os.urandom[n: i64]: ptr
|
||||||
let buffer: ptr = mem.alloc(n)
|
let buffer: ptr = mem.alloc(n)
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||||
@@ -707,6 +742,7 @@ struct os.Timeval
|
|||||||
tv_usec: i64
|
tv_usec: i64
|
||||||
|
|
||||||
func os.time[] : i64
|
func os.time[] : i64
|
||||||
|
// TODO: we really shouldnt need a heap allocation to check the time
|
||||||
let tv: os.Timeval = new os.Timeval
|
let tv: os.Timeval = new os.Timeval
|
||||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||||
let out: i64 = tv->tv_sec * 1000 + tv->tv_usec / 1000
|
let out: i64 = tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||||
|
|||||||
Reference in New Issue
Block a user