Path: blob/main/contrib/bearssl/src/ec/ec_p256_m62.c
39507 views
/*1* Copyright (c) 2018 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526#if BR_INT128 || BR_UMUL1282728#if BR_UMUL12829#include <intrin.h>30#endif3132static const unsigned char P256_G[] = {330x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,340xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,350x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,360x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,370x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,380xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,390x68, 0x37, 0xBF, 0x51, 0xF540};4142static const unsigned char P256_N[] = {430xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,440xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,450xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,460x25, 0x5147};4849static const unsigned char *50api_generator(int curve, size_t *len)51{52(void)curve;53*len = sizeof P256_G;54return P256_G;55}5657static const unsigned char *58api_order(int curve, size_t *len)59{60(void)curve;61*len = sizeof P256_N;62return P256_N;63}6465static size_t66api_xoff(int curve, size_t *len)67{68(void)curve;69*len = 32;70return 1;71}7273/*74* A field element is encoded as five 64-bit integers, in basis 2^52.75* Limbs may occasionally exceed 2^52.76*77* A _partially reduced_ value is such that the following hold:78* - top limb is less than 2^48 + 2^3079* - the other limbs fit on 53 bits each80* In particular, such a value is less than twice the modulus p.81*/8283#define BIT(n) ((uint64_t)1 << (n))84#define MASK48 (BIT(48) - BIT(0))85#define MASK52 (BIT(52) - BIT(0))8687/* R = 2^260 mod p */88static const uint64_t F256_R[] = {890x0000000000010, 0xF000000000000, 0xFFFFFFFFFFFFF,900xFFEFFFFFFFFFF, 0x00000000FFFFF91};9293/* Curve equation is y^2 = x^3 - 3*x + B. This constant is B*R mod p94(Montgomery representation of B). */95static const uint64_t P256_B_MONTY[] = {960xDF6229C4BDDFD, 0xCA8843090D89C, 0x212ED6ACF005C,970x83415A220ABF7, 0x0C30061DD487498};99100/*101* Addition in the field. Carry propagation is not performed.102* On input, limbs may be up to 63 bits each; on output, they will103* be up to one bit more than on input.104*/105static inline void106f256_add(uint64_t *d, const uint64_t *a, const uint64_t *b)107{108d[0] = a[0] + b[0];109d[1] = a[1] + b[1];110d[2] = a[2] + b[2];111d[3] = a[3] + b[3];112d[4] = a[4] + b[4];113}114115/*116* Partially reduce the provided value.117* Input: limbs can go up to 61 bits each.118* Output: partially reduced.119*/120static inline void121f256_partial_reduce(uint64_t *a)122{123uint64_t w, cc, s;124125/*126* Propagate carries.127*/128w = a[0];129a[0] = w & MASK52;130cc = w >> 52;131w = a[1] + cc;132a[1] = w & MASK52;133cc = w >> 52;134w = a[2] + cc;135a[2] = w & MASK52;136cc = w >> 52;137w = a[3] + cc;138a[3] = w & MASK52;139cc = w >> 52;140a[4] += cc;141142s = a[4] >> 48; /* s < 2^14 */143a[0] += s; /* a[0] < 2^52 + 2^14 */144w = a[1] - (s << 44);145a[1] = w & MASK52; /* a[1] < 2^52 */146cc = -(w >> 52) & 0xFFF; /* cc < 16 */147w = a[2] - cc;148a[2] = w & MASK52; /* a[2] < 2^52 */149cc = w >> 63; /* cc = 0 or 1 */150w = a[3] - cc - (s << 36);151a[3] = w & MASK52; /* a[3] < 2^52 */152cc = w >> 63; /* cc = 0 or 1 */153w = a[4] & MASK48;154a[4] = w + (s << 16) - cc; /* a[4] < 2^48 + 2^30 */155}156157/*158* Subtraction in the field.159* Input: limbs must fit on 60 bits each; in particular, the complete160* integer will be less than 2^268 + 2^217.161* Output: partially reduced.162*/163static inline void164f256_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)165{166uint64_t t[5], w, s, cc;167168/*169* We compute d = 2^13*p + a - b; this ensures a positive170* intermediate value.171*172* Each individual addition/subtraction may yield a positive or173* negative result; thus, we need to handle a signed carry, thus174* with sign extension. We prefer not to use signed types (int64_t)175* because conversion from unsigned to signed is cumbersome (a176* direct cast with the top bit set is undefined behavior; instead,177* we have to use pointer aliasing, using the guaranteed properties178* of exact-width types, but this requires the compiler to optimize179* away the writes and reads from RAM), and right-shifting a180* signed negative value is implementation-defined. Therefore,181* we use a custom sign extension.182*/183184w = a[0] - b[0] - BIT(13);185t[0] = w & MASK52;186cc = w >> 52;187cc |= -(cc & BIT(11));188w = a[1] - b[1] + cc;189t[1] = w & MASK52;190cc = w >> 52;191cc |= -(cc & BIT(11));192w = a[2] - b[2] + cc;193t[2] = (w & MASK52) + BIT(5);194cc = w >> 52;195cc |= -(cc & BIT(11));196w = a[3] - b[3] + cc;197t[3] = (w & MASK52) + BIT(49);198cc = w >> 52;199cc |= -(cc & BIT(11));200t[4] = (BIT(61) - BIT(29)) + a[4] - b[4] + cc;201202/*203* Perform partial reduction. Rule is:204* 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p205*206* At that point:207* 0 <= t[0] <= 2^52 - 1208* 0 <= t[1] <= 2^52 - 1209* 2^5 <= t[2] <= 2^52 + 2^5 - 1210* 2^49 <= t[3] <= 2^52 + 2^49 - 1211* 2^59 < t[4] <= 2^61 + 2^60 - 2^29212*213* Thus, the value 's' (t[4] / 2^48) will be necessarily214* greater than 2048, and less than 12288.215*/216s = t[4] >> 48;217218d[0] = t[0] + s; /* d[0] <= 2^52 + 12287 */219w = t[1] - (s << 44);220d[1] = w & MASK52; /* d[1] <= 2^52 - 1 */221cc = -(w >> 52) & 0xFFF; /* cc <= 48 */222w = t[2] - cc;223cc = w >> 63; /* cc = 0 or 1 */224d[2] = w + (cc << 52); /* d[2] <= 2^52 + 31 */225w = t[3] - cc - (s << 36);226cc = w >> 63; /* cc = 0 or 1 */227d[3] = w + (cc << 52); /* t[3] <= 2^52 + 2^49 - 1 */228d[4] = (t[4] & MASK48) + (s << 16) - cc; /* d[4] < 2^48 + 2^30 */229230/*231* If s = 0, then none of the limbs is modified, and there cannot232* be an overflow; if s != 0, then (s << 16) > cc, and there is233* no overflow either.234*/235}236237/*238* Montgomery multiplication in the field.239* Input: limbs must fit on 56 bits each.240* Output: partially reduced.241*/242static void243f256_montymul(uint64_t *d, const uint64_t *a, const uint64_t *b)244{245#if BR_INT128246247int i;248uint64_t t[5];249250t[0] = 0;251t[1] = 0;252t[2] = 0;253t[3] = 0;254t[4] = 0;255for (i = 0; i < 5; i ++) {256uint64_t x, f, cc, w, s;257unsigned __int128 z;258259/*260* Since limbs of a[] and b[] fit on 56 bits each,261* each individual product fits on 112 bits. Also,262* the factor f fits on 52 bits, so f<<48 fits on263* 112 bits too. This guarantees that carries (cc)264* will fit on 62 bits, thus no overflow.265*266* The operations below compute:267* t <- (t + x*b + f*p) / 2^64268*/269x = a[i];270z = (unsigned __int128)b[0] * (unsigned __int128)x271+ (unsigned __int128)t[0];272f = (uint64_t)z & MASK52;273cc = (uint64_t)(z >> 52);274z = (unsigned __int128)b[1] * (unsigned __int128)x275+ (unsigned __int128)t[1] + cc276+ ((unsigned __int128)f << 44);277t[0] = (uint64_t)z & MASK52;278cc = (uint64_t)(z >> 52);279z = (unsigned __int128)b[2] * (unsigned __int128)x280+ (unsigned __int128)t[2] + cc;281t[1] = (uint64_t)z & MASK52;282cc = (uint64_t)(z >> 52);283z = (unsigned __int128)b[3] * (unsigned __int128)x284+ (unsigned __int128)t[3] + cc285+ ((unsigned __int128)f << 36);286t[2] = (uint64_t)z & MASK52;287cc = (uint64_t)(z >> 52);288z = (unsigned __int128)b[4] * (unsigned __int128)x289+ (unsigned __int128)t[4] + cc290+ ((unsigned __int128)f << 48)291- ((unsigned __int128)f << 16);292t[3] = (uint64_t)z & MASK52;293t[4] = (uint64_t)(z >> 52);294295/*296* t[4] may be up to 62 bits here; we need to do a297* partial reduction. Note that limbs t[0] to t[3]298* fit on 52 bits each.299*/300s = t[4] >> 48; /* s < 2^14 */301t[0] += s; /* t[0] < 2^52 + 2^14 */302w = t[1] - (s << 44);303t[1] = w & MASK52; /* t[1] < 2^52 */304cc = -(w >> 52) & 0xFFF; /* cc < 16 */305w = t[2] - cc;306t[2] = w & MASK52; /* t[2] < 2^52 */307cc = w >> 63; /* cc = 0 or 1 */308w = t[3] - cc - (s << 36);309t[3] = w & MASK52; /* t[3] < 2^52 */310cc = w >> 63; /* cc = 0 or 1 */311w = t[4] & MASK48;312t[4] = w + (s << 16) - cc; /* t[4] < 2^48 + 2^30 */313314/*315* The final t[4] cannot overflow because cc is 0 or 1,316* and cc can be 1 only if s != 0.317*/318}319320d[0] = t[0];321d[1] = t[1];322d[2] = t[2];323d[3] = t[3];324d[4] = t[4];325326#elif BR_UMUL128327328int i;329uint64_t t[5];330331t[0] = 0;332t[1] = 0;333t[2] = 0;334t[3] = 0;335t[4] = 0;336for (i = 0; i < 5; i ++) {337uint64_t x, f, cc, w, s, zh, zl;338unsigned char k;339340/*341* Since limbs of a[] and b[] fit on 56 bits each,342* each individual product fits on 112 bits. Also,343* the factor f fits on 52 bits, so f<<48 fits on344* 112 bits too. This guarantees that carries (cc)345* will fit on 62 bits, thus no overflow.346*347* The operations below compute:348* t <- (t + x*b + f*p) / 2^64349*/350x = a[i];351zl = _umul128(b[0], x, &zh);352k = _addcarry_u64(0, t[0], zl, &zl);353(void)_addcarry_u64(k, 0, zh, &zh);354f = zl & MASK52;355cc = (zl >> 52) | (zh << 12);356357zl = _umul128(b[1], x, &zh);358k = _addcarry_u64(0, t[1], zl, &zl);359(void)_addcarry_u64(k, 0, zh, &zh);360k = _addcarry_u64(0, cc, zl, &zl);361(void)_addcarry_u64(k, 0, zh, &zh);362k = _addcarry_u64(0, f << 44, zl, &zl);363(void)_addcarry_u64(k, f >> 20, zh, &zh);364t[0] = zl & MASK52;365cc = (zl >> 52) | (zh << 12);366367zl = _umul128(b[2], x, &zh);368k = _addcarry_u64(0, t[2], zl, &zl);369(void)_addcarry_u64(k, 0, zh, &zh);370k = _addcarry_u64(0, cc, zl, &zl);371(void)_addcarry_u64(k, 0, zh, &zh);372t[1] = zl & MASK52;373cc = (zl >> 52) | (zh << 12);374375zl = _umul128(b[3], x, &zh);376k = _addcarry_u64(0, t[3], zl, &zl);377(void)_addcarry_u64(k, 0, zh, &zh);378k = _addcarry_u64(0, cc, zl, &zl);379(void)_addcarry_u64(k, 0, zh, &zh);380k = _addcarry_u64(0, f << 36, zl, &zl);381(void)_addcarry_u64(k, f >> 28, zh, &zh);382t[2] = zl & MASK52;383cc = (zl >> 52) | (zh << 12);384385zl = _umul128(b[4], x, &zh);386k = _addcarry_u64(0, t[4], zl, &zl);387(void)_addcarry_u64(k, 0, zh, &zh);388k = _addcarry_u64(0, cc, zl, &zl);389(void)_addcarry_u64(k, 0, zh, &zh);390k = _addcarry_u64(0, f << 48, zl, &zl);391(void)_addcarry_u64(k, f >> 16, zh, &zh);392k = _subborrow_u64(0, zl, f << 16, &zl);393(void)_subborrow_u64(k, zh, f >> 48, &zh);394t[3] = zl & MASK52;395t[4] = (zl >> 52) | (zh << 12);396397/*398* t[4] may be up to 62 bits here; we need to do a399* partial reduction. Note that limbs t[0] to t[3]400* fit on 52 bits each.401*/402s = t[4] >> 48; /* s < 2^14 */403t[0] += s; /* t[0] < 2^52 + 2^14 */404w = t[1] - (s << 44);405t[1] = w & MASK52; /* t[1] < 2^52 */406cc = -(w >> 52) & 0xFFF; /* cc < 16 */407w = t[2] - cc;408t[2] = w & MASK52; /* t[2] < 2^52 */409cc = w >> 63; /* cc = 0 or 1 */410w = t[3] - cc - (s << 36);411t[3] = w & MASK52; /* t[3] < 2^52 */412cc = w >> 63; /* cc = 0 or 1 */413w = t[4] & MASK48;414t[4] = w + (s << 16) - cc; /* t[4] < 2^48 + 2^30 */415416/*417* The final t[4] cannot overflow because cc is 0 or 1,418* and cc can be 1 only if s != 0.419*/420}421422d[0] = t[0];423d[1] = t[1];424d[2] = t[2];425d[3] = t[3];426d[4] = t[4];427428#endif429}430431/*432* Montgomery squaring in the field; currently a basic wrapper around433* multiplication (inline, should be optimized away).434* TODO: see if some extra speed can be gained here.435*/436static inline void437f256_montysquare(uint64_t *d, const uint64_t *a)438{439f256_montymul(d, a, a);440}441442/*443* Convert to Montgomery representation.444*/445static void446f256_tomonty(uint64_t *d, const uint64_t *a)447{448/*449* R2 = 2^520 mod p.450* If R = 2^260 mod p, then R2 = R^2 mod p; and the Montgomery451* multiplication of a by R2 is: a*R2/R = a*R mod p, i.e. the452* conversion to Montgomery representation.453*/454static const uint64_t R2[] = {4550x0000000000300, 0xFFFFFFFF00000, 0xFFFFEFFFFFFFB,4560xFDFFFFFFFFFFF, 0x0000004FFFFFF457};458459f256_montymul(d, a, R2);460}461462/*463* Convert from Montgomery representation.464*/465static void466f256_frommonty(uint64_t *d, const uint64_t *a)467{468/*469* Montgomery multiplication by 1 is division by 2^260 modulo p.470*/471static const uint64_t one[] = { 1, 0, 0, 0, 0 };472473f256_montymul(d, a, one);474}475476/*477* Inversion in the field. If the source value is 0 modulo p, then this478* returns 0 or p. This function uses Montgomery representation.479*/480static void481f256_invert(uint64_t *d, const uint64_t *a)482{483/*484* We compute a^(p-2) mod p. The exponent pattern (from high to485* low) is:486* - 32 bits of value 1487* - 31 bits of value 0488* - 1 bit of value 1489* - 96 bits of value 0490* - 94 bits of value 1491* - 1 bit of value 0492* - 1 bit of value 1493* To speed up the square-and-multiply algorithm, we precompute494* a^(2^31-1).495*/496497uint64_t r[5], t[5];498int i;499500memcpy(t, a, sizeof t);501for (i = 0; i < 30; i ++) {502f256_montysquare(t, t);503f256_montymul(t, t, a);504}505506memcpy(r, t, sizeof t);507for (i = 224; i >= 0; i --) {508f256_montysquare(r, r);509switch (i) {510case 0:511case 2:512case 192:513case 224:514f256_montymul(r, r, a);515break;516case 3:517case 34:518case 65:519f256_montymul(r, r, t);520break;521}522}523memcpy(d, r, sizeof r);524}525526/*527* Finalize reduction.528* Input value should be partially reduced.529* On output, limbs a[0] to a[3] fit on 52 bits each, limb a[4] fits530* on 48 bits, and the integer is less than p.531*/532static inline void533f256_final_reduce(uint64_t *a)534{535uint64_t r[5], t[5], w, cc;536int i;537538/*539* Propagate carries to ensure that limbs 0 to 3 fit on 52 bits.540*/541cc = 0;542for (i = 0; i < 5; i ++) {543w = a[i] + cc;544r[i] = w & MASK52;545cc = w >> 52;546}547548/*549* We compute t = r + (2^256 - p) = r + 2^224 - 2^192 - 2^96 + 1.550* If t < 2^256, then r < p, and we return r. Otherwise, we551* want to return r - p = t - 2^256.552*/553554/*555* Add 2^224 + 1, and propagate carries to ensure that limbs556* t[0] to t[3] fit in 52 bits each.557*/558w = r[0] + 1;559t[0] = w & MASK52;560cc = w >> 52;561w = r[1] + cc;562t[1] = w & MASK52;563cc = w >> 52;564w = r[2] + cc;565t[2] = w & MASK52;566cc = w >> 52;567w = r[3] + cc;568t[3] = w & MASK52;569cc = w >> 52;570t[4] = r[4] + cc + BIT(16);571572/*573* Subtract 2^192 + 2^96. Since we just added 2^224 + 1, the574* result cannot be negative.575*/576w = t[1] - BIT(44);577t[1] = w & MASK52;578cc = w >> 63;579w = t[2] - cc;580t[2] = w & MASK52;581cc = w >> 63;582w = t[3] - BIT(36) - cc;583t[3] = w & MASK52;584cc = w >> 63;585t[4] -= cc;586587/*588* If the top limb t[4] fits on 48 bits, then r[] is already589* in the proper range. Otherwise, t[] is the value to return590* (truncated to 256 bits).591*/592cc = -(t[4] >> 48);593t[4] &= MASK48;594for (i = 0; i < 5; i ++) {595a[i] = r[i] ^ (cc & (r[i] ^ t[i]));596}597}598599/*600* Points in affine and Jacobian coordinates.601*602* - In affine coordinates, the point-at-infinity cannot be encoded.603* - Jacobian coordinates (X,Y,Z) correspond to affine (X/Z^2,Y/Z^3);604* if Z = 0 then this is the point-at-infinity.605*/606typedef struct {607uint64_t x[5];608uint64_t y[5];609} p256_affine;610611typedef struct {612uint64_t x[5];613uint64_t y[5];614uint64_t z[5];615} p256_jacobian;616617/*618* Decode a field element (unsigned big endian notation).619*/620static void621f256_decode(uint64_t *a, const unsigned char *buf)622{623uint64_t w0, w1, w2, w3;624625w3 = br_dec64be(buf + 0);626w2 = br_dec64be(buf + 8);627w1 = br_dec64be(buf + 16);628w0 = br_dec64be(buf + 24);629a[0] = w0 & MASK52;630a[1] = ((w0 >> 52) | (w1 << 12)) & MASK52;631a[2] = ((w1 >> 40) | (w2 << 24)) & MASK52;632a[3] = ((w2 >> 28) | (w3 << 36)) & MASK52;633a[4] = w3 >> 16;634}635636/*637* Encode a field element (unsigned big endian notation). The field638* element MUST be fully reduced.639*/640static void641f256_encode(unsigned char *buf, const uint64_t *a)642{643uint64_t w0, w1, w2, w3;644645w0 = a[0] | (a[1] << 52);646w1 = (a[1] >> 12) | (a[2] << 40);647w2 = (a[2] >> 24) | (a[3] << 28);648w3 = (a[3] >> 36) | (a[4] << 16);649br_enc64be(buf + 0, w3);650br_enc64be(buf + 8, w2);651br_enc64be(buf + 16, w1);652br_enc64be(buf + 24, w0);653}654655/*656* Decode a point. The returned point is in Jacobian coordinates, but657* with z = 1. If the encoding is invalid, or encodes a point which is658* not on the curve, or encodes the point at infinity, then this function659* returns 0. Otherwise, 1 is returned.660*661* The buffer is assumed to have length exactly 65 bytes.662*/663static uint32_t664point_decode(p256_jacobian *P, const unsigned char *buf)665{666uint64_t x[5], y[5], t[5], x3[5], tt;667uint32_t r;668669/*670* Header byte shall be 0x04.671*/672r = EQ(buf[0], 0x04);673674/*675* Decode X and Y coordinates, and convert them into676* Montgomery representation.677*/678f256_decode(x, buf + 1);679f256_decode(y, buf + 33);680f256_tomonty(x, x);681f256_tomonty(y, y);682683/*684* Verify y^2 = x^3 + A*x + B. In curve P-256, A = -3.685* Note that the Montgomery representation of 0 is 0. We must686* take care to apply the final reduction to make sure we have687* 0 and not p.688*/689f256_montysquare(t, y);690f256_montysquare(x3, x);691f256_montymul(x3, x3, x);692f256_sub(t, t, x3);693f256_add(t, t, x);694f256_add(t, t, x);695f256_add(t, t, x);696f256_sub(t, t, P256_B_MONTY);697f256_final_reduce(t);698tt = t[0] | t[1] | t[2] | t[3] | t[4];699r &= EQ((uint32_t)(tt | (tt >> 32)), 0);700701/*702* Return the point in Jacobian coordinates (and Montgomery703* representation).704*/705memcpy(P->x, x, sizeof x);706memcpy(P->y, y, sizeof y);707memcpy(P->z, F256_R, sizeof F256_R);708return r;709}710711/*712* Final conversion for a point:713* - The point is converted back to affine coordinates.714* - Final reduction is performed.715* - The point is encoded into the provided buffer.716*717* If the point is the point-at-infinity, all operations are performed,718* but the buffer contents are indeterminate, and 0 is returned. Otherwise,719* the encoded point is written in the buffer, and 1 is returned.720*/721static uint32_t722point_encode(unsigned char *buf, const p256_jacobian *P)723{724uint64_t t1[5], t2[5], z;725726/* Set t1 = 1/z^2 and t2 = 1/z^3. */727f256_invert(t2, P->z);728f256_montysquare(t1, t2);729f256_montymul(t2, t2, t1);730731/* Compute affine coordinates x (in t1) and y (in t2). */732f256_montymul(t1, P->x, t1);733f256_montymul(t2, P->y, t2);734735/* Convert back from Montgomery representation, and finalize736reductions. */737f256_frommonty(t1, t1);738f256_frommonty(t2, t2);739f256_final_reduce(t1);740f256_final_reduce(t2);741742/* Encode. */743buf[0] = 0x04;744f256_encode(buf + 1, t1);745f256_encode(buf + 33, t2);746747/* Return success if and only if P->z != 0. */748z = P->z[0] | P->z[1] | P->z[2] | P->z[3] | P->z[4];749return NEQ((uint32_t)(z | z >> 32), 0);750}751752/*753* Point doubling in Jacobian coordinates: point P is doubled.754* Note: if the source point is the point-at-infinity, then the result is755* still the point-at-infinity, which is correct. Moreover, if the three756* coordinates were zero, then they still are zero in the returned value.757*/758static void759p256_double(p256_jacobian *P)760{761/*762* Doubling formulas are:763*764* s = 4*x*y^2765* m = 3*(x + z^2)*(x - z^2)766* x' = m^2 - 2*s767* y' = m*(s - x') - 8*y^4768* z' = 2*y*z769*770* These formulas work for all points, including points of order 2771* and points at infinity:772* - If y = 0 then z' = 0. But there is no such point in P-256773* anyway.774* - If z = 0 then z' = 0.775*/776uint64_t t1[5], t2[5], t3[5], t4[5];777778/*779* Compute z^2 in t1.780*/781f256_montysquare(t1, P->z);782783/*784* Compute x-z^2 in t2 and x+z^2 in t1.785*/786f256_add(t2, P->x, t1);787f256_sub(t1, P->x, t1);788789/*790* Compute 3*(x+z^2)*(x-z^2) in t1.791*/792f256_montymul(t3, t1, t2);793f256_add(t1, t3, t3);794f256_add(t1, t3, t1);795796/*797* Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).798*/799f256_montysquare(t3, P->y);800f256_add(t3, t3, t3);801f256_montymul(t2, P->x, t3);802f256_add(t2, t2, t2);803804/*805* Compute x' = m^2 - 2*s.806*/807f256_montysquare(P->x, t1);808f256_sub(P->x, P->x, t2);809f256_sub(P->x, P->x, t2);810811/*812* Compute z' = 2*y*z.813*/814f256_montymul(t4, P->y, P->z);815f256_add(P->z, t4, t4);816f256_partial_reduce(P->z);817818/*819* Compute y' = m*(s - x') - 8*y^4. Note that we already have820* 2*y^2 in t3.821*/822f256_sub(t2, t2, P->x);823f256_montymul(P->y, t1, t2);824f256_montysquare(t4, t3);825f256_add(t4, t4, t4);826f256_sub(P->y, P->y, t4);827}828829/*830* Point addition (Jacobian coordinates): P1 is replaced with P1+P2.831* This function computes the wrong result in the following cases:832*833* - If P1 == 0 but P2 != 0834* - If P1 != 0 but P2 == 0835* - If P1 == P2836*837* In all three cases, P1 is set to the point at infinity.838*839* Returned value is 0 if one of the following occurs:840*841* - P1 and P2 have the same Y coordinate.842* - P1 == 0 and P2 == 0.843* - The Y coordinate of one of the points is 0 and the other point is844* the point at infinity.845*846* The third case cannot actually happen with valid points, since a point847* with Y == 0 is a point of order 2, and there is no point of order 2 on848* curve P-256.849*850* Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller851* can apply the following:852*853* - If the result is not the point at infinity, then it is correct.854* - Otherwise, if the returned value is 1, then this is a case of855* P1+P2 == 0, so the result is indeed the point at infinity.856* - Otherwise, P1 == P2, so a "double" operation should have been857* performed.858*859* Note that you can get a returned value of 0 with a correct result,860* e.g. if P1 and P2 have the same Y coordinate, but distinct X coordinates.861*/862static uint32_t863p256_add(p256_jacobian *P1, const p256_jacobian *P2)864{865/*866* Addtions formulas are:867*868* u1 = x1 * z2^2869* u2 = x2 * z1^2870* s1 = y1 * z2^3871* s2 = y2 * z1^3872* h = u2 - u1873* r = s2 - s1874* x3 = r^2 - h^3 - 2 * u1 * h^2875* y3 = r * (u1 * h^2 - x3) - s1 * h^3876* z3 = h * z1 * z2877*/878uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt;879uint32_t ret;880881/*882* Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).883*/884f256_montysquare(t3, P2->z);885f256_montymul(t1, P1->x, t3);886f256_montymul(t4, P2->z, t3);887f256_montymul(t3, P1->y, t4);888889/*890* Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).891*/892f256_montysquare(t4, P1->z);893f256_montymul(t2, P2->x, t4);894f256_montymul(t5, P1->z, t4);895f256_montymul(t4, P2->y, t5);896897/*898* Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).899* We need to test whether r is zero, so we will do some extra900* reduce.901*/902f256_sub(t2, t2, t1);903f256_sub(t4, t4, t3);904f256_final_reduce(t4);905tt = t4[0] | t4[1] | t4[2] | t4[3] | t4[4];906ret = (uint32_t)(tt | (tt >> 32));907ret = (ret | -ret) >> 31;908909/*910* Compute u1*h^2 (in t6) and h^3 (in t5);911*/912f256_montysquare(t7, t2);913f256_montymul(t6, t1, t7);914f256_montymul(t5, t7, t2);915916/*917* Compute x3 = r^2 - h^3 - 2*u1*h^2.918*/919f256_montysquare(P1->x, t4);920f256_sub(P1->x, P1->x, t5);921f256_sub(P1->x, P1->x, t6);922f256_sub(P1->x, P1->x, t6);923924/*925* Compute y3 = r*(u1*h^2 - x3) - s1*h^3.926*/927f256_sub(t6, t6, P1->x);928f256_montymul(P1->y, t4, t6);929f256_montymul(t1, t5, t3);930f256_sub(P1->y, P1->y, t1);931932/*933* Compute z3 = h*z1*z2.934*/935f256_montymul(t1, P1->z, P2->z);936f256_montymul(P1->z, t1, t2);937938return ret;939}940941/*942* Point addition (mixed coordinates): P1 is replaced with P1+P2.943* This is a specialised function for the case when P2 is a non-zero point944* in affine coordinates.945*946* This function computes the wrong result in the following cases:947*948* - If P1 == 0949* - If P1 == P2950*951* In both cases, P1 is set to the point at infinity.952*953* Returned value is 0 if one of the following occurs:954*955* - P1 and P2 have the same Y (affine) coordinate.956* - The Y coordinate of P2 is 0 and P1 is the point at infinity.957*958* The second case cannot actually happen with valid points, since a point959* with Y == 0 is a point of order 2, and there is no point of order 2 on960* curve P-256.961*962* Therefore, assuming that P1 != 0 on input, then the caller963* can apply the following:964*965* - If the result is not the point at infinity, then it is correct.966* - Otherwise, if the returned value is 1, then this is a case of967* P1+P2 == 0, so the result is indeed the point at infinity.968* - Otherwise, P1 == P2, so a "double" operation should have been969* performed.970*971* Again, a value of 0 may be returned in some cases where the addition972* result is correct.973*/974static uint32_t975p256_add_mixed(p256_jacobian *P1, const p256_affine *P2)976{977/*978* Addtions formulas are:979*980* u1 = x1981* u2 = x2 * z1^2982* s1 = y1983* s2 = y2 * z1^3984* h = u2 - u1985* r = s2 - s1986* x3 = r^2 - h^3 - 2 * u1 * h^2987* y3 = r * (u1 * h^2 - x3) - s1 * h^3988* z3 = h * z1989*/990uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt;991uint32_t ret;992993/*994* Compute u1 = x1 (in t1) and s1 = y1 (in t3).995*/996memcpy(t1, P1->x, sizeof t1);997memcpy(t3, P1->y, sizeof t3);998999/*1000* Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).1001*/1002f256_montysquare(t4, P1->z);1003f256_montymul(t2, P2->x, t4);1004f256_montymul(t5, P1->z, t4);1005f256_montymul(t4, P2->y, t5);10061007/*1008* Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).1009* We need to test whether r is zero, so we will do some extra1010* reduce.1011*/1012f256_sub(t2, t2, t1);1013f256_sub(t4, t4, t3);1014f256_final_reduce(t4);1015tt = t4[0] | t4[1] | t4[2] | t4[3] | t4[4];1016ret = (uint32_t)(tt | (tt >> 32));1017ret = (ret | -ret) >> 31;10181019/*1020* Compute u1*h^2 (in t6) and h^3 (in t5);1021*/1022f256_montysquare(t7, t2);1023f256_montymul(t6, t1, t7);1024f256_montymul(t5, t7, t2);10251026/*1027* Compute x3 = r^2 - h^3 - 2*u1*h^2.1028*/1029f256_montysquare(P1->x, t4);1030f256_sub(P1->x, P1->x, t5);1031f256_sub(P1->x, P1->x, t6);1032f256_sub(P1->x, P1->x, t6);10331034/*1035* Compute y3 = r*(u1*h^2 - x3) - s1*h^3.1036*/1037f256_sub(t6, t6, P1->x);1038f256_montymul(P1->y, t4, t6);1039f256_montymul(t1, t5, t3);1040f256_sub(P1->y, P1->y, t1);10411042/*1043* Compute z3 = h*z1*z2.1044*/1045f256_montymul(P1->z, P1->z, t2);10461047return ret;1048}10491050#if 01051/* unused */1052/*1053* Point addition (mixed coordinates, complete): P1 is replaced with P1+P2.1054* This is a specialised function for the case when P2 is a non-zero point1055* in affine coordinates.1056*1057* This function returns the correct result in all cases.1058*/1059static uint32_t1060p256_add_complete_mixed(p256_jacobian *P1, const p256_affine *P2)1061{1062/*1063* Addtions formulas, in the general case, are:1064*1065* u1 = x11066* u2 = x2 * z1^21067* s1 = y11068* s2 = y2 * z1^31069* h = u2 - u11070* r = s2 - s11071* x3 = r^2 - h^3 - 2 * u1 * h^21072* y3 = r * (u1 * h^2 - x3) - s1 * h^31073* z3 = h * z11074*1075* These formulas mishandle the two following cases:1076*1077* - If P1 is the point-at-infinity (z1 = 0), then z3 is1078* incorrectly set to 0.1079*1080* - If P1 = P2, then u1 = u2 and s1 = s2, and x3, y3 and z31081* are all set to 0.1082*1083* However, if P1 + P2 = 0, then u1 = u2 but s1 != s2, and then1084* we correctly get z3 = 0 (the point-at-infinity).1085*1086* To fix the case P1 = 0, we perform at the end a copy of P21087* over P1, conditional to z1 = 0.1088*1089* For P1 = P2: in that case, both h and r are set to 0, and1090* we get x3, y3 and z3 equal to 0. We can test for that1091* occurrence to make a mask which will be all-one if P1 = P2,1092* or all-zero otherwise; then we can compute the double of P21093* and add it, combined with the mask, to (x3,y3,z3).1094*1095* Using the doubling formulas in p256_double() on (x2,y2),1096* simplifying since P2 is affine (i.e. z2 = 1, implicitly),1097* we get:1098* s = 4*x2*y2^21099* m = 3*(x2 + 1)*(x2 - 1)1100* x' = m^2 - 2*s1101* y' = m*(s - x') - 8*y2^41102* z' = 2*y21103* which requires only 6 multiplications. Added to the 111104* multiplications of the normal mixed addition in Jacobian1105* coordinates, we get a cost of 17 multiplications in total.1106*/1107uint64_t t1[5], t2[5], t3[5], t4[5], t5[5], t6[5], t7[5], tt, zz;1108int i;11091110/*1111* Set zz to -1 if P1 is the point at infinity, 0 otherwise.1112*/1113zz = P1->z[0] | P1->z[1] | P1->z[2] | P1->z[3] | P1->z[4];1114zz = ((zz | -zz) >> 63) - (uint64_t)1;11151116/*1117* Compute u1 = x1 (in t1) and s1 = y1 (in t3).1118*/1119memcpy(t1, P1->x, sizeof t1);1120memcpy(t3, P1->y, sizeof t3);11211122/*1123* Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).1124*/1125f256_montysquare(t4, P1->z);1126f256_montymul(t2, P2->x, t4);1127f256_montymul(t5, P1->z, t4);1128f256_montymul(t4, P2->y, t5);11291130/*1131* Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).1132* reduce.1133*/1134f256_sub(t2, t2, t1);1135f256_sub(t4, t4, t3);11361137/*1138* If both h = 0 and r = 0, then P1 = P2, and we want to set1139* the mask tt to -1; otherwise, the mask will be 0.1140*/1141f256_final_reduce(t2);1142f256_final_reduce(t4);1143tt = t2[0] | t2[1] | t2[2] | t2[3] | t2[4]1144| t4[0] | t4[1] | t4[2] | t4[3] | t4[4];1145tt = ((tt | -tt) >> 63) - (uint64_t)1;11461147/*1148* Compute u1*h^2 (in t6) and h^3 (in t5);1149*/1150f256_montysquare(t7, t2);1151f256_montymul(t6, t1, t7);1152f256_montymul(t5, t7, t2);11531154/*1155* Compute x3 = r^2 - h^3 - 2*u1*h^2.1156*/1157f256_montysquare(P1->x, t4);1158f256_sub(P1->x, P1->x, t5);1159f256_sub(P1->x, P1->x, t6);1160f256_sub(P1->x, P1->x, t6);11611162/*1163* Compute y3 = r*(u1*h^2 - x3) - s1*h^3.1164*/1165f256_sub(t6, t6, P1->x);1166f256_montymul(P1->y, t4, t6);1167f256_montymul(t1, t5, t3);1168f256_sub(P1->y, P1->y, t1);11691170/*1171* Compute z3 = h*z1.1172*/1173f256_montymul(P1->z, P1->z, t2);11741175/*1176* The "double" result, in case P1 = P2.1177*/11781179/*1180* Compute z' = 2*y2 (in t1).1181*/1182f256_add(t1, P2->y, P2->y);1183f256_partial_reduce(t1);11841185/*1186* Compute 2*(y2^2) (in t2) and s = 4*x2*(y2^2) (in t3).1187*/1188f256_montysquare(t2, P2->y);1189f256_add(t2, t2, t2);1190f256_add(t3, t2, t2);1191f256_montymul(t3, P2->x, t3);11921193/*1194* Compute m = 3*(x2^2 - 1) (in t4).1195*/1196f256_montysquare(t4, P2->x);1197f256_sub(t4, t4, F256_R);1198f256_add(t5, t4, t4);1199f256_add(t4, t4, t5);12001201/*1202* Compute x' = m^2 - 2*s (in t5).1203*/1204f256_montysquare(t5, t4);1205f256_sub(t5, t3);1206f256_sub(t5, t3);12071208/*1209* Compute y' = m*(s - x') - 8*y2^4 (in t6).1210*/1211f256_sub(t6, t3, t5);1212f256_montymul(t6, t6, t4);1213f256_montysquare(t7, t2);1214f256_sub(t6, t6, t7);1215f256_sub(t6, t6, t7);12161217/*1218* We now have the alternate (doubling) coordinates in (t5,t6,t1).1219* We combine them with (x3,y3,z3).1220*/1221for (i = 0; i < 5; i ++) {1222P1->x[i] |= tt & t5[i];1223P1->y[i] |= tt & t6[i];1224P1->z[i] |= tt & t1[i];1225}12261227/*1228* If P1 = 0, then we get z3 = 0 (which is invalid); if z1 is 0,1229* then we want to replace the result with a copy of P2. The1230* test on z1 was done at the start, in the zz mask.1231*/1232for (i = 0; i < 5; i ++) {1233P1->x[i] ^= zz & (P1->x[i] ^ P2->x[i]);1234P1->y[i] ^= zz & (P1->y[i] ^ P2->y[i]);1235P1->z[i] ^= zz & (P1->z[i] ^ F256_R[i]);1236}1237}1238#endif12391240/*1241* Inner function for computing a point multiplication. A window is1242* provided, with points 1*P to 15*P in affine coordinates.1243*1244* Assumptions:1245* - All provided points are valid points on the curve.1246* - Multiplier is non-zero, and smaller than the curve order.1247* - Everything is in Montgomery representation.1248*/1249static void1250point_mul_inner(p256_jacobian *R, const p256_affine *W,1251const unsigned char *k, size_t klen)1252{1253p256_jacobian Q;1254uint32_t qz;12551256memset(&Q, 0, sizeof Q);1257qz = 1;1258while (klen -- > 0) {1259int i;1260unsigned bk;12611262bk = *k ++;1263for (i = 0; i < 2; i ++) {1264uint32_t bits;1265uint32_t bnz;1266p256_affine T;1267p256_jacobian U;1268uint32_t n;1269int j;1270uint64_t m;12711272p256_double(&Q);1273p256_double(&Q);1274p256_double(&Q);1275p256_double(&Q);1276bits = (bk >> 4) & 0x0F;1277bnz = NEQ(bits, 0);12781279/*1280* Lookup point in window. If the bits are 0,1281* we get something invalid, which is not a1282* problem because we will use it only if the1283* bits are non-zero.1284*/1285memset(&T, 0, sizeof T);1286for (n = 0; n < 15; n ++) {1287m = -(uint64_t)EQ(bits, n + 1);1288T.x[0] |= m & W[n].x[0];1289T.x[1] |= m & W[n].x[1];1290T.x[2] |= m & W[n].x[2];1291T.x[3] |= m & W[n].x[3];1292T.x[4] |= m & W[n].x[4];1293T.y[0] |= m & W[n].y[0];1294T.y[1] |= m & W[n].y[1];1295T.y[2] |= m & W[n].y[2];1296T.y[3] |= m & W[n].y[3];1297T.y[4] |= m & W[n].y[4];1298}12991300U = Q;1301p256_add_mixed(&U, &T);13021303/*1304* If qz is still 1, then Q was all-zeros, and this1305* is conserved through p256_double().1306*/1307m = -(uint64_t)(bnz & qz);1308for (j = 0; j < 5; j ++) {1309Q.x[j] ^= m & (Q.x[j] ^ T.x[j]);1310Q.y[j] ^= m & (Q.y[j] ^ T.y[j]);1311Q.z[j] ^= m & (Q.z[j] ^ F256_R[j]);1312}1313CCOPY(bnz & ~qz, &Q, &U, sizeof Q);1314qz &= ~bnz;1315bk <<= 4;1316}1317}1318*R = Q;1319}13201321/*1322* Convert a window from Jacobian to affine coordinates. A single1323* field inversion is used. This function works for windows up to1324* 32 elements.1325*1326* The destination array (aff[]) and the source array (jac[]) may1327* overlap, provided that the start of aff[] is not after the start of1328* jac[]. Even if the arrays do _not_ overlap, the source array is1329* modified.1330*/1331static void1332window_to_affine(p256_affine *aff, p256_jacobian *jac, int num)1333{1334/*1335* Convert the window points to affine coordinates. We use the1336* following trick to mutualize the inversion computation: if1337* we have z1, z2, z3, and z4, and want to invert all of them,1338* we compute u = 1/(z1*z2*z3*z4), and then we have:1339* 1/z1 = u*z2*z3*z41340* 1/z2 = u*z1*z3*z41341* 1/z3 = u*z1*z2*z41342* 1/z4 = u*z1*z2*z31343*1344* The partial products are computed recursively:1345*1346* - on input (z_1,z_2), return (z_2,z_1) and z_1*z_21347* - on input (z_1,z_2,... z_n):1348* recurse on (z_1,z_2,... z_(n/2)) -> r1 and m11349* recurse on (z_(n/2+1),z_(n/2+2)... z_n) -> r2 and m21350* multiply elements of r1 by m2 -> s11351* multiply elements of r2 by m1 -> s21352* return r1||r2 and m1*m21353*1354* In the example below, we suppose that we have 14 elements.1355* Let z1, z2,... zE be the 14 values to invert (index noted in1356* hexadecimal, starting at 1).1357*1358* - Depth 1:1359* swap(z1, z2); z12 = z1*z21360* swap(z3, z4); z34 = z3*z41361* swap(z5, z6); z56 = z5*z61362* swap(z7, z8); z78 = z7*z81363* swap(z9, zA); z9A = z9*zA1364* swap(zB, zC); zBC = zB*zC1365* swap(zD, zE); zDE = zD*zE1366*1367* - Depth 2:1368* z1 <- z1*z34, z2 <- z2*z34, z3 <- z3*z12, z4 <- z4*z121369* z1234 = z12*z341370* z5 <- z5*z78, z6 <- z6*z78, z7 <- z7*z56, z8 <- z8*z561371* z5678 = z56*z781372* z9 <- z9*zBC, zA <- zA*zBC, zB <- zB*z9A, zC <- zC*z9A1373* z9ABC = z9A*zBC1374*1375* - Depth 3:1376* z1 <- z1*z5678, z2 <- z2*z5678, z3 <- z3*z5678, z4 <- z4*z56781377* z5 <- z5*z1234, z6 <- z6*z1234, z7 <- z7*z1234, z8 <- z8*z12341378* z12345678 = z1234*z56781379* z9 <- z9*zDE, zA <- zA*zDE, zB <- zB*zDE, zC <- zC*zDE1380* zD <- zD*z9ABC, zE*z9ABC1381* z9ABCDE = z9ABC*zDE1382*1383* - Depth 4:1384* multiply z1..z8 by z9ABCDE1385* multiply z9..zE by z123456781386* final z = z12345678*z9ABCDE1387*/13881389uint64_t z[16][5];1390int i, k, s;1391#define zt (z[15])1392#define zu (z[14])1393#define zv (z[13])13941395/*1396* First recursion step (pairwise swapping and multiplication).1397* If there is an odd number of elements, then we "invent" an1398* extra one with coordinate Z = 1 (in Montgomery representation).1399*/1400for (i = 0; (i + 1) < num; i += 2) {1401memcpy(zt, jac[i].z, sizeof zt);1402memcpy(jac[i].z, jac[i + 1].z, sizeof zt);1403memcpy(jac[i + 1].z, zt, sizeof zt);1404f256_montymul(z[i >> 1], jac[i].z, jac[i + 1].z);1405}1406if ((num & 1) != 0) {1407memcpy(z[num >> 1], jac[num - 1].z, sizeof zt);1408memcpy(jac[num - 1].z, F256_R, sizeof F256_R);1409}14101411/*1412* Perform further recursion steps. At the entry of each step,1413* the process has been done for groups of 's' points. The1414* integer k is the log2 of s.1415*/1416for (k = 1, s = 2; s < num; k ++, s <<= 1) {1417int n;14181419for (i = 0; i < num; i ++) {1420f256_montymul(jac[i].z, jac[i].z, z[(i >> k) ^ 1]);1421}1422n = (num + s - 1) >> k;1423for (i = 0; i < (n >> 1); i ++) {1424f256_montymul(z[i], z[i << 1], z[(i << 1) + 1]);1425}1426if ((n & 1) != 0) {1427memmove(z[n >> 1], z[n], sizeof zt);1428}1429}14301431/*1432* Invert the final result, and convert all points.1433*/1434f256_invert(zt, z[0]);1435for (i = 0; i < num; i ++) {1436f256_montymul(zv, jac[i].z, zt);1437f256_montysquare(zu, zv);1438f256_montymul(zv, zv, zu);1439f256_montymul(aff[i].x, jac[i].x, zu);1440f256_montymul(aff[i].y, jac[i].y, zv);1441}1442}14431444/*1445* Multiply the provided point by an integer.1446* Assumptions:1447* - Source point is a valid curve point.1448* - Source point is not the point-at-infinity.1449* - Integer is not 0, and is lower than the curve order.1450* If these conditions are not met, then the result is indeterminate1451* (but the process is still constant-time).1452*/1453static void1454p256_mul(p256_jacobian *P, const unsigned char *k, size_t klen)1455{1456union {1457p256_affine aff[15];1458p256_jacobian jac[15];1459} window;1460int i;14611462/*1463* Compute window, in Jacobian coordinates.1464*/1465window.jac[0] = *P;1466for (i = 2; i < 16; i ++) {1467window.jac[i - 1] = window.jac[(i >> 1) - 1];1468if ((i & 1) == 0) {1469p256_double(&window.jac[i - 1]);1470} else {1471p256_add(&window.jac[i - 1], &window.jac[i >> 1]);1472}1473}14741475/*1476* Convert the window points to affine coordinates. Point1477* window[0] is the source point, already in affine coordinates.1478*/1479window_to_affine(window.aff, window.jac, 15);14801481/*1482* Perform point multiplication.1483*/1484point_mul_inner(P, window.aff, k, klen);1485}14861487/*1488* Precomputed window for the conventional generator: P256_Gwin[n]1489* contains (n+1)*G (affine coordinates, in Montgomery representation).1490*/1491static const p256_affine P256_Gwin[] = {1492{1493{ 0x30D418A9143C1, 0xC4FEDB60179E7, 0x62251075BA95F,14940x5C669FB732B77, 0x08905F76B5375 },1495{ 0x5357CE95560A8, 0x43A19E45CDDF2, 0x21F3258B4AB8E,14960xD8552E88688DD, 0x0571FF18A5885 }1497},1498{1499{ 0x46D410DDD64DF, 0x0B433827D8500, 0x1490D9AA6AE3C,15000xA3A832205038D, 0x06BB32E52DCF3 },1501{ 0x48D361BEE1A57, 0xB7B236FF82F36, 0x042DBE152CD7C,15020xA3AA9A8FB0E92, 0x08C577517A5B8 }1503},1504{1505{ 0x3F904EEBC1272, 0x9E87D81FBFFAC, 0xCBBC98B027F84,15060x47E46AD77DD87, 0x06936A3FD6FF7 },1507{ 0x5C1FC983A7EBD, 0xC3861FE1AB04C, 0x2EE98E583E47A,15080xC06A88208311A, 0x05F06A2AB587C }1509},1510{1511{ 0xB50D46918DCC5, 0xD7623C17374B0, 0x100AF24650A6E,15120x76ABCDAACACE8, 0x077362F591B01 },1513{ 0xF24CE4CBABA68, 0x17AD6F4472D96, 0xDDD22E1762847,15140x862EB6C36DEE5, 0x04B14C39CC5AB }1515},1516{1517{ 0x8AAEC45C61F5C, 0x9D4B9537DBE1B, 0x76C20C90EC649,15180x3C7D41CB5AAD0, 0x0907960649052 },1519{ 0x9B4AE7BA4F107, 0xF75EB882BEB30, 0x7A1F6873C568E,15200x915C540A9877E, 0x03A076BB9DD1E }1521},1522{1523{ 0x47373E77664A1, 0xF246CEE3E4039, 0x17A3AD55AE744,15240x673C50A961A5B, 0x03074B5964213 },1525{ 0x6220D377E44BA, 0x30DFF14B593D3, 0x639F11299C2B5,15260x75F5424D44CEF, 0x04C9916DEA07F }1527},1528{1529{ 0x354EA0173B4F1, 0x3C23C00F70746, 0x23BB082BD2021,15300xE03E43EAAB50C, 0x03BA5119D3123 },1531{ 0xD0303F5B9D4DE, 0x17DA67BDD2847, 0xC941956742F2F,15320x8670F933BDC77, 0x0AEDD9164E240 }1533},1534{1535{ 0x4CD19499A78FB, 0x4BF9B345527F1, 0x2CFC6B462AB5C,15360x30CDF90F02AF0, 0x0763891F62652 },1537{ 0xA3A9532D49775, 0xD7F9EBA15F59D, 0x60BBF021E3327,15380xF75C23C7B84BE, 0x06EC12F2C706D }1539},1540{1541{ 0x6E8F264E20E8E, 0xC79A7A84175C9, 0xC8EB00ABE6BFE,15420x16A4CC09C0444, 0x005B3081D0C4E },1543{ 0x777AA45F33140, 0xDCE5D45E31EB7, 0xB12F1A56AF7BE,15440xF9B2B6E019A88, 0x086659CDFD835 }1545},1546{1547{ 0xDBD19DC21EC8C, 0x94FCF81392C18, 0x250B4998F9868,15480x28EB37D2CD648, 0x0C61C947E4B34 },1549{ 0x407880DD9E767, 0x0C83FBE080C2B, 0x9BE5D2C43A899,15500xAB4EF7D2D6577, 0x08719A555B3B4 }1551},1552{1553{ 0x260A6245E4043, 0x53E7FDFE0EA7D, 0xAC1AB59DE4079,15540x072EFF3A4158D, 0x0E7090F1949C9 },1555{ 0x85612B944E886, 0xE857F61C81A76, 0xAD643D250F939,15560x88DAC0DAA891E, 0x089300244125B }1557},1558{1559{ 0x1AA7D26977684, 0x58A345A3304B7, 0x37385EABDEDEF,15600x155E409D29DEE, 0x0EE1DF780B83E },1561{ 0x12D91CBB5B437, 0x65A8956370CAC, 0xDE6D66170ED2F,15620xAC9B8228CFA8A, 0x0FF57C95C3238 }1563},1564{1565{ 0x25634B2ED7097, 0x9156FD30DCCC4, 0x9E98110E35676,15660x7594CBCD43F55, 0x038477ACC395B },1567{ 0x2B90C00EE17FF, 0xF842ED2E33575, 0x1F5BC16874838,15680x7968CD06422BD, 0x0BC0876AB9E7B }1569},1570{1571{ 0xA35BB0CF664AF, 0x68F9707E3A242, 0x832660126E48F,15720x72D2717BF54C6, 0x0AAE7333ED12C },1573{ 0x2DB7995D586B1, 0xE732237C227B5, 0x65E7DBBE29569,15740xBBBD8E4193E2A, 0x052706DC3EAA1 }1575},1576{1577{ 0xD8B7BC60055BE, 0xD76E27E4B72BC, 0x81937003CC23E,15780xA090E337424E4, 0x02AA0E43EAD3D },1579{ 0x524F6383C45D2, 0x422A41B2540B8, 0x8A4797D766355,15800xDF444EFA6DE77, 0x0042170A9079A }1581},1582};15831584/*1585* Multiply the conventional generator of the curve by the provided1586* integer. Return is written in *P.1587*1588* Assumptions:1589* - Integer is not 0, and is lower than the curve order.1590* If this conditions is not met, then the result is indeterminate1591* (but the process is still constant-time).1592*/1593static void1594p256_mulgen(p256_jacobian *P, const unsigned char *k, size_t klen)1595{1596point_mul_inner(P, P256_Gwin, k, klen);1597}15981599/*1600* Return 1 if all of the following hold:1601* - klen <= 321602* - k != 01603* - k is lower than the curve order1604* Otherwise, return 0.1605*1606* Constant-time behaviour: only klen may be observable.1607*/1608static uint32_t1609check_scalar(const unsigned char *k, size_t klen)1610{1611uint32_t z;1612int32_t c;1613size_t u;16141615if (klen > 32) {1616return 0;1617}1618z = 0;1619for (u = 0; u < klen; u ++) {1620z |= k[u];1621}1622if (klen == 32) {1623c = 0;1624for (u = 0; u < klen; u ++) {1625c |= -(int32_t)EQ0(c) & CMP(k[u], P256_N[u]);1626}1627} else {1628c = -1;1629}1630return NEQ(z, 0) & LT0(c);1631}16321633static uint32_t1634api_mul(unsigned char *G, size_t Glen,1635const unsigned char *k, size_t klen, int curve)1636{1637uint32_t r;1638p256_jacobian P;16391640(void)curve;1641if (Glen != 65) {1642return 0;1643}1644r = check_scalar(k, klen);1645r &= point_decode(&P, G);1646p256_mul(&P, k, klen);1647r &= point_encode(G, &P);1648return r;1649}16501651static size_t1652api_mulgen(unsigned char *R,1653const unsigned char *k, size_t klen, int curve)1654{1655p256_jacobian P;16561657(void)curve;1658p256_mulgen(&P, k, klen);1659point_encode(R, &P);1660return 65;1661}16621663static uint32_t1664api_muladd(unsigned char *A, const unsigned char *B, size_t len,1665const unsigned char *x, size_t xlen,1666const unsigned char *y, size_t ylen, int curve)1667{1668/*1669* We might want to use Shamir's trick here: make a composite1670* window of u*P+v*Q points, to merge the two doubling-ladders1671* into one. This, however, has some complications:1672*1673* - During the computation, we may hit the point-at-infinity.1674* Thus, we would need p256_add_complete_mixed() (complete1675* formulas for point addition), with a higher cost (17 muls1676* instead of 11).1677*1678* - A 4-bit window would be too large, since it would involve1679* 16*16-1 = 255 points. For the same window size as in the1680* p256_mul() case, we would need to reduce the window size1681* to 2 bits, and thus perform twice as many non-doubling1682* point additions.1683*1684* - The window may itself contain the point-at-infinity, and1685* thus cannot be in all generality be made of affine points.1686* Instead, we would need to make it a window of points in1687* Jacobian coordinates. Even p256_add_complete_mixed() would1688* be inappropriate.1689*1690* For these reasons, the code below performs two separate1691* point multiplications, then computes the final point addition1692* (which is both a "normal" addition, and a doubling, to handle1693* all cases).1694*/16951696p256_jacobian P, Q;1697uint32_t r, t, s;1698uint64_t z;16991700(void)curve;1701if (len != 65) {1702return 0;1703}1704r = point_decode(&P, A);1705p256_mul(&P, x, xlen);1706if (B == NULL) {1707p256_mulgen(&Q, y, ylen);1708} else {1709r &= point_decode(&Q, B);1710p256_mul(&Q, y, ylen);1711}17121713/*1714* The final addition may fail in case both points are equal.1715*/1716t = p256_add(&P, &Q);1717f256_final_reduce(P.z);1718z = P.z[0] | P.z[1] | P.z[2] | P.z[3] | P.z[4];1719s = EQ((uint32_t)(z | (z >> 32)), 0);1720p256_double(&Q);17211722/*1723* If s is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we1724* have the following:1725*1726* s = 0, t = 0 return P (normal addition)1727* s = 0, t = 1 return P (normal addition)1728* s = 1, t = 0 return Q (a 'double' case)1729* s = 1, t = 1 report an error (P+Q = 0)1730*/1731CCOPY(s & ~t, &P, &Q, sizeof Q);1732point_encode(A, &P);1733r &= ~(s & t);1734return r;1735}17361737/* see bearssl_ec.h */1738const br_ec_impl br_ec_p256_m62 = {1739(uint32_t)0x00800000,1740&api_generator,1741&api_order,1742&api_xoff,1743&api_mul,1744&api_mulgen,1745&api_muladd1746};17471748/* see bearssl_ec.h */1749const br_ec_impl *1750br_ec_p256_m62_get(void)1751{1752return &br_ec_p256_m62;1753}17541755#else17561757/* see bearssl_ec.h */1758const br_ec_impl *1759br_ec_p256_m62_get(void)1760{1761return 0;1762}17631764#endif176517661767