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: LookupId struct instead of u128 #1427

Merged
merged 6 commits into from
Aug 29, 2024
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
10 changes: 5 additions & 5 deletions crates/core/executor/src/events/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::Opcode;

use super::create_alu_lookups;
use super::{create_alu_lookups, LookupId};

/// Arithmetic Logic Unit (ALU) Event.
///
Expand All @@ -11,7 +11,7 @@ use super::create_alu_lookups;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct AluEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand All @@ -26,16 +26,16 @@ pub struct AluEvent {
pub b: u32,
/// The third operand.
pub c: u32,
/// The result of the operation.
pub sub_lookups: [u128; 6],
/// The result of the operation in the format of [``LookupId``; 6]
pub sub_lookups: [LookupId; 6],
}

impl AluEvent {
/// Create a new [`AluEvent`].
#[must_use]
pub fn new(shard: u32, channel: u8, clk: u32, opcode: Opcode, a: u32, b: u32, c: u32) -> Self {
Self {
lookup_id: 0,
lookup_id: LookupId::default(),
shard,
channel,
clk,
Expand Down
22 changes: 11 additions & 11 deletions crates/core/executor/src/events/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::Instruction;

use super::memory::MemoryRecordEnum;
use super::{memory::MemoryRecordEnum, LookupId};

/// CPU Event.
///
Expand Down Expand Up @@ -41,23 +41,23 @@ pub struct CpuEvent {
/// The exit code.
pub exit_code: u32,
/// The ALU lookup id.
pub alu_lookup_id: u128,
pub alu_lookup_id: LookupId,
/// The syscall lookup id.
pub syscall_lookup_id: u128,
pub syscall_lookup_id: LookupId,
/// The memory add lookup id.
pub memory_add_lookup_id: u128,
pub memory_add_lookup_id: LookupId,
/// The memory sub lookup id.
pub memory_sub_lookup_id: u128,
pub memory_sub_lookup_id: LookupId,
/// The branch gt lookup id.
pub branch_gt_lookup_id: u128,
pub branch_gt_lookup_id: LookupId,
/// The branch lt lookup id.
pub branch_lt_lookup_id: u128,
pub branch_lt_lookup_id: LookupId,
/// The branch add lookup id.
pub branch_add_lookup_id: u128,
pub branch_add_lookup_id: LookupId,
/// The jump jal lookup id.
pub jump_jal_lookup_id: u128,
pub jump_jal_lookup_id: LookupId,
/// The jump jalr lookup id.
pub jump_jalr_lookup_id: u128,
pub jump_jalr_lookup_id: LookupId,
/// The auipc lookup id.
pub auipc_lookup_id: u128,
pub auipc_lookup_id: LookupId,
}
11 changes: 7 additions & 4 deletions crates/core/executor/src/events/precompiles/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use sp1_primitives::consts::{bytes_to_words_le_vec, words_to_bytes_le_vec};
use typenum::Unsigned;

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

Expand All @@ -18,7 +21,7 @@ use crate::{
/// This event is emitted when an elliptic curve addition operation is performed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EllipticCurveAddEvent {
pub(crate) lookup_id: u128,
pub(crate) lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand All @@ -45,7 +48,7 @@ pub struct EllipticCurveAddEvent {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EllipticCurveDoubleEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand All @@ -66,7 +69,7 @@ pub struct EllipticCurveDoubleEvent {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EllipticCurveDecompressEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
7 changes: 5 additions & 2 deletions crates/core/executor/src/events/precompiles/edwards.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use serde::{Deserialize, Serialize};
use sp1_curves::{edwards::WORDS_FIELD_ELEMENT, COMPRESSED_POINT_BYTES, NUM_BYTES_FIELD_ELEMENT};

use crate::events::memory::{MemoryReadRecord, MemoryWriteRecord};
use crate::events::{
memory::{MemoryReadRecord, MemoryWriteRecord},
LookupId,
};

/// Edwards Decompress Event.
///
/// This event is emitted when an edwards decompression operation is performed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdDecompressEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
10 changes: 5 additions & 5 deletions crates/core/executor/src/events/precompiles/fptower.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};

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

/// Airthmetic operation for emulating modular arithmetic.
/// This is an arithmetic operation for emulating modular arithmetic.
#[derive(PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum FieldOperation {
/// Addition.
Expand All @@ -21,7 +21,7 @@ pub enum FieldOperation {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FpOpEvent {
/// The lookup id.
pub lookup_id: usize,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct FpOpEvent {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fp2AddSubEvent {
/// The lookup id.
pub lookup_id: usize,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand All @@ -77,7 +77,7 @@ pub struct Fp2AddSubEvent {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fp2MulEvent {
/// The lookup id.
pub lookup_id: usize,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::{Deserialize, Serialize};

use crate::events::memory::{MemoryReadRecord, MemoryWriteRecord};
use crate::events::{
memory::{MemoryReadRecord, MemoryWriteRecord},
LookupId,
};

pub(crate) const STATE_SIZE: usize = 25;

Expand All @@ -10,7 +13,7 @@ pub(crate) const STATE_SIZE: usize = 25;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeccakPermuteEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use serde::{Deserialize, Serialize};

use crate::events::memory::{MemoryReadRecord, MemoryWriteRecord};
use crate::events::{
memory::{MemoryReadRecord, MemoryWriteRecord},
LookupId,
};

/// SHA-256 Compress Event.
///
/// This event is emitted when a SHA-256 compress operation is performed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShaCompressEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
3 changes: 2 additions & 1 deletion crates/core/executor/src/events/precompiles/sha256_extend.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use serde::{Deserialize, Serialize};

use crate::events::memory::{MemoryReadRecord, MemoryWriteRecord};
use crate::events::LookupId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a space after an import


/// SHA-256 Extend Event.
///
/// This event is emitted when a SHA-256 extend operation is performed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShaExtendEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
7 changes: 5 additions & 2 deletions crates/core/executor/src/events/precompiles/uint256.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use serde::{Deserialize, Serialize};

use crate::events::memory::{MemoryReadRecord, MemoryWriteRecord};
use crate::events::{
memory::{MemoryReadRecord, MemoryWriteRecord},
LookupId,
};

/// Uint256 Mul Event.
///
/// This event is emitted when a uint256 mul operation is performed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Uint256MulEvent {
/// The lookup identifer.
pub lookup_id: u128,
pub lookup_id: LookupId,
/// The shard number.
pub shard: u32,
/// The channel number.
Expand Down
37 changes: 31 additions & 6 deletions crates/core/executor/src/events/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
use serde::Deserialize;
use serde::Serialize;
use std::{
fmt::Display,
iter::{Map, Peekable},
};

use rand::{thread_rng, Rng};

/// Creates a new ALU lookup id.
/// A unique identifier for lookups.
///
/// We use 4 u32s instead of a u128 to make it compatible with C.
#[derive(Deserialize, Serialize, Debug, Clone, Copy, Default, Eq, Hash, PartialEq)]
jtguibas marked this conversation as resolved.
Show resolved Hide resolved

pub struct LookupId {
/// First part of the id.
pub a: u32,
/// Second part of the id.
pub b: u32,
/// Third part of the id.
pub c: u32,
/// Fourth part of the id.
pub d: u32,
}

/// Creates a new ALU lookup id with ``LookupId``
#[must_use]
pub fn create_alu_lookup_id() -> u128 {
pub fn create_alu_lookup_id() -> LookupId {
let mut rng = thread_rng();
rng.gen()
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() }
}

/// Creates a new ALU lookup ids.
/// Creates a new ALU lookup id with ``LookupId``
#[must_use]
pub fn create_alu_lookups() -> [u128; 6] {
pub fn create_alu_lookups() -> [LookupId; 6] {
let mut rng = thread_rng();
[rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()]
[
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
LookupId { a: rng.gen(), b: rng.gen(), c: rng.gen(), d: rng.gen() },
]
}

/// Returns sorted and formatted rows of a table of counts (e.g. `opcode_counts`).
Expand Down
27 changes: 17 additions & 10 deletions crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use thiserror::Error;
use crate::{
context::SP1Context,
events::{
create_alu_lookup_id, create_alu_lookups, AluEvent, CpuEvent, MemoryAccessPosition,
MemoryInitializeFinalizeEvent, MemoryReadRecord, MemoryRecord, MemoryWriteRecord,
create_alu_lookup_id, create_alu_lookups, AluEvent, CpuEvent, LookupId,
MemoryAccessPosition, MemoryInitializeFinalizeEvent, MemoryReadRecord, MemoryRecord,
MemoryWriteRecord,
},
hook::{HookEnv, HookRegistry},
record::{ExecutionRecord, MemoryAccessRecord},
Expand Down Expand Up @@ -525,8 +526,8 @@ impl<'a> Executor<'a> {
memory_store_value: Option<u32>,
record: MemoryAccessRecord,
exit_code: u32,
lookup_id: u128,
syscall_lookup_id: u128,
lookup_id: LookupId,
syscall_lookup_id: LookupId,
) {
let cpu_event = CpuEvent {
shard,
Expand Down Expand Up @@ -560,7 +561,7 @@ impl<'a> Executor<'a> {
}

/// Emit an ALU event.
fn emit_alu(&mut self, clk: u32, opcode: Opcode, a: u32, b: u32, c: u32, lookup_id: u128) {
fn emit_alu(&mut self, clk: u32, opcode: Opcode, a: u32, b: u32, c: u32, lookup_id: LookupId) {
let event = AluEvent {
lookup_id,
shard: self.shard(),
Expand Down Expand Up @@ -628,7 +629,7 @@ impl<'a> Executor<'a> {
a: u32,
b: u32,
c: u32,
lookup_id: u128,
lookup_id: LookupId,
) {
self.rw(rd, a);
if self.executor_mode == ExecutorMode::Trace {
Expand Down Expand Up @@ -688,10 +689,16 @@ impl<'a> Executor<'a> {
if self.executor_mode != ExecutorMode::Simple {
self.memory_accesses = MemoryAccessRecord::default();
}
let lookup_id =
if self.executor_mode == ExecutorMode::Simple { 0 } else { create_alu_lookup_id() };
let syscall_lookup_id =
if self.executor_mode == ExecutorMode::Simple { 0 } else { create_alu_lookup_id() };
let lookup_id = if self.executor_mode == ExecutorMode::Simple {
LookupId::default()
} else {
create_alu_lookup_id()
};
let syscall_lookup_id = if self.executor_mode == ExecutorMode::Simple {
LookupId::default()
} else {
create_alu_lookup_id()
};

if self.print_report && !self.unconstrained {
self.report
Expand Down
4 changes: 2 additions & 2 deletions crates/core/executor/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{program::Program, Opcode};
use crate::events::{
add_sharded_byte_lookup_events, AluEvent, ByteLookupEvent, ByteRecord, CpuEvent,
EdDecompressEvent, EllipticCurveAddEvent, EllipticCurveDecompressEvent,
EllipticCurveDoubleEvent, Fp2AddSubEvent, Fp2MulEvent, FpOpEvent, KeccakPermuteEvent,
EllipticCurveDoubleEvent, Fp2AddSubEvent, Fp2MulEvent, FpOpEvent, KeccakPermuteEvent, LookupId,
MemoryInitializeFinalizeEvent, MemoryRecordEnum, ShaCompressEvent, ShaExtendEvent,
Uint256MulEvent,
};
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct ExecutionRecord {
/// The public values.
pub public_values: PublicValues<u32, u32>,
/// The nonce lookup.
pub nonce_lookup: HashMap<u128, u32>,
pub nonce_lookup: HashMap<LookupId, u32>,
}

impl ExecutionRecord {
Expand Down
Loading
Loading