@@ -17,40 +17,63 @@ mod ristretto;
1717use core:: ops:: { Add , Mul , Sub } ;
1818
1919use digest:: core_api:: BlockSizeUser ;
20- use digest:: { Digest , FixedOutputReset } ;
20+ use digest:: { Digest , FixedOutputReset , HashMarker } ;
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
29- /// A prime-order subgroup of a base field (EC, prime-order field ...). This
30- /// 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+ /// Ciphersuite identifier for a combination of a [`Group`] and a
32+ /// [hash](HashMarker).
33+ pub trait Voprf < H : HashMarker > {
3834 /// The ciphersuite identifier as dictated by
3935 /// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-05.txt>
40- const SUITE_ID : usize ;
36+ const SUITE_ID : u16 ;
37+ }
38+
39+ /// A prime-order subgroup of a base field (EC, prime-order field ...). This
40+ /// subgroup is noted additively — as in the draft RFC — in this trait.
41+ pub trait Group {
42+ /// Type representing a group element.
43+ type Elem : ConstantTimeEq
44+ + Copy
45+ + Sized
46+ + Zeroize
47+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Elem >
48+ + for < ' a > Add < & ' a Self :: Elem , Output = Self :: Elem > ;
49+ /// The byte length necessary to represent group elements
50+ type ElemLen : ArrayLength < u8 > + ' static ;
51+
52+ /// The type of base field scalars
53+ type Scalar : ConstantTimeEq
54+ + Copy
55+ + Zeroize
56+ + for < ' a > Add < & ' a Self :: Scalar , Output = Self :: Scalar >
57+ + for < ' a > Sub < & ' a Self :: Scalar , Output = Self :: Scalar >
58+ + for < ' a > Mul < & ' a Self :: Scalar , Output = Self :: Scalar > ;
59+ /// The byte length necessary to represent scalars
60+ type ScalarLen : ArrayLength < u8 > + ' static ;
4161
4262 /// transforms a password and domain separation tag (DST) into a curve point
43- fn hash_to_curve < H : BlockSizeUser + Digest + FixedOutputReset , D : ArrayLength < u8 > + Add < U1 > > (
63+ fn hash_to_curve <
64+ H : BlockSizeUser + Digest + FixedOutputReset + HashMarker ,
65+ D : ArrayLength < u8 > + Add < U1 > ,
66+ > (
4467 msg : & [ u8 ] ,
4568 dst : GenericArray < u8 , D > ,
46- ) -> Result < Self >
69+ ) -> Result < Self :: Elem >
4770 where
4871 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
4972
5073 /// Hashes a slice of pseudo-random bytes to a scalar
5174 fn hash_to_scalar <
5275 ' a ,
53- H : BlockSizeUser + Digest + FixedOutputReset ,
76+ H : BlockSizeUser + Digest + FixedOutputReset + HashMarker ,
5477 D : ArrayLength < u8 > + Add < U1 > ,
5578 I : IntoIterator < Item = & ' a [ u8 ] > ,
5679 > (
@@ -60,58 +83,32 @@ pub trait Group:
6083 where
6184 <D as Add < U1 > >:: Output : ArrayLength < u8 > ;
6285
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 ;
86+ /// Get the base point for the group
87+ fn base_point ( ) -> Self :: Elem ;
7288
73- /// Return a scalar from its fixed-length bytes representation, without
74- /// checking if the scalar is zero.
75- fn from_scalar_slice_unchecked (
76- scalar_bits : & GenericArray < u8 , Self :: ScalarLen > ,
77- ) -> Result < Self :: Scalar > ;
89+ /// Returns the identity group element
90+ fn identity ( ) -> Self :: Elem ;
7891
79- /// Return a scalar from its fixed-length bytes representation. If the
80- /// scalar is zero, then return an error.
81- fn from_scalar_slice < ' a > (
82- scalar_bits : impl Into < & ' a GenericArray < u8 , Self :: ScalarLen > > ,
83- ) -> Result < Self :: Scalar > {
84- let scalar = Self :: from_scalar_slice_unchecked ( scalar_bits. into ( ) ) ?;
85- if scalar. ct_eq ( & Self :: scalar_zero ( ) ) . into ( ) {
86- return Err ( Error :: ZeroScalarError ) ;
87- }
88- Ok ( scalar)
92+ /// Returns if the group element is equal to the identity (1)
93+ fn is_identity ( elem : Self :: Elem ) -> bool {
94+ elem. ct_eq ( & <Self as Group >:: identity ( ) ) . into ( )
8995 }
9096
91- /// picks a scalar at random
92- fn random_nonzero_scalar < R : RngCore + CryptoRng > ( rng : & mut R ) -> Self :: Scalar ;
93- /// Serializes a scalar to bytes
94- fn scalar_as_bytes ( scalar : Self :: Scalar ) -> GenericArray < u8 , Self :: ScalarLen > ;
95- /// The multiplicative inverse of this scalar
96- fn scalar_invert ( scalar : & Self :: Scalar ) -> Self :: Scalar ;
97-
98- /// The byte length necessary to represent group elements
99- type ElemLen : ArrayLength < u8 > + ' static ;
100-
10197 /// Return an element from its fixed-length bytes representation. This is
10298 /// the unchecked version, which does not check for deserializing the
10399 /// identity element
104- fn from_element_slice_unchecked ( element_bits : & GenericArray < u8 , Self :: ElemLen > )
105- -> Result < Self > ;
100+ fn from_element_slice_unchecked (
101+ element_bits : & GenericArray < u8 , Self :: ElemLen > ,
102+ ) -> Result < Self :: Elem > ;
106103
107104 /// Return an element from its fixed-length bytes representation. If the
108105 /// element is the identity element, return an error.
109106 fn from_element_slice < ' a > (
110107 element_bits : impl Into < & ' a GenericArray < u8 , Self :: ElemLen > > ,
111- ) -> Result < Self > {
108+ ) -> Result < Self :: Elem > {
112109 let elem = Self :: from_element_slice_unchecked ( element_bits. into ( ) ) ?;
113110
114- if Self :: ct_eq ( & elem , & < Self as Group > :: identity ( ) ) . into ( ) {
111+ if elem . ct_eq ( & Self :: identity ( ) ) . into ( ) {
115112 // found the identity element
116113 return Err ( Error :: PointError ) ;
117114 }
@@ -120,26 +117,37 @@ pub trait Group:
120117 }
121118
122119 /// Serializes the `self` group element
123- fn to_arr ( & self ) -> GenericArray < u8 , Self :: ElemLen > ;
124-
125- /// Get the base point for the group
126- fn base_point ( ) -> Self ;
127-
128- /// 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 ( )
131- }
120+ fn element_to_bytes ( elem : Self :: Elem ) -> GenericArray < u8 , Self :: ElemLen > ;
132121
133- /// Returns the identity group element
134- fn identity ( ) -> Self ;
122+ /// picks a scalar at random
123+ fn random_nonzero_scalar < R : RngCore + CryptoRng > ( rng : & mut R ) -> Self :: Scalar ;
135124
136125 /// Returns the scalar representing zero
137126 fn scalar_zero ( ) -> Self :: Scalar ;
138127
139- /// Set the contents of self to the identity value
140- fn zeroize ( & mut self ) {
141- * self = <Self as Group >:: identity ( ) ;
128+ /// The multiplicative inverse of this scalar
129+ fn scalar_invert ( scalar : & Self :: Scalar ) -> Self :: Scalar ;
130+
131+ /// Return a scalar from its fixed-length bytes representation, without
132+ /// checking if the scalar is zero.
133+ fn from_scalar_slice_unchecked (
134+ scalar_bits : & GenericArray < u8 , Self :: ScalarLen > ,
135+ ) -> Result < Self :: Scalar > ;
136+
137+ /// Return a scalar from its fixed-length bytes representation. If the
138+ /// scalar is zero, then return an error.
139+ fn from_scalar_slice < ' a > (
140+ scalar_bits : impl Into < & ' a GenericArray < u8 , Self :: ScalarLen > > ,
141+ ) -> Result < Self :: Scalar > {
142+ let scalar = Self :: from_scalar_slice_unchecked ( scalar_bits. into ( ) ) ?;
143+ if scalar. ct_eq ( & Self :: scalar_zero ( ) ) . into ( ) {
144+ return Err ( Error :: ZeroScalarError ) ;
145+ }
146+ Ok ( scalar)
142147 }
148+
149+ /// Serializes a scalar to bytes
150+ fn scalar_to_bytes ( scalar : Self :: Scalar ) -> GenericArray < u8 , Self :: ScalarLen > ;
143151}
144152
145153#[ cfg( test) ]
0 commit comments