|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// |
| 3 | +// This source code is dual-licensed under either the MIT license found in the |
| 4 | +// LICENSE-MIT file in the root directory of this source tree or the Apache |
| 5 | +// License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 6 | +// of this source tree. You may select, at your option, one of the above-listed |
| 7 | +// licenses. |
| 8 | + |
| 9 | +use ed448::{CompressedDecaf, DecafPoint, Scalar}; |
| 10 | +use elliptic_curve::bigint::{Encoding, NonZero, U448, U512}; |
| 11 | +use elliptic_curve::hash2curve::{ExpandMsg, ExpandMsgXof, Expander}; |
| 12 | +use hybrid_array::Array; |
| 13 | +use hybrid_array::typenum::U56; |
| 14 | +use rand_core::{TryCryptoRng, TryRngCore}; |
| 15 | +use subtle::ConstantTimeEq; |
| 16 | + |
| 17 | +use super::Group; |
| 18 | +use crate::{Error, InternalError, Result}; |
| 19 | + |
| 20 | +/// [`Group`] implementation for Decaf448. |
| 21 | +#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 22 | +pub struct Decaf448; |
| 23 | + |
| 24 | +const WIDE_ORDER: NonZero<U512> = NonZero::<U512>::new_unwrap(U512::from_be_hex("00000000000000003fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3")); |
| 25 | +const ORDER: NonZero<U448> = NonZero::<U448>::new_unwrap(ed448::ORDER); |
| 26 | + |
| 27 | +#[cfg(feature = "decaf448-ciphersuite")] |
| 28 | +impl crate::CipherSuite for Decaf448 { |
| 29 | + const ID: &'static str = "decaf448-SHAKE256"; |
| 30 | + |
| 31 | + type Group = Decaf448; |
| 32 | + |
| 33 | + type Hash = super::xof_fixed_wrapper::XofFixedWrapper<sha3::Shake256, hybrid_array::sizes::U64>; |
| 34 | + |
| 35 | + type ExpandMsg = ExpandMsgXof<Self::Hash>; |
| 36 | +} |
| 37 | + |
| 38 | +impl Group for Decaf448 { |
| 39 | + type Elem = DecafPoint; |
| 40 | + |
| 41 | + type ElemLen = U56; |
| 42 | + |
| 43 | + type Scalar = Scalar; |
| 44 | + |
| 45 | + type ScalarLen = U56; |
| 46 | + |
| 47 | + // Implements the `hash_to_ristretto255()` function from |
| 48 | + // https://www.rfc-editor.org/rfc/rfc9380.html#appendix-C |
| 49 | + fn hash_to_curve<X>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Elem, InternalError> |
| 50 | + where |
| 51 | + X: for<'a> ExpandMsg<'a>, |
| 52 | + { |
| 53 | + let mut uniform_bytes = [0; 112]; |
| 54 | + X::expand_message(input, dst, 112) |
| 55 | + .map_err(|_| InternalError::Input)? |
| 56 | + .fill_bytes(&mut uniform_bytes); |
| 57 | + |
| 58 | + Ok(DecafPoint::from_uniform_bytes(&uniform_bytes)) |
| 59 | + } |
| 60 | + |
| 61 | + // Implements the `HashToScalar()` function from |
| 62 | + // https://www.rfc-editor.org/rfc/rfc9497#section-4.2 |
| 63 | + fn hash_to_scalar<X>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Scalar, InternalError> |
| 64 | + where |
| 65 | + X: for<'a> ExpandMsg<'a>, |
| 66 | + { |
| 67 | + let mut uniform_bytes = [0; 64]; |
| 68 | + X::expand_message(input, dst, 64) |
| 69 | + .map_err(|_| InternalError::Input)? |
| 70 | + .fill_bytes(&mut uniform_bytes); |
| 71 | + let uniform_bytes = U512::from_le_slice(&uniform_bytes); |
| 72 | + |
| 73 | + let scalar = uniform_bytes.rem(&WIDE_ORDER); |
| 74 | + let scalar = Scalar::from_bytes(&scalar.to_le_bytes()[..56].try_into().unwrap()); |
| 75 | + |
| 76 | + Ok(scalar) |
| 77 | + } |
| 78 | + |
| 79 | + fn base_elem() -> Self::Elem { |
| 80 | + DecafPoint::GENERATOR |
| 81 | + } |
| 82 | + |
| 83 | + fn identity_elem() -> Self::Elem { |
| 84 | + DecafPoint::IDENTITY |
| 85 | + } |
| 86 | + |
| 87 | + // serialization of a group element |
| 88 | + fn serialize_elem(elem: Self::Elem) -> Array<u8, Self::ElemLen> { |
| 89 | + elem.compress().0.into() |
| 90 | + } |
| 91 | + |
| 92 | + fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem> { |
| 93 | + let result = element_bits |
| 94 | + .try_into() |
| 95 | + .map(CompressedDecaf) |
| 96 | + .map_err(|_| Error::Deserialization)? |
| 97 | + .decompress(); |
| 98 | + Option::from(result) |
| 99 | + .filter(|point| point != &DecafPoint::IDENTITY) |
| 100 | + .ok_or(Error::Deserialization) |
| 101 | + } |
| 102 | + |
| 103 | + fn random_scalar<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Result<Self::Scalar, R::Error> { |
| 104 | + loop { |
| 105 | + let mut scalar_bytes = [0; 64]; |
| 106 | + rng.try_fill_bytes(&mut scalar_bytes)?; |
| 107 | + let scalar_bytes = U512::from_le_slice(&scalar_bytes); |
| 108 | + let scalar = scalar_bytes.rem(&WIDE_ORDER); |
| 109 | + let scalar = Scalar::from_bytes(&scalar.to_le_bytes()[..56].try_into().unwrap()); |
| 110 | + |
| 111 | + if scalar != Scalar::ZERO { |
| 112 | + break Ok(scalar); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar { |
| 118 | + scalar.invert() |
| 119 | + } |
| 120 | + |
| 121 | + fn is_zero_scalar(scalar: Self::Scalar) -> subtle::Choice { |
| 122 | + scalar.ct_eq(&Scalar::ZERO) |
| 123 | + } |
| 124 | + |
| 125 | + #[cfg(test)] |
| 126 | + fn zero_scalar() -> Self::Scalar { |
| 127 | + Scalar::ZERO |
| 128 | + } |
| 129 | + |
| 130 | + fn serialize_scalar(scalar: Self::Scalar) -> Array<u8, Self::ScalarLen> { |
| 131 | + scalar.to_bytes().into() |
| 132 | + } |
| 133 | + |
| 134 | + fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar> { |
| 135 | + scalar_bits |
| 136 | + .try_into() |
| 137 | + .ok() |
| 138 | + .map(U448::from_le_bytes) |
| 139 | + .map(|value| Scalar::from_bytes(&value.rem(&ORDER).to_le_bytes())) |
| 140 | + .filter(|scalar| scalar != &Scalar::ZERO) |
| 141 | + .ok_or(Error::Deserialization) |
| 142 | + } |
| 143 | +} |
0 commit comments