P256_LIMBS
void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS]);
void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS],
if (P256_LIMBS == 8)
const BN_ULONG b[P256_LIMBS]);
if (P256_LIMBS == 8) {
void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS],
const BN_ULONG b[P256_LIMBS]);
if (!bn_set_words(r->X, p.p.X, P256_LIMBS) || !bn_set_words(r->Y, p.p.Y, P256_LIMBS) || !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {
void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS]);
BN_ULONG z_inv2[P256_LIMBS];
BN_ULONG z_inv3[P256_LIMBS];
BN_ULONG x_aff[P256_LIMBS];
BN_ULONG y_aff[P256_LIMBS];
BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
if (!bn_set_words(x, x_ret, P256_LIMBS))
const BN_ULONG in[P256_LIMBS]);
if (!bn_set_words(y, y_ret, P256_LIMBS))
void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG in[P256_LIMBS]);
void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS],
const BN_ULONG b[P256_LIMBS]);
void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS],
static const BN_ULONG RR[P256_LIMBS] = {
static const BN_ULONG one[P256_LIMBS] = {
BN_ULONG table[15][P256_LIMBS];
BN_ULONG out[P256_LIMBS], t[P256_LIMBS];
if (bn_wexpand(r, P256_LIMBS) == NULL) {
static const BN_ULONG ONE[P256_LIMBS] = {
if (!bn_set_words(r, out, P256_LIMBS))
static void copy_conditional(BN_ULONG dst[P256_LIMBS],
const BN_ULONG src[P256_LIMBS], BN_ULONG move)
if (P256_LIMBS == 8) {
static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
const BN_ULONG b[P256_LIMBS])
if (P256_LIMBS == 8) {
if (bn_get_top(z) == (P256_LIMBS - P256_LIMBS / 8)) {
if (P256_LIMBS == 8) {
BN_ULONG S[P256_LIMBS];
BN_ULONG M[P256_LIMBS];
BN_ULONG Zsqr[P256_LIMBS];
BN_ULONG tmp0[P256_LIMBS];
BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
BN_ULONG Z1sqr[P256_LIMBS];
BN_ULONG Z2sqr[P256_LIMBS];
BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
BN_ULONG Hsqr[P256_LIMBS];
BN_ULONG Rsqr[P256_LIMBS];
BN_ULONG Hcub[P256_LIMBS];
BN_ULONG res_x[P256_LIMBS];
BN_ULONG res_y[P256_LIMBS];
BN_ULONG res_z[P256_LIMBS];
if (P256_LIMBS == 8)
if (P256_LIMBS == 8)
BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
BN_ULONG Z1sqr[P256_LIMBS];
BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
BN_ULONG Hsqr[P256_LIMBS];
BN_ULONG Rsqr[P256_LIMBS];
BN_ULONG Hcub[P256_LIMBS];
BN_ULONG res_x[P256_LIMBS];
BN_ULONG res_y[P256_LIMBS];
BN_ULONG res_z[P256_LIMBS];
BN_ULONG X[P256_LIMBS];
if (P256_LIMBS == 8)
if (P256_LIMBS == 8)
BN_ULONG Y[P256_LIMBS];
BN_ULONG Z[P256_LIMBS];
static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
const BN_ULONG in[P256_LIMBS])
BN_ULONG X[P256_LIMBS];
BN_ULONG p2[P256_LIMBS];
BN_ULONG p4[P256_LIMBS];
BN_ULONG p8[P256_LIMBS];
BN_ULONG p16[P256_LIMBS];
BN_ULONG p32[P256_LIMBS];
BN_ULONG Y[P256_LIMBS];
BN_ULONG res[P256_LIMBS];
__owur static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
return bn_copy_words(out, in, P256_LIMBS);
static const BN_ULONG def_xG[P256_LIMBS] = {
static const BN_ULONG def_yG[P256_LIMBS] = {
return (bn_get_top(generator->X) == P256_LIMBS) && (bn_get_top(generator->Y) == P256_LIMBS) && is_equal(bn_get_words(generator->X), def_xG) && is_equal(bn_get_words(generator->Y), def_yG) && is_one(generator->Z);
void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS],
const BN_ULONG b[P256_LIMBS]);
if ((ret = bn_set_words(out->X, in->X, P256_LIMBS))
&& (ret = bn_set_words(out->Y, in->Y, P256_LIMBS))
&& (ret = bn_set_words(out->Z, ONE, P256_LIMBS)))
void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS]);
void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
const BN_ULONG a[P256_LIMBS]);
for (i = P256_LIMBS - 1; i >= 0; --i) {
bn_copy_words(out, in, P256_LIMBS)
ALIGN32 BN_ULONG tmp0[P256_LIMBS];
ALIGN32 BN_ULONG tmp1[P256_LIMBS];
ALIGN32 BN_ULONG tmp2[P256_LIMBS];
for (i = 0; i < P256_LIMBS; ++i)
ALIGN32 BN_ULONG tmp0[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG tmp1[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG tmp2[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG tmp3[P256_LIMBS] = { 0 };
for (i = 0; i < P256_LIMBS; ++i) {
for (i = 0; i < P256_LIMBS; ++i) {
for (i = 0; i < P256_LIMBS; ++i)
ALIGN32 BN_ULONG tmp0[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG tmp1[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG tmp2[P256_LIMBS] = { 0 };
for (i = 0; i < P256_LIMBS; ++i) {
for (i = 0; i < P256_LIMBS; ++i) {
BN_ULONG X[P256_LIMBS];
BN_ULONG Y[P256_LIMBS];
BN_ULONG Z[P256_LIMBS];
memcpy(R->Y, ecp_sm2p256_precomputed + index + P256_LIMBS, 32);
memcpy(Q.Y, ecp_sm2p256_precomputed + index + P256_LIMBS, 32);
BN_ULONG X[P256_LIMBS];
BN_ULONG Y[P256_LIMBS];
ALIGN32 BN_ULONG z_inv3[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG z_inv2[P256_LIMBS] = { 0 };
ALIGN32 static const BN_ULONG def_xG[P256_LIMBS] = {
return (bn_get_top(generator->X) == P256_LIMBS)
&& (bn_get_top(generator->Y) == P256_LIMBS)
ALIGN32 BN_ULONG k[P256_LIMBS] = { 0 };
ALIGN32 static const BN_ULONG def_yG[P256_LIMBS] = {
ALIGN32 BN_ULONG k[P256_LIMBS] = { 0 };
ALIGN32 static const BN_ULONG def_p[P256_LIMBS] = {
if (!bn_set_words(r->X, p.p.X, P256_LIMBS)
|| !bn_set_words(r->Y, p.p.Y, P256_LIMBS)
|| !bn_set_words(r->Z, p.p.Z, P256_LIMBS))
ALIGN32 BN_ULONG a_fe[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG b_fe[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG r_fe[P256_LIMBS] = { 0 };
if (!bn_set_words(r, r_fe, P256_LIMBS))
ALIGN32 BN_ULONG a_fe[P256_LIMBS] = { 0 };
ALIGN32 BN_ULONG r_fe[P256_LIMBS] = { 0 };
ALIGN32 static const BN_ULONG ONE[P256_LIMBS] = { 1, 0, 0, 0 };
if (!bn_set_words(r, r_fe, P256_LIMBS))