Skip to content

Commit 5d8307b

Browse files
committed
Add Succinct SP1 as an arbitrator-like runner
1 parent 68126b8 commit 5d8307b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+11523
-134
lines changed

arbitrator/arbutil/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ serde = { workspace = true, features = ["derive", "rc"] }
2121
siphasher = { workspace = true }
2222
tiny-keccak = { workspace = true, features = ["keccak"] }
2323
wasmparser = { workspace = true }
24+
25+
[features]
26+
default = []
27+
sp1 = []

arbitrator/arbutil/src/operator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,8 @@ impl From<&Operator<'_>> for OperatorCode {
11781178
O::I16x8RelaxedQ15mulrS { .. } => 0xfd111,
11791179
O::I16x8RelaxedDotI8x16I7x16S { .. } => 0xfd112,
11801180
O::I32x4RelaxedDotI8x16I7x16AddS { .. } => 0xfd113,
1181+
#[cfg(feature = "sp1")]
1182+
_ => todo!(),
11811183
})
11821184
}
11831185
}

arbitrator/prover/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ counters = []
6464
native = ["dep:wasmer", "dep:wasmer-compiler-singlepass", "brotli/wasmer_traits", "dep:c-kzg"]
6565
singlepass_rayon = ["wasmer-compiler-singlepass?/rayon"]
6666
rayon = ["dep:rayon"]
67+
sp1 = []

arbitrator/prover/src/binary.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
use crate::{
55
programs::{
6+
FuncMiddleware, Middleware, ModuleMod, STYLUS_ENTRY_POINT, StylusData,
67
config::CompileConfig, counter::Counter, depth::DepthChecker, dynamic::DynamicMeter,
7-
heap::HeapBound, meter::Meter, start::StartMover, FuncMiddleware, Middleware, ModuleMod,
8-
StylusData, STYLUS_ENTRY_POINT,
8+
heap::HeapBound, meter::Meter, start::StartMover,
99
},
1010
value::{ArbValueType, FunctionType, IntegerValType, Value},
1111
};
1212
use arbutil::{
13-
evm::ARBOS_VERSION_STYLUS_CHARGING_FIXES, math::SaturatingSum, Bytes32, Color, DebugColor,
13+
Bytes32, Color, DebugColor, evm::ARBOS_VERSION_STYLUS_CHARGING_FIXES, math::SaturatingSum,
1414
};
15-
use eyre::{bail, ensure, eyre, Result, WrapErr};
15+
use eyre::{Result, WrapErr, bail, ensure, eyre};
1616
use fnv::{FnvHashMap as HashMap, FnvHashSet as HashSet};
1717
use nom::{
1818
branch::alt,
@@ -22,7 +22,7 @@ use nom::{
2222
};
2323
use serde::{Deserialize, Serialize};
2424
use std::{convert::TryInto, fmt::Debug, hash::Hash, mem, path::Path, str::FromStr};
25-
use wasmer_types::{entity::EntityRef, ExportIndex, FunctionIndex, LocalFunctionIndex};
25+
use wasmer_types::{ExportIndex, FunctionIndex, LocalFunctionIndex, entity::EntityRef};
2626
use wasmparser::{
2727
Data, Element, ExternalKind, MemoryType, Name, NameSectionReader, Naming, Operator, Parser,
2828
Payload, TableType, TypeRef, ValType, Validator, WasmFeatures,
@@ -255,6 +255,8 @@ impl From<ExportIndex> for ExportKind {
255255
E::Table(_) => Self::Table,
256256
E::Memory(_) => Self::Memory,
257257
E::Global(_) => Self::Global,
258+
#[cfg(feature = "sp1")]
259+
E::Tag(_) => Self::Tag,
258260
}
259261
}
260262
}
@@ -301,6 +303,7 @@ pub struct WasmBinary<'a> {
301303
}
302304

303305
pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
306+
#[cfg(not(feature = "sp1"))]
304307
let features = WasmFeatures {
305308
mutable_global: true,
306309
saturating_float_to_int: true,
@@ -324,6 +327,17 @@ pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
324327
component_model_values: false,
325328
component_model_nested_names: false,
326329
};
330+
#[cfg(feature = "sp1")]
331+
let features = {
332+
let mut features = WasmFeatures::empty();
333+
features.set(WasmFeatures::MUTABLE_GLOBAL, true);
334+
features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
335+
features.set(WasmFeatures::SIGN_EXTENSION, true);
336+
features.set(WasmFeatures::MULTI_VALUE, true);
337+
features.set(WasmFeatures::BULK_MEMORY, true); // not all ops supported yet
338+
features.set(WasmFeatures::FLOATS, true);
339+
features
340+
};
327341
Validator::new_with_features(features)
328342
.validate_all(input)
329343
.wrap_err_with(|| eyre!("failed to validate {}", path.to_string_lossy().red()))?;
@@ -429,7 +443,11 @@ pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
429443
}
430444

