Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] make assembly block Solidity memory safe #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion snark-verifier/src/loader/evm/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ pragma solidity ^0.8.0;

contract Halo2Verifier {{
fallback(bytes calldata) external returns (bytes memory) {{
assembly {{
assembly (\"memory-safe\") {{
// Enforce that Solidity memory layout is respected
let data := mload(0x40)
if iszero(eq(data, 0x80)) {{
revert(0, 0)
}}

let success := true
let f_p := {base_modulus}
let f_q := {scalar_modulus}
Expand Down
5 changes: 4 additions & 1 deletion snark-verifier/src/loader/evm/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use std::{
rc::Rc,
};

/// Memory pointer starts at 0x80, which is the end of the Solidity memory layout scratch space.
pub const MEM_PTR_START: usize = 0x80;

#[derive(Clone, Debug)]
pub enum Value<T> {
Constant(T),
Expand Down Expand Up @@ -77,7 +80,7 @@ impl EvmLoader {
base_modulus,
scalar_modulus,
code: RefCell::new(code),
ptr: Default::default(),
ptr: RefCell::new(MEM_PTR_START),
cache: Default::default(),
})
}
Expand Down
11 changes: 8 additions & 3 deletions snark-verifier/src/system/halo2/transcript/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

use crate::{
loader::{
evm::{loader::Value, u256_to_fe, util::MemoryChunk, EcPoint, EvmLoader, Scalar, U256},
evm::{
loader::{Value, MEM_PTR_START},
u256_to_fe,
util::MemoryChunk,
EcPoint, EvmLoader, Scalar, U256,
},
native::{self, NativeLoader},
Loader,
},
Expand Down Expand Up @@ -40,7 +45,7 @@ where
/// u256 for `transcript_initial_state`.
pub fn new(loader: &Rc<EvmLoader>) -> Self {
let ptr = loader.allocate(0x20);
assert_eq!(ptr, 0);
assert_eq!(ptr, MEM_PTR_START);
let mut buf = MemoryChunk::new(ptr);
buf.extend(0x20);
Self {
Expand Down Expand Up @@ -118,7 +123,7 @@ where

fn common_scalar(&mut self, scalar: &Scalar) -> Result<(), Error> {
match scalar.value() {
Value::Constant(_) if self.buf.ptr() == 0 => {
Value::Constant(_) if self.buf.ptr() == MEM_PTR_START => {
self.loader.copy_scalar(scalar, self.buf.ptr());
}
Value::Memory(ptr) => {
Expand Down
Loading