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

[draft] feat: Add p384 precompile #741

Closed
wants to merge 3 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
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ curve25519-dalek = { version = "4.1.2" }
elliptic-curve = "0.13.8"
hex = "0.4.3"
k256 = { version = "0.13.3", features = ["expose-field"] }
p384 = { version = "0.13.0", features = ["expose-field"] }
num_cpus = "1.16.0"
serde_with = "3.8.1"
size = "0.4.1"
Expand Down
14 changes: 11 additions & 3 deletions core/src/operations/field/field_den.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ where
V: Into<AB::Expr>,
{
let p_a = Polynomial::from(*a);
let p_b = (*b).into();
// let p_b = (*b).into();
let p_b = Polynomial::from(*b);
let p_result = self.result.into();
let p_carry = self.carry.into();
// let p_carry = self.carry.into();
let p_carry = Polynomial::from(self.carry);

// Compute the vanishing polynomial:
// lhs(x) = sign * (b(x) * result(x) + result(x)) + (1 - sign) * (b(x) * result(x) + a(x))
Expand All @@ -136,7 +138,13 @@ where
let p_witness_low = self.witness_low.0.iter().into();
let p_witness_high = self.witness_high.0.iter().into();

eval_field_operation::<AB, P>(builder, &p_vanishing, &p_witness_low, &p_witness_high);
eval_field_operation::<AB, P>(
builder,
&p_vanishing,
&p_witness_low,
&p_witness_high,
is_real.clone(),
);

// Range checks for the result, carry, and witness columns.
builder.slice_range_check_u8(&self.result.0, shard.clone(), is_real.clone());
Expand Down
10 changes: 8 additions & 2 deletions core/src/operations/field/field_inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ where
let p_a_vec: Vec<Polynomial<AB::Expr>> = a.iter().map(|x| (*x).into()).collect();
let p_b_vec: Vec<Polynomial<AB::Expr>> = b.iter().map(|x| (*x).into()).collect();
let p_result = self.result.into();
let p_carry = self.carry.into();
let p_carry: Polynomial<AB::Expr> = self.carry.into();

let p_zero = Polynomial::<AB::Expr>::new(vec![AB::Expr::zero()]);

Expand All @@ -135,7 +135,13 @@ where
let p_witness_low = self.witness_low.0.iter().into();
let p_witness_high = self.witness_high.0.iter().into();

eval_field_operation::<AB, P>(builder, &p_vanishing, &p_witness_low, &p_witness_high);
eval_field_operation::<AB, P>(
builder,
&p_vanishing,
&p_witness_low,
&p_witness_high,
is_real.clone(),
);

// Range checks for the result, carry, and witness columns.
builder.slice_range_check_u8(&self.result.0, shard.clone(), is_real.clone());
Expand Down
26 changes: 16 additions & 10 deletions core/src/operations/field/field_op.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;

use num::{BigUint, Zero};
use num::BigUint;
use p3_air::AirBuilder;
use p3_field::PrimeField32;
use sp1_derive::AlignedBorrow;
Expand Down Expand Up @@ -117,14 +117,14 @@ impl<F: PrimeField32, P: FieldParameters> FieldOpCols<F, P> {
modulus: &BigUint,
op: FieldOperation,
) -> BigUint {
if b == &BigUint::zero() && op == FieldOperation::Div {
// Division by 0 is allowed only when dividing 0 so that padded rows can be all 0.
assert_eq!(
*a,
BigUint::zero(),
"division by zero is allowed only when dividing zero"
);
}
// if b == &BigUint::zero() && op == FieldOperation::Div {
// // Division by 0 is allowed only when dividing 0 so that padded rows can be all 0.
// assert_eq!(
// *a,
// BigUint::zero(),
// "division by zero is allowed only when dividing zero"
// );
// }

let result = match op {
// If doing the subtraction operation, a - b = result, equivalent to a = result + b.
Expand Down Expand Up @@ -212,7 +212,13 @@ impl<V: Copy, P: FieldParameters> FieldOpCols<V, P> {
let p_vanishing = p_op_minus_result - &(&p_carry * &p_modulus);
let p_witness_low = self.witness_low.0.iter().into();
let p_witness_high = self.witness_high.0.iter().into();
eval_field_operation::<AB, P>(builder, &p_vanishing, &p_witness_low, &p_witness_high);
eval_field_operation::<AB, P>(
builder,
&p_vanishing,
&p_witness_low,
&p_witness_high,
is_real.clone(),
);

// Range checks for the result, carry, and witness columns.
builder.slice_range_check_u8(&self.result.0, shard.clone(), is_real.clone());
Expand Down
7 changes: 5 additions & 2 deletions core/src/operations/field/util_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use p3_field::AbstractField;

use crate::air::Polynomial;
use crate::air::SP1AirBuilder;
use crate::operations::field::params::FieldParameters;
use p3_air::AirBuilder;

use super::params::FieldParameters;

pub fn eval_field_operation<AB: SP1AirBuilder, P: FieldParameters>(
builder: &mut AB,
p_vanishing: &Polynomial<AB::Expr>,
p_witness_low: &Polynomial<AB::Expr>,
p_witness_high: &Polynomial<AB::Expr>,
is_real: impl Into<AB::Expr> + Clone,
) {
// Reconstruct and shift back the witness polynomial
let limb: AB::Expr = AB::F::from_canonical_u32(2u32.pow(P::NB_BITS_PER_LIMB as u32)).into();
Expand All @@ -26,6 +29,6 @@ pub fn eval_field_operation<AB: SP1AirBuilder, P: FieldParameters>(

let constraints = p_vanishing - &(p_witness * root_monomial);
for constr in constraints.as_coefficients() {
builder.assert_zero(constr);
builder.when(is_real.clone()).assert_zero(constr);
}
}
40 changes: 40 additions & 0 deletions core/src/runtime/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct ExecutionRecord {

pub secp256k1_double_events: Vec<ECDoubleEvent>,

pub secp384r1_add_events: Vec<ECAddEvent>,

pub secp384r1_double_events: Vec<ECDoubleEvent>,

pub bn254_add_events: Vec<ECAddEvent>,

pub bn254_double_events: Vec<ECDoubleEvent>,
Expand Down Expand Up @@ -119,6 +123,8 @@ pub struct ShardingConfig {
pub keccak_len: usize,
pub secp256k1_add_len: usize,
pub secp256k1_double_len: usize,
pub secp384r1_add_len: usize,
pub secp384r1_double_len: usize,
pub bn254_add_len: usize,
pub bn254_double_len: usize,
pub bls12381_add_len: usize,
Expand Down Expand Up @@ -149,6 +155,8 @@ impl Default for ShardingConfig {
keccak_len: shard_size,
secp256k1_add_len: shard_size,
secp256k1_double_len: shard_size,
secp384r1_add_len: shard_size,
secp384r1_double_len: shard_size,
bn254_add_len: shard_size,
bn254_double_len: shard_size,
bls12381_add_len: shard_size,
Expand Down Expand Up @@ -211,6 +219,14 @@ impl MachineRecord for ExecutionRecord {
"secp256k1_double_events".to_string(),
self.secp256k1_double_events.len(),
);
stats.insert(
"secp384r1_add_events".to_string(),
self.secp384r1_add_events.len(),
);
stats.insert(
"secp384r1_double_events".to_string(),
self.secp384r1_double_events.len(),
);
stats.insert("bn254_add_events".to_string(), self.bn254_add_events.len());
stats.insert(
"bn254_double_events".to_string(),
Expand Down Expand Up @@ -267,6 +283,10 @@ impl MachineRecord for ExecutionRecord {
.append(&mut other.secp256k1_add_events);
self.secp256k1_double_events
.append(&mut other.secp256k1_double_events);
self.secp384r1_add_events
.append(&mut other.secp384r1_add_events);
self.secp384r1_double_events
.append(&mut other.secp384r1_double_events);
self.bn254_add_events.append(&mut other.bn254_add_events);
self.bn254_double_events
.append(&mut other.bn254_double_events);
Expand Down Expand Up @@ -452,6 +472,26 @@ impl MachineRecord for ExecutionRecord {
.extend_from_slice(secp256k1_double_chunk);
}

// secp384r1 curve add events.
for (secp384r1_add_chunk, shard) in take(&mut self.secp384r1_add_events)
.chunks_mut(config.secp384r1_add_len)
.zip(shards.iter_mut())
{
shard
.secp384r1_add_events
.extend_from_slice(secp384r1_add_chunk);
}

// secp384r1 curve double events.
for (secp384r1_double_chunk, shard) in take(&mut self.secp384r1_double_events)
.chunks_mut(config.secp384r1_double_len)
.zip(shards.iter_mut())
{
shard
.secp384r1_double_events
.extend_from_slice(secp384r1_double_chunk);
}

// bn254 curve add events.
for (bn254_add_chunk, shard) in take(&mut self.bn254_add_events)
.chunks_mut(config.bn254_add_len)
Expand Down
24 changes: 23 additions & 1 deletion core/src/runtime/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
};
use crate::utils::ec::edwards::ed25519::{Ed25519, Ed25519Parameters};
use crate::utils::ec::weierstrass::bls12_381::Bls12381;
use crate::utils::ec::weierstrass::{bn254::Bn254, secp256k1::Secp256k1};
use crate::utils::ec::weierstrass::{bn254::Bn254, secp256k1::Secp256k1, secp384r1::Secp384r1};
use crate::{runtime::ExecutionRecord, runtime::MemoryReadRecord, runtime::MemoryWriteRecord};

/// A system call is invoked by the the `ecall` instruction with a specific value in register t0.
Expand Down Expand Up @@ -102,6 +102,12 @@

/// Executes the `BLS12381_DOUBLE` precompile.
BLS12381_DOUBLE = 0x00_00_01_1F,

/// Executes the `SECP256K1_ADD` precompile.
SECP384R1_ADD = 0x00_01_01_16,

/// Executes the `SECP256K1_DOUBLE` precompile.
SECP384R1_DOUBLE = 0x00_00_01_17,
}

impl SyscallCode {
Expand Down Expand Up @@ -132,6 +138,8 @@
0x00_00_00_F1 => SyscallCode::HINT_READ,
0x00_00_01_1D => SyscallCode::UINT256_MUL,
0x00_00_01_1C => SyscallCode::BLS12381_DECOMPRESS,
0x00_01_01_16 => SyscallCode::SECP384R1_ADD,

Check failure on line 141 in core/src/runtime/syscall.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

mistyped literal suffix
0x00_00_01_17 => SyscallCode::SECP384R1_DOUBLE,
_ => panic!("invalid syscall number: {}", value),
}
}
Expand Down Expand Up @@ -281,6 +289,14 @@
SyscallCode::SECP256K1_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Secp256k1>::new()),
);
syscall_map.insert(
SyscallCode::SECP384R1_ADD,
Rc::new(WeierstrassAddAssignChip::<Secp384r1>::new()),
);
syscall_map.insert(
SyscallCode::SECP384R1_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Secp384r1>::new()),
);
syscall_map.insert(SyscallCode::SHA_COMPRESS, Rc::new(ShaCompressChip::new()));
syscall_map.insert(
SyscallCode::SECP256K1_DECOMPRESS,
Expand Down Expand Up @@ -402,6 +418,12 @@
SyscallCode::SECP256K1_DOUBLE => {
assert_eq!(code as u32, sp1_zkvm::syscalls::SECP256K1_DOUBLE)
}
SyscallCode::SECP384R1_ADD => {
assert_eq!(code as u32, sp1_zkvm::syscalls::SECP384R1_ADD)
}
SyscallCode::SECP384R1_DOUBLE => {
assert_eq!(code as u32, sp1_zkvm::syscalls::SECP384R1_DOUBLE)
}
SyscallCode::BLAKE3_COMPRESS_INNER => {
assert_eq!(code as u32, sp1_zkvm::syscalls::BLAKE3_COMPRESS_INNER)
}
Expand Down
Loading
Loading