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

Scroll dev 0830 (v2.0.0) #14

Draft
wants to merge 73 commits into
base: upstream-v2.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
d89f1d1
add bn254 scalar arith chip
kunxian-xia Apr 18, 2024
11bf5ad
refactor
kunxian-xia Apr 19, 2024
c926509
finish bn254-scalar-arith chip and add unit test
kunxian-xia Apr 23, 2024
30186b7
include entrypoint in unit test
kunxian-xia Apr 23, 2024
7bf8d1e
include bn254 scalar arith chip in riscv air
kunxian-xia Apr 24, 2024
52b5103
add selector
kunxian-xia Apr 24, 2024
3a6f21b
pass op through value not pointer and refine testcase
kunxian-xia Apr 24, 2024
a0afb44
add poseidon_bn254 example
kunxian-xia May 9, 2024
e769fb4
handle bn254 scalar add and mul separately
kunxian-xia May 16, 2024
89e1ec8
chore: update poseidon-base dep
kunxian-xia May 16, 2024
eb0fddd
reduce total cycles from 2M to 1.4M
kunxian-xia May 17, 2024
28b0bfb
reduce total cycles from 1.4M to 1M
kunxian-xia May 21, 2024
df2d65b
add log in runtime execution
kunxian-xia May 21, 2024
a293b1b
add mac
lightsing May 21, 2024
fb1c13f
add chip
lightsing May 22, 2024
15114bf
fix
lightsing May 22, 2024
c4e0ac3
fix & fmt
lightsing May 22, 2024
ee578f8
fix p3-baby-bear dep
lightsing May 22, 2024
6aaec72
remove add
lightsing May 22, 2024
6397a79
fix twirp & reqwest
lightsing May 22, 2024
c228ae3
add memcpy chip
lightsing Jun 6, 2024
9ee89cd
add eval_memory_access_slice
lightsing Jun 6, 2024
61b3431
update Cargo.lock
lightsing Jun 13, 2024
6ed60da
update Cargo.toml
lightsing Jun 13, 2024
6f5d81b
cleanup
lightsing Jun 13, 2024
a61929f
Merge remote-tracking branch 'scroll/main' into feat/poseidon_bn254
kunxian-xia Jun 13, 2024
7eb3efb
fix errors after merge upstream
kunxian-xia Jun 13, 2024
46cd5a9
Merge remote-tracking branch 'scroll/main' into feat/poseidon_bn254
kunxian-xia Jun 13, 2024
4da5a36
fix merge errors
kunxian-xia Jun 13, 2024
42be5d2
Merge remote-tracking branch 'scroll/feat/poseidon_bn254' into feat/m…
kunxian-xia Jun 13, 2024
e84c37c
Merge branch 'refs/heads/feat/poseidon_bn254' into feat/memcpy
lightsing Jun 14, 2024
003227d
Merge remote-tracking branch 'refs/remotes/scroll/feat/memcpy' into f…
lightsing Jun 14, 2024
8821135
fix upgrade
lightsing Jun 14, 2024
15cf7f1
use self-hosted runner for pr ci
kunxian-xia Jun 14, 2024
340d982
Merge remote-tracking branch 'scroll/dev' into feat/poseidon_bn254
kunxian-xia Jun 14, 2024
d455d69
self-hosted runner
kunxian-xia Jun 14, 2024
30d29bc
fix
kunxian-xia Jun 14, 2024
795eefc
Merge remote-tracking branch 'scroll/feat/poseidon_bn254' into feat/m…
kunxian-xia Jun 14, 2024
ec58b04
fix
kunxian-xia Jun 14, 2024
ad46cdf
update elf
kunxian-xia Jun 14, 2024
c7d0a5e
Merge pull request #2 from scroll-tech/feat/memcpy
kunxian-xia Jun 14, 2024
e755c43
Merge remote-tracking branch 'origin/main' into feat/poseidon_bn254
lispc Jun 27, 2024
1b558fe
use 0 as bn254 scalar nonce. FIX later
lispc Jun 27, 2024
1b8e677
Merge remote-tracking branch 'origin/main' into scroll-dev-0720
lispc Jul 20, 2024
d70246f
pass cargo check
lispc Jul 20, 2024
97cddcb
update SP1_CIRCUIT_VERSION and use native for gnark
lispc Jul 20, 2024
85dfdb4
fix NumWords::USIZE
lispc Jul 20, 2024
52febcc
Merge remote-tracking branch 'origin/main' into scroll-dev-0720
lispc Jul 23, 2024
187e723
fix syscall nonce
lispc Jul 24, 2024
3c6a63e
minor..
lispc Jul 24, 2024
2dddad4
separate mul mac
lispc Jul 24, 2024
cfc395d
more log
lispc Jul 25, 2024
d614a30
cache srs
lispc Jul 25, 2024
5b5cb73
try fix syscall nonce
lispc Jul 25, 2024
2e84640
expect none
lispc Jul 25, 2024
8c3a2dd
use syscall nonce 0 for scroll syscall
lispc Jul 25, 2024
8ff49ac
disable some logs
lispc Jul 26, 2024
0d28654
Merge remote-tracking branch 'origin/main' into scroll-dev-0803
lispc Aug 3, 2024
2664885
pass build
lispc Aug 3, 2024
5e39747
add compress instrument
lispc Aug 3, 2024
226bc0b
Merge remote-tracking branch 'origin/main' into scroll-dev-0807
lispc Aug 7, 2024
956c3ab
Merge tag 'v1.2.0-rc2' into scroll-v1.2.0-rc2
lispc Aug 30, 2024
955657a
build
lispc Aug 30, 2024
b68b0cd
fix syscall id
lispc Aug 30, 2024
b5c7c48
fix syscall id
lispc Aug 30, 2024
0329f4e
fix syscall id
lispc Aug 30, 2024
9332c76
Merge tag 'v1.2.0' into scroll-dev-0830
lispc Sep 4, 2024
55f0b92
add debug feature to prover crate
lispc Sep 6, 2024
b90a7f8
fix crates/core/machine/src/riscv/mod.rs
lispc Sep 8, 2024
8db9e57
fix CostEstimator
lispc Sep 8, 2024
b6d2941
Merge tag 'v2.0.0' into scroll-dev-0830
lispc Sep 18, 2024
4ae13b7
fix build
lispc Sep 18, 2024
bc5b34f
dont use gpu for wrap_bn254 and shrink
lispc Sep 18, 2024
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
14 changes: 14 additions & 0 deletions crates/core/executor/src/events/memcpy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use super::{LookupId, MemoryReadRecord, MemoryWriteRecord};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MemCopyEvent {
pub lookup_id: LookupId,
pub shard: u32,
pub channel: u8,
pub clk: u32,
pub src_ptr: u32,
pub dst_ptr: u32,
pub read_records: Vec<MemoryReadRecord>,
pub write_records: Vec<MemoryWriteRecord>,
}
2 changes: 2 additions & 0 deletions crates/core/executor/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
mod alu;
mod byte;
mod cpu;
mod memcpy;
mod memory;
mod precompiles;
mod utils;

