pub mod cryptopro_a {
use elliptic_curve::bigint::{Odd, U256};
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct GostCryptoProA;
const MODULUS_HEX: &str = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97";
const ORDER_HEX: &str = "ffffffffffffffffffffffffffffffff6c611070995ad10045841b09b761b893";
const ORDER: Odd<U256> = Odd::<U256>::from_be_hex(ORDER_HEX);
#[allow(deprecated)]
mod field {
use super::MODULUS_HEX;
use elliptic_curve::bigint::U256;
use elliptic_curve::{
ff::PrimeField,
ops::BatchInvert,
subtle::{Choice, ConstantTimeEq, CtOption},
};
primefield::monty_field_params! {
name: FieldParams,
modulus: MODULUS_HEX,
uint: U256,
byte_order: primefield::ByteOrder::BigEndian,
multiplicative_generator: 7,
doc: "Montgomery parameters for this curve's field modulus"
}
primefield::monty_field_element! {
name: FieldElement,
params: FieldParams,
uint: U256,
doc: "Element of the base field"
}
primefield::monty_field_arithmetic! {
name: FieldElement,
params: FieldParams,
uint: U256
}
impl BatchInvert for FieldElement {}
#[cfg(test)]
mod tests {
use super::{FieldElement, U256};
primefield::test_primefield!(FieldElement, U256);
}
}
#[allow(deprecated)]
mod scalar {
use super::{GostCryptoProA, ORDER, ORDER_HEX};
use elliptic_curve::bigint::U256;
use elliptic_curve::{
ff::PrimeField,
ops::BatchInvert,
scalar::{FromUintUnchecked, IsHigh},
subtle::{Choice, ConstantTimeEq, ConstantTimeGreater as _, CtOption},
};
primefield::monty_field_params! {
name: ScalarParams,
modulus: ORDER_HEX,
uint: U256,
byte_order: primefield::ByteOrder::BigEndian,
multiplicative_generator: 2,
doc: "Montgomery parameters for this curve's scalar modulus"
}
primefield::monty_field_element! {
name: Scalar,
params: ScalarParams,
uint: U256,
doc: "Element of the scalar field"
}
primefield::monty_field_arithmetic! {
name: Scalar,
params: ScalarParams,
uint: U256
}
primefield::monty_field_reduce! {
name: Scalar,
params: ScalarParams,
uint: U256,
}
elliptic_curve::scalar_impls!(GostCryptoProA, Scalar);
primeorder::wnaf::impl_wnaf_size_for_scalar!(Scalar);
impl BatchInvert for Scalar {}
impl AsRef<Scalar> for Scalar {
fn as_ref(&self) -> &Scalar {
self
}
}
impl FromUintUnchecked for Scalar {
type Uint = U256;
fn from_uint_unchecked(uint: Self::Uint) -> Self {
Self::from_uint_unchecked(uint)
}
}
impl IsHigh for Scalar {
fn is_high(&self) -> Choice {
const MODULUS_SHR1: U256 = ORDER.as_ref().shr_vartime(1);
self.to_canonical().ct_gt(&MODULUS_SHR1)
}
}
#[cfg(test)]
mod tests {
use super::{Scalar, U256};
primefield::test_primefield!(Scalar, U256);
}
}
pub use self::{field::FieldElement, scalar::Scalar};
impl elliptic_curve::Curve for GostCryptoProA {
type FieldBytesSize = elliptic_curve::consts::U32;
type Uint = U256;
const ORDER: Odd<U256> = ORDER;
}
impl elliptic_curve::PrimeCurve for GostCryptoProA {}
impl elliptic_curve::point::PointCompression for GostCryptoProA {
const COMPRESS_POINTS: bool = false;
}
pub type AffinePoint = primeorder::AffinePoint<GostCryptoProA>;
pub type ProjectivePoint = primeorder::ProjectivePoint<GostCryptoProA>;
impl elliptic_curve::CurveArithmetic for GostCryptoProA {
type AffinePoint = AffinePoint;
type ProjectivePoint = ProjectivePoint;
type Scalar = Scalar;
}
impl elliptic_curve::hazmat::FieldArithmetic for GostCryptoProA {
type FieldElement = FieldElement;
}
impl elliptic_curve::PrimeCurveArithmetic for GostCryptoProA {
type CurveGroup = ProjectivePoint;
}
impl primeorder::PrimeCurveParams for GostCryptoProA {
type PointArithmetic = primeorder::point_arithmetic::EquationAIsGeneric;
type Backend = primeorder::mul_backend::VariableOnly;
const EQUATION_A: FieldElement = FieldElement::from_hex_vartime(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd94",
);
const EQUATION_B: FieldElement = FieldElement::from_hex_vartime(
"00000000000000000000000000000000000000000000000000000000000000a6",
);
const GENERATOR: (FieldElement, FieldElement) = (
FieldElement::from_hex_vartime(
"0000000000000000000000000000000000000000000000000000000000000001",
),
FieldElement::from_hex_vartime(
"8d91e471e0989cda27df505a453f2b7635294f2ddf23e3b122acc99c9e9f1e14",
),
);
}
}
pub mod test_param_set {
use elliptic_curve::bigint::{Odd, U256};
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct GostTestParamSet;
const MODULUS_HEX: &str = "8000000000000000000000000000000000000000000000000000000000000431";
const ORDER_HEX: &str = "8000000000000000000000000000000150fe8a1892976154c59cfc193accf5b3";
const ORDER: Odd<U256> = Odd::<U256>::from_be_hex(ORDER_HEX);
#[allow(deprecated)]
mod field {
use super::MODULUS_HEX;
use elliptic_curve::bigint::U256;
use elliptic_curve::{
ff::PrimeField,
ops::BatchInvert,
subtle::{Choice, ConstantTimeEq, CtOption},
};
primefield::monty_field_params! {
name: FieldParams,
modulus: MODULUS_HEX,
uint: U256,
byte_order: primefield::ByteOrder::BigEndian,
multiplicative_generator: 7,
doc: "Montgomery parameters for this curve's field modulus"
}
primefield::monty_field_element! {
name: FieldElement,
params: FieldParams,
uint: U256,
doc: "Element of the base field"
}
primefield::monty_field_arithmetic! {
name: FieldElement,
params: FieldParams,
uint: U256
}
impl BatchInvert for FieldElement {}
#[cfg(test)]
mod tests {
use super::{FieldElement, U256};
primefield::test_primefield!(FieldElement, U256);
}
}
#[allow(deprecated)]
mod scalar {
use super::{GostTestParamSet, ORDER, ORDER_HEX};
use elliptic_curve::bigint::U256;
use elliptic_curve::{
ff::PrimeField,
ops::BatchInvert,
scalar::{FromUintUnchecked, IsHigh},
subtle::{Choice, ConstantTimeEq, ConstantTimeGreater as _, CtOption},
};
primefield::monty_field_params! {
name: ScalarParams,
modulus: ORDER_HEX,
uint: U256,
byte_order: primefield::ByteOrder::BigEndian,
multiplicative_generator: 2,
doc: "Montgomery parameters for this curve's scalar modulus"
}
primefield::monty_field_element! {
name: Scalar,
params: ScalarParams,
uint: U256,
doc: "Element of the scalar field"
}
primefield::monty_field_arithmetic! {
name: Scalar,
params: ScalarParams,
uint: U256
}
primefield::monty_field_reduce! {
name: Scalar,
params: ScalarParams,
uint: U256,
}
elliptic_curve::scalar_impls!(GostTestParamSet, Scalar);
primeorder::wnaf::impl_wnaf_size_for_scalar!(Scalar);
impl BatchInvert for Scalar {}
impl AsRef<Scalar> for Scalar {
fn as_ref(&self) -> &Scalar {
self
}
}
impl FromUintUnchecked for Scalar {
type Uint = U256;
fn from_uint_unchecked(uint: Self::Uint) -> Self {
Self::from_uint_unchecked(uint)
}
}
impl IsHigh for Scalar {
fn is_high(&self) -> Choice {
const MODULUS_SHR1: U256 = ORDER.as_ref().shr_vartime(1);
self.to_canonical().ct_gt(&MODULUS_SHR1)
}
}
#[cfg(test)]
mod tests {
use super::{Scalar, U256};
primefield::test_primefield!(Scalar, U256);
}
}
pub use self::{field::FieldElement, scalar::Scalar};
impl elliptic_curve::Curve for GostTestParamSet {
type FieldBytesSize = elliptic_curve::consts::U32;
type Uint = U256;
const ORDER: Odd<U256> = ORDER;
}
impl elliptic_curve::PrimeCurve for GostTestParamSet {}
impl elliptic_curve::point::PointCompression for GostTestParamSet {
const COMPRESS_POINTS: bool = false;
}
pub type AffinePoint = primeorder::AffinePoint<GostTestParamSet>;
pub type ProjectivePoint = primeorder::ProjectivePoint<GostTestParamSet>;
impl elliptic_curve::CurveArithmetic for GostTestParamSet {
type AffinePoint = AffinePoint;
type ProjectivePoint = ProjectivePoint;
type Scalar = Scalar;
}
impl elliptic_curve::hazmat::FieldArithmetic for GostTestParamSet {
type FieldElement = FieldElement;
}
impl elliptic_curve::PrimeCurveArithmetic for GostTestParamSet {
type CurveGroup = ProjectivePoint;
}
impl primeorder::PrimeCurveParams for GostTestParamSet {
type PointArithmetic = primeorder::point_arithmetic::EquationAIsGeneric;
type Backend = primeorder::mul_backend::VariableOnly;
const EQUATION_A: FieldElement = FieldElement::from_hex_vartime(
"0000000000000000000000000000000000000000000000000000000000000007",
);
const EQUATION_B: FieldElement = FieldElement::from_hex_vartime(
"5fbff498aa938ce739b8e022fbafef40563f6e6a3472fc2a514c0ce9dae23b7e",
);
const GENERATOR: (FieldElement, FieldElement) = (
FieldElement::from_hex_vartime(
"0000000000000000000000000000000000000000000000000000000000000002",
),
FieldElement::from_hex_vartime(
"08e2a8a0e65147d4bd6316030e16d19c85c97f0a9ca267122b96abbcea7e8fc8",
),
);
}
}
macro_rules! gost_verify_for_curve {
($fn_name:ident, $curve:ty, $scalar:ty) => {
pub fn $fn_name(
qx_be: &[u8; 32],
qy_be: &[u8; 32],
r_be: &[u8; 32],
s_be: &[u8; 32],
e_be: &[u8; 32],
) -> bool {
use elliptic_curve::{
PrimeField as _, bigint::U256, point::AffineCoordinates as _,
sec1::FromSec1Point as _,
};
let r = <$scalar>::from_repr((*r_be).into());
let s = <$scalar>::from_repr((*s_be).into());
let (Some(r), Some(s)) = (Option::<$scalar>::from(r), Option::<$scalar>::from(s))
else {
return false;
};
if bool::from(r.is_zero()) || bool::from(s.is_zero()) {
return false;
}
let e = <$scalar as elliptic_curve::bigint::Reduce<U256>>::reduce(
&U256::from_be_slice(e_be),
);
let e = if bool::from(e.is_zero()) {
<$scalar>::ONE
} else {
e
};
let Some(v) = Option::<$scalar>::from(e.invert()) else {
return false;
};
let z1 = s * v;
let z2 = -(r * v);
let mut sec1 = [0u8; 65];
sec1[0] = 0x04;
sec1[1..33].copy_from_slice(qx_be);
sec1[33..65].copy_from_slice(qy_be);
let Ok(point) = elliptic_curve::sec1::Sec1Point::<$curve>::from_bytes(&sec1) else {
return false;
};
let q_affine = Option::<primeorder::AffinePoint<$curve>>::from(
primeorder::AffinePoint::<$curve>::from_sec1_point(&point),
);
let Some(q_affine) = q_affine else {
return false;
};
let generator = primeorder::ProjectivePoint::<$curve>::GENERATOR;
let c = generator * z1 + primeorder::ProjectivePoint::<$curve>::from(q_affine) * z2;
if bool::from(elliptic_curve::group::Group::is_identity(&c)) {
return false;
}
let x_c = c.to_affine().x();
let big_r = <$scalar as elliptic_curve::bigint::Reduce<U256>>::reduce(
&U256::from_be_slice(&x_c),
);
big_r == r
}
};
}
gost_verify_for_curve!(
verify_cryptopro_a,
cryptopro_a::GostCryptoProA,
cryptopro_a::Scalar
);
gost_verify_for_curve!(
verify_test_param_set,
test_param_set::GostTestParamSet,
test_param_set::Scalar
);