From 20742c8b0e23f60c2efeaa10321d2675c0a93b35 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 8 Mar 2024 18:22:07 +0100 Subject: [PATCH] some fixes --- .../src/curve/projective_point.rs | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/crates/starknet-types-core/src/curve/projective_point.rs b/crates/starknet-types-core/src/curve/projective_point.rs index 2125cda..4fb3b04 100644 --- a/crates/starknet-types-core/src/curve/projective_point.rs +++ b/crates/starknet-types-core/src/curve/projective_point.rs @@ -79,6 +79,20 @@ impl ops::AddAssign<&ProjectivePoint> for ProjectivePoint { } } +impl ops::Add for ProjectivePoint { + type Output = ProjectivePoint; + + fn add(self, rhs: ProjectivePoint) -> ProjectivePoint { + ProjectivePoint(self.0.operate_with(&rhs.0)) + } +} + +impl ops::AddAssign for ProjectivePoint { + fn add_assign(&mut self, rhs: ProjectivePoint) { + self.0 = self.0.operate_with(&rhs.0); + } +} + impl core::ops::Neg for &ProjectivePoint { type Output = ProjectivePoint; @@ -101,6 +115,20 @@ impl core::ops::SubAssign<&ProjectivePoint> for ProjectivePoint { } } +impl core::ops::Sub for ProjectivePoint { + type Output = ProjectivePoint; + + fn sub(self, rhs: ProjectivePoint) -> ProjectivePoint { + &self - &rhs + } +} + +impl core::ops::SubAssign for ProjectivePoint { + fn sub_assign(&mut self, rhs: ProjectivePoint) { + *self -= &rhs; + } +} + impl ops::Mul for &ProjectivePoint { type Output = ProjectivePoint; @@ -325,7 +353,7 @@ mod test { } #[test] - fn sub_operations() { + fn sub_operations_pointers() { let mut a = ProjectivePoint::new( Felt::from_dec_str( "685118385380464480289795596422487144864558069280897344382334516257395969277", @@ -345,4 +373,26 @@ mod test { a -= &b; assert_eq!(a, ProjectivePoint::identity()); } + + #[test] + fn sub_operations() { + let mut a = ProjectivePoint::new( + Felt::from_dec_str( + "685118385380464480289795596422487144864558069280897344382334516257395969277", + ) + .unwrap(), + Felt::from_dec_str( + "2157469546853095472290556201984093730375838368522549154974787195581425752638", + ) + .unwrap(), + Felt::from(1), + ); + let b = a.clone(); + + assert_eq!(ProjectivePoint::identity() - a.clone(), -&a); + assert_eq!(a.clone() - a.clone(), ProjectivePoint::identity()); + assert_eq!(a.clone() - a.clone() + a.clone(), a); + a -= b; + assert_eq!(a, ProjectivePoint::identity()); + } }