Files
voprf-vx/src/group/elliptic_curve.rs
T

109 lines
3.7 KiB
Rust
Raw Normal View History

2023-05-22 23:04:39 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2022-01-21 22:52:09 +01:00
//
2023-05-22 23:04:39 -07:00
// This source code is dual-licensed under either the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree or the Apache
2022-01-21 22:52:09 +01:00
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
2023-05-22 23:04:39 -07:00
// of this source tree. You may select, at your option, one of the above-listed
// licenses.
2022-01-21 22:52:09 +01:00
use digest::core_api::BlockSizeUser;
2023-02-08 09:18:04 +01:00
use digest::{FixedOutput, HashMarker};
2022-01-21 22:52:09 +01:00
use elliptic_curve::group::cofactor::CofactorGroup;
use elliptic_curve::hash2curve::{ExpandMsgXmd, FromOkm, GroupDigest};
use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint};
use elliptic_curve::{
2023-02-08 09:18:04 +01:00
AffinePoint, Field, FieldBytesSize, Group as _, ProjectivePoint, PublicKey, Scalar, SecretKey,
2022-01-21 22:52:09 +01:00
};
use generic_array::typenum::{IsLess, IsLessOrEqual, U256};
use generic_array::GenericArray;
use rand_core::{CryptoRng, RngCore};
use super::Group;
2022-04-01 21:18:32 +02:00
use crate::{Error, InternalError, Result};
2022-01-21 22:52:09 +01:00
impl<C> Group for C
where
C: GroupDigest,
ProjectivePoint<Self>: CofactorGroup + ToEncodedPoint<Self>,
2023-02-08 09:18:04 +01:00
FieldBytesSize<Self>: ModulusSize,
2022-01-21 22:52:09 +01:00
AffinePoint<Self>: FromEncodedPoint<Self> + ToEncodedPoint<Self>,
Scalar<Self>: FromOkm,
{
type Elem = ProjectivePoint<Self>;
2023-02-08 09:18:04 +01:00
type ElemLen = <FieldBytesSize<Self> as ModulusSize>::CompressedPointSize;
2022-01-21 22:52:09 +01:00
type Scalar = Scalar<Self>;
2023-02-08 09:18:04 +01:00
type ScalarLen = FieldBytesSize<Self>;
2022-01-21 22:52:09 +01:00
// Implements the `hash_to_curve()` function from
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-3
2023-02-08 09:18:04 +01:00
fn hash_to_curve<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Elem, InternalError>
2022-01-21 22:52:09 +01:00
where
2023-02-08 09:18:04 +01:00
H: BlockSizeUser + Default + FixedOutput + HashMarker,
2022-04-01 21:18:32 +02:00
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>,
2022-01-21 22:52:09 +01:00
{
2022-04-01 21:18:32 +02:00
Self::hash_from_bytes::<ExpandMsgXmd<H>>(input, dst).map_err(|_| InternalError::Input)
2022-01-21 22:52:09 +01:00
}
// Implements the `HashToScalar()` function
2023-02-08 09:18:04 +01:00
fn hash_to_scalar<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Scalar, InternalError>
2022-01-21 22:52:09 +01:00
where
2023-02-08 09:18:04 +01:00
H: BlockSizeUser + Default + FixedOutput + HashMarker,
2022-04-01 21:18:32 +02:00
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>,
2022-01-21 22:52:09 +01:00
{
2022-04-01 21:18:32 +02:00
<Self as GroupDigest>::hash_to_scalar::<ExpandMsgXmd<H>>(input, dst)
2022-01-25 05:55:02 +01:00
.map_err(|_| InternalError::Input)
2022-01-21 22:52:09 +01:00
}
fn base_elem() -> Self::Elem {
ProjectivePoint::<Self>::generator()
}
fn identity_elem() -> Self::Elem {
ProjectivePoint::<Self>::identity()
}
fn serialize_elem(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen> {
let bytes = elem.to_encoded_point(true);
2022-01-21 22:52:09 +01:00
let bytes = bytes.as_bytes();
let mut result = GenericArray::default();
result[..bytes.len()].copy_from_slice(bytes);
result
}
2022-01-28 01:38:17 +01:00
fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem> {
2022-01-21 22:52:09 +01:00
PublicKey::<Self>::from_sec1_bytes(element_bits)
.map(|public_key| public_key.to_projective())
2022-01-25 05:55:02 +01:00
.map_err(|_| Error::Deserialization)
2022-01-21 22:52:09 +01:00
}
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
*SecretKey::<Self>::random(rng).to_nonzero_scalar()
}
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar {
Option::from(scalar.invert()).unwrap()
}
2022-01-28 01:38:17 +01:00
fn is_zero_scalar(scalar: Self::Scalar) -> subtle::Choice {
scalar.is_zero()
}
2022-01-21 22:52:09 +01:00
#[cfg(test)]
fn zero_scalar() -> Self::Scalar {
2023-02-08 09:18:04 +01:00
Scalar::<Self>::ZERO
2022-01-21 22:52:09 +01:00
}
fn serialize_scalar(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> {
scalar.into()
}
2022-01-28 01:38:17 +01:00
fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar> {
2023-02-08 09:18:04 +01:00
SecretKey::<Self>::from_slice(scalar_bits)
2022-01-21 22:52:09 +01:00
.map(|secret_key| *secret_key.to_nonzero_scalar())
2022-01-25 05:55:02 +01:00
.map_err(|_| Error::Deserialization)
2022-01-21 22:52:09 +01:00
}
}