bigint.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /* bigint.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file bigint.c
  18. * \author Daniel Otte
  19. * \date 2010-02-22
  20. *
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #define STRING2(x) #x
  25. #define STRING(x) STRING2(x)
  26. #define STR_LINE STRING(__LINE__)
  27. #include <crypto/bigint.h>
  28. #include <string.h>
  29. #define DEBUG 0
  30. #if DEBUG
  31. #include <crypto/cli.h>
  32. #include <crypto/bigint_io.h>
  33. #endif
  34. #ifndef MAX
  35. #define MAX(a,b) (((a)>(b))?(a):(b))
  36. #endif
  37. #ifndef MIN
  38. #define MIN(a,b) (((a)<(b))?(a):(b))
  39. #endif
  40. #define SET_FBS(a, v) do{(a)->info &=~BIGINT_FBS_MASK; (a)->info |= (v);}while(0)
  41. #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
  42. #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
  43. #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
  44. #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
  45. #define XCHG_PTR(a,b) do{ a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
  46. b = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
  47. a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b)));}while(0)
  48. #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
  49. /******************************************************************************/
  50. void bigint_adjust(bigint_t* a){
  51. while(a->length_W!=0 && a->wordv[a->length_W-1]==0){
  52. a->length_W--;
  53. }
  54. if(a->length_W==0){
  55. a->info=0;
  56. return;
  57. }
  58. bigint_word_t t;
  59. uint8_t i = BIGINT_WORD_SIZE-1;
  60. t = a->wordv[a->length_W-1];
  61. while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){
  62. t<<=1;
  63. i--;
  64. }
  65. SET_FBS(a, i);
  66. }
  67. /******************************************************************************/
  68. uint16_t bigint_length_b(const bigint_t* a){
  69. if(!a->length_W || a->length_W==0){
  70. return 0;
  71. }
  72. return (a->length_W-1) * BIGINT_WORD_SIZE + GET_FBS(a);
  73. }
  74. /******************************************************************************/
  75. uint16_t bigint_length_B(const bigint_t* a){
  76. return a->length_W * sizeof(bigint_word_t);
  77. }
  78. /******************************************************************************/
  79. uint32_t bigint_get_first_set_bit(const bigint_t* a){
  80. if(a->length_W==0){
  81. return (uint32_t)(-1);
  82. }
  83. return (a->length_W-1)*sizeof(bigint_word_t)*8+GET_FBS(a);
  84. }
  85. /******************************************************************************/
  86. uint32_t bigint_get_last_set_bit(const bigint_t* a){
  87. uint32_t r=0;
  88. uint8_t b=0;
  89. bigint_word_t x=1;
  90. if(a->length_W==0){
  91. return (uint32_t)(-1);
  92. }
  93. while(a->wordv[r]==0 && r<a->length_W){
  94. ++r;
  95. }
  96. if(a->wordv[r] == 0){
  97. return (uint32_t)(-1);
  98. }
  99. while((x&a->wordv[r])==0){
  100. ++b;
  101. x <<= 1;
  102. }
  103. return r*BIGINT_WORD_SIZE+b;
  104. }
  105. /******************************************************************************/
  106. void bigint_copy(bigint_t* dest, const bigint_t* src){
  107. memcpy(dest->wordv, src->wordv, src->length_W*sizeof(bigint_word_t));
  108. dest->length_W = src->length_W;
  109. dest->info = src->info;
  110. }
  111. /******************************************************************************/
  112. /* this should be implemented in assembly */
  113. void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  114. uint16_t i;
  115. bigint_wordplus_t t=0LL;
  116. if(a->length_W < b->length_W){
  117. XCHG_PTR(a,b);
  118. }
  119. for(i=0; i<b->length_W; ++i){
  120. // t = (bigint_wordplus_t)(a->wordv[i]) + (bigint_wordplus_t)(b->wordv[i]) + t;
  121. t += a->wordv[i];
  122. t += b->wordv[i];
  123. dest->wordv[i] = (bigint_word_t)t;
  124. t>>=BIGINT_WORD_SIZE;
  125. }
  126. for(; i<a->length_W; ++i){
  127. t += a->wordv[i];
  128. dest->wordv[i] = (bigint_word_t)t;
  129. t>>=BIGINT_WORD_SIZE;
  130. }
  131. if(t){
  132. dest->wordv[i++] = (bigint_word_t)t;
  133. }
  134. dest->length_W = i;
  135. bigint_adjust(dest);
  136. }
  137. /******************************************************************************/
  138. /* this should be implemented in assembly */
  139. void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
  140. if(a->length_W == 0){
  141. return;
  142. }
  143. if(scale == 0){
  144. bigint_add_u(dest, dest, a);
  145. return;
  146. }
  147. bigint_t x;
  148. #if BIGINT_WORD_SIZE == 8
  149. memset(dest->wordv + dest->length_W, 0, MAX(dest->length_W, a->length_W + scale) - dest->length_W);
  150. x.wordv = dest->wordv + scale;
  151. x.length_W = dest->length_W - scale;
  152. if((int16_t)x.length_W < 0){
  153. x.length_W = 0;
  154. x.info = 0;
  155. } else {
  156. x.info = dest->info;
  157. }
  158. bigint_add_u(&x, &x, a);
  159. dest->length_W = x.length_W + scale;
  160. dest->info = 0;
  161. bigint_adjust(dest);
  162. #else
  163. bigint_t s;
  164. uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t);
  165. bigint_word_t bv[a->length_W + 1];
  166. s.wordv = bv;
  167. bv[0] = bv[a->length_W] = 0;
  168. memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_W * sizeof(bigint_word_t));
  169. s.length_W = a->length_W + 1;
  170. bigint_adjust(&s);
  171. memset(dest->wordv + dest->length_W, 0, (MAX(dest->length_W, s.length_W + word_shift) - dest->length_W) * sizeof(bigint_word_t));
  172. x.wordv = dest->wordv + word_shift;
  173. x.length_W = dest->length_W - word_shift;
  174. if((int16_t)x.length_W < 0){
  175. x.length_W = 0;
  176. x.info = 0;
  177. }else{
  178. x.info = dest->info;
  179. }
  180. bigint_add_u(&x, &x, &s);
  181. dest->length_W = x.length_W + word_shift;
  182. dest->info = 0;
  183. bigint_adjust(dest);
  184. #endif
  185. /* uint16_t i,j=0;
  186. uint16_t scale_w;
  187. bigint_word_t *dst;
  188. bigint_wordplus_t t=0;
  189. scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
  190. if(scale>dest->length_W*sizeof(bigint_word_t)){
  191. memset(((uint8_t*)dest->wordv)+dest->length_W*sizeof(bigint_word_t), 0, scale-dest->length_W*sizeof(bigint_word_t));
  192. }
  193. // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3));
  194. dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1));
  195. for(i=scale/sizeof(bigint_word_t); i<a->length_W+scale_w; ++i,++j){
  196. t += a->wordv[j];
  197. if(dest->length_W>i){
  198. t += dst[i];
  199. }
  200. dst[i] = (bigint_word_t)t;
  201. t>>=BIGINT_WORD_SIZE;
  202. }
  203. while(t){
  204. if(dest->length_W>i){
  205. t += dst[i];
  206. }
  207. dst[i] = (bigint_word_t)t;
  208. t>>=BIGINT_WORD_SIZE;
  209. ++i;
  210. }
  211. if(dest->length_W < i){
  212. dest->length_W = i;
  213. }
  214. bigint_adjust(dest);
  215. */
  216. }
  217. /******************************************************************************/
  218. /* this should be implemented in assembly */
  219. void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  220. int8_t borrow=0;
  221. int8_t r;
  222. bigint_wordplus_signed_t t=0LL;
  223. uint16_t i, min, max;
  224. min = MIN(a->length_W, b->length_W);
  225. max = MAX(a->length_W, b->length_W);
  226. r = bigint_cmp_u(a,b);
  227. if(r==0){
  228. bigint_set_zero(dest);
  229. return;
  230. }
  231. if(b->length_W==0){
  232. bigint_copy(dest, a);
  233. SET_POS(dest);
  234. return;
  235. }
  236. if(a->length_W==0){
  237. bigint_copy(dest, b);
  238. SET_NEG(dest);
  239. return;
  240. }
  241. if(r<0){
  242. bigint_sub_u(dest, b, a);
  243. SET_NEG(dest);
  244. return;
  245. }
  246. for(i=0; i<max; ++i){
  247. t = a->wordv[i];
  248. if(i<min){
  249. t -= b->wordv[i];
  250. }
  251. t -= borrow;
  252. dest->wordv[i]=(bigint_word_t)t;
  253. if(t<0){
  254. borrow = 1;
  255. }else{
  256. borrow = 0;
  257. }
  258. }
  259. SET_POS(dest);
  260. dest->length_W = i;
  261. bigint_adjust(dest);
  262. }
  263. /******************************************************************************/
  264. int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
  265. if(a->length_W > b->length_W){
  266. return 1;
  267. }
  268. if(a->length_W < b->length_W){
  269. return -1;
  270. }
  271. if(a->length_W==0){
  272. return 0;
  273. }
  274. uint16_t i;
  275. i = a->length_W-1;
  276. do{
  277. if(a->wordv[i] != b->wordv[i]){
  278. if(a->wordv[i] > b->wordv[i]){
  279. return 1;
  280. }else{
  281. return -1;
  282. }
  283. }
  284. }while(i--);
  285. return 0;
  286. }
  287. /******************************************************************************/
  288. void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  289. uint8_t s;
  290. s = GET_SIGN(a)?2:0;
  291. s |= GET_SIGN(b)?1:0;
  292. switch(s){
  293. case 0: /* both positive */
  294. bigint_add_u(dest, a,b);
  295. SET_POS(dest);
  296. break;
  297. case 1: /* a positive, b negative */
  298. bigint_sub_u(dest, a, b);
  299. break;
  300. case 2: /* a negative, b positive */
  301. bigint_sub_u(dest, b, a);
  302. break;
  303. case 3: /* both negative */
  304. bigint_add_u(dest, a, b);
  305. SET_NEG(dest);
  306. break;
  307. default: /* how can this happen?*/
  308. break;
  309. }
  310. }
  311. /******************************************************************************/
  312. void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  313. uint8_t s;
  314. s = GET_SIGN(a)?2:0;
  315. s |= GET_SIGN(b)?1:0;
  316. switch(s){
  317. case 0: /* both positive */
  318. bigint_sub_u(dest, a,b);
  319. break;
  320. case 1: /* a positive, b negative */
  321. bigint_add_u(dest, a, b);
  322. SET_POS(dest);
  323. break;
  324. case 2: /* a negative, b positive */
  325. bigint_add_u(dest, a, b);
  326. SET_NEG(dest);
  327. break;
  328. case 3: /* both negative */
  329. bigint_sub_u(dest, b, a);
  330. break;
  331. default: /* how can this happen?*/
  332. break;
  333. }
  334. }
  335. /******************************************************************************/
  336. int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
  337. uint8_t s;
  338. if(a->length_W==0 && b->length_W==0){
  339. return 0;
  340. }
  341. s = GET_SIGN(a)?2:0;
  342. s |= GET_SIGN(b)?1:0;
  343. switch(s){
  344. case 0: /* both positive */
  345. return bigint_cmp_u(a, b);
  346. break;
  347. case 1: /* a positive, b negative */
  348. return 1;
  349. break;
  350. case 2: /* a negative, b positive */
  351. return -1;
  352. break;
  353. case 3: /* both negative */
  354. return bigint_cmp_u(b, a);
  355. break;
  356. default: /* how can this happen?*/
  357. break;
  358. }
  359. return 0; /* just to satisfy the compiler */
  360. }
  361. /******************************************************************************/
  362. void bigint_shiftleft(bigint_t* a, uint16_t shift){
  363. uint16_t byteshift, word_alloc, words_to_shift;
  364. int16_t i;
  365. uint8_t bitshift;
  366. bigint_word_t *p;
  367. bigint_wordplus_t t=0;
  368. if(shift==0){
  369. return;
  370. }
  371. byteshift = shift/8;
  372. bitshift = shift&7;
  373. for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
  374. a->wordv[a->length_W+i] = 0;
  375. }
  376. if(byteshift){
  377. memmove(((uint8_t*)a->wordv) + byteshift, a->wordv, a->length_W * sizeof(bigint_word_t));
  378. memset(a->wordv, 0, byteshift);
  379. }
  380. p = a->wordv + byteshift / sizeof(bigint_word_t);
  381. words_to_shift = a->length_W + (byteshift % sizeof(bigint_word_t)?1:0);
  382. word_alloc = a->length_W + (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t) + 1;
  383. a->wordv[word_alloc-1]=0;
  384. if(bitshift!=0){
  385. for(i=0; i < words_to_shift; ++i){
  386. t |= ((bigint_wordplus_t)p[i])<<bitshift;
  387. p[i] = (bigint_word_t)t;
  388. t >>= BIGINT_WORD_SIZE;
  389. }
  390. p[i] = (bigint_word_t)t;
  391. }
  392. a->length_W = word_alloc;
  393. bigint_adjust(a);
  394. }
  395. /******************************************************************************/
  396. void bigint_shiftright(bigint_t* a, uint16_t shift){
  397. uint16_t byteshift;
  398. uint16_t i;
  399. uint8_t bitshift;
  400. bigint_wordplus_t t=0;
  401. byteshift = shift/8;
  402. bitshift = shift&7;
  403. if(byteshift >= a->length_W * sizeof(bigint_word_t)){ /* we would shift out more than we have */
  404. bigint_set_zero(a);
  405. return;
  406. }
  407. if(byteshift == a->length_W * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){
  408. bigint_set_zero(a);
  409. return;
  410. }
  411. if(byteshift){
  412. memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift);
  413. memset((uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - byteshift, 0, byteshift);
  414. }
  415. byteshift /= sizeof(bigint_word_t);
  416. a->length_W -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
  417. if(bitshift != 0 && a->length_W){
  418. /* shift to the right */
  419. i = a->length_W - 1;
  420. do{
  421. t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift);
  422. a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE);
  423. t <<= BIGINT_WORD_SIZE;
  424. }while(i--);
  425. }
  426. bigint_adjust(a);
  427. }
  428. /******************************************************************************/
  429. void bigint_xor(bigint_t* dest, const bigint_t* a){
  430. uint16_t i;
  431. for(i=0; i<a->length_W; ++i){
  432. dest->wordv[i] ^= a->wordv[i];
  433. }
  434. bigint_adjust(dest);
  435. }
  436. /******************************************************************************/
  437. void bigint_set_zero(bigint_t* a){
  438. a->length_W=0;
  439. }
  440. /******************************************************************************/
  441. /* using the Karatsuba-Algorithm */
  442. /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
  443. void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  444. if(a->length_W==0 || b->length_W==0){
  445. bigint_set_zero(dest);
  446. return;
  447. }
  448. if(dest==a || dest==b){
  449. bigint_t d;
  450. bigint_word_t d_b[a->length_W+b->length_W];
  451. d.wordv = d_b;
  452. bigint_mul_u(&d, a, b);
  453. bigint_copy(dest, &d);
  454. return;
  455. }
  456. if(a->length_W==1 || b->length_W==1){
  457. if(a->length_W!=1){
  458. XCHG_PTR(a,b);
  459. }
  460. bigint_wordplus_t t=0;
  461. uint16_t i;
  462. bigint_word_t x = a->wordv[0];
  463. for(i=0; i < b->length_W; ++i){
  464. t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
  465. dest->wordv[i] = (bigint_word_t)t;
  466. t>>=BIGINT_WORD_SIZE;
  467. }
  468. dest->wordv[i] = (bigint_word_t)t;
  469. dest->length_W = i+1;
  470. dest->info = 0;
  471. bigint_adjust(dest);
  472. return;
  473. }
  474. if(a->length_W * sizeof(bigint_word_t) <= 4 && b->length_W * sizeof(bigint_word_t) <= 4){
  475. uint32_t p=0, q=0;
  476. uint64_t r;
  477. memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
  478. memcpy(&q, b->wordv, b->length_W*sizeof(bigint_word_t));
  479. r = (uint64_t)p * (uint64_t)q;
  480. memcpy(dest->wordv, &r, (dest->length_W = a->length_W + b->length_W)*sizeof(bigint_word_t));
  481. bigint_adjust(dest);
  482. return;
  483. }
  484. bigint_set_zero(dest);
  485. /* split a in xh & xl; split b in yh & yl */
  486. const uint16_t n = (MAX(a->length_W, b->length_W)+1)/2;
  487. bigint_t xl, xh, yl, yh;
  488. xl.wordv = a->wordv;
  489. yl.wordv = b->wordv;
  490. if(a->length_W<=n){
  491. bigint_set_zero(&xh);
  492. xl.length_W = a->length_W;
  493. xl.info = a->info;
  494. }else{
  495. xl.length_W=n;
  496. xl.info = 0;
  497. bigint_adjust(&xl);
  498. xh.wordv = &(a->wordv[n]);
  499. xh.length_W = a->length_W-n;
  500. xh.info = a->info;
  501. }
  502. if(b->length_W<=n){
  503. bigint_set_zero(&yh);
  504. yl.length_W = b->length_W;
  505. yl.info = b->info;
  506. }else{
  507. yl.length_W=n;
  508. yl.info = 0;
  509. bigint_adjust(&yl);
  510. yh.wordv = &(b->wordv[n]);
  511. yh.length_W = b->length_W-n;
  512. yh.info = b->info;
  513. }
  514. /* now we have split up a and b */
  515. /* remember we want to do:
  516. * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl
  517. * 5 9 2 4 3 7 5 6 1 8 1
  518. */
  519. bigint_word_t tmp_b[2*n+2], m_b[2*(n+1)];
  520. bigint_t tmp, tmp2, m;
  521. tmp.wordv = tmp_b;
  522. tmp2.wordv = &(tmp_b[n+1]);
  523. m.wordv = m_b;
  524. bigint_mul_u(dest, &xl, &yl); /* 1: dest <= xl*yl */
  525. bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl */
  526. bigint_add_u(&tmp, &yh, &yl); /* 3: tmp <= yh+yl */
  527. bigint_mul_u(&m, &tmp2, &tmp); /* 4: m <= tmp2*tmp */
  528. bigint_mul_u(&tmp, &xh, &yh); /* 5: h <= xh*yh */
  529. bigint_sub_u(&m, &m, dest); /* 6: m <= m-dest */
  530. bigint_sub_u(&m, &m, &tmp); /* 7: m <= m-h */
  531. bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
  532. bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
  533. }
  534. /******************************************************************************/
  535. void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
  536. uint8_t s;
  537. s = GET_SIGN(a)?2:0;
  538. s |= GET_SIGN(b)?1:0;
  539. switch(s){
  540. case 0: /* both positive */
  541. bigint_mul_u(dest, a,b);
  542. SET_POS(dest);
  543. break;
  544. case 1: /* a positive, b negative */
  545. bigint_mul_u(dest, a,b);
  546. SET_NEG(dest);
  547. break;
  548. case 2: /* a negative, b positive */
  549. bigint_mul_u(dest, a,b);
  550. SET_NEG(dest);
  551. break;
  552. case 3: /* both negative */
  553. bigint_mul_u(dest, a,b);
  554. SET_POS(dest);
  555. break;
  556. default: /* how can this happen?*/
  557. break;
  558. }
  559. }
  560. /******************************************************************************/
  561. /* square */
  562. /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
  563. void bigint_square(bigint_t* dest, const bigint_t* a){
  564. if(a->length_W*sizeof(bigint_word_t)<=4){
  565. uint64_t r=0;
  566. memcpy(&r, a->wordv, a->length_W*sizeof(bigint_word_t));
  567. r = r*r;
  568. memcpy(dest->wordv, &r, 2*a->length_W*sizeof(bigint_word_t));
  569. SET_POS(dest);
  570. dest->length_W=2*a->length_W;
  571. bigint_adjust(dest);
  572. return;
  573. }
  574. if(dest==a){
  575. bigint_t d;
  576. bigint_word_t d_b[a->length_W*2];
  577. d.wordv = d_b;
  578. bigint_square(&d, a);
  579. bigint_copy(dest, &d);
  580. return;
  581. }
  582. uint16_t n;
  583. n=(a->length_W+1)/2;
  584. bigint_t xh, xl, tmp; /* x-high, x-low, temp */
  585. bigint_word_t buffer[2*n+1];
  586. xl.wordv = a->wordv;
  587. xl.length_W = n;
  588. xl.info = 0;
  589. xh.wordv = &(a->wordv[n]);
  590. xh.length_W = a->length_W-n;
  591. xh.info = 0;
  592. bigint_adjust(&xl);
  593. bigint_adjust(&xh);
  594. tmp.wordv = buffer;
  595. /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
  596. // cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
  597. // cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
  598. bigint_square(dest, &xl);
  599. // cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
  600. bigint_square(&tmp, &xh);
  601. // cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
  602. bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
  603. // cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
  604. bigint_mul_u(&tmp, &xl, &xh);
  605. // cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
  606. bigint_shiftleft(&tmp, 1);
  607. // cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
  608. bigint_add_scale_u(dest, &tmp, n*sizeof(bigint_word_t));
  609. // cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
  610. // cli_putstr("\r\n");
  611. }
  612. /******************************************************************************/
  613. void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
  614. bigint_t tmp, x;
  615. bigint_word_t tmp_b[b->length_W + 1];
  616. const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
  617. if(a->length_W < b->length_W + word_shift){
  618. #if DEBUG
  619. cli_putstr("\r\nDBG: *bang*\r\n");
  620. #endif
  621. bigint_set_zero(a);
  622. return;
  623. }
  624. tmp.wordv = tmp_b;
  625. bigint_copy(&tmp, b);
  626. bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
  627. x.info = a->info;
  628. x.wordv = &(a->wordv[word_shift]);
  629. x.length_W = a->length_W - word_shift;
  630. bigint_sub_u(&x, &x, &tmp);
  631. bigint_adjust(a);
  632. return;
  633. }
  634. /******************************************************************************/
  635. void bigint_reduce(bigint_t* a, const bigint_t* r){
  636. // bigint_adjust((bigint_t*)r);
  637. uint8_t rfbs = GET_FBS(r);
  638. #if DEBUG
  639. cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
  640. #endif
  641. if(r->length_W==0 || a->length_W==0){
  642. return;
  643. }
  644. if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){
  645. uint32_t p=0, q=0;
  646. memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
  647. memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t));
  648. p %= q;
  649. memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t));
  650. bigint_adjust(a);
  651. // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
  652. return;
  653. }
  654. uint16_t shift;
  655. while(a->length_W > r->length_W){
  656. shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
  657. /*
  658. if((a->wordv[a->length_W-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_W-1]){
  659. // cli_putc('~');
  660. cli_putstr("\r\n ~ [a] = ");
  661. cli_hexdump_rev(&a->wordv[a->length_W-1], 4);
  662. cli_putstr(" [r] = ");
  663. cli_hexdump_rev(&r->wordv[r->length_W-1], 4);
  664. shift += 1;
  665. }
  666. */
  667. // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
  668. // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2);
  669. // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2);
  670. // uart_flush(0);
  671. bigint_sub_u_bitscale(a, r, shift);
  672. // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
  673. }
  674. while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){
  675. shift = GET_FBS(a)-rfbs-1;
  676. // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
  677. bigint_sub_u_bitscale(a, r, shift);
  678. // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
  679. }
  680. while(bigint_cmp_u(a,r)>=0){
  681. bigint_sub_u(a,a,r);
  682. // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
  683. }
  684. bigint_adjust(a);
  685. // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
  686. // cli_putstr("\r\n");
  687. }
  688. /******************************************************************************/
  689. /* calculate dest = a**exp % r */
  690. /* using square&multiply */
  691. void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
  692. if(a->length_W==0 || r->length_W==0){
  693. return;
  694. }
  695. bigint_t res, base;
  696. bigint_word_t t, base_b[MAX(a->length_W,r->length_W)], res_b[r->length_W*2];
  697. uint16_t i;
  698. uint8_t j;
  699. // uint16_t *xaddr = &i;
  700. // cli_putstr("\r\npre-alloc (");
  701. // cli_hexdump_rev(&xaddr, 4);
  702. // cli_putstr(") ...");
  703. res.wordv = res_b;
  704. base.wordv = base_b;
  705. bigint_copy(&base, a);
  706. // cli_putstr("\r\npost-copy");
  707. bigint_reduce(&base, r);
  708. res.wordv[0]=1;
  709. res.length_W=1;
  710. res.info = 0;
  711. bigint_adjust(&res);
  712. if(exp->length_W == 0){
  713. bigint_copy(dest, &res);
  714. return;
  715. }
  716. uint8_t flag = 0;
  717. t=exp->wordv[exp->length_W - 1];
  718. for(i=exp->length_W; i > 0; --i){
  719. t = exp->wordv[i - 1];
  720. for(j=BIGINT_WORD_SIZE; j > 0; --j){
  721. if(!flag){
  722. if(t & (1<<(BIGINT_WORD_SIZE-1))){
  723. flag = 1;
  724. }
  725. }
  726. if(flag){
  727. bigint_square(&res, &res);
  728. bigint_reduce(&res, r);
  729. if(t & (1 << (BIGINT_WORD_SIZE - 1))){
  730. bigint_mul_u(&res, &res, &base);
  731. bigint_reduce(&res, r);
  732. }
  733. }
  734. t <<= 1;
  735. }
  736. }
  737. // cli_putc('+');
  738. SET_POS(&res);
  739. bigint_copy(dest, &res);
  740. }
  741. /******************************************************************************/
  742. #define cli_putstr(a)
  743. #define bigint_print_hex(a)
  744. #define cli_hexdump_rev(a,b)
  745. #define uart_flush(a)
  746. /* gcd <-- gcd(x,y) a*x+b*y=gcd */
  747. void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
  748. bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
  749. uint16_t i=0;
  750. if(x->length_W==0 || y->length_W==0){
  751. return;
  752. }
  753. if(x->length_W==1 && x->wordv[0]==1){
  754. gcd->length_W = 1;
  755. gcd->wordv[0] = 1;
  756. if(a){
  757. a->length_W = 1;
  758. a->wordv[0] = 1;
  759. SET_POS(a);
  760. bigint_adjust(a);
  761. }
  762. if(b){
  763. bigint_set_zero(b);
  764. }
  765. return;
  766. }
  767. if(y->length_W==1 && y->wordv[0]==1){
  768. gcd->length_W = 1;
  769. gcd->wordv[0] = 1;
  770. if(b){
  771. b->length_W = 1;
  772. b->wordv[0] = 1;
  773. SET_POS(b);
  774. bigint_adjust(b);
  775. }
  776. if(a){
  777. bigint_set_zero(a);
  778. }
  779. return;
  780. }
  781. while(x->wordv[i]==0 && y->wordv[i]==0){
  782. ++i;
  783. }
  784. bigint_word_t g_b[i+2], x_b[x->length_W-i], y_b[y->length_W-i];
  785. bigint_word_t u_b[x->length_W-i], v_b[y->length_W-i];
  786. bigint_word_t a_b[y->length_W+2], c_b[y->length_W+2];
  787. bigint_word_t b_b[x->length_W+2], d_b[x->length_W+2];
  788. g.wordv = g_b;
  789. x_.wordv = x_b;
  790. y_.wordv = y_b;
  791. memset(g_b, 0, i*sizeof(bigint_word_t));
  792. g_b[i]=1;
  793. g.length_W = i+1;
  794. g.info=0;
  795. x_.info = y_.info = 0;
  796. x_.length_W = x->length_W-i;
  797. y_.length_W = y->length_W-i;
  798. memcpy(x_.wordv, x->wordv+i, x_.length_W*sizeof(bigint_word_t));
  799. memcpy(y_.wordv, y->wordv+i, y_.length_W*sizeof(bigint_word_t));
  800. for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
  801. }
  802. bigint_adjust(&x_);
  803. bigint_adjust(&y_);
  804. if(i){
  805. bigint_shiftleft(&g, i);
  806. bigint_shiftright(&x_, i);
  807. bigint_shiftright(&y_, i);
  808. }
  809. u.wordv = u_b;
  810. v.wordv = v_b;
  811. a_.wordv = a_b;
  812. b_.wordv = b_b;
  813. c_.wordv = c_b;
  814. d_.wordv = d_b;
  815. bigint_copy(&u, &x_);
  816. bigint_copy(&v, &y_);
  817. a_.wordv[0] = 1;
  818. a_.length_W = 1;
  819. a_.info = 0;
  820. d_.wordv[0] = 1;
  821. d_.length_W = 1;
  822. d_.info = 0;
  823. bigint_set_zero(&b_);
  824. bigint_set_zero(&c_);
  825. do{
  826. cli_putstr("\r\nDBG (gcdext) 0");
  827. while((u.wordv[0]&1)==0){
  828. cli_putstr("\r\nDBG (gcdext) 0.1");
  829. bigint_shiftright(&u, 1);
  830. if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
  831. bigint_add_s(&a_, &a_, &y_);
  832. bigint_sub_s(&b_, &b_, &x_);
  833. }
  834. bigint_shiftright(&a_, 1);
  835. bigint_shiftright(&b_, 1);
  836. }
  837. while((v.wordv[0]&1)==0){
  838. cli_putstr("\r\nDBG (gcdext) 0.2");
  839. bigint_shiftright(&v, 1);
  840. if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
  841. bigint_add_s(&c_, &c_, &y_);
  842. bigint_sub_s(&d_, &d_, &x_);
  843. }
  844. bigint_shiftright(&c_, 1);
  845. bigint_shiftright(&d_, 1);
  846. }
  847. if(bigint_cmp_u(&u, &v)>=0){
  848. bigint_sub_u(&u, &u, &v);
  849. bigint_sub_s(&a_, &a_, &c_);
  850. bigint_sub_s(&b_, &b_, &d_);
  851. }else{
  852. bigint_sub_u(&v, &v, &u);
  853. bigint_sub_s(&c_, &c_, &a_);
  854. bigint_sub_s(&d_, &d_, &b_);
  855. }
  856. }while(u.length_W);
  857. if(gcd){
  858. bigint_mul_s(gcd, &v, &g);
  859. }
  860. if(a){
  861. bigint_copy(a, &c_);
  862. }
  863. if(b){
  864. bigint_copy(b, &d_);
  865. }
  866. }
  867. /******************************************************************************/
  868. void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
  869. bigint_gcdext(NULL, dest, NULL, a, m);
  870. while(dest->info&BIGINT_NEG_MASK){
  871. bigint_add_s(dest, dest, m);
  872. }
  873. }
  874. /******************************************************************************/
  875. void bigint_changeendianess(bigint_t* a){
  876. uint8_t t, *p, *q;
  877. p = (uint8_t*)(a->wordv);
  878. q = p + a->length_W * sizeof(bigint_word_t) - 1;
  879. while(p<q){
  880. t = *p;
  881. *p = *q;
  882. *q = t;
  883. ++p; --q;
  884. }
  885. }
  886. /******************************************************************************/