18template <
typename Params>
19class Sm2p256v1Rep
final {
21 static constexpr auto P = Params::P;
22 static constexpr size_t N = Params::N;
23 typedef typename Params::W W;
25 constexpr static std::array<W, N> redc(
const std::array<W, 2 * N>& z) {
43 const int64_t S0 = X00 + X08 + X09 + X10 + X11 + X12 + 2 * (X13 + X14 + X15);
44 const int64_t S1 = X01 + X09 + X10 + X11 + X12 + X13 + 2 * (X14 + X15);
45 const int64_t S2 = X02 - (X08 + X09 + X13 + X14);
46 const int64_t S3 = X03 + X08 + X11 + X12 + 2 * X13 + X14 + X15;
47 const int64_t S4 = X04 + X09 + X12 + X13 + 2 * X14 + X15;
48 const int64_t S5 = X05 + X10 + X13 + X14 + 2 * X15;
49 const int64_t S6 = X06 + X11 + X14 + X15;
50 const int64_t S7 = X07 + X08 + X09 + X10 + X11 + 2 * (X12 + X13 + X14 + X15) + X15;
52 std::array<W, N> r = {};
66 const auto correction = sm2_mul_mod_256(S);
67 W borrow =
bigint_sub2(r.data(), N, correction.data(), N);
74 constexpr static std::array<W, N> one() {
return std::array<W, N>{1}; }
76 constexpr static std::array<W, N> to_rep(
const std::array<W, N>& x) {
return x; }
78 constexpr static std::array<W, N> wide_to_rep(
const std::array<W, 2 * N>& x) {
return redc(x); }
80 constexpr static std::array<W, N> from_rep(
const std::array<W, N>& z) {
return z; }
86 constexpr static std::array<W, N> sm2_mul_mod_256(W i) {
99 const uint64_t i32 =
static_cast<uint64_t
>(i) << 32;
111class Params
final :
public EllipticCurveParameters<
112 "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",
113 "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",
114 "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",
115 "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",
116 "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
117 "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"> {
122class Curve
final :
public EllipticCurve<Params, Sm2p256v1Rep> {
125 static FieldElement fe_invert2(
const FieldElement& x) {
134 auto t2 = t1.square();