mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-27 08:32:33 +00:00
92 lines
2.7 KiB
C
92 lines
2.7 KiB
C
|
#include "crypto_scalarmult.h"
|
||
|
|
||
|
#define mult crypto_scalarmult_curve25519_athlon_mult
|
||
|
#define square crypto_scalarmult_curve25519_athlon_square
|
||
|
|
||
|
void crypto_scalarmult_curve25519_athlon_recip(double out[10],const double z[10])
|
||
|
{
|
||
|
double z2[10];
|
||
|
double z9[10];
|
||
|
double z11[10];
|
||
|
double z2_5_0[10];
|
||
|
double z2_10_0[10];
|
||
|
double z2_20_0[10];
|
||
|
double z2_50_0[10];
|
||
|
double z2_100_0[10];
|
||
|
double t0[10];
|
||
|
double t1[10];
|
||
|
int i;
|
||
|
|
||
|
/* 2 */ square(z2,z);
|
||
|
/* 4 */ square(t1,z2);
|
||
|
/* 8 */ square(t0,t1);
|
||
|
/* 9 */ mult(z9,t0,z);
|
||
|
/* 11 */ mult(z11,z9,z2);
|
||
|
/* 22 */ square(t0,z11);
|
||
|
/* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
|
||
|
|
||
|
/* 2^6 - 2^1 */ square(t0,z2_5_0);
|
||
|
/* 2^7 - 2^2 */ square(t1,t0);
|
||
|
/* 2^8 - 2^3 */ square(t0,t1);
|
||
|
/* 2^9 - 2^4 */ square(t1,t0);
|
||
|
/* 2^10 - 2^5 */ square(t0,t1);
|
||
|
/* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
|
||
|
|
||
|
/* 2^11 - 2^1 */ square(t0,z2_10_0);
|
||
|
/* 2^12 - 2^2 */ square(t1,t0);
|
||
|
/* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
|
||
|
/* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
|
||
|
|
||
|
/* 2^21 - 2^1 */ square(t0,z2_20_0);
|
||
|
/* 2^22 - 2^2 */ square(t1,t0);
|
||
|
/* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
|
||
|
/* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
|
||
|
|
||
|
/* 2^41 - 2^1 */ square(t1,t0);
|
||
|
/* 2^42 - 2^2 */ square(t0,t1);
|
||
|
/* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
|
||
|
/* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
|
||
|
|
||
|
/* 2^51 - 2^1 */ square(t0,z2_50_0);
|
||
|
/* 2^52 - 2^2 */ square(t1,t0);
|
||
|
/* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
|
||
|
/* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
|
||
|
|
||
|
/* 2^101 - 2^1 */ square(t1,z2_100_0);
|
||
|
/* 2^102 - 2^2 */ square(t0,t1);
|
||
|
/* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
|
||
|
/* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
|
||
|
|
||
|
/* 2^201 - 2^1 */ square(t0,t1);
|
||
|
/* 2^202 - 2^2 */ square(t1,t0);
|
||
|
/* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
|
||
|
/* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
|
||
|
|
||
|
/* 2^251 - 2^1 */ square(t1,t0);
|
||
|
/* 2^252 - 2^2 */ square(t0,t1);
|
||
|
/* 2^253 - 2^3 */ square(t1,t0);
|
||
|
/* 2^254 - 2^4 */ square(t0,t1);
|
||
|
/* 2^255 - 2^5 */ square(t1,t0);
|
||
|
/* 2^255 - 21 */ mult(out,t1,z11);
|
||
|
}
|
||
|
|
||
|
int crypto_scalarmult(unsigned char *q,
|
||
|
const unsigned char *n,
|
||
|
const unsigned char *p)
|
||
|
{
|
||
|
double work[30];
|
||
|
unsigned char e[32];
|
||
|
int i;
|
||
|
for (i = 0;i < 32;++i) e[i] = n[i];
|
||
|
e[0] &= 248;
|
||
|
e[31] &= 127;
|
||
|
e[31] |= 64;
|
||
|
crypto_scalarmult_curve25519_athlon_init();
|
||
|
crypto_scalarmult_curve25519_athlon_todouble(work,p);
|
||
|
crypto_scalarmult_curve25519_athlon_mainloop(work,e);
|
||
|
crypto_scalarmult_curve25519_athlon_recip(work + 10,work + 10);
|
||
|
mult(work + 20,work,work + 10);
|
||
|
crypto_scalarmult_curve25519_athlon_fromdouble(q,work + 20);
|
||
|
return 0;
|
||
|
}
|