|
| 1 | +// Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +// |
| 3 | +// This source code is licensed under both the MIT license found in the |
| 4 | +// LICENSE-MIT file in the root directory of this source tree and the Apache |
| 5 | +// License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 6 | +// of this source tree. |
| 7 | + |
| 8 | +use digest::core_api::BlockSizeUser; |
| 9 | +use digest::OutputSizeUser; |
| 10 | +use elliptic_curve::group::cofactor::CofactorGroup; |
| 11 | +use elliptic_curve::hash2curve::{ExpandMsgXmd, FromOkm, GroupDigest}; |
| 12 | +use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint}; |
| 13 | +use elliptic_curve::{ |
| 14 | + AffinePoint, Field, FieldSize, Group as _, ProjectivePoint, PublicKey, Scalar, SecretKey, |
| 15 | +}; |
| 16 | +use generic_array::sequence::Concat; |
| 17 | +use generic_array::typenum::{IsLess, IsLessOrEqual, U256}; |
| 18 | +use generic_array::GenericArray; |
| 19 | +use rand_core::{CryptoRng, RngCore}; |
| 20 | + |
| 21 | +use super::Group; |
| 22 | +use crate::group::{STR_HASH_TO_GROUP, STR_HASH_TO_SCALAR}; |
| 23 | +use crate::voprf::{self, Mode}; |
| 24 | +use crate::{CipherSuite, Error, Result}; |
| 25 | + |
| 26 | +impl<C> Group for C |
| 27 | +where |
| 28 | + C: GroupDigest, |
| 29 | + ProjectivePoint<Self>: CofactorGroup, |
| 30 | + FieldSize<Self>: ModulusSize, |
| 31 | + AffinePoint<Self>: FromEncodedPoint<Self> + ToEncodedPoint<Self>, |
| 32 | + Scalar<Self>: FromOkm, |
| 33 | +{ |
| 34 | + type Elem = ProjectivePoint<Self>; |
| 35 | + |
| 36 | + type ElemLen = <FieldSize<Self> as ModulusSize>::CompressedPointSize; |
| 37 | + |
| 38 | + type Scalar = Scalar<Self>; |
| 39 | + |
| 40 | + type ScalarLen = FieldSize<Self>; |
| 41 | + |
| 42 | + // Implements the `hash_to_curve()` function from |
| 43 | + // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-3 |
| 44 | + fn hash_to_curve<CS: CipherSuite>(msg: &[&[u8]], mode: Mode) -> Result<Self::Elem> |
| 45 | + where |
| 46 | + <CS::Hash as OutputSizeUser>::OutputSize: |
| 47 | + IsLess<U256> + IsLessOrEqual<<CS::Hash as BlockSizeUser>::BlockSize>, |
| 48 | + { |
| 49 | + let dst = |
| 50 | + GenericArray::from(STR_HASH_TO_GROUP).concat(voprf::get_context_string::<CS>(mode)); |
| 51 | + |
| 52 | + Self::hash_from_bytes::<ExpandMsgXmd<CS::Hash>>(msg, &dst).map_err(|_| Error::PointError) |
| 53 | + } |
| 54 | + |
| 55 | + // Implements the `HashToScalar()` function |
| 56 | + fn hash_to_scalar<CS: CipherSuite>(input: &[&[u8]], mode: Mode) -> Result<Self::Scalar> |
| 57 | + where |
| 58 | + <CS::Hash as OutputSizeUser>::OutputSize: |
| 59 | + IsLess<U256> + IsLessOrEqual<<CS::Hash as BlockSizeUser>::BlockSize>, |
| 60 | + { |
| 61 | + let dst = |
| 62 | + GenericArray::from(STR_HASH_TO_SCALAR).concat(voprf::get_context_string::<CS>(mode)); |
| 63 | + |
| 64 | + <Self as GroupDigest>::hash_to_scalar::<ExpandMsgXmd<CS::Hash>>(input, &dst) |
| 65 | + .map_err(|_| Error::PointError) |
| 66 | + } |
| 67 | + |
| 68 | + fn base_elem() -> Self::Elem { |
| 69 | + ProjectivePoint::<Self>::generator() |
| 70 | + } |
| 71 | + |
| 72 | + fn identity_elem() -> Self::Elem { |
| 73 | + ProjectivePoint::<Self>::identity() |
| 74 | + } |
| 75 | + |
| 76 | + fn serialize_elem(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen> { |
| 77 | + let point: AffinePoint<Self> = elem.into(); |
| 78 | + let bytes = point.to_encoded_point(true); |
| 79 | + let bytes = bytes.as_bytes(); |
| 80 | + let mut result = GenericArray::default(); |
| 81 | + result[..bytes.len()].copy_from_slice(bytes); |
| 82 | + result |
| 83 | + } |
| 84 | + |
| 85 | + fn deserialize_elem(element_bits: &GenericArray<u8, Self::ElemLen>) -> Result<Self::Elem> { |
| 86 | + PublicKey::<Self>::from_sec1_bytes(element_bits) |
| 87 | + .map(|public_key| public_key.to_projective()) |
| 88 | + .map_err(|_| Error::PointError) |
| 89 | + } |
| 90 | + |
| 91 | + fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar { |
| 92 | + *SecretKey::<Self>::random(rng).to_nonzero_scalar() |
| 93 | + } |
| 94 | + |
| 95 | + fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar { |
| 96 | + Option::from(scalar.invert()).unwrap() |
| 97 | + } |
| 98 | + |
| 99 | + #[cfg(test)] |
| 100 | + fn zero_scalar() -> Self::Scalar { |
| 101 | + Scalar::<Self>::zero() |
| 102 | + } |
| 103 | + |
| 104 | + fn serialize_scalar(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> { |
| 105 | + scalar.into() |
| 106 | + } |
| 107 | + |
| 108 | + fn deserialize_scalar(scalar_bits: &GenericArray<u8, Self::ScalarLen>) -> Result<Self::Scalar> { |
| 109 | + SecretKey::<Self>::from_be_bytes(scalar_bits) |
| 110 | + .map(|secret_key| *secret_key.to_nonzero_scalar()) |
| 111 | + .map_err(|_| Error::ScalarError) |
| 112 | + } |
| 113 | +} |
0 commit comments