-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from g-r-a-n-t/precompiles
MemoryBuffer, precompiles, raw_call
- Loading branch information
Showing
13 changed files
with
898 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
use ingot::evm | ||
use ingot::math | ||
|
||
unsafe fn avail() -> u256 { | ||
let ptr: u256 = evm::mload(offset: 64) | ||
|
||
if ptr == 0x00 { | ||
return 96 | ||
} else { | ||
return ptr | ||
} | ||
} | ||
|
||
unsafe fn alloc(len: u256) -> u256 { | ||
let ptr: u256 = avail() | ||
evm::mstore(offset: 64, value: ptr + len) | ||
return ptr | ||
} | ||
|
||
struct Cursor { | ||
cur: u256 | ||
len: u256 | ||
|
||
pub fn new(len: u256) -> Self { | ||
return Cursor(cur: 0, len) | ||
} | ||
|
||
/// Increment the value of `cur` by `len` and return the value of `cur` before being incremented. | ||
/// Reverts if the cursor is advanced beyond the given length. | ||
pub fn advance(mut self, len: u256) -> u256 { | ||
let cur: u256 = self.cur | ||
assert cur + len < self.len + 1 | ||
self.cur += len | ||
return cur | ||
} | ||
|
||
/// Length of the cursor remaining. | ||
pub fn remainder(self) -> u256 { | ||
return self.len - self.cur | ||
} | ||
} | ||
|
||
/// EVM memory buffer abstraction. | ||
pub struct MemoryBuffer { | ||
offset: u256 | ||
len: u256 | ||
|
||
pub fn new(len: u256) -> Self { | ||
unsafe { | ||
return MemoryBuffer(offset: alloc(len), len) | ||
} | ||
} | ||
|
||
pub fn from_u8(value: u8) -> Self { | ||
let mut buf: MemoryBuffer = MemoryBuffer::new(len: 1) | ||
let mut writer: MemoryBufferWriter = buf.writer() | ||
writer.write_u8(value) | ||
return buf | ||
} | ||
|
||
/// Length of the buffer in bytes. | ||
pub fn len(self) -> u256 { | ||
return self.len | ||
} | ||
|
||
/// The start of the buffer in EVM memory. | ||
pub fn offset(self) -> u256 { | ||
return self.offset | ||
} | ||
|
||
/// Returns a new buffer reader. | ||
pub fn reader(self) -> MemoryBufferReader { | ||
return MemoryBufferReader::new(buf: self) | ||
} | ||
|
||
/// Returns a new buffer writer. | ||
pub fn writer(mut self) -> MemoryBufferWriter { | ||
return MemoryBufferWriter::new(buf: self) | ||
} | ||
} | ||
|
||
/// Memory buffer writer abstraction. | ||
pub struct MemoryBufferWriter { | ||
buf: MemoryBuffer | ||
cur: Cursor | ||
|
||
/// Returns a new writer for the given buffer. | ||
pub fn new(mut buf: MemoryBuffer) -> Self { | ||
return MemoryBufferWriter( | ||
buf, | ||
cur: Cursor::new(len: buf.len()) | ||
) | ||
} | ||
|
||
/// The number of bytes remaining to be written. | ||
pub fn remainder(self) -> u256 { | ||
return self.cur.remainder() | ||
} | ||
|
||
fn write_offset(mut self, len: u256) -> u256 { | ||
return self.buf.offset() + self.cur.advance(len) | ||
} | ||
|
||
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) { | ||
let offset: u256 = self.write_offset(len: 1) | ||
unsafe { evm::mstore8(offset, value) } | ||
} | ||
|
||
pub fn write_u16(mut self, value: u16) { | ||
self.write_n(value: u256(value), len: 2) | ||
} | ||
|
||
pub fn write_u32(mut self, value: u32) { | ||
self.write_n(value: u256(value), len: 4) | ||
} | ||
|
||
pub fn write_u64(mut self, value: u64) { | ||
self.write_n(value: u256(value), len: 8) | ||
} | ||
|
||
pub fn write_u128(mut self, value: u128) { | ||
self.write_n(value: u256(value), len: 16) | ||
} | ||
|
||
pub fn write_u256(mut self, value: u256) { | ||
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.remainder() | ||
|
||
if bytes_remaining >= 32 { | ||
self.write_u256(value: reader.read_u256()) | ||
} else if bytes_remaining == 0 { | ||
break | ||
} else { | ||
self.write_u8(value: reader.read_u8()) | ||
} | ||
} | ||
} | ||
|
||
// panicked at 'missing impl', crates/mir/src/lower/function.rs:1036:22 | ||
// pub fn write<T: MemoryBufferWrite>(mut self, value: T) { | ||
// value.write_buf(writer: self) | ||
// } | ||
} | ||
|
||
// pub trait MemoryBufferWrite { | ||
// fn write_buf(self, mut writer: MemoryBufferWriter); | ||
// } | ||
// | ||
// impl MemoryBufferWrite for u256 { .. } | ||
// . | ||
// . | ||
// impl MemoryBufferWrite for u8 { .. } | ||
|
||
/// Rewrites the left-most `len` bytes in slot with the right-most `len` bytes of `value`. | ||
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: len * 8, | ||
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff | ||
) | ||
// new value shifted to left | ||
let shifted_value: u256 = evm::shl( | ||
bits: 256 - len * 8, | ||
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) | ||
} | ||
|
||
/// Memory buffer reader abstraction. | ||
pub struct MemoryBufferReader { | ||
buf: MemoryBuffer | ||
cur: Cursor | ||
|
||
/// Returns a new reader for the given buffer. | ||
pub fn new(buf: MemoryBuffer) -> Self { | ||
return MemoryBufferReader(buf, cur: Cursor::new(len: buf.len())) | ||
} | ||
|
||
/// The number of bytes remaining to be read. | ||
pub fn remainder(self) -> u256 { | ||
return self.cur.remainder() | ||
} | ||
|
||
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 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 { | ||
return u16(self.read_n(len: 2)) | ||
} | ||
|
||
pub fn read_u32(mut self) -> u32 { | ||
return u32(self.read_n(len: 4)) | ||
} | ||
|
||
pub fn read_u64(mut self) -> u64 { | ||
return u64(self.read_n(len: 8)) | ||
} | ||
|
||
pub fn read_u128(mut self) -> u128 { | ||
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) | ||
return value | ||
} | ||
} | ||
|
||
pub fn read_buf(mut self, len: u256) -> MemoryBuffer { | ||
let mut buf: MemoryBuffer = MemoryBuffer::new(len) | ||
let mut writer: MemoryBufferWriter = buf.writer() | ||
|
||
while true { | ||
let bytes_remaining: u256 = writer.remainder() | ||
|
||
if bytes_remaining >= 32 { | ||
writer.write_u256(value: self.read_u256()) | ||
} else if bytes_remaining == 0 { | ||
break | ||
} else { | ||
writer.write_u8(value: self.read_u8()) | ||
} | ||
} | ||
|
||
return buf | ||
} | ||
|
||
// `T` has not been defined | ||
// pub fn read<T: MemoryBufferRead>(mut self) -> T { | ||
// T::read_buf(writer: self) | ||
// } | ||
} | ||
|
||
// pub trait MemoryBufferRead { | ||
// fn read_buf(self, mut reader: MemoryBufferReader) -> Self; | ||
// } | ||
// | ||
// impl MemoryBufferRead for u256 { .. } | ||
// . | ||
// . | ||
// impl MemoryBufferRead for u8 { .. } | ||
|
||
/// `MemoryBuffer` wrapper for raw calls to other contracts. | ||
pub struct RawCallBuffer { | ||
input_len: u256 | ||
output_len: u256 | ||
buf: MemoryBuffer | ||
|
||
pub fn new(input_len: u256, output_len: u256) -> Self { | ||
let len: u256 = math::max(input_len, output_len) | ||
let buf: MemoryBuffer = MemoryBuffer::new(len) | ||
|
||
return RawCallBuffer(input_len, output_len, buf) | ||
} | ||
|
||
pub fn input_len(self) -> u256 { | ||
return self.input_len | ||
} | ||
|
||
pub fn output_len(self) -> u256 { | ||
return self.output_len | ||
} | ||
|
||
pub fn offset(self) -> u256 { | ||
return self.buf.offset() | ||
} | ||
|
||
pub fn reader(self) -> MemoryBufferReader { | ||
return self.buf.reader() | ||
} | ||
|
||
pub fn writer(mut self) -> MemoryBufferWriter { | ||
return self.buf.writer() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.