pub use alu::*;
pub use byte::*;
pub use cpu::*;
pub use memcpy::*;
pub use memory::*;
pub use precompiles::*;
pub use utils::*;
167 changes: 167 additions & 0 deletions crates/core/executor/src/events/precompiles/bn254_scalar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
use crate::{
operations::field::params::{FieldParameters, NumWords},
operations::field::{field_op::FieldOperation, params::Limbs},
runtime::{MemoryReadRecord, MemoryWriteRecord, SyscallContext},
};
*/
use num::BigUint;
use sp1_curves::{
params::{FieldParameters, NumWords},
weierstrass::bn254::Bn254ScalarField,
};
use typenum::Unsigned;

use serde::{Deserialize, Serialize};

use crate::{
events::{LookupId, MemoryReadRecord, MemoryWriteRecord},
syscalls::SyscallContext,
};

use super::FieldOperation;

pub const NUM_WORDS_PER_FE: usize = 8;

#[derive(PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Bn254FieldOperation {
Mul = 2,
Mac = 4,
}

impl Bn254FieldOperation {
pub const fn to_field_operation(&self) -> FieldOperation {
match self {
Bn254FieldOperation::Mul => FieldOperation::Mul,
Bn254FieldOperation::Mac => panic!("not supported"),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Bn254FieldArithEvent {
pub lookup_id: LookupId,
pub shard: u32,
pub channel: u8,
pub clk: u32,
pub op: Bn254FieldOperation,
pub arg1: FieldArithMemoryAccess<MemoryWriteRecord>,
pub arg2: FieldArithMemoryAccess<MemoryReadRecord>,
pub a: Option<FieldArithMemoryAccess<MemoryReadRecord>>,
pub b: Option<FieldArithMemoryAccess<MemoryReadRecord>>,
}

pub fn create_bn254_scalar_arith_event(
rt: &mut SyscallContext,
arg1: u32,
arg2: u32,
op: Bn254FieldOperation,
) -> Bn254FieldArithEvent {
let start_clk = rt.clk;
let p_ptr = arg1;
let q_ptr = arg2;

assert_eq!(p_ptr % 4, 0, "p_ptr({:x}) is not aligned", p_ptr);
assert_eq!(q_ptr % 4, 0, "q_ptr({:x}) is not aligned", q_ptr);

let nw_per_fe = <Bn254ScalarField as NumWords>::WordsFieldElement::USIZE;
debug_assert_eq!(nw_per_fe, NUM_WORDS_PER_FE);

let arg1: Vec<u32> = rt.slice_unsafe(p_ptr, nw_per_fe);
let arg2 = match op {
Bn254FieldOperation::Mac => FieldArithMemoryAccess::read(rt, arg2, 2),
_ => FieldArithMemoryAccess::read(rt, arg2, nw_per_fe),
};

let bn_arg1 = BigUint::from_bytes_le(
&arg1.iter().copied().flat_map(|word| word.to_le_bytes()).collect::<Vec<u8>>(),
);
let modulus = Bn254ScalarField::modulus();

let (a, b, bn_arg1_out) = if matches!(op, Bn254FieldOperation::Mac) {
let a = FieldArithMemoryAccess::read(rt, arg2.memory_records[0].value, nw_per_fe);
let b = FieldArithMemoryAccess::read(rt, arg2.memory_records[1].value, nw_per_fe);

let bn_a = a.value_as_biguint();
let bn_b = b.value_as_biguint();
let bn_arg1_out = (&bn_a * &bn_b + &bn_arg1) % modulus;

(Some(a), Some(b), bn_arg1_out)
} else {
let bn_arg2 = arg2.value_as_biguint();

let bn_arg1_out = match op {
Bn254FieldOperation::Mul => (&bn_arg1 * &bn_arg2) % modulus,
_ => unimplemented!("not supported"),
};
(None, None, bn_arg1_out)
};

log::trace!(
"shard: {}, clk: {}, op: {:?}, arg1: {:?}, arg2: {:?}, a: {:?}, b: {:?}",
rt.current_shard(),
rt.clk,
op,
arg1,
arg2,
a,
b
);

let mut result_words = bn_arg1_out.to_u32_digits();
result_words.resize(nw_per_fe, 0);

let arg1 = FieldArithMemoryAccess::write(rt, p_ptr, &result_words);

let shard = rt.current_shard();
Bn254FieldArithEvent {
lookup_id: rt.syscall_lookup_id,
shard,
channel: rt.current_channel(),
clk: start_clk,
op,
arg1,
arg2,
a,
b,
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FieldArithMemoryAccess<T> {
pub ptr: u32,
pub memory_records: Vec<T>,
}

impl FieldArithMemoryAccess<MemoryReadRecord> {
pub fn read(rt: &mut SyscallContext, ptr: u32, len: usize) -> Self {
let (memory_records, _) = rt.mr_slice(ptr, len);
Self { ptr, memory_records }
}

pub fn value_as_biguint(&self) -> BigUint {
BigUint::from_bytes_le(
&self
.memory_records
.iter()
.flat_map(|word| word.value.to_le_bytes())
.collect::<Vec<u8>>(),
)
}
}

impl FieldArithMemoryAccess<MemoryWriteRecord> {
pub fn write(rt: &mut SyscallContext, ptr: u32, values: &[u32]) -> Self {
Self { ptr, memory_records: rt.mw_slice(ptr, &values) }
}

pub fn prev_value_as_biguint(&self) -> BigUint {
BigUint::from_bytes_le(
&self
.memory_records
.iter()
.flat_map(|word| word.prev_value.to_le_bytes())
.collect::<Vec<u8>>(),
)
}
}
4 changes: 4 additions & 0 deletions crates/core/executor/src/events/precompiles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bn254_scalar;
mod ec;
mod edwards;
mod fptower;
Expand All @@ -6,6 +7,9 @@ mod sha256_compress;
mod sha256_extend;
mod uint256;

pub use bn254_scalar::{
create_bn254_scalar_arith_event, Bn254FieldArithEvent, Bn254FieldOperation, NUM_WORDS_PER_FE,
};
pub use ec::*;
pub use edwards::*;
pub use fptower::*;
Expand Down
48 changes: 47 additions & 1 deletion crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ impl<'a> Executor<'a> {
let value = (memory_read_value).to_le_bytes()[(addr % 4) as usize];
a = ((value as i8) as i32) as u32;
memory_store_value = Some(memory_read_value);
//println!(
// "[clk: {}, pc: 0x{:x}] LB: {:?} <- {:x}",
// self.state.global_clk, self.state.pc, rd, a
//);
self.rw(rd, a);
}
Opcode::LH => {
Expand All @@ -784,6 +788,10 @@ impl<'a> Executor<'a> {
};
a = ((value as i16) as i32) as u32;
memory_store_value = Some(memory_read_value);
//println!(
// "[clk: {}, pc: 0x{:x}] LH: {:?} <- {:x}",
// self.state.global_clk, self.state.pc, rd, a
//);
self.rw(rd, a);
}
Opcode::LW => {
Expand All @@ -793,6 +801,10 @@ impl<'a> Executor<'a> {
}
a = memory_read_value;
memory_store_value = Some(memory_read_value);
//println!(
// "[clk: {}, pc: 0x{:x}] LW: {:?} <- {}",
// self.state.global_clk, self.state.pc, rd, a
//);
self.rw(rd, a);
}
Opcode::LBU => {
Expand Down Expand Up @@ -828,6 +840,10 @@ impl<'a> Executor<'a> {
_ => unreachable!(),
};
memory_store_value = Some(value);
//println!(
// "[clk: {}, pc: 0x{:x}] SB 0x{:x} <- 0x{:x}",
// self.state.global_clk, pc, addr, value
//);
self.mw_cpu(align(addr), value, MemoryAccessPosition::Memory);
}
Opcode::SH => {
Expand All @@ -841,6 +857,10 @@ impl<'a> Executor<'a> {
_ => unreachable!(),
};
memory_store_value = Some(value);
//println!(
// "[clk: {}, pc: 0x{:x}] SH 0x{:x} <- 0x{:x}",
// self.state.global_clk, pc, addr, value
//);
self.mw_cpu(align(addr), value, MemoryAccessPosition::Memory);
}
Opcode::SW => {
Expand All @@ -850,6 +870,10 @@ impl<'a> Executor<'a> {
}
let value = a;
memory_store_value = Some(value);
//println!(
// "[clk: {}, pc: 0x{:x}] SW 0x{:x} <- 0x{:x}",
// self.state.global_clk, pc, addr, value
//);
self.mw_cpu(align(addr), value, MemoryAccessPosition::Memory);
}

Expand Down Expand Up @@ -941,9 +965,18 @@ impl<'a> Executor<'a> {
return Err(ExecutionError::InvalidSyscallUsage(syscall_id as u64));
}

let global_clk = self.state.global_clk;
let syscall_impl = self.get_syscall(syscall).cloned();
let mut precompile_rt = SyscallContext::new(self);
precompile_rt.syscall_lookup_id = syscall_lookup_id;
log::trace!(
"[clk: {}, pc: 0x{:x}] ecall syscall_id=0x{:x}, b: 0x{:x}, c: 0x{:x}",
global_clk,
pc,
syscall_id,
b,
c,
);
let (precompile_next_pc, precompile_cycles, returned_exit_code) =
if let Some(syscall_impl) = syscall_impl {
// Executing a syscall optionally returns a value to write to the t0
Expand Down Expand Up @@ -991,7 +1024,20 @@ impl<'a> Executor<'a> {
_ => (self.opts.split_opts.deferred, 1),
};
let nonce = (((*syscall_count as usize) % threshold) * multiplier) as u32;
self.record.nonce_lookup.insert(syscall_lookup_id, nonce);
// FIXME
match syscall {
SyscallCode::BN254_SCALAR_MAC
| SyscallCode::BN254_SCALAR_MUL
| SyscallCode::MEMCPY_32
| SyscallCode::MEMCPY_64 => {}
_ => {
self.record.nonce_lookup.insert(syscall_lookup_id, nonce);
}
}

//log::info!(
// "execute_instruction {syscall:?} {syscall_count} {nonce} {syscall_lookup_id}"
//);
*syscall_count += 1;
}
Opcode::EBREAK => {
Expand Down
Loading
Loading