Skip to content

Commit

Permalink
Finish buf refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Aug 24, 2023
1 parent 79cc5c2 commit 0bf96fd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ error: the struct `OutOfReachMarker` is private
6fn emit(self, _ val: OutOfReachMarker) {
^^^^^^^^^^^^^^^^ this struct is not `pub`
┌─ src/context.fe:13:8
┌─ src/context.fe:14:8
13struct OutOfReachMarker {}
14struct OutOfReachMarker {}
---------------- `OutOfReachMarker` is defined here
= `OutOfReachMarker` can only be used within `context`
Expand Down
30 changes: 18 additions & 12 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct MemoryBuffer {
}

/// The start of the buffer in EVM memory.
pub fn offset(self) -> u256 {
pub unsafe fn offset(self) -> u256 {
return self.offset
}

Expand Down Expand Up @@ -146,14 +146,16 @@ pub struct MemoryBufferWriter {
return self.cur.remainder()
}

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

pub fn write_n(mut self, value: u256, len: u256) {
let offset: u256 = self.write_offset(len)
let shifted_value: u256 = evm::shl(bits: 256 - len * 8, value)
unsafe { evm::mstore(offset, value: shifted_value) }
unsafe {
let offset: u256 = self.write_offset(len)
evm::mstore(offset, value: shifted_value)
}
}

pub fn write_buf(mut self, buf: MemoryBuffer) {
Expand Down Expand Up @@ -183,8 +185,10 @@ pub trait MemoryBufferWrite {

impl MemoryBufferWrite for u256 {
fn write_buf(self, mut writer: MemoryBufferWriter) {
let offset: u256 = writer.write_offset(len: 32)
unsafe { evm::mstore(offset, value: self) }
unsafe {
let offset: u256 = writer.write_offset(len: 32)
evm::mstore(offset, value: self)
}
}
}

Expand Down Expand Up @@ -214,8 +218,10 @@ impl MemoryBufferWrite for u16 {

impl MemoryBufferWrite for u8 {
fn write_buf(self, mut writer: MemoryBufferWriter) {
let offset: u256 = writer.write_offset(len: 1)
unsafe { evm::mstore8(offset, value: self) }
unsafe {
let offset: u256 = writer.write_offset(len: 1)
evm::mstore8(offset, value: self)
}
}
}

Expand All @@ -239,13 +245,13 @@ pub struct MemoryBufferReader {
return self.cur.remainder()
}

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

fn read_n(mut self, len: u256) -> u256 {
let offset: u256 = self.read_offset(len)
unsafe {
let offset: u256 = self.read_offset(len)
let value: u256 = evm::mload(offset)
return evm::shr(bits: 256 - len * 8, value)
}
Expand All @@ -272,8 +278,8 @@ pub struct MemoryBufferReader {
}

pub fn read_u256(mut self) -> u256 {
let offset: u256 = self.read_offset(len: 32)
unsafe {
let offset: u256 = self.read_offset(len: 32)
let value: u256 = evm::mload(offset)
return value
}
Expand Down Expand Up @@ -334,7 +340,7 @@ pub struct RawCallBuffer {
return self.output_len
}

pub fn offset(self) -> u256 {
pub unsafe fn offset(self) -> u256 {
return self.buf.offset()
}

Expand Down
1 change: 1 addition & 0 deletions crates/library/std/src/context.fe
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ingot::error::{
}
use ingot::buf::{
RawCallBuffer,
MemoryBuffer,
MemoryBufferReader,
MemoryBufferWriter
}
Expand Down
50 changes: 33 additions & 17 deletions crates/library/std/src/evm.fe
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,33 @@ pub unsafe fn call_data_size() -> u256 {
return __calldatasize()
}

pub fn call_data_copy(buf: MemoryBuffer, from_offset f: u256) {
pub fn call_data_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __calldatacopy(buf.offset(), f, buf.len()) }
}

pub unsafe fn code_size() -> u256 {
return __codesize()
}

pub unsafe fn code_copy(to_offset t: u256, from_offset f: u256, len: u256) {
__codecopy(t, f, len)
pub fn code_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __codecopy(buf.offset(), f, buf.len()) }
}

pub unsafe fn return_data_size() -> u256 {
return __returndatasize()
}

pub unsafe fn return_data_copy(to_offset t: u256, from_offset f: u256, len: u256) {
__returndatacopy(t, f, len)
pub fn return_data_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __returndatacopy(buf.offset(), f, buf.len()) }
}

pub unsafe fn extcodesize(_ addr: address) -> u256 {
return __extcodesize(u256(addr))
}

pub unsafe fn ext_code_copy(_ addr: address, to_offset t: u256, from_offset f: u256, len: u256) {
__extcodecopy(u256(addr), t, f, len)
// todo
pub unsafe fn ext_code_copy(addr: address, mut buf: MemoryBuffer, from_offset f: u256) {
__extcodecopy(u256(addr), buf.offset(), f, buf.len())
}

pub unsafe fn ext_code_hash(_ addr: address) -> u256 {
Expand All @@ -278,28 +279,43 @@ pub fn keccak256_mem(buf: MemoryBuffer) -> u256 {
// Contract creation and calling

pub fn create(value v: u256, buf: MemoryBuffer) -> address {
unsafe { return address(__create(v, buf.offset(), buf.len())) }
unsafe { return address(__create(v, buf.offset(), buf.len())) }
}

pub fn create2(value v: u256, buf: MemoryBuffer, salt s: u256) -> address {
unsafe { return address(__create2(v, buf.offset(), buf.len(), s)) }
unsafe { return address(__create2(v, buf.offset(), buf.len(), s)) }
}

// TODO: return bool (success)
pub fn call(gas: u256, addr: address, value: u256, mut buf: RawCallBuffer) -> bool {
unsafe{ return __call(gas, u256(addr), value, buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
unsafe { return __call(gas, u256(addr), value, buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}

pub unsafe fn call_code(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> u256 {
return __callcode(gas, u256(addr), value, input_offset, input_len, output_offset, output_len)
pub fn call_code(gas: u256, addr: address, value: u256, mut buf: RawCallBuffer) -> bool {
unsafe { return __callcode(gas, u256(addr), value, buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}

pub unsafe fn delegate_call(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> u256 {
return __delegatecall(gas, u256(addr), input_offset, input_len, output_offset, output_len)
pub fn delegate_call(gas: u256, addr: address, mut buf: RawCallBuffer) -> bool {
unsafe { return __delegatecall(gas, u256(addr), buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}

pub unsafe fn static_call(gas: u256, addr: address, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> u256 {
return __staticcall(gas, u256(addr), input_offset, input_len, output_offset, output_len)
pub fn static_call(gas: u256, addr: address, mut buf: RawCallBuffer) -> bool {
unsafe { return __staticcall(gas, u256(addr), buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}

pub unsafe fn call_2(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __call(gas, u256(addr), value, input_offset, input_len, output_offset, output_len) == 1
}

pub unsafe fn call_code_2(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __callcode(gas, u256(addr), value, input_offset, input_len, output_offset, output_len) == 1
}

pub unsafe fn delegate_call_2(gas: u256, addr: address, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __delegatecall(gas, u256(addr), input_offset, input_len, output_offset, output_len) == 1
}

pub unsafe fn static_call_2(gas: u256, addr: address, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __staticcall(gas, u256(addr), input_offset, input_len, output_offset, output_len) == 1
}

// Logging functions
Expand Down
50 changes: 25 additions & 25 deletions crates/library/std/src/precompiles.fe
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ enum Precompile {

pub fn single_buf_call(self, mut buf: MemoryBuffer) {
unsafe {
assert evm::static_call(
assert evm::static_call_2(
gas: evm::gas_remaining(),
addr: self.addr(),
input_offset: buf.offset(),
input_len: buf.len(),
output_offset: buf.offset(),
output_len: buf.len()
) == 1
)
}
}

pub fn call(self, input: MemoryBuffer, mut output: MemoryBuffer) {
pub fn call(self, input_buf: MemoryBuffer, mut output_buf: MemoryBuffer) {
unsafe {
assert evm::static_call(
assert evm::static_call_2(
gas: evm::gas_remaining(),
addr: self.addr(),
input_offset: input.offset(),
input_len: input.len(),
output_offset: output.offset(),
output_len: output.len()
) == 1
input_offset: input_buf.offset(),
input_len: input_buf.len(),
output_offset: output_buf.offset(),
output_len: output_buf.len()
)
}
}
}
Expand All @@ -70,26 +70,26 @@ pub fn ec_recover(hash: u256, v: u256, r: u256, s: u256) -> address {
}

/// SHA2 256 precompile call.
pub fn sha2_256(buf: MemoryBuffer) -> u256 {
let mut output: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output.reader()
Precompile::Sha2256.call(input: buf, output)
pub fn sha2_256(buf input_buf: MemoryBuffer) -> u256 {
let mut output_buf: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output_buf.reader()
Precompile::Sha2256.call(input_buf, output_buf)
return reader.read_u256()
}

/// Ripemd 160 precompile call.
pub fn ripemd_160(buf: MemoryBuffer) -> u256 {
let mut output: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output.reader()
Precompile::Ripemd160.call(input: buf, output)
pub fn ripemd_160(buf input_buf: MemoryBuffer) -> u256 {
let mut output_buf: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output_buf.reader()
Precompile::Ripemd160.call(input_buf, output_buf)
return reader.read_u256()
}

/// Identity precompile call.
pub fn identity(buf: MemoryBuffer) -> MemoryBuffer {
let mut output: MemoryBuffer = MemoryBuffer::new(len: buf.len())
Precompile::Identity.call(input: buf, output)
return output
pub fn identity(buf input_buf: MemoryBuffer) -> MemoryBuffer {
let mut output_buf: MemoryBuffer = MemoryBuffer::new(len: input_buf.len())
Precompile::Identity.call(input_buf, output_buf)
return output_buf
}

/// Mod exp preocmpile call.
Expand Down Expand Up @@ -151,10 +151,10 @@ pub fn ec_mul(x: u256, y: u256, s: u256) -> (u256, u256) {
}

/// EC pairing precompile call.
pub fn ec_pairing(buf: MemoryBuffer) -> bool {
let mut output: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output.reader()
Precompile::EcPairing.call(input: buf, output)
pub fn ec_pairing(buf input_buf: MemoryBuffer) -> bool {
let mut output_buf: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut reader: MemoryBufferReader = output_buf.reader()
Precompile::EcPairing.call(input_buf, output_buf)
return reader.read_u256() == 1
}

Expand Down

0 comments on commit 0bf96fd

Please sign in to comment.