@@ -4,6 +4,7 @@ use dusk_jubjub::{
44 SubgroupPoint as JubjubSubgroup ,
55} ;
66use group:: { Group , GroupEncoding } ;
7+ use std:: ops:: { Add , Mul } ;
78
89use super :: { BaseFieldElement , ScalarFieldElement } ;
910use crate :: { StmResult , signature_scheme:: SchnorrSignatureError } ;
@@ -16,14 +17,6 @@ impl AffinePoint {
1617 AffinePoint ( JubjubAffinePoint :: from ( projective_point. 0 ) )
1718 }
1819
19- pub ( crate ) fn from_prime_order_projective_point (
20- prime_order_projective_point : & PrimeOrderProjectivePoint ,
21- ) -> Self {
22- AffinePoint ( JubjubAffinePoint :: from (
23- ProjectivePoint :: from_prime_order_projective_point ( * prime_order_projective_point) . 0 ,
24- ) )
25- }
26-
2720 pub ( crate ) fn get_u ( & self ) -> BaseFieldElement {
2821 BaseFieldElement ( self . 0 . get_u ( ) )
2922 }
@@ -33,6 +26,14 @@ impl AffinePoint {
3326 }
3427}
3528
29+ impl From < & PrimeOrderProjectivePoint > for AffinePoint {
30+ fn from ( prime_order_projective_point : & PrimeOrderProjectivePoint ) -> Self {
31+ AffinePoint ( JubjubAffinePoint :: from ( JubjubExtended :: from (
32+ prime_order_projective_point. 0 ,
33+ ) ) )
34+ }
35+ }
36+
3637#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
3738pub ( crate ) struct ProjectivePoint ( pub ( crate ) JubjubExtended ) ;
3839
@@ -41,14 +42,6 @@ impl ProjectivePoint {
4142 ProjectivePoint ( JubjubExtended :: hash_to_point ( input) )
4243 }
4344
44- pub ( crate ) fn add ( & self , other : Self ) -> Self {
45- ProjectivePoint ( self . 0 + other. 0 )
46- }
47-
48- pub ( crate ) fn scalar_multiplication ( & self , scalar : & ScalarFieldElement ) -> Self {
49- ProjectivePoint ( self . 0 * scalar. 0 )
50- }
51-
5245 pub ( crate ) fn get_coordinates ( & self ) -> ( BaseFieldElement , BaseFieldElement ) {
5346 let affine_point = AffinePoint :: from_projective_point ( * self ) ;
5447
@@ -66,23 +59,37 @@ impl ProjectivePoint {
6659
6760 match JubjubExtended :: from_bytes ( & projective_point_bytes) . into_option ( ) {
6861 Some ( projective_point) => Ok ( Self ( projective_point) ) ,
69- None => Err ( anyhow ! (
70- SchnorrSignatureError :: ProjectivePointSerializationError
71- ) ) ,
62+ None => Err ( anyhow ! ( SchnorrSignatureError :: ProjectivePointSerialization ) ) ,
7263 }
7364 }
7465
75- pub ( crate ) fn from_prime_order_projective_point (
76- prime_order_projective_point : PrimeOrderProjectivePoint ,
77- ) -> Self {
78- ProjectivePoint ( JubjubExtended :: from ( prime_order_projective_point. 0 ) )
79- }
80-
8166 pub ( crate ) fn is_prime_order ( self ) -> bool {
8267 self . 0 . is_prime_order ( ) . into ( )
8368 }
8469}
8570
71+ impl Add for ProjectivePoint {
72+ type Output = ProjectivePoint ;
73+
74+ fn add ( self , other : ProjectivePoint ) -> ProjectivePoint {
75+ ProjectivePoint ( self . 0 + other. 0 )
76+ }
77+ }
78+
79+ impl Mul < ProjectivePoint > for ScalarFieldElement {
80+ type Output = ProjectivePoint ;
81+
82+ fn mul ( self , point : ProjectivePoint ) -> ProjectivePoint {
83+ ProjectivePoint ( point. 0 * self . 0 )
84+ }
85+ }
86+
87+ impl From < PrimeOrderProjectivePoint > for ProjectivePoint {
88+ fn from ( prime_order_projective_point : PrimeOrderProjectivePoint ) -> Self {
89+ ProjectivePoint ( JubjubExtended :: from ( prime_order_projective_point. 0 ) )
90+ }
91+ }
92+
8693#[ derive( Debug , Clone , Copy , Default , PartialEq , Eq ) ]
8794pub ( crate ) struct PrimeOrderProjectivePoint ( pub ( crate ) JubjubSubgroup ) ;
8895
@@ -91,28 +98,18 @@ impl PrimeOrderProjectivePoint {
9198 PrimeOrderProjectivePoint ( JubjubSubgroup :: generator ( ) )
9299 }
93100
94- pub ( crate ) fn add ( & self , other : Self ) -> Self {
95- PrimeOrderProjectivePoint ( self . 0 + other. 0 )
96- }
97-
98- pub ( crate ) fn scalar_multiplication ( & self , scalar : & ScalarFieldElement ) -> Self {
99- PrimeOrderProjectivePoint ( self . 0 * scalar. 0 )
100- }
101-
102101 /// Check if the given point is on the curve using its coordinates
103102 pub ( crate ) fn is_on_curve ( & self ) -> StmResult < PrimeOrderProjectivePoint > {
104- let point_affine_representation = AffinePoint :: from_prime_order_projective_point ( self ) ;
103+ let point_affine_representation = AffinePoint :: from ( self ) ;
105104 let ( x, y) = (
106105 point_affine_representation. get_u ( ) ,
107106 point_affine_representation. get_v ( ) ,
108107 ) ;
109- let x_square = x . square ( ) ;
110- let y_square = y . square ( ) ;
108+ let x_square = & x * & x ;
109+ let y_square = & y * & y ;
111110
112- let lhs = y_square. sub ( & x_square) ;
113- let mut rhs = x_square. mul ( & y_square) ;
114- rhs = rhs. mul ( & BaseFieldElement ( EDWARDS_D ) ) ;
115- rhs = rhs. add ( & BaseFieldElement :: get_one ( ) ) ;
111+ let lhs = & y_square - & x_square;
112+ let rhs = ( x_square * y_square) * BaseFieldElement ( EDWARDS_D ) + BaseFieldElement :: get_one ( ) ;
116113
117114 if lhs != rhs {
118115 return Err ( anyhow ! ( SchnorrSignatureError :: PointIsNotOnCurve ( Box :: new(
@@ -134,12 +131,28 @@ impl PrimeOrderProjectivePoint {
134131 match JubjubSubgroup :: from_bytes ( & prime_order_projective_point_bytes) . into_option ( ) {
135132 Some ( prime_order_projective_point) => Ok ( Self ( prime_order_projective_point) ) ,
136133 None => Err ( anyhow ! (
137- SchnorrSignatureError :: PrimeOrderProjectivePointSerializationError
134+ SchnorrSignatureError :: PrimeOrderProjectivePointSerialization
138135 ) ) ,
139136 }
140137 }
141138}
142139
140+ impl Add for PrimeOrderProjectivePoint {
141+ type Output = PrimeOrderProjectivePoint ;
142+
143+ fn add ( self , other : PrimeOrderProjectivePoint ) -> PrimeOrderProjectivePoint {
144+ PrimeOrderProjectivePoint ( self . 0 + other. 0 )
145+ }
146+ }
147+
148+ impl Mul < PrimeOrderProjectivePoint > for ScalarFieldElement {
149+ type Output = PrimeOrderProjectivePoint ;
150+
151+ fn mul ( self , point : PrimeOrderProjectivePoint ) -> PrimeOrderProjectivePoint {
152+ PrimeOrderProjectivePoint ( point. 0 * self . 0 )
153+ }
154+ }
155+
143156#[ cfg( test) ]
144157mod tests {
145158 use super :: * ;
@@ -156,7 +169,7 @@ mod tests {
156169 let mut rng = ChaCha20Rng :: from_seed ( [ 0u8 ; 32 ] ) ;
157170 let scalar = ScalarFieldElement :: new_random_nonzero_scalar ( & mut rng) . unwrap ( ) ;
158171 let point = PrimeOrderProjectivePoint :: create_generator ( ) ;
159- point. scalar_multiplication ( & scalar)
172+ PrimeOrderProjectivePoint ( point. 0 * scalar. 0 )
160173 }
161174
162175 #[ test]
0 commit comments