Skip to content

Commit

Permalink
feat(ecc): Weierstrass affine curves over complex fields (#1127)
Browse files Browse the repository at this point in the history
* chore: add invert to Field trait

* chore: generalize WeierstrassPoint trait

* working version of G2Affine

* chore: make into macro

* chore: split out FromCompress trait

* chore: refactor macros for weierstrass curves

* chore: fixes

* add unit tests
  • Loading branch information
jonathanpwang authored Dec 24, 2024
1 parent 2ef5f76 commit e4eaff4
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 255 deletions.
10 changes: 9 additions & 1 deletion extensions/algebra/guest/src/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{

use crate::{DivAssignUnsafe, DivUnsafe};

// TODO: this can now extend IntMod trait
// TODO: the shared parts of Field and IntMod should be moved into a new `IntegralDomain` trait.
/// This is a simplified trait for field elements.
pub trait Field:
Sized
Expand Down Expand Up @@ -48,6 +48,14 @@ pub trait Field:

/// Square `self` in-place
fn square_assign(&mut self);

/// Unchecked inversion. See [DivUnsafe].
///
/// ## Panics
/// If `self` is zero.
fn invert(&self) -> Self {
Self::ONE.div_unsafe(self)
}
}

/// Field extension trait. BaseField is the base field of the extension field.
Expand Down
3 changes: 2 additions & 1 deletion extensions/ecc/guest/src/affine_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct AffinePoint<F> {
}

impl<F: Field> AffinePoint<F> {
pub fn new(x: F, y: F) -> Self {
pub const fn new(x: F, y: F) -> Self {
Self { x, y }
}

Expand All @@ -29,6 +29,7 @@ impl<F: Field> AffinePoint<F> {
}
}

// Note: this is true for weierstrass curves but maybe not in general
impl<F> Neg for AffinePoint<F>
where
F: Neg<Output = F>,
Expand Down
7 changes: 5 additions & 2 deletions extensions/ecc/guest/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use elliptic_curve::PrimeCurve;
use openvm_algebra_guest::{DivUnsafe, IntMod, Reduce};

use crate::{
weierstrass::{IntrinsicCurve, WeierstrassPoint},
weierstrass::{FromCompressed, IntrinsicCurve, WeierstrassPoint},
CyclicGroup, Group,
};

Expand Down Expand Up @@ -40,6 +40,9 @@ impl<C: IntrinsicCurve> VerifyingKey<C> {
impl<C> VerifyingKey<C>
where
C: PrimeCurve + IntrinsicCurve,
C::Point: WeierstrassPoint + CyclicGroup + FromCompressed<Coordinate<C>>,
Coordinate<C>: IntMod,
C::Scalar: IntMod + Reduce,
{
/// Ref: <https://github.com/RustCrypto/signatures/blob/85c984bcc9927c2ce70c7e15cbfe9c6936dd3521/ecdsa/src/recovery.rs#L297>
///
Expand Down Expand Up @@ -84,7 +87,7 @@ where
}
let rec_id = recovery_id.to_byte();
// The point R decompressed from x-coordinate `r`
let R: C::Point = WeierstrassPoint::decompress(x, &rec_id);
let R: C::Point = FromCompressed::decompress(x, &rec_id);

let neg_u1 = z.div_unsafe(&r);
let u2 = s.div_unsafe(&r);
Expand Down
4 changes: 3 additions & 1 deletion extensions/ecc/guest/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub trait Group:

const IDENTITY: Self;

fn is_identity(&self) -> bool;
fn is_identity(&self) -> bool {
self == &Self::IDENTITY
}

fn double(&self) -> Self;
fn double_assign(&mut self);
Expand Down
25 changes: 21 additions & 4 deletions extensions/ecc/guest/src/k256/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::ops::{Add, AddAssign, Neg};
use core::ops::{Add, Neg};

use hex_literal::hex;
#[cfg(not(target_os = "zkvm"))]
use lazy_static::lazy_static;
#[cfg(not(target_os = "zkvm"))]
use num_bigint_dig::BigUint;
use openvm_algebra_guest::IntMod;
use openvm_algebra_guest::{Field, IntMod};
use openvm_algebra_moduli_setup::moduli_declare;
use openvm_ecc_sw_setup::sw_declare;

use super::group::{CyclicGroup, Group};
use crate::weierstrass::{CachedMulTable, IntrinsicCurve};
Expand All @@ -30,15 +32,30 @@ const fn seven_le() -> [u8; 32] {
buf
}

openvm_algebra_moduli_setup::moduli_declare! {
moduli_declare! {
Secp256k1Coord { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F" },
Secp256k1Scalar { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141" },
}

openvm_ecc_sw_setup::sw_declare! {
sw_declare! {
Secp256k1Point { mod_type = Secp256k1Coord, b = CURVE_B },
}

impl Field for Secp256k1Coord {
const ZERO: Self = <Self as IntMod>::ZERO;
const ONE: Self = <Self as IntMod>::ONE;

type SelfRef<'a> = &'a Self;

fn double_assign(&mut self) {
IntMod::double_assign(self);
}

fn square_assign(&mut self) {
IntMod::square_assign(self);
}
}

impl CyclicGroup for Secp256k1Point {
const GENERATOR: Self = Secp256k1Point {
x: Secp256k1Coord::from_const_bytes(hex!(
Expand Down
Loading

0 comments on commit e4eaff4

Please sign in to comment.