Skip to content

Commit b8e5c3a

Browse files
perf: switch SW_EC to projective coordinates - guest macros (#2409)
Resolves INT-6135. Update guest-side Weierstrass EC from affine to projective coordinates: - WeierstrassPoint trait: projective interface with (X, Y, Z), identity (0, 1, 0), complete addition/doubling formulas, normalize(), is_identity() - impl_sw_proj! macro: replaces impl_sw_affine!, a=0 complete formulas from ePrint 2015/1060 - impl_sw_group_ops!: simplified with no branching (complete formulas handle all edge cases) - sw_declare!/sw_init! proc macros: projective struct, SwAddProj/SwDoubleProj opcodes, setup sends (modulus, a, b), projective PartialEq via cross-multiplication - ECDSA: normalize before extracting affine coordinates - Test programs: normalize results before comparing to known affine values
1 parent aa1d833 commit b8e5c3a

File tree

9 files changed

+399
-414
lines changed

9 files changed

+399
-414
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ecc/circuit/src/weierstrass_chip/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub use double::*;
88
#[cfg(test)]
99
mod tests;
1010

11-
1211
use openvm_circuit::arch::{VmAirWrapper, VmChipWrapper};
1312
use openvm_mod_circuit_builder::{FieldExpressionCoreAir, FieldExpressionFiller};
1413
use openvm_rv32_adapters::{Rv32VecHeapAdapterAir, Rv32VecHeapAdapterFiller};

extensions/ecc/guest/src/ecdsa.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ where
9797
pub fn from_affine(point: AffinePoint<C>) -> Result<Self> {
9898
// Internally this calls `is_eq` on `x` and `y` coordinates, which will assert `x, y` are
9999
// reduced.
100-
if point.is_identity() {
100+
if Group::is_identity(&point) {
101101
Err(Error::new())
102102
} else {
103103
Ok(Self { point })
@@ -148,11 +148,11 @@ where
148148
}
149149

150150
pub fn to_sec1_bytes(&self, compress: bool) -> Vec<u8> {
151-
if self.point.is_identity() {
151+
if Group::is_identity(&self.point) {
152152
return vec![0x00];
153153
}
154154

155-
let (x, y) = self.point.clone().into_coords();
155+
let (x, y, _) = self.point.normalize().into_coords();
156156

157157
if compress {
158158
let mut bytes = Vec::<u8>::with_capacity(1 + Coordinate::<C>::NUM_LIMBS);
@@ -534,12 +534,12 @@ where
534534
// public key
535535
let Q = pubkey;
536536
let R = <C as IntrinsicCurve>::msm(&[u1, u2], &[G, Q]);
537-
// For Coordinate<C>: IntMod, the internal implementation of is_identity will assert x, y
538-
// coordinates of R are both reduced.
539-
if R.is_identity() {
537+
// For Coordinate<C>: IntMod, the internal implementation of is_identity will assert z
538+
// coordinate of R is reduced.
539+
if Group::is_identity(&R) {
540540
return Err(Error::new());
541541
}
542-
let (x_1, _) = R.into_coords();
542+
let (x_1, _, _) = R.normalize().into_coords();
543543
// Scalar and Coordinate may be different byte lengths, so we use an inefficient reduction
544544
let x_mod_n = Scalar::<C>::reduce_le_bytes(x_1.as_le_bytes());
545545
if x_mod_n == r {

extensions/ecc/guest/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub const SW_FUNCT3: u8 = 0b001;
2929
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromRepr)]
3030
#[repr(u8)]
3131
pub enum SwBaseFunct7 {
32-
SwAddNe = 0,
33-
SwDouble,
32+
SwAddProj = 0,
33+
SwDoubleProj,
3434
SwSetup,
3535
}
3636

0 commit comments

Comments
 (0)