|
| 1 | +#[cfg(not(feature = "std"))] |
| 2 | +extern crate alloc; |
| 3 | +#[cfg(not(feature = "std"))] |
| 4 | +use alloc::{boxed::Box, vec::Vec}; |
| 5 | + |
| 6 | +use core::{ |
| 7 | + cmp, |
| 8 | + fmt::Debug, |
| 9 | + iter::Sum, |
| 10 | + ops::{Add, Mul, Neg, Sub}, |
| 11 | +}; |
| 12 | + |
| 13 | +use rand_core::RngCore; |
| 14 | +use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; |
| 15 | + |
| 16 | +use crate::{ |
| 17 | + ff::{Field, PrimeField, WithSmallOrderMulGroup}, |
| 18 | + group::{prime::PrimeCurveAffine, Curve, Group as _, GroupEncoding}, |
| 19 | + t256::{Fp, Fq}, |
| 20 | + Coordinates, CurveAffine, CurveExt, |
| 21 | +}; |
| 22 | + |
| 23 | +impl group::cofactor::CofactorGroup for T256 { |
| 24 | + type Subgroup = T256; |
| 25 | + |
| 26 | + fn clear_cofactor(&self) -> Self { |
| 27 | + *self |
| 28 | + } |
| 29 | + |
| 30 | + fn into_subgroup(self) -> CtOption<Self::Subgroup> { |
| 31 | + CtOption::new(self, 1.into()) |
| 32 | + } |
| 33 | + |
| 34 | + fn is_torsion_free(&self) -> Choice { |
| 35 | + 1.into() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// We take these parameters from: https://github.com/pag-crypto/sigpop/blob/eb7ea5914e8303b5484fe5bb8d200d08345a46c6/circ_fields/src/t256/curves/mod.rs |
| 40 | +// Generator (x = 5, y as in sigpop) |
| 41 | +const T256_GENERATOR_X: Fp = Fp::from_raw([ |
| 42 | + 0x0000000000000005, |
| 43 | + 0x0000000000000000, |
| 44 | + 0x0000000000000000, |
| 45 | + 0x0000000000000000, |
| 46 | +]); |
| 47 | + |
| 48 | +const T256_GENERATOR_Y: Fp = Fp::from_raw([ |
| 49 | + 0x5826108A653DE28D, |
| 50 | + 0x0ED60B9E33CE397C, |
| 51 | + 0x5EFC7B55F6B24FBE, |
| 52 | + 0x3E86C0CFEBF2C716, |
| 53 | +]); |
| 54 | + |
| 55 | +// a = p − 3 |
| 56 | +const T256_A: Fp = Fp::from_raw([ |
| 57 | + 0x93135661B1C4B114, |
| 58 | + 0x7E72B42B30E73177, |
| 59 | + 0x0000000000000001, |
| 60 | + 0xFFFFFFFF00000001, |
| 61 | +]); |
| 62 | + |
| 63 | +// b = 0xB441071B12F4A0366FB552F8E21ED4AC36B06ACEEB354224863E60F20219FC56 |
| 64 | +const T256_B: Fp = Fp::from_raw([ |
| 65 | + 0x863E60F20219FC56, |
| 66 | + 0x36B06ACEEB354224, |
| 67 | + 0x6FB552F8E21ED4AC, |
| 68 | + 0xB441071B12F4A036, |
| 69 | +]); |
| 70 | + |
| 71 | +use crate::{ |
| 72 | + impl_binops_additive, impl_binops_additive_specify_output, impl_binops_multiplicative, |
| 73 | + impl_binops_multiplicative_mixed, new_curve_impl, |
| 74 | +}; |
| 75 | + |
| 76 | +new_curve_impl!( |
| 77 | + (pub), |
| 78 | + T256, |
| 79 | + T256Affine, |
| 80 | + Fp, |
| 81 | + Fq, |
| 82 | + (T256_GENERATOR_X,T256_GENERATOR_Y), |
| 83 | + T256_A, |
| 84 | + T256_B, |
| 85 | + "t256", |
| 86 | + |domain_prefix| hash_to_curve(domain_prefix, hash_to_curve_suite(b"T256_XMD:SHA-256_SSWU_RO_")), |
| 87 | + crate::serde::CompressedFlagConfig::Extra, |
| 88 | + standard_sign |
| 89 | +); |
| 90 | + |
| 91 | +fn hash_to_curve_suite(domain: &[u8]) -> crate::hash_to_curve::Suite<T256, sha2::Sha256, 48> { |
| 92 | + // Z : <https://datatracker.ietf.org/doc/html/rfc9380#sswu-z-code> |
| 93 | + // Z = -2 |
| 94 | + const SSWU_Z: Fp = Fp::from_raw([ |
| 95 | + 0x93135661B1C4B115, |
| 96 | + 0x7E72B42B30E73177, |
| 97 | + 0x0000000000000001, |
| 98 | + 0xFFFFFFFF00000001, |
| 99 | + ]); |
| 100 | + |
| 101 | + let iso_map = crate::hash_to_curve::Iso { |
| 102 | + a: T256::a(), |
| 103 | + b: T256::b(), |
| 104 | + map: Box::new(move |x, y, z| T256 { x, y, z }), |
| 105 | + }; |
| 106 | + |
| 107 | + crate::hash_to_curve::Suite::new(domain, SSWU_Z, crate::hash_to_curve::Method::SSWU(iso_map)) |
| 108 | +} |
| 109 | + |
| 110 | +#[allow(clippy::type_complexity)] |
| 111 | +pub(crate) fn hash_to_curve<'a>( |
| 112 | + domain_prefix: &'a str, |
| 113 | + suite: crate::hash_to_curve::Suite<T256, sha2::Sha256, 48>, |
| 114 | +) -> Box<dyn Fn(&[u8]) -> T256 + 'a> { |
| 115 | + Box::new(move |message| suite.hash_to_curve(domain_prefix, message)) |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod test { |
| 120 | + use group::UncompressedEncoding; |
| 121 | + use rand_core::OsRng; |
| 122 | + |
| 123 | + use super::*; |
| 124 | + use crate::serde::SerdeObject; |
| 125 | + crate::curve_testing_suite!(T256); |
| 126 | + crate::curve_testing_suite!(T256, "ecdsa_example"); |
| 127 | + crate::curve_testing_suite!( |
| 128 | + T256, |
| 129 | + "constants", |
| 130 | + Fp::MODULUS, |
| 131 | + T256_A, |
| 132 | + T256_B, |
| 133 | + T256_GENERATOR_X, |
| 134 | + T256_GENERATOR_Y, |
| 135 | + Fq::MODULUS |
| 136 | + ); |
| 137 | +} |
0 commit comments