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 2 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
81 changes: 75 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,15 @@ impl AffinePoint {
pub fn y(&self) -> Felt {
Felt(*self.0.y())
}

pub fn generator() -> Self {
FilipLaurentiu marked this conversation as resolved.
Show resolved Hide resolved
AffinePoint(StarkCurve::generator())
}

pub fn mul(&self, exponent: Felt) -> Self {
FilipLaurentiu marked this conversation as resolved.
Show resolved Hide resolved
let exponent = exponent.representative();
AffinePoint(self.0.operate_with_self(exponent))
}
}

impl core::ops::Neg for &AffinePoint {
Expand All @@ -57,6 +70,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 +138,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()
);
}
}
14 changes: 9 additions & 5 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl Felt {

/// 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.0.representative());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this change the behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the self.representative() function returns self.0.representative() so it's the same.
I had to add the public function representative to get the representation of a Felt for the operate_with_self function in order to multiply the point with itself.
Adding representative() function to Felt and replace the old way of using it make the code cleaner.

(Self(FieldElement::from(&q)), Self(FieldElement::from(&r)))
}

Expand Down Expand Up @@ -412,20 +412,24 @@ 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
}

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 +965,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