Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc committed Sep 14, 2024
1 parent 13403cf commit 7fa84e7
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 182 deletions.
7 changes: 4 additions & 3 deletions crates/core/executor/src/events/memcpy.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::{LookupId, MemoryReadRecord, MemoryWriteRecord};
use super::{LookupId, MemoryLocalEvent, MemoryReadRecord, MemoryWriteRecord};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Default, 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>,
/// The local memory access records.
pub local_mem_access: Vec<MemoryLocalEvent>,
}
23 changes: 10 additions & 13 deletions crates/core/executor/src/events/precompiles/bn254_scalar.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
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},
Expand All @@ -15,16 +8,18 @@ use typenum::Unsigned;
use serde::{Deserialize, Serialize};

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

use super::FieldOperation;

pub const NUM_WORDS_PER_FE: usize = 8;

#[derive(PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
#[derive(Default, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Bn254FieldOperation {
#[default]
Invalid = 0,
Mul = 2,
Mac = 4,
}
Expand All @@ -34,21 +29,23 @@ impl Bn254FieldOperation {
match self {
Bn254FieldOperation::Mul => FieldOperation::Mul,
Bn254FieldOperation::Mac => panic!("not supported"),
Bn254FieldOperation::Invalid => panic!("what??"),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Default, 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>>,
/// The local memory access records.
pub local_mem_access: Vec<MemoryLocalEvent>,
}

pub fn create_bn254_scalar_arith_event(
Expand Down Expand Up @@ -117,17 +114,17 @@ pub fn create_bn254_scalar_arith_event(
Bn254FieldArithEvent {
lookup_id: rt.syscall_lookup_id,
shard,
channel: rt.current_channel(),
clk: start_clk,
op,
arg1,
arg2,
a,
b,
local_mem_access: rt.postprocess(),
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct FieldArithMemoryAccess<T> {
pub ptr: u32,
pub memory_records: Vec<T>,
Expand Down
12 changes: 11 additions & 1 deletion crates/core/executor/src/events/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use sha256_extend::*;
use strum::EnumIter;
pub use uint256::*;

use super::MemoryLocalEvent;
use super::{MemCopyEvent, MemoryLocalEvent};

#[derive(Clone, Debug, Serialize, Deserialize, EnumIter)]
/// Precompile event. There should be one variant for every precompile syscall.
Expand All @@ -39,6 +39,10 @@ pub enum PrecompileEvent {
Bn254Fp(FpOpEvent),
Bn254Fp2AddSub(Fp2AddSubEvent),
Bn254Fp2Mul(Fp2MulEvent),
Bn254ScalarMac(Bn254FieldArithEvent),
Bn254ScalarMul(Bn254FieldArithEvent),
MemCopy32(MemCopyEvent),
MemCopy64(MemCopyEvent),
Bls12381Add(EllipticCurveAddEvent),
Bls12381Double(EllipticCurveDoubleEvent),
Bls12381Decompress(EllipticCurveDecompressEvent),
Expand Down Expand Up @@ -98,6 +102,12 @@ impl PrecompileLocalMemory for Vec<PrecompileEvent> {
PrecompileEvent::Bls12381Fp2Mul(e) | PrecompileEvent::Bn254Fp2Mul(e) => {
iterators.push(e.local_mem_access.iter());
}
PrecompileEvent::Bn254ScalarMac(e) | PrecompileEvent::Bn254ScalarMul(e) => {
iterators.push(e.local_mem_access.iter());
}
PrecompileEvent::MemCopy32(e) | PrecompileEvent::MemCopy64(e) => {
iterators.push(e.local_mem_access.iter());
}
}
}

Expand Down
53 changes: 0 additions & 53 deletions crates/core/executor/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ pub struct ExecutionRecord {
/// A trace of all the syscall events.
pub syscall_events: Vec<SyscallEvent>,

pub bn254_scalar_mul_events: Vec<Bn254FieldArithEvent>,
pub bn254_scalar_mac_events: Vec<Bn254FieldArithEvent>,
pub memcpy32_events: Vec<MemCopyEvent>,
pub memcpy64_events: Vec<MemCopyEvent>,

/// The public values.
pub public_values: PublicValues<u32, u32>,
/// The nonce lookup.
Expand Down Expand Up @@ -134,10 +129,6 @@ impl ExecutionRecord {
execution_record.global_memory_finalize_events =
std::mem::take(&mut self.global_memory_finalize_events);
execution_record
bn254_scalar_mul_events: std::mem::take(&mut self.bn254_scalar_mul_events),
bn254_scalar_mac_events: std::mem::take(&mut self.bn254_scalar_mac_events),
memcpy32_events: std::mem::take(&mut self.memcpy32_events),
memcpy64_events: std::mem::take(&mut self.memcpy64_events),
}

/// Splits the deferred [`ExecutionRecord`] into multiple [`ExecutionRecord`]s, each which
Expand Down Expand Up @@ -176,10 +167,6 @@ impl ExecutionRecord {
shards.append(&mut event_shards);
}

split_events!(self, memcpy32_events, shards, opts.deferred, last);
split_events!(self, memcpy64_events, shards, opts.deferred, last);
split_events!(self, bn254_scalar_mul_events, shards, opts.deferred, last);
split_events!(self, bn254_scalar_mac_events, shards, opts.deferred, last);
// _ = last_pct;

if last {
Expand Down Expand Up @@ -320,12 +307,6 @@ impl MachineRecord for ExecutionRecord {
);
stats.insert("local_memory_access_events".to_string(), self.cpu_local_memory_access.len());

stats.insert("bn254_scalar_mul_events".to_string(), self.bn254_scalar_mul_events.len());
stats.insert("bn254_scalar_mac_events".to_string(), self.bn254_scalar_mac_events.len());

stats.insert("memcpy32_events".to_string(), self.memcpy32_events.len());
stats.insert("memcpy64_events".to_string(), self.memcpy64_events.len());

if !self.cpu_events.is_empty() {
let shard = self.cpu_events[0].shard;
stats.insert(
Expand All @@ -351,11 +332,6 @@ impl MachineRecord for ExecutionRecord {
self.syscall_events.append(&mut other.syscall_events);

self.precompile_events.append(&mut other.precompile_events);
self.bn254_scalar_mul_events.append(&mut other.bn254_scalar_mul_events);
self.bn254_scalar_mac_events.append(&mut other.bn254_scalar_mac_events);
self.memcpy32_events.append(&mut other.memcpy32_events);
self.memcpy64_events.append(&mut other.memcpy64_events);


if self.byte_lookups.is_empty() {
self.byte_lookups = std::mem::take(&mut other.byte_lookups);
Expand Down Expand Up @@ -400,35 +376,6 @@ impl MachineRecord for ExecutionRecord {
self.lt_events.iter().enumerate().for_each(|(i, event)| {
self.nonce_lookup.insert(event.lookup_id, i as u32);
});

/*
self.memcpy32_events
.iter()
.enumerate()
.for_each(|(i, event)| {
self.nonce_lookup.insert(event.lookup_id, i as u32);
});
self.memcpy64_events
.iter()
.enumerate()
.for_each(|(i, event)| {
self.nonce_lookup.insert(event.lookup_id, i as u32);
});
self.bn254_scalar_mul_events
.iter()
.enumerate()
.for_each(|(i, event)| {
self.nonce_lookup.insert(event.lookup_id, i as u32);
});
self.bn254_scalar_mac_events
.iter()
.enumerate()
.for_each(|(i, event)| {
self.nonce_lookup.insert(event.lookup_id, i as u32);
});
*/
}

/// Retrieves the public values. This method is needed for the `MachineRecord` trait, since
Expand Down
24 changes: 18 additions & 6 deletions crates/core/executor/src/syscalls/precompiles/bn254_scalar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use crate::{
events::{create_bn254_scalar_arith_event, Bn254FieldOperation},
syscalls::{Syscall, SyscallContext},
events::{create_bn254_scalar_arith_event, Bn254FieldOperation, PrecompileEvent},
syscalls::{Syscall, SyscallCode, SyscallContext},
};

pub(crate) struct Bn254ScalarMacSyscall;

impl Syscall for Bn254ScalarMacSyscall {
fn execute(&self, rt: &mut SyscallContext, arg1: u32, arg2: u32) -> Option<u32> {
fn execute(
&self,
rt: &mut SyscallContext,
syscall_code: SyscallCode,
arg1: u32,
arg2: u32,
) -> Option<u32> {
let event = create_bn254_scalar_arith_event(rt, arg1, arg2, Bn254FieldOperation::Mac);
rt.record_mut().bn254_scalar_mac_events.push(event);
rt.record_mut().add_precompile_event(syscall_code, PrecompileEvent::Bn254ScalarMac(event));

None
}
Expand All @@ -20,9 +26,15 @@ impl Syscall for Bn254ScalarMacSyscall {

pub(crate) struct Bn254ScalarMulSyscall;
impl Syscall for Bn254ScalarMulSyscall {
fn execute(&self, rt: &mut SyscallContext, arg1: u32, arg2: u32) -> Option<u32> {
fn execute(
&self,
rt: &mut SyscallContext,
syscall_code: SyscallCode,
arg1: u32,
arg2: u32,
) -> Option<u32> {
let event = create_bn254_scalar_arith_event(rt, arg1, arg2, Bn254FieldOperation::Mul);
rt.record_mut().bn254_scalar_mul_events.push(event);
rt.record_mut().add_precompile_event(syscall_code, PrecompileEvent::Bn254ScalarMul(event));

None
}
Expand Down
38 changes: 23 additions & 15 deletions crates/core/executor/src/syscalls/precompiles/memcopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::marker::PhantomData;
use generic_array::ArrayLength;

use crate::{
events::MemCopyEvent,
syscalls::{Syscall, SyscallContext},
events::{MemCopyEvent, PrecompileEvent},
syscalls::{Syscall, SyscallCode, SyscallContext},
};

pub struct MemCopySyscall<NumWords: ArrayLength, NumBytes: ArrayLength> {
Expand All @@ -20,26 +20,34 @@ impl<NumWords: ArrayLength, NumBytes: ArrayLength> MemCopySyscall<NumWords, NumB
impl<NumWords: ArrayLength + Send + Sync, NumBytes: ArrayLength + Send + Sync> Syscall
for MemCopySyscall<NumWords, NumBytes>
{
fn execute(&self, ctx: &mut SyscallContext, src: u32, dst: u32) -> Option<u32> {
let (read, read_bytes) = ctx.mr_slice(src, NumWords::USIZE);
let write = ctx.mw_slice(dst, &read_bytes);
fn execute(
&self,
rt: &mut SyscallContext,
syscall_code: SyscallCode,
src: u32,
dst: u32,
) -> Option<u32> {
let (read, read_bytes) = rt.mr_slice(src, NumWords::USIZE);
let write = rt.mw_slice(dst, &read_bytes);

let event = MemCopyEvent {
lookup_id: ctx.syscall_lookup_id,
shard: ctx.current_shard(),
channel: ctx.current_channel(),
clk: ctx.clk,
lookup_id: rt.syscall_lookup_id,
shard: rt.current_shard(),
clk: rt.clk,
src_ptr: src,
dst_ptr: dst,
read_records: read,
write_records: write,
local_mem_access: rt.postprocess(),
};
(match NumWords::USIZE {
8 => &mut ctx.record_mut().memcpy32_events,
16 => &mut ctx.record_mut().memcpy64_events,
_ => panic!("invalid uszie {}", NumWords::USIZE),
})
.push(event);
rt.record_mut().add_precompile_event(
syscall_code,
match NumWords::USIZE {
8 => PrecompileEvent::MemCopy32(event),
16 => PrecompileEvent::MemCopy64(event),
_ => panic!("invalid uszie {}", NumWords::USIZE),
},
);

None
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/machine/src/riscv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
syscall::{
memcpy::{self, MemCopy32Chip, MemCopy64Chip, MemCopyChip},
precompiles::{
bn254_scalar::{self, Bn254ScalarMacChip},
bn254_scalar::{self, Bn254ScalarMacChip, Bn254ScalarMulChip},
fptower::{Fp2AddSubAssignChip, Fp2MulAssignChip, FpOpChip},
},
},
Expand Down
Loading

0 comments on commit 7fa84e7

Please sign in to comment.