431445
// CHECK: maybe reader.data_offset()
446+
#[cfg(not(feature = "sp1"))]
432447
let name_reader = NameSectionReader::new(reader.data(), 0);
448+
#[cfg(feature = "sp1")]
449+
let name_reader =
450+
NameSectionReader::new(wasmparser::BinaryReader::new(reader.data(), 0));
433451

434452
for name in name_reader {
435453
match name? {

arbitrator/prover/src/host.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,14 @@ use crate::{
88
machine::{Function, InboxIdentifier},
99
programs::StylusData,
1010
utils,
11-
value::{ArbValueType, FunctionType},
11+
value::{ArbValueType, FunctionType, InternalFunc},
1212
wavm::{wasm_to_wavm, Instruction, Opcode},
1313
};
1414
use arbutil::{evm::user::UserOutcomeKind, Color, PreimageType};
1515
use eyre::{bail, ErrReport, Result};
1616
use lazy_static::lazy_static;
17-
use num_derive::FromPrimitive;
1817
use std::{collections::HashMap, path::Path, str::FromStr};
1918

20-
/// Represents the internal hostio functions a module may have.
21-
#[derive(Clone, Copy, Debug, FromPrimitive)]
22-
#[repr(u64)]
23-
pub enum InternalFunc {
24-
WavmCallerLoad8,
25-
WavmCallerLoad32,
26-
WavmCallerStore8,
27-
WavmCallerStore32,
28-
MemoryFill,
29-
MemoryCopy,
30-
UserInkLeft,
31-
UserInkStatus,
32-
UserSetInk,
33-
UserStackLeft,
34-
UserSetStack,
35-
UserMemorySize,
36-
CallMain,
37-
}
38-
39-
impl InternalFunc {
40-
pub fn ty(&self) -> FunctionType {
41-
use ArbValueType::*;
42-
use InternalFunc::*;
43-
macro_rules! func {
44-
([$($args:expr),*], [$($outs:expr),*]) => {
45-
FunctionType::new(vec![$($args),*], vec![$($outs),*])
46-
};
47-
}
48-
#[rustfmt::skip]
49-
let ty = match self {
50-
WavmCallerLoad8 | WavmCallerLoad32 => func!([I32], [I32]),
51-
WavmCallerStore8 | WavmCallerStore32 => func!([I32, I32], []),
52-
MemoryFill | MemoryCopy => func!([I32, I32, I32], []),
53-
UserInkLeft => func!([], [I64]), // λ() → ink_left
54-
UserInkStatus => func!([], [I32]), // λ() → ink_status
55-
UserSetInk => func!([I64, I32], []), // λ(ink_left, ink_status)
56-
UserStackLeft => func!([], [I32]), // λ() → stack_left
57-
UserSetStack => func!([I32], []), // λ(stack_left)
58-
UserMemorySize => func!([], [I32]), // λ() → memory_size
59-
CallMain => func!([I32], [I32]), // λ(args_len) → status
60-
};
61-
ty
62-
}
63-
}
64-
6519
/// Represents the internal hostio functions a module may have.
6620
pub enum Hostio {
6721
WavmCallerLoad8,

arbitrator/prover/src/memory.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
};
88
use arbutil::Bytes32;
99
use digest::Digest;
10-
use eyre::{bail, ErrReport, Result};
10+
use eyre::{bail, Result};
1111
use parking_lot::Mutex;
1212
use serde::{Deserialize, Serialize};
1313
use sha3::Keccak256;
@@ -16,8 +16,6 @@ use std::{borrow::Cow, collections::HashSet, convert::TryFrom};
1616
#[cfg(feature = "counters")]
1717
use std::sync::atomic::{AtomicUsize, Ordering};
1818

19-
use wasmer_types::Pages;
20-
2119
#[cfg(feature = "rayon")]
2220
use rayon::prelude::*;
2321

@@ -37,34 +35,6 @@ pub fn print_counters() {
3735
);
3836
}
3937

40-
pub struct MemoryType {
41-
pub min: Pages,
42-
pub max: Option<Pages>,
43-
}
44-
45-
impl MemoryType {
46-
pub fn new(min: Pages, max: Option<Pages>) -> Self {
47-
Self { min, max }
48-
}
49-
}
50-
51-
impl From<&wasmer_types::MemoryType> for MemoryType {
52-
fn from(value: &wasmer_types::MemoryType) -> Self {
53-
Self::new(value.minimum, value.maximum)
54-
}
55-
}
56-
57-
impl TryFrom<&wasmparser::MemoryType> for MemoryType {
58-
type Error = ErrReport;
59-
60-
fn try_from(value: &wasmparser::MemoryType) -> std::result::Result<Self, Self::Error> {
61-
Ok(Self {
62-
min: Pages(value.initial.try_into()?),
63-
max: value.maximum.map(|x| x.try_into()).transpose()?.map(Pages),
64-
})
65-
}
66-
}
67-
6838
#[derive(Debug, Default, Serialize, Deserialize)]
6939
pub struct Memory {
7040
buffer: Vec<u8>,

arbitrator/prover/src/parse_input.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use arbutil::Bytes32;
22
use serde::Deserialize;
33
use serde_json;
4-
use serde_with::base64::Base64;
54
use serde_with::As;
65
use serde_with::DisplayFromStr;
6+
use serde_with::base64::Base64;
77
use std::{
88
collections::HashMap,
99
io::{self, BufRead},
@@ -54,13 +54,22 @@ impl AsRef<[u8]> for UserWasm {
5454
}
5555

5656
/// The Vec<u8> is compressed using brotli, and must be decompressed before use.
57+
#[cfg(not(feature = "sp1"))]
5758
impl From<Vec<u8>> for UserWasm {
5859
fn from(data: Vec<u8>) -> Self {
5960
let decompressed = brotli::decompress(&data, brotli::Dictionary::Empty).unwrap();
6061
Self(decompressed)
6162
}
6263
}
6364

65+
/// Due to alignment reason, we will do the decompression elsewhere in SP1.
66+
#[cfg(feature = "sp1")]
67+
impl From<Vec<u8>> for UserWasm {
68+
fn from(data: Vec<u8>) -> Self {
69+
Self(data)
70+
}
71+
}
72+
6473
#[derive(Debug, Clone, Deserialize)]
6574
#[serde(rename_all = "PascalCase")]
6675
pub struct BatchInfo {

arbitrator/prover/src/print.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md
33

44
use crate::{
5-
host::InternalFunc,
65
machine::Module,
7-
value::{FunctionType, Value},
6+
value::{FunctionType, InternalFunc, Value},
87
wavm::{self, Opcode},
98
};
109
use arbutil::Color;

arbitrator/prover/src/programs/config.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ use wasmparser::Operator;
1414
#[cfg(feature = "native")]
1515
use {
1616
super::{
17-
counter::Counter, depth::DepthChecker, dynamic::DynamicMeter, heap::HeapBound,
18-
meter::Meter, start::StartMover, MiddlewareWrapper,
17+
MiddlewareWrapper, counter::Counter, depth::DepthChecker, dynamic::DynamicMeter,
18+
heap::HeapBound, meter::Meter, start::StartMover,
1919
},
2020
std::sync::Arc,
21-
wasmer::{Cranelift, CraneliftOptLevel, Engine, Store, Target},
22-
wasmer_compiler_singlepass::Singlepass,
2321
};
2422

2523
#[derive(Clone, Copy, Debug)]
@@ -177,8 +175,16 @@ impl CompileConfig {
177175

178176
config
179177
}
178+
}
180179

181-
#[cfg(feature = "native")]
180+
#[cfg(all(feature = "native", not(feature = "sp1")))]
181+
use {
182+
wasmer::{Cranelift, CraneliftOptLevel, Engine, Store, Target},
183+
wasmer_compiler_singlepass::Singlepass,
184+
};
185+
186+
#[cfg(all(feature = "native", not(feature = "sp1")))]
187+
impl CompileConfig {
182188
fn engine_type(&self, target: Target, cranelift: bool) -> Engine {
183189
use wasmer::sys::EngineBuilder;
184190

@@ -217,13 +223,11 @@ impl CompileConfig {
217223
.into()
218224
}
219225

220-
#[cfg(feature = "native")]
221226
// cranelift only matters for compilation and not usually needed
222227
pub fn engine(&self, target: Target) -> Engine {
223228
self.engine_type(target, true)
224229
}
225230

226-
#[cfg(feature = "native")]
227231
pub fn store(&self, target: Target, cranelift: bool) -> Store {
228232
Store::new(self.engine_type(target, cranelift))
229233
}

arbitrator/prover/src/programs/counter.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md
33

44
use super::{FuncMiddleware, Middleware, ModuleMod};
5-
use crate::Machine;
65

6+
#[cfg(feature = "sp1")]
7+
use crate::operator::{OperatorCode, OperatorInfo};
8+
#[cfg(not(feature = "sp1"))]
79
use arbutil::operator::{OperatorCode, OperatorInfo};
8-
use eyre::{eyre, Result};
10+
use eyre::{Result, eyre};
911
use fnv::FnvHashMap as HashMap;
1012
use lazy_static::lazy_static;
1113
use parking_lot::Mutex;
@@ -139,7 +141,8 @@ pub trait CountingMachine {
139141
fn operator_counts(&mut self) -> Result<BTreeMap<OperatorCode, u64>>;
140142
}
141143

142-
impl CountingMachine for Machine {
144+
#[cfg(not(feature = "sp1"))]
145+
impl CountingMachine for crate::Machine {
143146
fn operator_counts(&mut self) -> Result<BTreeMap<OperatorCode, u64>> {
144147
let mut counts = BTreeMap::new();
145148

0 commit comments

Comments
 (0)