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: uint256 add/mul #1299

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 7 additions & 8 deletions core/src/runtime/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::syscall::precompiles::edwards::EdDecompressEvent;
use crate::syscall::precompiles::fptower::{Fp2AddSubEvent, Fp2MulEvent, FpOpEvent};
use crate::syscall::precompiles::keccak256::KeccakPermuteEvent;
use crate::syscall::precompiles::sha256::{ShaCompressEvent, ShaExtendEvent};
use crate::syscall::precompiles::uint256::Uint256MulEvent;
use crate::syscall::precompiles::uint256::Uint256OpEvent;
use crate::syscall::precompiles::ECDecompressEvent;
use crate::syscall::precompiles::{ECAddEvent, ECDoubleEvent};
use crate::utils::SP1CoreOpts;
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct ExecutionRecord {

pub bls12381_double_events: Vec<ECDoubleEvent>,

pub uint256_mul_events: Vec<Uint256MulEvent>,
pub uint256_op_events: Vec<Uint256OpEvent>,

pub memory_initialize_events: Vec<MemoryInitializeFinalizeEvent>,

Expand Down Expand Up @@ -201,8 +201,8 @@ impl MachineRecord for ExecutionRecord {
self.bls12381_double_events.len(),
);
stats.insert(
"uint256_mul_events".to_string(),
self.uint256_mul_events.len(),
"uint256_op_events".to_string(),
self.uint256_op_events.len(),
);
stats.insert(
"bls12381_fp_event".to_string(),
Expand Down Expand Up @@ -281,8 +281,7 @@ impl MachineRecord for ExecutionRecord {
.append(&mut other.bls12381_add_events);
self.bls12381_double_events
.append(&mut other.bls12381_double_events);
self.uint256_mul_events
.append(&mut other.uint256_mul_events);
self.uint256_op_events.append(&mut other.uint256_op_events);
self.bls12381_fp_events
.append(&mut other.bls12381_fp_events);
self.bls12381_fp2_addsub_events
Expand Down Expand Up @@ -426,7 +425,7 @@ impl ExecutionRecord {
ed_add_events: std::mem::take(&mut self.ed_add_events),
ed_decompress_events: std::mem::take(&mut self.ed_decompress_events),
k256_decompress_events: std::mem::take(&mut self.k256_decompress_events),
uint256_mul_events: std::mem::take(&mut self.uint256_mul_events),
uint256_op_events: std::mem::take(&mut self.uint256_op_events),
bls12381_fp_events: std::mem::take(&mut self.bls12381_fp_events),
bls12381_fp2_mul_events: std::mem::take(&mut self.bls12381_fp2_mul_events),
bls12381_decompress_events: std::mem::take(&mut self.bls12381_decompress_events),
Expand Down Expand Up @@ -554,7 +553,7 @@ impl ExecutionRecord {
);
split_events!(
self,
uint256_mul_events,
uint256_op_events,
shards,
opts.deferred_shift_threshold,
last
Expand Down
42 changes: 39 additions & 3 deletions core/src/runtime/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::syscall::precompiles::edwards::EdDecompressChip;
use crate::syscall::precompiles::fptower::{Fp2AddSubSyscall, Fp2MulAssignChip, FpOpSyscall};
use crate::syscall::precompiles::keccak256::KeccakPermuteChip;
use crate::syscall::precompiles::sha256::{ShaCompressChip, ShaExtendChip};
use crate::syscall::precompiles::uint256::Uint256MulChip;
use crate::syscall::precompiles::uint256::Uint256OpSyscall;
use crate::syscall::precompiles::weierstrass::WeierstrassAddAssignChip;
use crate::syscall::precompiles::weierstrass::WeierstrassDecompressChip;
use crate::syscall::precompiles::weierstrass::WeierstrassDoubleAssignChip;
Expand Down Expand Up @@ -97,6 +97,12 @@ pub enum SyscallCode {
/// Executes the `HINT_READ` precompile.
HINT_READ = 0x00_00_00_F1,

/// Executes the `UINT256_ADD` precompile.
UINT256_ADD = 0x00_01_01_1B,

/// Executes the `UINT256_SUB` precompile.
UINT256_SUB = 0x00_01_01_1C,

/// Executes the `UINT256_MUL` precompile.
UINT256_MUL = 0x00_01_01_1D,

Expand Down Expand Up @@ -168,6 +174,8 @@ impl SyscallCode {
0x00_00_00_1B => SyscallCode::VERIFY_SP1_PROOF,
0x00_00_00_F0 => SyscallCode::HINT_LEN,
0x00_00_00_F1 => SyscallCode::HINT_READ,
0x00_01_01_1B => SyscallCode::UINT256_ADD,
0x00_01_01_1C => SyscallCode::UINT256_SUB,
0x00_01_01_1D => SyscallCode::UINT256_MUL,
0x00_01_01_20 => SyscallCode::BLS12381_FP_ADD,
0x00_01_01_21 => SyscallCode::BLS12381_FP_SUB,
Expand Down Expand Up @@ -376,7 +384,18 @@ pub fn default_syscall_map() -> HashMap<SyscallCode, Arc<dyn Syscall>> {
SyscallCode::BLS12381_DOUBLE,
Arc::new(WeierstrassDoubleAssignChip::<Bls12381>::new()),
);
syscall_map.insert(SyscallCode::UINT256_MUL, Arc::new(Uint256MulChip::new()));
syscall_map.insert(
SyscallCode::UINT256_ADD,
Arc::new(Uint256OpSyscall::new(FieldOperation::Add)),
);
syscall_map.insert(
SyscallCode::UINT256_SUB,
Arc::new(Uint256OpSyscall::new(FieldOperation::Sub)),
);
syscall_map.insert(
SyscallCode::UINT256_MUL,
Arc::new(Uint256OpSyscall::new(FieldOperation::Mul)),
);
syscall_map.insert(
SyscallCode::BLS12381_FP_ADD,
Arc::new(FpOpSyscall::<Bls12381BaseField>::new(FieldOperation::Add)),
Expand Down Expand Up @@ -453,7 +472,18 @@ pub fn default_syscall_map() -> HashMap<SyscallCode, Arc<dyn Syscall>> {
SyscallCode::BLS12381_DECOMPRESS,
Arc::new(WeierstrassDecompressChip::<Bls12381>::with_lexicographic_rule()),
);
syscall_map.insert(SyscallCode::UINT256_MUL, Arc::new(Uint256MulChip::new()));
syscall_map.insert(
SyscallCode::UINT256_ADD,
Arc::new(Uint256OpSyscall::new(FieldOperation::Add)),
);
syscall_map.insert(
SyscallCode::UINT256_SUB,
Arc::new(Uint256OpSyscall::new(FieldOperation::Sub)),
);
syscall_map.insert(
SyscallCode::UINT256_MUL,
Arc::new(Uint256OpSyscall::new(FieldOperation::Mul)),
);

syscall_map
}
Expand Down Expand Up @@ -529,6 +559,12 @@ mod tests {
SyscallCode::BN254_DOUBLE => {
assert_eq!(code as u32, sp1_zkvm::syscalls::BN254_DOUBLE)
}
SyscallCode::UINT256_ADD => {
assert_eq!(code as u32, sp1_zkvm::syscalls::UINT256_ADD)
}
SyscallCode::UINT256_SUB => {
assert_eq!(code as u32, sp1_zkvm::syscalls::UINT256_SUB)
}
SyscallCode::UINT256_MUL => {
assert_eq!(code as u32, sp1_zkvm::syscalls::UINT256_MUL)
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/stark/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) mod riscv_chips {
pub use crate::syscall::precompiles::keccak256::KeccakPermuteChip;
pub use crate::syscall::precompiles::sha256::ShaCompressChip;
pub use crate::syscall::precompiles::sha256::ShaExtendChip;
pub use crate::syscall::precompiles::uint256::Uint256MulChip;
pub use crate::syscall::precompiles::uint256::Uint256OpChip;
pub use crate::syscall::precompiles::weierstrass::WeierstrassAddAssignChip;
pub use crate::syscall::precompiles::weierstrass::WeierstrassDecompressChip;
pub use crate::syscall::precompiles::weierstrass::WeierstrassDoubleAssignChip;
Expand Down Expand Up @@ -101,7 +101,7 @@ pub enum RiscvAir<F: PrimeField32> {
/// A precompile for doubling a point on the Elliptic curve bls12_381.
Bls12381Double(WeierstrassDoubleAssignChip<SwCurve<Bls12381Parameters>>),
/// A precompile for uint256 mul.
Uint256Mul(Uint256MulChip),
Uint256Op(Uint256OpChip),
/// A precompile for decompressing a point on the BLS12-381 curve.
Bls12381Decompress(WeierstrassDecompressChip<SwCurve<Bls12381Parameters>>),
/// A precompile for BLS12-381 fp operation.
Expand Down Expand Up @@ -163,8 +163,8 @@ impl<F: PrimeField32> RiscvAir<F> {
chips.push(RiscvAir::Bls12381Add(bls12381_add));
let bls12381_double = WeierstrassDoubleAssignChip::<SwCurve<Bls12381Parameters>>::new();
chips.push(RiscvAir::Bls12381Double(bls12381_double));
let uint256_mul = Uint256MulChip::default();
chips.push(RiscvAir::Uint256Mul(uint256_mul));
let uint256_mul = Uint256OpChip::default();
chips.push(RiscvAir::Uint256Op(uint256_mul));
let bls12381_fp = FpOpChip::<Bls12381BaseField>::new();
chips.push(RiscvAir::Bls12381Fp(bls12381_fp));
let bls12381_fp2_addsub = Fp2AddSubAssignChip::<Bls12381BaseField>::new();
Expand Down
Loading
Loading