| 1 |
#include "endianess.h" |
| 2 |
#include <assert.h> |
| 3 |
#include "ed25519.h" |
| 4 |
|
| 5 |
void mul_25519(uint32_t out[10], const uint32_t f[10], const uint32_t g[10]); |
| 6 |
void invert_25519(uint32_t out[10], const uint32_t x[10]); |
| 7 |
int convert_behex_to_le25p5(uint32_t out[10], const char *in); |
| 8 |
int convert_le25p5_to_behex(char **out, uint32_t in[10]); |
| 9 |
void convert_le25p5_to_le8(uint8_t out[32], const uint32_t in[10]); |
| 10 |
void convert_le8_to_le25p5(uint32_t out[10], const uint8_t in[32]); |
| 11 |
|
| 12 |
|
| 13 |
void test_point_add_1(void) |
| 14 |
{ |
| 15 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 16 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 17 |
char PAIx_hex[] = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 18 |
char PAIy_hex[] = "0000000000000000000000000000000000000000000000000000000000000001"; |
| 19 |
|
| 20 |
Point G, Gout; |
| 21 |
uint32_t invz[10]; |
| 22 |
char *c; |
| 23 |
int res; |
| 24 |
|
| 25 |
memset(&G, 0, sizeof G); |
| 26 |
memset(&Gout, 0, sizeof Gout); |
| 27 |
|
| 28 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 29 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 30 |
G.Z[0] = 1; |
| 31 |
mul_25519(G.T, G.X, G.Y); |
| 32 |
|
| 33 |
convert_behex_to_le25p5(Gout.X, PAIx_hex); |
| 34 |
convert_behex_to_le25p5(Gout.Y, PAIy_hex); |
| 35 |
Gout.Z[0] = 1; |
| 36 |
mul_25519(G.T, G.X, G.Y); |
| 37 |
|
| 38 |
res = ed25519_add(&Gout, &G); |
| 39 |
assert(res == 0); |
| 40 |
|
| 41 |
invert_25519(invz, Gout.Z); |
| 42 |
|
| 43 |
|
| 44 |
mul_25519(Gout.X, Gout.X, invz); |
| 45 |
convert_le25p5_to_behex(&c, Gout.X); |
| 46 |
assert(0 == strcmp(c, Gx_hex)); |
| 47 |
free(c); |
| 48 |
|
| 49 |
|
| 50 |
mul_25519(Gout.Y, Gout.Y, invz); |
| 51 |
convert_le25p5_to_behex(&c, Gout.Y); |
| 52 |
assert(0 == strcmp(c, Gy_hex)); |
| 53 |
free(c); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
void test_point_add_2(void) |
| 58 |
{ |
| 59 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 60 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 61 |
char G2x_hex[] = "36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e"; |
| 62 |
char G2y_hex[] = "2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9"; |
| 63 |
|
| 64 |
Point G, G2; |
| 65 |
uint32_t invz[10]; |
| 66 |
char *c; |
| 67 |
int res; |
| 68 |
|
| 69 |
memset(&G, 0, sizeof G); |
| 70 |
memset(&G2, 0, sizeof G2); |
| 71 |
|
| 72 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 73 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 74 |
G.Z[0] = 1; |
| 75 |
mul_25519(G.T, G.X, G.Y); |
| 76 |
|
| 77 |
G2 = G; |
| 78 |
|
| 79 |
res = ed25519_add(&G2, &G); |
| 80 |
assert(res == 0); |
| 81 |
invert_25519(invz, G2.Z); |
| 82 |
|
| 83 |
|
| 84 |
mul_25519(G2.X, G2.X, invz); |
| 85 |
convert_le25p5_to_behex(&c, G2.X); |
| 86 |
assert(0 == strcmp(c, G2x_hex)); |
| 87 |
free(c); |
| 88 |
|
| 89 |
|
| 90 |
mul_25519(G2.Y, G2.Y, invz); |
| 91 |
convert_le25p5_to_behex(&c, G2.Y); |
| 92 |
assert(0 == strcmp(c, G2y_hex)); |
| 93 |
free(c); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
void test_point_double_1(void) |
| 98 |
{ |
| 99 |
char PAIx_hex[] = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 100 |
char PAIy_hex[] = "0000000000000000000000000000000000000000000000000000000000000001"; |
| 101 |
|
| 102 |
Point PAI; |
| 103 |
uint32_t invz[10]; |
| 104 |
char *c; |
| 105 |
int res; |
| 106 |
|
| 107 |
memset(&PAI, 0, sizeof PAI); |
| 108 |
|
| 109 |
convert_behex_to_le25p5(PAI.X, PAIx_hex); |
| 110 |
convert_behex_to_le25p5(PAI.Y, PAIy_hex); |
| 111 |
PAI.Z[0] = 1; |
| 112 |
mul_25519(PAI.T, PAI.X, PAI.Y); |
| 113 |
|
| 114 |
res = ed25519_double(&PAI); |
| 115 |
assert(res == 0); |
| 116 |
invert_25519(invz, PAI.Z); |
| 117 |
|
| 118 |
|
| 119 |
mul_25519(PAI.X, PAI.X, invz); |
| 120 |
convert_le25p5_to_behex(&c, PAI.X); |
| 121 |
assert(0 == strcmp(c, PAIx_hex)); |
| 122 |
free(c); |
| 123 |
|
| 124 |
|
| 125 |
mul_25519(PAI.Y, PAI.Y, invz); |
| 126 |
convert_le25p5_to_behex(&c, PAI.Y); |
| 127 |
assert(0 == strcmp(c, PAIy_hex)); |
| 128 |
free(c); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
void test_point_add_3(void) |
| 133 |
{ |
| 134 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 135 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 136 |
char G2x_hex[] = "36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e"; |
| 137 |
char G2y_hex[] = "2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9"; |
| 138 |
char G3x_hex[] = "67ae9c4a22928f491ff4ae743edac83a6343981981624886ac62485fd3f8e25c"; |
| 139 |
char G3y_hex[] = "1267b1d177ee69aba126a18e60269ef79f16ec176724030402c3684878f5b4d4"; |
| 140 |
|
| 141 |
Point G, G2; |
| 142 |
uint32_t invz[10]; |
| 143 |
char *c; |
| 144 |
int res; |
| 145 |
|
| 146 |
memset(&G, 0, sizeof G); |
| 147 |
memset(&G2, 0, sizeof G2); |
| 148 |
|
| 149 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 150 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 151 |
G.Z[0] = 1; |
| 152 |
mul_25519(G.T, G.X, G.Y); |
| 153 |
|
| 154 |
convert_behex_to_le25p5(G2.X, G2x_hex); |
| 155 |
convert_behex_to_le25p5(G2.Y, G2y_hex); |
| 156 |
G2.Z[0] = 1; |
| 157 |
mul_25519(G2.T, G2.X, G2.Y); |
| 158 |
|
| 159 |
res = ed25519_add(&G2, &G); |
| 160 |
assert(res == 0); |
| 161 |
invert_25519(invz, G2.Z); |
| 162 |
|
| 163 |
|
| 164 |
mul_25519(G2.X, G2.X, invz); |
| 165 |
convert_le25p5_to_behex(&c, G2.X); |
| 166 |
assert(0 == strcmp(c, G3x_hex)); |
| 167 |
free(c); |
| 168 |
|
| 169 |
|
| 170 |
mul_25519(G2.Y, G2.Y, invz); |
| 171 |
convert_le25p5_to_behex(&c, G2.Y); |
| 172 |
assert(0 == strcmp(c, G3y_hex)); |
| 173 |
free(c); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
void test_point_double_2(void) |
| 178 |
{ |
| 179 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 180 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 181 |
char G2x_hex[] = "36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e"; |
| 182 |
char G2y_hex[] = "2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9"; |
| 183 |
|
| 184 |
Point G, G2; |
| 185 |
uint32_t invz[10]; |
| 186 |
char *c; |
| 187 |
int res; |
| 188 |
|
| 189 |
memset(&G, 0, sizeof G); |
| 190 |
|
| 191 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 192 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 193 |
G.Z[0] = 1; |
| 194 |
mul_25519(G.T, G.X, G.Y); |
| 195 |
|
| 196 |
G2 = G; |
| 197 |
|
| 198 |
res = ed25519_double(&G2); |
| 199 |
assert(res == 0); |
| 200 |
invert_25519(invz, G2.Z); |
| 201 |
|
| 202 |
|
| 203 |
mul_25519(G2.X, G2.X, invz); |
| 204 |
convert_le25p5_to_behex(&c, G2.X); |
| 205 |
assert(0 == strcmp(c, G2x_hex)); |
| 206 |
free(c); |
| 207 |
|
| 208 |
|
| 209 |
mul_25519(G2.Y, G2.Y, invz); |
| 210 |
convert_le25p5_to_behex(&c, G2.Y); |
| 211 |
assert(0 == strcmp(c, G2y_hex)); |
| 212 |
free(c); |
| 213 |
} |
| 214 |
|
| 215 |
void from_affine(Point *P, const uint8_t x[32], const uint8_t y[32]) |
| 216 |
{ |
| 217 |
memset(P, 0, sizeof *P); |
| 218 |
convert_le8_to_le25p5(P->X, x); |
| 219 |
convert_le8_to_le25p5(P->Y, y); |
| 220 |
P->Z[0] = 1; |
| 221 |
mul_25519(P->T, P->X, P->Y); |
| 222 |
} |
| 223 |
|
| 224 |
void to_affine(uint8_t x[32], uint8_t y[32], const Point *P) |
| 225 |
{ |
| 226 |
uint32_t invz[10]; |
| 227 |
uint32_t tmp[10]; |
| 228 |
|
| 229 |
invert_25519(invz, P->Z); |
| 230 |
mul_25519(tmp, P->X, invz); |
| 231 |
convert_le25p5_to_le8(x, tmp); |
| 232 |
mul_25519(tmp, P->Y, invz); |
| 233 |
convert_le25p5_to_le8(y, tmp); |
| 234 |
} |
| 235 |
|
| 236 |
void test_scalar_mult(void) |
| 237 |
{ |
| 238 |
uint8_t xout[32], yout[32]; |
| 239 |
|
| 240 |
const uint8_t G0x[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 241 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 242 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 243 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 244 |
const uint8_t G0y[32] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 245 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 246 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 247 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 248 |
const uint8_t Gx[32] = {0x1A, 0xD5, 0x25, 0x8F, 0x60, 0x2D, 0x56, 0xC9, |
| 249 |
0xB2, 0xA7, 0x25, 0x95, 0x60, 0xC7, 0x2C, 0x69, |
| 250 |
0x5C, 0xDC, 0xD6, 0xFD, 0x31, 0xE2, 0xA4, 0xC0, |
| 251 |
0xFE, 0x53, 0x6E, 0xCD, 0xD3, 0x36, 0x69, 0x21}; |
| 252 |
const uint8_t Gy[32] = {0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, |
| 253 |
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, |
| 254 |
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, |
| 255 |
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}; |
| 256 |
const uint8_t G2x[32] = {0x0E, 0xCE, 0x43, 0x28, 0x4E, 0xA1, 0xC5, 0x83, |
| 257 |
0x5F, 0xA4, 0xD7, 0x15, 0x45, 0x8E, 0x0D, 0x08, |
| 258 |
0xAC, 0xE7, 0x33, 0x18, 0x7D, 0x3B, 0x04, 0x3D, |
| 259 |
0x6C, 0x04, 0x5A, 0x9F, 0x4C, 0x38, 0xAB, 0x36}; |
| 260 |
const uint8_t G2y[32] = {0xC9, 0xA3, 0xF8, 0x6A, 0xAE, 0x46, 0x5F, 0x0E, |
| 261 |
0x56, 0x51, 0x38, 0x64, 0x51, 0x0F, 0x39, 0x97, |
| 262 |
0x56, 0x1F, 0xA2, 0xC9, 0xE8, 0x5E, 0xA2, 0x1D, |
| 263 |
0xC2, 0x29, 0x23, 0x09, 0xF3, 0xCD, 0x60, 0x22}; |
| 264 |
const uint8_t G5y[32] = {0xED, 0xC8, 0x76, 0xD6, 0x83, 0x1F, 0xD2, 0x10, |
| 265 |
0x5D, 0x0B, 0x43, 0x89, 0xCA, 0x2E, 0x28, 0x31, |
| 266 |
0x66, 0x46, 0x92, 0x89, 0x14, 0x6E, 0x2C, 0xE0, |
| 267 |
0x6F, 0xAE, 0xFE, 0x98, 0xB2, 0x25, 0x48, 0x5F}; |
| 268 |
const uint8_t Gry[32] = {0xC9, 0x72, 0x8D, 0x51, 0x1D, 0xF5, 0xB3, 0x05, |
| 269 |
0x12, 0xD4, 0x81, 0xCC, 0x41, 0xDE, 0x72, 0x0E, |
| 270 |
0x73, 0x90, 0xF1, 0x53, 0xFE, 0xF6, 0xF0, 0x59, |
| 271 |
0xDC, 0xF4, 0xB8, 0xAF, 0xEE, 0x92, 0x77, 0x16}; |
| 272 |
Point Q; |
| 273 |
int res; |
| 274 |
|
| 275 |
|
| 276 |
from_affine(&Q, Gx, Gy); |
| 277 |
res = ed25519_scalar(&Q, (uint8_t*)"\x00", 1, 0); |
| 278 |
assert(res == 0); |
| 279 |
to_affine(xout, yout, &Q); |
| 280 |
assert(0 == memcmp(G0x, xout, sizeof G0x)); |
| 281 |
assert(0 == memcmp(G0y, yout, sizeof G0y)); |
| 282 |
|
| 283 |
|
| 284 |
from_affine(&Q, Gx, Gy); |
| 285 |
res = ed25519_scalar(&Q, (uint8_t*)"\x01", 1, 0); |
| 286 |
assert(res == 0); |
| 287 |
to_affine(xout, yout, &Q); |
| 288 |
assert(0 == memcmp(Gx, xout, sizeof Gx)); |
| 289 |
assert(0 == memcmp(Gy, yout, sizeof Gy)); |
| 290 |
|
| 291 |
|
| 292 |
from_affine(&Q, Gx, Gy); |
| 293 |
res = ed25519_scalar(&Q, (uint8_t*)"\x02", 1, 0); |
| 294 |
assert(res == 0); |
| 295 |
to_affine(xout, yout, &Q); |
| 296 |
assert(0 == memcmp(G2x, xout, sizeof G2x)); |
| 297 |
assert(0 == memcmp(G2y, yout, sizeof G2y)); |
| 298 |
|
| 299 |
|
| 300 |
from_affine(&Q, Gx, Gy); |
| 301 |
res = ed25519_scalar(&Q, (uint8_t*)"\x05", 1, 0); |
| 302 |
assert(res == 0); |
| 303 |
to_affine(xout, yout, &Q); |
| 304 |
assert(0 == memcmp(G5y, yout, sizeof G5y)); |
| 305 |
|
| 306 |
|
| 307 |
const uint8_t r[32] = {0x08, 0x68, 0xBA, 0x7A, 0x34, 0x73, 0x4F, 0x3E, |
| 308 |
0x93, 0xDD, 0x24, 0x26, 0x32, 0x7F, 0x0F, 0x34, |
| 309 |
0x14, 0x5C, 0xD9, 0x43, 0x02, 0xE4, 0xD5, 0xDD, |
| 310 |
0x95, 0x00, 0xEE, 0x1B, 0x57, 0x11, 0x39, 0xDD}; |
| 311 |
|
| 312 |
from_affine(&Q, Gx, Gy); |
| 313 |
res = ed25519_scalar(&Q, r, 32, 0); |
| 314 |
assert(res == 0); |
| 315 |
to_affine(xout, yout, &Q); |
| 316 |
assert(0 == memcmp(Gry, yout, sizeof Gry)); |
| 317 |
} |
| 318 |
|
| 319 |
void test_cmp_1(void) |
| 320 |
{ |
| 321 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 322 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 323 |
|
| 324 |
Point G, *P; |
| 325 |
uint32_t invz[10]; |
| 326 |
char *c; |
| 327 |
int res; |
| 328 |
|
| 329 |
memset(&G, 0, sizeof G); |
| 330 |
|
| 331 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 332 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 333 |
G.Z[0] = 1; |
| 334 |
mul_25519(G.T, G.X, G.Y); |
| 335 |
|
| 336 |
res = ed25519_clone(&P, &G); |
| 337 |
assert(res == 0); |
| 338 |
|
| 339 |
invert_25519(invz, P->Z); |
| 340 |
|
| 341 |
|
| 342 |
mul_25519(P->X, P->X, invz); |
| 343 |
convert_le25p5_to_behex(&c, P->X); |
| 344 |
assert(0 == strcmp(c, Gx_hex)); |
| 345 |
free(c); |
| 346 |
|
| 347 |
|
| 348 |
mul_25519(P->Y, P->Y, invz); |
| 349 |
convert_le25p5_to_behex(&c, P->Y); |
| 350 |
assert(0 == strcmp(c, Gy_hex)); |
| 351 |
free(c); |
| 352 |
|
| 353 |
ed25519_free_point(P); |
| 354 |
} |
| 355 |
|
| 356 |
void test_neg_1(void) |
| 357 |
{ |
| 358 |
char Gx_hex[] = "216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"; |
| 359 |
char Gy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 360 |
char Gnegx_hex[] = "5e96c92c3291ac013f5b1dce022923a396d3389f6ada584d36a9d29f70da2ad3"; |
| 361 |
char Gnegy_hex[] = "6666666666666666666666666666666666666666666666666666666666666658"; |
| 362 |
|
| 363 |
Point G; |
| 364 |
uint32_t invz[10]; |
| 365 |
char *c; |
| 366 |
int res; |
| 367 |
|
| 368 |
memset(&G, 0, sizeof G); |
| 369 |
|
| 370 |
convert_behex_to_le25p5(G.X, Gx_hex); |
| 371 |
convert_behex_to_le25p5(G.Y, Gy_hex); |
| 372 |
G.Z[0] = 1; |
| 373 |
mul_25519(G.T, G.X, G.Y); |
| 374 |
|
| 375 |
res = ed25519_neg(&G); |
| 376 |
assert(res == 0); |
| 377 |
|
| 378 |
invert_25519(invz, G.Z); |
| 379 |
|
| 380 |
|
| 381 |
mul_25519(G.X, G.X, invz); |
| 382 |
convert_le25p5_to_behex(&c, G.X); |
| 383 |
assert(0 == strcmp(c, Gnegx_hex)); |
| 384 |
free(c); |
| 385 |
|
| 386 |
|
| 387 |
mul_25519(G.Y, G.Y, invz); |
| 388 |
convert_le25p5_to_behex(&c, G.Y); |
| 389 |
assert(0 == strcmp(c, Gnegy_hex)); |
| 390 |
free(c); |
| 391 |
} |
| 392 |
|
| 393 |
int main(void) |
| 394 |
{ |
| 395 |
test_point_add_1(); |
| 396 |
test_point_add_2(); |
| 397 |
test_point_add_3(); |
| 398 |
test_point_double_1(); |
| 399 |
test_point_double_2(); |
| 400 |
test_scalar_mult(); |
| 401 |
test_cmp_1(); |
| 402 |
test_neg_1(); |
| 403 |
return 0; |
| 404 |
} |