|
1 | 1 | //! Traits for handling hash to curve. |
2 | 2 |
|
3 | 3 | use super::{ExpandMsg, FromOkm, MapToCurve, hash_to_field}; |
4 | | -use crate::{CurveArithmetic, ProjectivePoint, Result}; |
5 | | -use group::cofactor::CofactorGroup; |
| 4 | +use crate::{ProjectivePoint, Result}; |
6 | 5 | use hybrid_array::typenum::Unsigned; |
7 | 6 |
|
8 | | -/// Adds hashing arbitrary byte sequences to a valid group element |
9 | | -pub trait GroupDigest: CurveArithmetic |
10 | | -where |
11 | | - ProjectivePoint<Self>: CofactorGroup, |
12 | | -{ |
13 | | - /// The field element representation for a group value with multiple elements |
14 | | - type FieldElement: FromOkm + MapToCurve<Output = ProjectivePoint<Self>> + Default + Copy; |
15 | | - |
| 7 | +/// Hash arbitrary byte sequences to a valid group element. |
| 8 | +pub trait GroupDigest: MapToCurve { |
16 | 9 | /// The target security level in bytes: |
17 | 10 | /// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2> |
18 | 11 | /// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels> |
|
58 | 51 | ) -> Result<ProjectivePoint<Self>> { |
59 | 52 | let mut u = [Self::FieldElement::default(), Self::FieldElement::default()]; |
60 | 53 | hash_to_field::<X, _>(msgs, dsts, &mut u)?; |
61 | | - let q0 = u[0].map_to_curve(); |
62 | | - let q1 = u[1].map_to_curve(); |
63 | | - // Ideally we could add and then clear cofactor once |
64 | | - // thus saving a call but the field elements may not |
65 | | - // add properly due to the underlying implementation |
66 | | - // which could result in an incorrect subgroup. |
67 | | - // This is caused curve coefficients being different than |
68 | | - // what is usually implemented. |
69 | | - // FieldElement expects the `a` and `b` to be the original values |
70 | | - // isogenies are different with curves like k256 and bls12-381. |
71 | | - // This problem doesn't manifest for curves with no isogeny like p256. |
72 | | - // For k256 and p256 clear_cofactor doesn't do anything anyway so it will be a no-op. |
73 | | - Ok(q0.clear_cofactor().into() + q1.clear_cofactor()) |
| 54 | + let q0 = Self::map_to_curve(u[0]); |
| 55 | + let q1 = Self::map_to_curve(u[1]); |
| 56 | + Ok(Self::add_and_map_to_subgroup(q0, q1)) |
74 | 57 | } |
75 | 58 |
|
76 | 59 | /// Computes the encode to curve routine. |
|
98 | 81 | ) -> Result<ProjectivePoint<Self>> { |
99 | 82 | let mut u = [Self::FieldElement::default()]; |
100 | 83 | hash_to_field::<X, _>(msgs, dsts, &mut u)?; |
101 | | - let q0 = u[0].map_to_curve(); |
102 | | - Ok(q0.clear_cofactor().into()) |
| 84 | + let q0 = Self::map_to_curve(u[0]); |
| 85 | + Ok(Self::map_to_subgroup(q0)) |
103 | 86 | } |
104 | 87 |
|
105 | 88 | /// Computes the hash to field routine according to |
|
0 commit comments