Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed May 17, 2023
1 parent 7a7d712 commit e0a9b09
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 122 deletions.
184 changes: 71 additions & 113 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ unsafe fn alloc(len: u256) -> u256 {
return ptr
}

struct Cursor {
cur: u256
len: u256

pub fn new(len: u256) -> Self {
return Cursor(cur: 0, len)
}

pub fn advance(mut self, len: u256) -> u256 {
let cur: u256 = self.cur
assert cur + len < self.len + 1
self.cur += len
return cur
}

pub fn remainder(self) -> u256 {
return self.len - self.cur
}
}

pub struct MemoryBuffer {
offset: u256
len: u256
Expand Down Expand Up @@ -52,99 +72,59 @@ pub struct MemoryBuffer {

pub struct MemoryBufferWriter {
buf: MemoryBuffer
cur: u256
cur: Cursor

pub fn new(mut buf: MemoryBuffer) -> Self {
return MemoryBufferWriter(buf, cur: 0)
return MemoryBufferWriter(
buf,
cur: Cursor::new(len: buf.len())
)
}

pub fn buf(self) -> MemoryBuffer {
return self.buf
pub fn remainder(self) -> u256 {
return self.cur.remainder()
}

pub fn cur(self) -> u256 {
return self.cur
fn write_offset(mut self, len: u256) -> u256 {
return self.buf.offset() + self.cur.advance(len)
}

pub fn bytes_remaining(self) -> u256 {
return self.buf().len() - self.cur()
fn write_n(mut self, value: u256, len: u256) {
let offset: u256 = self.write_offset(len)
unsafe { rewrite_slot(offset, value, len) }
}

pub fn write_u8(mut self, value: u8) {
unsafe {
evm::mstore8(
offset: self.buf().offset() + self.cur(),
value
)
}
self.cur += 1
let offset: u256 = self.write_offset(len: 1)
unsafe { evm::mstore8(offset, value) }
}

pub fn write_u16(mut self, value: u16) {
let size: u256 = 2
write_slot(
offset: self.buf().offset() + self.cur(),
value: u256(value),
size
)

self.cur += size
self.write_n(value: u256(value), len: 2)
}

pub fn write_u32(mut self, value: u32) {
let size: u256 = 4
write_slot(
offset: self.buf().offset() + self.cur(),
value: u256(value),
size
)

self.cur += size
self.write_n(value: u256(value), len: 4)
}

pub fn write_u64(mut self, value: u64) {
let size: u256 = 8
write_slot(
offset: self.buf().offset() + self.cur(),
value: u256(value),
size
)

self.cur += size
self.write_n(value: u256(value), len: 8)
}

pub fn write_u128(mut self, value: u128) {
let size: u256 = 16
write_slot(
offset: self.buf().offset() + self.cur(),
value: u256(value),
size
)
// unsafe {
// evm::mstore(
// offset: self.buf().offset() + self.cur(),
// value: 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000
// )
// }

self.cur += size
self.write_n(value: u256(value), len: 16)
}

pub fn write_u256(mut self, value: u256) {
unsafe {
evm::mstore(
offset: self.buf().offset() + self.cur(),
value
)
}
self.cur += 32
let offset: u256 = self.write_offset(len: 32)
unsafe { evm::mstore(offset, value) }
}

pub fn write_buf(mut self, buf: MemoryBuffer) {
let mut reader: MemoryBufferReader = buf.reader()

while true {
let bytes_remaining: u256 = reader.bytes_remaining()
let bytes_remaining: u256 = reader.remainder()

if bytes_remaining >= 32 {
self.write_u256(value: reader.read_u256())
Expand All @@ -162,92 +142,74 @@ pub struct MemoryBufferWriter {
// }
}

fn write_slot(offset: u256, value: u256, size: u256) {
unsafe fn rewrite_slot(offset: u256, value: u256, len: u256) {
// bit mask for right side of 256 bit slot
let mask: u256 = evm::shr(
bits: size * 8,
bits: len * 8,
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
)
// new value shifted to left
let shifted_value: u256 = evm::shl(
bits: 256 - size * 8,
bits: 256 - len * 8,
value
)

unsafe {
let old_value: u256 = evm::mload(offset)
let new_value: u256 = evm::bitwise_or(
evm::bitwise_and(mask, old_value),
shifted_value
)
evm::mstore(offset, value: new_value)
}
let old_value: u256 = evm::mload(offset)
let new_value: u256 = evm::bitwise_or(
evm::bitwise_and(mask, old_value),
shifted_value
)
evm::mstore(offset, value: new_value)
}

pub struct MemoryBufferReader {
buf: MemoryBuffer
cur: u256
cur: Cursor

pub fn new(buf: MemoryBuffer) -> Self {
return MemoryBufferReader(buf, cur: 0)
return MemoryBufferReader(buf, cur: Cursor::new(len: buf.len()))
}

pub fn buf(self) -> MemoryBuffer {
return self.buf
pub fn remainder(self) -> u256 {
return self.cur.remainder()
}

pub fn cur(self) -> u256 {
return self.cur
fn read_offset(mut self, len: u256) -> u256 {
return self.buf.offset() + self.cur.advance(len)
}

pub fn read_u8(mut self) -> u8 {
fn read_n(mut self, len: u256) -> u256 {
let offset: u256 = self.read_offset(len)
unsafe {
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += 1
return u8(evm::shr(bits: 248, value))
let value: u256 = evm::mload(offset)
return evm::shr(bits: 256 - len * 8, value)
}
}

pub fn read_u8(mut self) -> u8 {
return u8(self.read_n(len: 1))
}

pub fn read_u16(mut self) -> u16 {
unsafe {
let size: u256 = 2
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += size
return u16(evm::shr(bits: 256 - size * 8, value))
}
return u16(self.read_n(len: 2))
}

pub fn read_u32(mut self) -> u32 {
unsafe {
let size: u256 = 4
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += size
return u32(evm::shr(bits: 256 - size * 8, value))
}
return u32(self.read_n(len: 4))
}

pub fn read_u64(mut self) -> u64 {
unsafe {
let size: u256 = 8
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += size
return u64(evm::shr(bits: 256 - size * 8, value))
}
return u64(self.read_n(len: 8))
}

pub fn read_u128(mut self) -> u128 {
unsafe {
let size: u256 = 16
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += size
return u128(evm::shr(bits: 256 - size * 8, value))
}
return u128(self.read_n(len: 16))
}

pub fn read_u256(mut self) -> u256 {
let offset: u256 = self.read_offset(len: 32)
unsafe {
let value: u256 = evm::mload(offset: self.buf().offset() + self.cur())
self.cur += 32
let value: u256 = evm::mload(offset)
return value
}
}
Expand All @@ -257,7 +219,7 @@ pub struct MemoryBufferReader {
let mut writer: MemoryBufferWriter = buf.writer()

while true {
let bytes_remaining: u256 = writer.bytes_remaining()
let bytes_remaining: u256 = writer.remainder()

if bytes_remaining >= 32 {
writer.write_u256(value: self.read_u256())
Expand All @@ -271,10 +233,6 @@ pub struct MemoryBufferReader {
return buf
}

pub fn bytes_remaining(self) -> u256 {
return self.buf().len() - self.cur()
}

// todo: fix compiler panic produced by this
// pub fn write<T: MemoryBufferWrite>(mut self, value: T) {
// self.cur += value.write_buf(writer: self)
Expand Down
5 changes: 2 additions & 3 deletions crates/tests/fixtures/files/buf.fe
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::buf::{MemoryBuffer, MemoryBufferReader, MemoryBufferWriter}
use std::traits::Max
use std::evm

#test
fn test_buf_rw() {
let mut buf: MemoryBuffer = MemoryBuffer::new(len: 1000)
let mut buf: MemoryBuffer = MemoryBuffer::new(len: 161)
let mut writer: MemoryBufferWriter = buf.writer()
let mut reader: MemoryBufferReader = buf.reader()

Expand All @@ -31,4 +30,4 @@ fn test_buf_rw() {
assert reader.read_u16() == u16::max()
assert reader.read_u8() == u8::max()
assert reader.read_u8() == 0
}
}
6 changes: 0 additions & 6 deletions crates/tests/fixtures/files/precompiles.fe
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::precompiles
use std::buf::{MemoryBuffer, MemoryBufferReader, MemoryBufferWriter}
use std::evm

#test
fn test_ec_recover() {
Expand All @@ -14,10 +13,6 @@ fn test_ec_recover() {
assert result == address(0x7156526fbd7a3c72969b54f64e42c10fbb768c8a)
}

struct RevertData {
pub value: u256
}

#test
fn test_sha2_256() {
let buf: MemoryBuffer = MemoryBuffer::from_u8(value: 0xff)
Expand All @@ -41,7 +36,6 @@ fn test_identity() {
let buf: MemoryBuffer = MemoryBuffer::from_u8(value: 0x42)
let mut result: MemoryBufferReader = precompiles::identity(buf).reader()
assert result.read_u8() == 0x42

}

#test
Expand Down

0 comments on commit e0a9b09

Please sign in to comment.