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: merge mac mul chip #6

Closed
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions core/src/runtime/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use tracing_subscriber::fmt::time;

/// An record of a write to a memory address.
#[derive(Debug, Copy, Clone, Default, Serialize, Deserialize)]
Expand Down
14 changes: 2 additions & 12 deletions core/src/runtime/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use typenum::{U16, U32, U64, U8};

use crate::runtime::{Register, Runtime};
use crate::stark::Blake3CompressInnerChip;
use crate::syscall::precompiles::bn254_scalar::{Bn254ScalarMacChip, Bn254ScalarMulChip};
use crate::syscall::precompiles::bn254_scalar::Bn254ScalarMacChip;
use crate::syscall::precompiles::edwards::EdAddAssignChip;
use crate::syscall::precompiles::edwards::EdDecompressChip;
use crate::syscall::precompiles::keccak256::KeccakPermuteChip;
Expand Down Expand Up @@ -107,9 +107,6 @@ pub enum SyscallCode {
/// Executes the `BLS12381_DOUBLE` precompile.
BLS12381_DOUBLE = 0x00_00_01_1F,

/// Execute the `BN254_SCALAR_MUL` precompile.
BN254_SCALAR_MUL = 0x00_01_01_20,

/// Execute the `BN254_SCALAR_MAC` precompile.
BN254_SCALAR_MAC = 0x00_01_01_21,

Expand Down Expand Up @@ -148,7 +145,6 @@ impl SyscallCode {
0x00_00_00_F1 => SyscallCode::HINT_READ,
0x00_00_01_1D => SyscallCode::UINT256_MUL,
0x00_00_01_1C => SyscallCode::BLS12381_DECOMPRESS,
0x00_01_01_20 => SyscallCode::BN254_SCALAR_MUL,
0x00_01_01_21 => SyscallCode::BN254_SCALAR_MAC,
0x00_00_01_30 => SyscallCode::MEMCPY_32,
0x00_00_01_31 => SyscallCode::MEMCPY_64,
Expand Down Expand Up @@ -365,10 +361,7 @@ pub fn default_syscall_map() -> HashMap<SyscallCode, Arc<dyn Syscall>> {
Arc::new(WeierstrassDecompressChip::<Bls12381>::new()),
);
syscall_map.insert(SyscallCode::UINT256_MUL, Arc::new(Uint256MulChip::new()));
syscall_map.insert(
SyscallCode::BN254_SCALAR_MUL,
Arc::new(Bn254ScalarMulChip::new()),
);

syscall_map.insert(
SyscallCode::BN254_SCALAR_MAC,
Arc::new(Bn254ScalarMacChip::new()),
Expand Down Expand Up @@ -479,9 +472,6 @@ mod tests {
SyscallCode::BLS12381_DECOMPRESS => {
assert_eq!(code as u32, sp1_zkvm::syscalls::BLS12381_DECOMPRESS)
}
SyscallCode::BN254_SCALAR_MUL => {
assert_eq!(code as u32, sp1_zkvm::syscalls::BN254_SCALAR_MUL)
}
SyscallCode::BN254_SCALAR_MAC => {
assert_eq!(code as u32, sp1_zkvm::syscalls::BN254_SCALAR_MAC)
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/stark/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) mod riscv_chips {
pub use crate::memory::MemoryChip;
pub use crate::program::ProgramChip;
pub use crate::syscall::precompiles::blake3::Blake3CompressInnerChip;
pub use crate::syscall::precompiles::bn254_scalar::{Bn254ScalarMacChip, Bn254ScalarMulChip};
pub use crate::syscall::precompiles::bn254_scalar::Bn254ScalarMacChip;
pub use crate::syscall::precompiles::edwards::EdAddAssignChip;
pub use crate::syscall::precompiles::edwards::EdDecompressChip;
pub use crate::syscall::precompiles::keccak256::KeccakPermuteChip;
Expand Down Expand Up @@ -103,8 +103,6 @@ pub enum RiscvAir<F: PrimeField32> {
Bls12381Double(WeierstrassDoubleAssignChip<SwCurve<Bls12381Parameters>>),
/// A precompile for uint256 mul.
Uint256Mul(Uint256MulChip),
/// A precompile for bn254 scalar multiplication.
Bn254ScalarMul(Bn254ScalarMulChip),
/// A precompile for bn254 scalar mul-add
Bn254ScalarMac(Bn254ScalarMacChip),
/// A precompile for decompressing a point on the BLS12-381 curve.
Expand Down Expand Up @@ -160,8 +158,6 @@ impl<F: PrimeField32> RiscvAir<F> {
chips.push(RiscvAir::Bls12381Double(bls12381_double));
let uint256_mul = Uint256MulChip::default();
chips.push(RiscvAir::Uint256Mul(uint256_mul));
let bn254_scalar_mul = Bn254ScalarMulChip::new();
chips.push(RiscvAir::Bn254ScalarMul(bn254_scalar_mul));
let bn254_scalar_mac = Bn254ScalarMacChip::new();
chips.push(RiscvAir::Bn254ScalarMac(bn254_scalar_mac));
let bls12381_decompress = WeierstrassDecompressChip::<SwCurve<Bls12381Parameters>>::new();
Expand Down
116 changes: 68 additions & 48 deletions core/src/syscall/precompiles/bn254_scalar/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use p3_field::AbstractField;
use p3_field::{Field, PrimeField32};
use p3_matrix::{dense::RowMajorMatrix, Matrix};
use sp1_derive::AlignedBorrow;
use typenum::U8;
use typenum::{U12, U8};

use crate::{
air::MachineAir,
Expand All @@ -21,10 +21,9 @@ use crate::{
utils::{ec::weierstrass::bn254::Bn254ScalarField, limbs_from_prev_access, pad_rows},
};

use super::{create_bn254_scalar_arith_event, Bn254FieldOperation, NUM_WORDS_PER_FE};
use super::{create_bn254_scalar_arith_event, NUM_WORDS_PER_FE};

const NUM_COLS: usize = core::mem::size_of::<Bn254ScalarMacCols<u8>>();
const OP: Bn254FieldOperation = Bn254FieldOperation::Mac;

#[derive(Debug, Clone, AlignedBorrow)]
#[repr(C)]
Expand All @@ -36,10 +35,11 @@ pub struct Bn254ScalarMacCols<T> {
arg1_ptr: T,
arg2_ptr: T,
arg1_access: [MemoryWriteCols<T>; NUM_WORDS_PER_FE],
arg2_access: [MemoryReadCols<T>; 2],
arg2_access: [MemoryReadCols<T>; 3],
a_access: [MemoryReadCols<T>; NUM_WORDS_PER_FE],
b_access: [MemoryReadCols<T>; NUM_WORDS_PER_FE],
mul_eval: FieldOpCols<T, Bn254ScalarField>,
c_access: [MemoryReadCols<T>; NUM_WORDS_PER_FE],
mul_eval: [FieldOpCols<T, Bn254ScalarField>; 2],
add_eval: FieldOpCols<T, Bn254ScalarField>,
}

Expand All @@ -58,7 +58,7 @@ impl Syscall for Bn254ScalarMacChip {
arg1: u32,
arg2: u32,
) -> Option<u32> {
let event = create_bn254_scalar_arith_event(rt, arg1, arg2, OP);
let event = create_bn254_scalar_arith_event(rt, arg1, arg2);
rt.record_mut().bn254_scalar_arith_events.push(event);

None
Expand All @@ -82,17 +82,14 @@ impl<F: PrimeField32> MachineAir<F> for Bn254ScalarMacChip {
let mut rows = vec![];
let mut new_byte_lookup_events = vec![];

for event in input
.bn254_scalar_arith_events
.iter()
.filter(|e| e.op == OP)
{
for event in input.bn254_scalar_arith_events.iter() {
let mut row = [F::zero(); NUM_COLS];
let cols: &mut Bn254ScalarMacCols<F> = row.as_mut_slice().borrow_mut();

let arg1 = event.arg1.prev_value_as_biguint();
let a = event.a.as_ref().unwrap().value_as_biguint();
let b = event.b.as_ref().unwrap().value_as_biguint();
let a = event.a.value_as_biguint();
let b = event.b.value_as_biguint();
let c = event.c.value_as_biguint();

cols.is_real = F::one();
cols.shard = F::from_canonical_u32(event.shard);
Expand All @@ -101,20 +98,29 @@ impl<F: PrimeField32> MachineAir<F> for Bn254ScalarMacChip {
cols.arg1_ptr = F::from_canonical_u32(event.arg1.ptr);
cols.arg2_ptr = F::from_canonical_u32(event.arg2.ptr);

let mul = cols.mul_eval.populate(
let mul_a_b = cols.mul_eval[0].populate(
&mut new_byte_lookup_events,
event.shard,
event.channel,
&a,
&b,
FieldOperation::Mul,
);
cols.add_eval.populate(
let mul_arg1_c = cols.mul_eval[1].populate(
&mut new_byte_lookup_events,
event.shard,
event.channel,
&arg1,
&mul,
&c,
FieldOperation::Mul,
);

cols.add_eval.populate(
&mut new_byte_lookup_events,
event.shard,
event.channel,
&mul_a_b,
&mul_arg1_c,
FieldOperation::Add,
);

Expand All @@ -125,26 +131,16 @@ impl<F: PrimeField32> MachineAir<F> for Bn254ScalarMacChip {
&mut new_byte_lookup_events,
);
}
for i in 0..cols.arg2_access.len() {
cols.arg2_access[i].populate(
event.channel,
event.arg2.memory_records[i],
&mut new_byte_lookup_events,
);
}
for i in 0..cols.a_access.len() {
cols.a_access[i].populate(
event.channel,
event.a.as_ref().unwrap().memory_records[i],
&mut new_byte_lookup_events,
);
}
for i in 0..cols.b_access.len() {
cols.b_access[i].populate(
event.channel,
event.b.as_ref().unwrap().memory_records[i],
&mut new_byte_lookup_events,
);

for (read_col, record) in cols
.arg2_access
.iter_mut()
.zip(event.arg2.memory_records.iter())
.chain(cols.a_access.iter_mut().zip(event.a.memory_records.iter()))
.chain(cols.b_access.iter_mut().zip(event.b.memory_records.iter()))
.chain(cols.c_access.iter_mut().zip(event.c.memory_records.iter()))
{
read_col.populate(event.channel, *record, &mut new_byte_lookup_events);
}

rows.push(row);
Expand All @@ -156,8 +152,8 @@ impl<F: PrimeField32> MachineAir<F> for Bn254ScalarMacChip {
let cols: &mut Bn254ScalarMacCols<F> = row.as_mut_slice().borrow_mut();

let zero = BigUint::zero();
cols.mul_eval
.populate(&mut vec![], 0, 0, &zero, &zero, FieldOperation::Mul);
cols.mul_eval[0].populate(&mut vec![], 0, 0, &zero, &zero, FieldOperation::Mul);
cols.mul_eval[1].populate(&mut vec![], 0, 0, &zero, &zero, FieldOperation::Mul);
cols.add_eval
.populate(&mut vec![], 0, 0, &zero, &zero, FieldOperation::Add);

Expand All @@ -168,12 +164,7 @@ impl<F: PrimeField32> MachineAir<F> for Bn254ScalarMacChip {
}

fn included(&self, shard: &Self::Record) -> bool {
shard
.bn254_scalar_arith_events
.iter()
.filter(|e| e.op == OP)
.count()
!= 0
!shard.bn254_scalar_arith_events.is_empty()
}
}

Expand All @@ -196,13 +187,15 @@ where

let arg1: Limbs<<AB as AirBuilder>::Var, <Bn254ScalarField as NumLimbs>::Limbs> =
limbs_from_prev_access(&row.arg1_access);
let arg2: Limbs<<AB as AirBuilder>::Var, U8> = limbs_from_prev_access(&row.arg2_access);
let arg2: Limbs<<AB as AirBuilder>::Var, U12> = limbs_from_prev_access(&row.arg2_access);
let a: Limbs<<AB as AirBuilder>::Var, <Bn254ScalarField as NumLimbs>::Limbs> =
limbs_from_prev_access(&row.a_access);
let b: Limbs<<AB as AirBuilder>::Var, <Bn254ScalarField as NumLimbs>::Limbs> =
limbs_from_prev_access(&row.b_access);
let c: Limbs<<AB as AirBuilder>::Var, <Bn254ScalarField as NumLimbs>::Limbs> =
limbs_from_prev_access(&row.c_access);

row.mul_eval.eval(
row.mul_eval[0].eval(
builder,
&a,
&b,
Expand All @@ -211,10 +204,19 @@ where
row.channel,
row.is_real,
);
row.add_eval.eval(
row.mul_eval[1].eval(
builder,
&arg1,
&row.mul_eval.result,
&c,
FieldOperation::Mul,
row.shard,
row.channel,
row.is_real,
);
row.add_eval.eval(
builder,
&row.mul_eval[0].result,
&row.mul_eval[1].result,
FieldOperation::Add,
row.shard,
row.channel,
Expand Down Expand Up @@ -264,6 +266,15 @@ where
acc * AB::Expr::from_canonical_u16(0x100) + b
});

let c_ptr = arg2.0[8..12]
.iter()
.rev()
.cloned()
.map(|v| v.into())
.fold(AB::Expr::zero(), |acc, b| {
acc * AB::Expr::from_canonical_u16(0x100) + b
});

builder.eval_memory_access_slice(
row.shard,
row.channel,
Expand All @@ -282,6 +293,15 @@ where
row.is_real,
);

builder.eval_memory_access_slice(
row.shard,
row.channel,
row.clk.into(),
c_ptr,
&row.c_access,
row.is_real,
);

let syscall_id = AB::F::from_canonical_u32(SyscallCode::BN254_SCALAR_MAC.syscall_id());
builder.receive_syscall(
row.shard,
Expand Down
Loading
Loading