@@ -21,29 +21,47 @@ use digest::{Digest, FixedOutputReset};
2121use generic_array:: typenum:: U1 ;
2222use generic_array:: { ArrayLength , GenericArray } ;
2323use rand_core:: { CryptoRng , RngCore } ;
24+ #[ cfg( feature = "ristretto255" ) ]
25+ pub use ristretto:: Ristretto255 ;
2426use subtle:: ConstantTimeEq ;
2527use zeroize:: Zeroize ;
2628
2729use crate :: { Error , Result } ;
2830
2931/// A prime-order subgroup of a base field (EC, prime-order field ...). This
3032/// 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- {
33+ pub trait Group {
3834 /// The ciphersuite identifier as dictated by
3935 /// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-05.txt>
4036 const SUITE_ID : usize ;
4137
38+ /// The type of group elements
39+ type Elem : Copy
40+ + Sized
41+ + ConstantTimeEq
42+ + Zeroize
43+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Elem >
44+ + for < ' a > Add < & ' a Self :: Elem , Output = Self :: Elem > ;
45+
46+ /// The byte length necessary to represent group elements
47+ type ElemLen : ArrayLength < u8 > + ' static ;
48+
49+ /// The type of base field scalars
50+ type Scalar : Zeroize
51+ + Copy
52+ + ConstantTimeEq
53+ + for < ' a > Add < & ' a Self :: Scalar , Output = Self :: Scalar >
54+ + for < ' a > Sub < & ' a Self :: Scalar , Output = Self :: Scalar >
55+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Scalar > ;
56+
57+ /// The byte length necessary to represent scalars
58+ type ScalarLen : ArrayLength < u8 > + ' static ;
59+
4260 /// transforms a password and domain separation tag (DST) into a curve point
4361 fn hash_to_curve < H : BlockSizeUser + Digest + FixedOutputReset , D : ArrayLength < u8 > + Add < U1 > > (
4462 msg : & [ u8 ] ,
4563 dst : GenericArray < u8 , D > ,
46- ) -> Result < Self >
64+ ) -> Result < Self :: Elem >
4765 where
4866 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
4967
@@ -60,16 +78,6 @@ pub trait Group:
6078 where
6179 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
6280
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-
7381 /// Return a scalar from its fixed-length bytes representation, without
7482 /// checking if the scalar is zero.
7583 fn from_scalar_slice_unchecked (
@@ -90,28 +98,28 @@ pub trait Group:
9098
9199 /// picks a scalar at random
92100 fn random_nonzero_scalar < R : RngCore + CryptoRng > ( rng : & mut R ) -> Self :: Scalar ;
101+
93102 /// Serializes a scalar to bytes
94103 fn scalar_as_bytes ( scalar : Self :: Scalar ) -> GenericArray < u8 , Self :: ScalarLen > ;
104+
95105 /// The multiplicative inverse of this scalar
96106 fn scalar_invert ( scalar : & Self :: Scalar ) -> Self :: Scalar ;
97107
98- /// The byte length necessary to represent group elements
99- type ElemLen : ArrayLength < u8 > + ' static ;
100-
101108 /// Return an element from its fixed-length bytes representation. This is
102109 /// the unchecked version, which does not check for deserializing the
103110 /// identity element
104- fn from_element_slice_unchecked ( element_bits : & GenericArray < u8 , Self :: ElemLen > )
105- -> Result < Self > ;
111+ fn from_element_slice_unchecked (
112+ element_bits : & GenericArray < u8 , Self :: ElemLen > ,
113+ ) -> Result < Self :: Elem > ;
106114
107115 /// Return an element from its fixed-length bytes representation. If the
108116 /// element is the identity element, return an error.
109117 fn from_element_slice < ' a > (
110118 element_bits : impl Into < & ' a GenericArray < u8 , Self :: ElemLen > > ,
111- ) -> Result < Self > {
119+ ) -> Result < Self :: Elem > {
112120 let elem = Self :: from_element_slice_unchecked ( element_bits. into ( ) ) ?;
113121
114- if Self :: ct_eq ( & elem, & < Self as Group > :: identity ( ) ) . into ( ) {
122+ if Self :: Elem :: ct_eq ( & elem, & Self :: identity ( ) ) . into ( ) {
115123 // found the identity element
116124 return Err ( Error :: PointError ) ;
117125 }
@@ -120,26 +128,21 @@ pub trait Group:
120128 }
121129
122130 /// Serializes the `self` group element
123- fn to_arr ( & self ) -> GenericArray < u8 , Self :: ElemLen > ;
131+ fn to_arr ( elem : Self :: Elem ) -> GenericArray < u8 , Self :: ElemLen > ;
124132
125133 /// Get the base point for the group
126- fn base_point ( ) -> Self ;
134+ fn base_point ( ) -> Self :: Elem ;
127135
128136 /// 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 ( )
137+ fn is_identity ( elem : Self :: Elem ) -> bool {
138+ elem . ct_eq ( & Self :: identity ( ) ) . into ( )
131139 }
132140
133141 /// Returns the identity group element
134- fn identity ( ) -> Self ;
142+ fn identity ( ) -> Self :: Elem ;
135143
136144 /// Returns the scalar representing zero
137145 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- }
143146}
144147
145148#[ cfg( test) ]
0 commit comments