@@ -28,22 +28,38 @@ use crate::{Error, Result};
2828
2929/// A prime-order subgroup of a base field (EC, prime-order field ...). This
3030/// subgroup is noted additively — as in the draft RFC — in this trait.
31- pub trait Group :
32- Copy
33- + Sized
34- + ConstantTimeEq
35- + for < ' a > Mul < & ' a <Self as Group >:: Scalar , Output = Self >
36- + for < ' a > Add < & ' a Self , Output = Self >
37- {
31+ pub trait Group {
3832 /// The ciphersuite identifier as dictated by
3933 /// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-05.txt>
4034 const SUITE_ID : usize ;
4135
36+ /// The type of group elements
37+ type Elem : Copy
38+ + Sized
39+ + ConstantTimeEq
40+ + Zeroize
41+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Elem >
42+ + for < ' a > Add < & ' a Self :: Elem , Output = Self :: Elem > ;
43+
44+ /// The byte length necessary to represent group elements
45+ type ElemLen : ArrayLength < u8 > + ' static ;
46+
47+ /// The type of base field scalars
48+ type Scalar : Zeroize
49+ + Copy
50+ + ConstantTimeEq
51+ + for < ' a > Add < & ' a Self :: Scalar , Output = Self :: Scalar >
52+ + for < ' a > Sub < & ' a Self :: Scalar , Output = Self :: Scalar >
53+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Scalar > ;
54+
55+ /// The byte length necessary to represent scalars
56+ type ScalarLen : ArrayLength < u8 > + ' static ;
57+
4258 /// transforms a password and domain separation tag (DST) into a curve point
4359 fn hash_to_curve < H : BlockSizeUser + Digest + FixedOutputReset , D : ArrayLength < u8 > + Add < U1 > > (
4460 msg : & [ u8 ] ,
4561 dst : GenericArray < u8 , D > ,
46- ) -> Result < Self >
62+ ) -> Result < Self :: Elem >
4763 where
4864 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
4965
@@ -60,16 +76,6 @@ pub trait Group:
6076 where
6177 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
6278
63- /// The type of base field scalars
64- type Scalar : Zeroize
65- + Copy
66- + ConstantTimeEq
67- + for < ' a > Add < & ' a Self :: Scalar , Output = Self :: Scalar >
68- + for < ' a > Sub < & ' a Self :: Scalar , Output = Self :: Scalar >
69- + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Scalar > ;
70- /// The byte length necessary to represent scalars
71- type ScalarLen : ArrayLength < u8 > + ' static ;
72-
7379 /// Return a scalar from its fixed-length bytes representation, without
7480 /// checking if the scalar is zero.
7581 fn from_scalar_slice_unchecked (
@@ -90,28 +96,28 @@ pub trait Group:
9096
9197 /// picks a scalar at random
9298 fn random_nonzero_scalar < R : RngCore + CryptoRng > ( rng : & mut R ) -> Self :: Scalar ;
99+
93100 /// Serializes a scalar to bytes
94101 fn scalar_as_bytes ( scalar : Self :: Scalar ) -> GenericArray < u8 , Self :: ScalarLen > ;
102+
95103 /// The multiplicative inverse of this scalar
96104 fn scalar_invert ( scalar : & Self :: Scalar ) -> Self :: Scalar ;
97105
98- /// The byte length necessary to represent group elements
99- type ElemLen : ArrayLength < u8 > + ' static ;
100-
101106 /// Return an element from its fixed-length bytes representation. This is
102107 /// the unchecked version, which does not check for deserializing the
103108 /// identity element
104- fn from_element_slice_unchecked ( element_bits : & GenericArray < u8 , Self :: ElemLen > )
105- -> Result < Self > ;
109+ fn from_element_slice_unchecked (
110+ element_bits : & GenericArray < u8 , Self :: ElemLen > ,
111+ ) -> Result < Self :: Elem > ;
106112
107113 /// Return an element from its fixed-length bytes representation. If the
108114 /// element is the identity element, return an error.
109115 fn from_element_slice < ' a > (
110116 element_bits : impl Into < & ' a GenericArray < u8 , Self :: ElemLen > > ,
111- ) -> Result < Self > {
117+ ) -> Result < Self :: Elem > {
112118 let elem = Self :: from_element_slice_unchecked ( element_bits. into ( ) ) ?;
113119
114- if Self :: ct_eq ( & elem, & < Self as Group > :: identity ( ) ) . into ( ) {
120+ if Self :: Elem :: ct_eq ( & elem, & Self :: identity ( ) ) . into ( ) {
115121 // found the identity element
116122 return Err ( Error :: PointError ) ;
117123 }
@@ -120,26 +126,21 @@ pub trait Group:
120126 }
121127
122128 /// Serializes the `self` group element
123- fn to_arr ( & self ) -> GenericArray < u8 , Self :: ElemLen > ;
129+ fn to_arr ( elem : Self :: Elem ) -> GenericArray < u8 , Self :: ElemLen > ;
124130
125131 /// Get the base point for the group
126- fn base_point ( ) -> Self ;
132+ fn base_point ( ) -> Self :: Elem ;
127133
128134 /// Returns if the group element is equal to the identity (1)
129- fn is_identity ( & self ) -> bool {
130- self . ct_eq ( & < Self as Group > :: identity ( ) ) . into ( )
135+ fn is_identity ( elem : Self :: Elem ) -> bool {
136+ elem . ct_eq ( & Self :: identity ( ) ) . into ( )
131137 }
132138
133139 /// Returns the identity group element
134- fn identity ( ) -> Self ;
140+ fn identity ( ) -> Self :: Elem ;
135141
136142 /// Returns the scalar representing zero
137143 fn scalar_zero ( ) -> Self :: Scalar ;
138-
139- /// Set the contents of self to the identity value
140- fn zeroize ( & mut self ) {
141- * self = <Self as Group >:: identity ( ) ;
142- }
143144}
144145
145146#[ cfg( test) ]
0 commit comments