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

Implement addition and multiplication operations for AffinePoint #112

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
83 changes: 77 additions & 6 deletions crates/starknet-types-core/src/curve/affine_point.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::curve::curve_errors::CurveError;
use crate::felt::Felt;
use lambdaworks_math::cyclic_group::IsGroup;
use lambdaworks_math::elliptic_curve::short_weierstrass::curves::stark_curve::StarkCurve;
use lambdaworks_math::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
use lambdaworks_math::elliptic_curve::traits::FromAffine;
use crate::{curve::curve_errors::CurveError, felt::Felt};
use lambdaworks_math::{
cyclic_group::IsGroup,
elliptic_curve::{
short_weierstrass::{
curves::stark_curve::StarkCurve, point::ShortWeierstrassProjectivePoint,
},
traits::{FromAffine, IsEllipticCurve},
},
};

/// Represents a point on the Stark elliptic curve.
/// Doc: https://docs.starkware.co/starkex/crypto/stark-curve.html
Expand Down Expand Up @@ -47,6 +51,17 @@ impl AffinePoint {
pub fn y(&self) -> Felt {
Felt(*self.0.y())
}

// Returns the generator point of the StarkCurve
pub fn generator() -> Self {
AffinePoint(StarkCurve::generator())
}

// Add the point (`self`) to itself for `scalar` many times
pub fn mul(&self, scalar: &Felt) -> Self {
let scalar = scalar.representative();
AffinePoint(self.0.operate_with_self(scalar))
}
}

impl core::ops::Neg for &AffinePoint {
Expand All @@ -57,6 +72,14 @@ impl core::ops::Neg for &AffinePoint {
}
}

impl core::ops::Add for &AffinePoint {
type Output = AffinePoint;

fn add(self, rhs: Self) -> Self::Output {
AffinePoint(self.0.operate_with_affine(&rhs.0))
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -117,4 +140,52 @@ mod test {
);
assert_eq!(-&AffinePoint::identity(), AffinePoint::identity());
}

#[test]
fn affine_add() {
let p = AffinePoint::new(
Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"),
Felt::from_hex_unchecked(
"0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b",
),
)
.unwrap();

assert_eq!(
&p + &p,
AffinePoint::new(
Felt::from_hex_unchecked(
"0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab",
),
Felt::from_hex_unchecked(
"0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62",
),
)
.unwrap()
);
}

#[test]
fn affine_mul() {
let p = AffinePoint::new(
Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"),
Felt::from_hex_unchecked(
"0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b",
),
)
.unwrap();

assert_eq!(
p.mul(&Felt::from(2)),
AffinePoint::new(
Felt::from_hex_unchecked(
"0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab",
),
Felt::from_hex_unchecked(
"0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62",
),
)
.unwrap()
);
}
}
30 changes: 20 additions & 10 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ impl NonZeroFelt {
pub const fn from_felt_unchecked(value: Felt) -> Self {
Self(value.0)
}

// Returns the representative of the value stored
pub fn representative(&self) -> UnsignedInteger<4> {
self.0.representative()
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -289,13 +294,13 @@ impl Felt {
/// Truncated quotient between `self` and `rhs`.
pub fn floor_div(&self, rhs: &NonZeroFelt) -> Self {
Self(FieldElement::from(
&(self.0.representative().div_rem(&rhs.0.representative())).0,
&(self.representative().div_rem(&rhs.representative())).0,
))
}

/// Quotient and remainder between `self` and `rhs`.
pub fn div_rem(&self, rhs: &NonZeroFelt) -> (Self, Self) {
let (q, r) = self.0.representative().div_rem(&rhs.0.representative());
let (q, r) = self.representative().div_rem(&rhs.representative());
(Self(FieldElement::from(&q)), Self(FieldElement::from(&r)))
}

Expand Down Expand Up @@ -327,7 +332,7 @@ impl Felt {

/// Raises `self` to the power of `exponent`.
pub fn pow_felt(&self, exponent: &Felt) -> Self {
Self(self.0.pow(exponent.0.representative()))
Self(self.0.pow(exponent.representative()))
}

// Implemention taken from Jonathan Lei's starknet-rs
Expand Down Expand Up @@ -412,20 +417,25 @@ impl Felt {
/// Convert `self`'s representative into an array of `u64` digits,
/// least significant digits first.
pub fn to_le_digits(&self) -> [u64; 4] {
let mut limbs = self.0.representative().limbs;
let mut limbs = self.representative().limbs;
limbs.reverse();
limbs
}

// Returns the representative of the value stored
pub fn representative(&self) -> UnsignedInteger<4> {
self.0.representative()
}

/// Convert `self`'s representative into an array of `u64` digits,
/// most significant digits first.
pub fn to_be_digits(&self) -> [u64; 4] {
self.0.representative().limbs
self.representative().limbs
}

/// Count the minimum number of bits needed to express `self`'s representative.
pub fn bits(&self) -> usize {
self.0.representative().bits_le()
self.representative().bits_le()
}

pub fn to_biguint(&self) -> BigUint {
Expand Down Expand Up @@ -961,7 +971,7 @@ mod formatting {

let mut buf = [0u8; 4 * 20];
let mut i = buf.len() - 1;
let mut current = self.0.representative();
let mut current = self.representative();
let ten = UnsignedInteger::from(10_u16);

loop {
Expand Down Expand Up @@ -1084,7 +1094,7 @@ mod test {
let mut bits = Vec::new();

// Iterate over each limb in the representative of x
for limb in x.0.representative().limbs {
for limb in x.representative().limbs {
// Convert the limb to a sequence of 8 bytes (u8) in big-endian
let bytes = limb.to_be_bytes();

Expand Down Expand Up @@ -1254,8 +1264,8 @@ mod test {

#[test]
fn floor_div_is_mul_inv(x in any::<Felt>(), y in nonzero_felt()) {
let x = Felt(FieldElement::from(&x.0.representative().shl(127)));
let y = Felt(FieldElement::from(&y.0.representative().shl(127)));
let x = Felt(FieldElement::from(&x.representative().shl(127)));
let y = Felt(FieldElement::from(&y.representative().shl(127)));
let q = x.field_div(&NonZeroFelt(y.0));
prop_assert!(q <= Felt::MAX);
prop_assert_eq!(q * y, x);
Expand Down