Skip to content

Commit

Permalink
tests passgs
Browse files Browse the repository at this point in the history
  • Loading branch information
luffykai committed Jan 6, 2025
1 parent 50eb3dd commit 3f86b6b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
6 changes: 0 additions & 6 deletions crates/circuits/mod-builder/src/core_chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,6 @@ where
// The same as last row, except "is_valid" (the first element of core part) is zero.
core_row.copy_from_slice(last_row_core);
core_row[0] = F::ZERO;
if self.air.expr.needs_setup() {
// Setup will be derived by `is_valid - sum(all_flags)`, so we need to also set all the flags to 0.
for i in 0..self.air.num_flags() {
core_row[core_width - 1 - i] = F::ZERO;
}
}
}
}
}
21 changes: 21 additions & 0 deletions extensions/ecc/circuit/src/weierstrass_chip/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::{AirBuilder, BaseAir},
p3_field::{AbstractField, Field, PrimeField32},
p3_matrix::{dense::RowMajorMatrix, Matrix},
rap::BaseAirWithPublicValues,
};

Expand Down Expand Up @@ -173,6 +174,7 @@ impl EcDoubleCoreChip {
}
}

#[derive(Clone)]
pub struct EcDoubleCoreRecord {
pub x: BigUint,
pub y: BigUint,
Expand Down Expand Up @@ -262,4 +264,23 @@ where
fn air(&self) -> &Self::Air {
&self.air
}

// We need finalize for double, as it might have a constant (a of y^2 = x^3 + ax + b)
fn finalize(&self, trace: &mut RowMajorMatrix<F>, num_records: usize) {
if num_records == 0 {
return;
}
let core_width = <Self::Air as BaseAir<F>>::width(&self.air);
let adapter_width = trace.width() - core_width;
// We will be setting is_valid = 0. That forces is_double to be 0 (otherwise setup will be -1).
// So the computation is like doing setup.
// Thus we will copy over the first row (which is a setup row) and set is_valid = 0.
let first_row = trace.rows().nth(0).unwrap().collect::<Vec<_>>();
let first_row_core = first_row.split_at(adapter_width).1;
for row in trace.rows_mut().skip(num_records) {
let core_row = row.split_at_mut(adapter_width).1;
core_row.copy_from_slice(first_row_core);
core_row[0] = F::ZERO; // is_valid = 0
}
}
}
11 changes: 7 additions & 4 deletions extensions/ecc/sw-setup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn sw_declare(input: TokenStream) -> TokenStream {
for item in items.into_iter() {
let struct_name = item.name.to_string();
let struct_name = syn::Ident::new(&struct_name, span.into());
let struct_path: syn::Path = syn::parse_quote!(#struct_name);
let mut intmod_type: Option<syn::Path> = None;
let mut const_a: Option<syn::Expr> = None;
let mut const_b: Option<syn::Expr> = None;
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn sw_declare(input: TokenStream) -> TokenStream {
&format!(
"{}_{}",
stringify!($name),
intmod_type
struct_path
.segments
.iter()
.map(|x| x.ident.to_string())
Expand Down Expand Up @@ -427,10 +428,12 @@ pub fn sw_init(input: TokenStream) -> TokenStream {
{
// p1 is (x1, y1), and x1 must be the modulus.
// y1 can be anything for SetupEcAdd, but must equal `a` for SetupEcDouble
let modulus_bytes = <#item as openvm_algebra_guest::IntMod>::MODULUS;
let one = [0u8; <#item as openvm_algebra_guest::IntMod>::NUM_LIMBS];
let modulus_bytes = <<#item as openvm_ecc_guest::weierstrass::WeierstrassPoint>::Coordinate as openvm_algebra_guest::IntMod>::MODULUS;
let mut one = [0u8; <<#item as openvm_ecc_guest::weierstrass::WeierstrassPoint>::Coordinate as openvm_algebra_guest::IntMod>::NUM_LIMBS];
one[0] = 1;
let p1 = [modulus_bytes.as_ref(), one.as_ref()].concat();
let curve_a_bytes = <#item as openvm_ecc_guest::weierstrass::WeierstrassPoint>::CURVE_A.as_le_bytes();
// p1 should be (p, a)
let p1 = [modulus_bytes.as_ref(), curve_a_bytes.as_ref()].concat();
// (EcAdd only) p2 is (x2, y2), and x1 - x2 has to be non-zero to avoid division over zero in add.
let p2 = [one.as_ref(), one.as_ref()].concat();
let mut uninit: core::mem::MaybeUninit<[#item; 2]> = core::mem::MaybeUninit::uninit();
Expand Down
2 changes: 1 addition & 1 deletion extensions/ecc/tests/programs/examples/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ openvm_algebra_moduli_setup::moduli_init! {
"0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141"
}
openvm_ecc_sw_setup::sw_init! {
Secp256k1Coord,
Secp256k1Point,
}

pub fn main() {
Expand Down
2 changes: 1 addition & 1 deletion extensions/ecc/tests/programs/examples/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ openvm_algebra_moduli_setup::moduli_init! {
}

openvm_ecc_sw_setup::sw_init! {
Secp256k1Coord,
Secp256k1Point,
}

openvm::entry!(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ openvm_algebra_moduli_setup::moduli_init! {
}

openvm_ecc_sw_setup::sw_init! {
P256Coord,
P256Point,
}

pub fn main() {
Expand All @@ -46,12 +46,12 @@ pub fn main() {
// Add assign and double assign
let mut sum = P256Point::from_xy(x1, y1).unwrap();
sum += &p2;
if sum.x != p3.x || sum.y != p3.y {
if sum.x() != p3.x() || sum.y() != p3.y() {
panic!();
}
let mut double = P256Point::from_xy(x2, y2).unwrap();
double.double_assign();
if double.x != p4.x || double.y != p4.y {
if double.x() != p4.x() || double.y() != p4.y() {
panic!();
}
}
4 changes: 2 additions & 2 deletions extensions/ecc/tests/programs/examples/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use k256::{
Secp256k1,
};
use openvm_ecc_guest::{
algebra::IntMod, ecdsa::VerifyingKey, k256::Secp256k1Coord, weierstrass::WeierstrassPoint,
algebra::IntMod, ecdsa::VerifyingKey, k256::Secp256k1Point, weierstrass::WeierstrassPoint,
};
use openvm_keccak256_guest::keccak256;
openvm::entry!(main);
Expand All @@ -20,7 +20,7 @@ openvm_algebra_moduli_setup::moduli_init! {
"0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141"
}
openvm_ecc_sw_setup::sw_init! {
Secp256k1Coord,
Secp256k1Point,
}

// Ref: https://docs.rs/k256/latest/k256/ecdsa/index.html
Expand Down
4 changes: 2 additions & 2 deletions extensions/ecc/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ mod tests {
}

#[test]
fn test_ec_weierstrass_runtime() -> Result<()> {
fn test_ec_nonzero_a() -> Result<()> {
let elf = build_example_program_at_path_with_features(
get_programs_dir!(),
"ec_weierstrass",
"ec_nonzero_a",
["p256"],
)?;
let openvm_exe = VmExe::from_elf(
Expand Down

0 comments on commit 3f86b6b

Please sign in to comment.