11//! Traits for handling hash to curve.
22
3- use super :: { ExpandMsg , MapToCurve , hash_to_field } ;
3+ use crate :: { ExpandMsg , MapToCurve } ;
44use elliptic_curve:: ProjectivePoint ;
5- use elliptic_curve:: array:: typenum:: NonZero ;
6- use elliptic_curve:: array:: { Array , ArraySize } ;
7- use elliptic_curve:: group:: cofactor:: CofactorGroup ;
8- use elliptic_curve:: ops:: Reduce ;
95
106/// Hash arbitrary byte sequences to a valid group element.
117pub trait GroupDigest : MapToCurve {
@@ -40,7 +36,7 @@ pub trait GroupDigest: MapToCurve {
4036 dst : & [ u8 ] ,
4137 ) -> Result < ProjectivePoint < Self > , <Self :: ExpandMsg as ExpandMsg < Self :: SecurityLevel > >:: Error >
4238 {
43- hash_from_bytes :: < Self , Self :: ExpandMsg > ( & [ msg] , & [ dst] )
39+ crate :: hash_from_bytes :: < Self , Self :: ExpandMsg > ( & [ msg] , & [ dst] )
4440 }
4541
4642 /// Computes the encode to curve routine.
@@ -65,77 +61,6 @@ pub trait GroupDigest: MapToCurve {
6561 dst : & [ u8 ] ,
6662 ) -> Result < ProjectivePoint < Self > , <Self :: ExpandMsg as ExpandMsg < Self :: SecurityLevel > >:: Error >
6763 {
68- encode_from_bytes :: < Self , Self :: ExpandMsg > ( & [ msg] , & [ dst] )
64+ crate :: encode_from_bytes :: < Self , Self :: ExpandMsg > ( & [ msg] , & [ dst] )
6965 }
7066}
71-
72- /// Computes the hash to curve routine.
73- /// See [`GroupDigest::hash_from_bytes()`] for more details.
74- ///
75- /// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length * 2`.
76- /// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
77- ///
78- /// # Errors
79- ///
80- /// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
81- /// and [`ExpandMsgXofError`] for examples.
82- ///
83- /// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
84- /// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
85- pub fn hash_from_bytes < C , X > ( msg : & [ & [ u8 ] ] , dst : & [ & [ u8 ] ] ) -> Result < ProjectivePoint < C > , X :: Error >
86- where
87- C : MapToCurve ,
88- X : ExpandMsg < C :: SecurityLevel > ,
89- {
90- let [ u0, u1] = hash_to_field :: < 2 , X , _ , C :: FieldElement , C :: Length > ( msg, dst) ?;
91- let q0 = C :: map_to_curve ( u0) ;
92- let q1 = C :: map_to_curve ( u1) ;
93- Ok ( ( q0 + q1) . clear_cofactor ( ) )
94- }
95-
96- /// Computes the encode to curve routine.
97- /// See [`GroupDigest::encode_from_bytes()`] for more details.
98- ///
99- /// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
100- ///
101- /// # Errors
102- ///
103- /// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
104- /// and [`ExpandMsgXofError`] for examples.
105- ///
106- /// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
107- /// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
108- pub fn encode_from_bytes < C , X > ( msg : & [ & [ u8 ] ] , dst : & [ & [ u8 ] ] ) -> Result < ProjectivePoint < C > , X :: Error >
109- where
110- C : MapToCurve ,
111- X : ExpandMsg < C :: SecurityLevel > ,
112- {
113- let [ u] = hash_to_field :: < 1 , X , _ , C :: FieldElement , C :: Length > ( msg, dst) ?;
114- let q0 = C :: map_to_curve ( u) ;
115- Ok ( q0. clear_cofactor ( ) )
116- }
117-
118- /// Computes the hash to field routine according to
119- /// <https://www.rfc-editor.org/rfc/rfc9380.html#section-5-4>
120- /// and returns a scalar.
121- ///
122- /// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
123- /// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
124- ///
125- /// # Errors
126- ///
127- /// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
128- /// and [`ExpandMsgXofError`] for examples.
129- ///
130- /// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
131- /// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
132- pub fn hash_to_scalar < C , X , L > ( msg : & [ & [ u8 ] ] , dst : & [ & [ u8 ] ] ) -> Result < C :: Scalar , X :: Error >
133- where
134- C : MapToCurve ,
135- X : ExpandMsg < C :: SecurityLevel > ,
136- L : ArraySize + NonZero ,
137- C :: Scalar : Reduce < Array < u8 , L > > ,
138- {
139- let [ u] = hash_to_field :: < 1 , X , _ , C :: Scalar , L > ( msg, dst) ?;
140- Ok ( u)
141- }
0 commit comments