OpenCores
URL https://opencores.org/ocsvn/sha_core/sha_core/trunk

Subversion Repositories sha_core

[/] [sha_core/] [trunk/] [src/] [miracl.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marsgod
#ifndef MIRACL_H
2
#define MIRACL_H
3
 
4
/*
5
 *   main MIRACL header - miracl.h.
6
 *
7
 *   Copyright (c) 1988-2001 Shamus Software Ltd.
8
 */
9
 
10
#include "mirdef.h"
11
 
12
#ifdef __ia64__
13
#if MIRACL==64
14
#define MR_ITANIUM
15
#include <ia64intrin.h>
16
#endif
17
#endif
18
 
19
#ifdef MR_FP
20
#include <math.h>
21
#endif
22
 
23
#ifndef MR_NO_FILE_IO
24
#include <stdio.h>
25
#endif
26
               /* error returns */
27
 
28
#define MR_ERR_BASE_TOO_BIG    1
29
#define MR_ERR_DIV_BY_ZERO     2
30
#define MR_ERR_OVERFLOW        3
31
#define MR_ERR_NEG_RESULT      4
32
#define MR_ERR_BAD_FORMAT      5
33
#define MR_ERR_BAD_BASE        6
34
#define MR_ERR_BAD_PARAMETERS  7
35
#define MR_ERR_OUT_OF_MEMORY   8
36
#define MR_ERR_NEG_ROOT        9
37
#define MR_ERR_NEG_POWER       10
38
#define MR_ERR_BAD_ROOT        11
39
#define MR_ERR_INT_OP          12
40
#define MR_ERR_FLASH_OVERFLOW  13
41
#define MR_ERR_TOO_BIG         14
42
#define MR_ERR_NEG_LOG         15
43
#define MR_ERR_DOUBLE_FAIL     16
44
#define MR_ERR_IO_OVERFLOW     17
45
#define MR_ERR_NO_MIRSYS       18
46
#define MR_ERR_BAD_MODULUS     19
47
#define MR_ERR_NO_MODULUS      20
48
#define MR_ERR_EXP_TOO_BIG     21
49
#define MR_ERR_NOT_SUPPORTED   22
50
#define MR_ERR_NOT_DOUBLE_LEN  23
51
#define MR_ERR_NOT_IRREDUC     24
52
#define MR_ERR_NO_ROUNDING     25
53
 
54
               /* some useful definitions */
55
 
56
 
57
 
58
#define forever for(;;)   
59
 
60
#ifndef TRUE
61
  #define TRUE 1
62
#endif
63
#ifndef FALSE
64
  #define FALSE 0
65
#endif
66
 
67
#define OFF 0
68
#define ON 1
69
#define PLUS 1
70
#define MINUS (-1)
71
 
72
#define MR_MAXDEPTH 24
73
                              /* max routine stack depth */
74
/* big and flash variables consist of an encoded length, *
75
 * and an array of mr_smalls containing the digits       */
76
 
77
typedef int BOOL;
78
 
79
#define MR_BYTE unsigned char
80
 
81
#ifdef MR_BITSINCHAR
82
 #if MR_BITSINCHAR == 8
83
  #define MR_TOBYTE(x) ((MR_BYTE)(x))
84
 #else
85
  #define MR_TOBYTE(x) ((MR_BYTE)((x)&0xFF))
86
 #endif
87
#else
88
 #define MR_TOBYTE(x) ((MR_BYTE)(x))
89
#endif
90
 
91
#ifdef MR_FP
92
 
93
  typedef mr_utype mr_small;
94
  #ifdef mr_dltype
95
  typedef mr_dltype mr_large;
96
  #endif
97
 
98
  #define MR_DIV(a,b)    (modf((a)/(b),&dres),dres)
99
 
100
  #ifdef MR_FP_ROUNDING
101
 
102
/* slightly dicey - the optimizer might remove the MAGIC ! */
103
 
104
    #define MR_LROUND(a)   ( ( (a) + MR_MAGIC ) - MR_MAGIC )
105
  #else
106
    #define MR_LROUND(a)   (modfl((a),&ldres),ldres)
107
  #endif
108
 
109
  #define MR_REMAIN(a,b) ((a)-(b)*MR_DIV((a),(b)))
110
 
111
#else
112
 
113
  typedef unsigned mr_utype mr_small;
114
  #ifdef mr_dltype
115
    typedef unsigned mr_dltype mr_large;
116
  #endif
117
 
118
  #define MR_DIV(a,b)    ((a)/(b))
119
  #define MR_REMAIN(a,b) ((a)%(b))
120
  #define MR_LROUND(a)   ((a))
121
#endif
122
 
123
struct bigtype
124
{
125
    mr_unsign32 len;
126
    mr_small *w;
127
};
128
 
129
typedef struct bigtype *big;
130
typedef big zzn;
131
 
132
/* Macro to create big x on the stack - x_t and x_g must be distinct variables
133
   By convention use like this. See brute.c and identity.c for examples
134
 
135
   BIG(x,x_t,x_g,10)
136
   BIG(y,y_t,y_g,10)
137
 
138
*/
139
 
140
#define BIG(x,xt,xg,s) mr_small xg[s]; struct bigtype xt={s,xg}; big x=&xt;
141
 
142
typedef big flash;
143
 
144
#define MR_MSBIT ((mr_unsign32)1<<31)
145
#define MR_OBITS (MR_MSBIT-1)
146
 
147
#if MIRACL >= MR_IBITS
148
#define MR_TOOBIG (1<<(MR_IBITS-2))
149
#else
150
#define MR_TOOBIG (1<<(MIRACL-1))
151
#endif
152
 
153
#ifdef  MR_FLASH
154
#define MR_EBITS (8*sizeof(double) - MR_FLASH)
155
                                  /* no of Bits per double exponent */
156
#define MR_BTS 16
157
#define MR_MSK 0xFFFF
158
 
159
#endif
160
 
161
#define MR_HASH_BYTES     20
162
 
163
/* Marsaglia & Zaman Random number generator */
164
/*         constants      alternatives       */
165
#define NK   37           /* 21 */
166
#define NJ   24           /*  6 */
167
#define NV   14           /*  8 */
168
 
169
 
170
#ifdef MR_LITTLE_ENDIAN
171
#define MR_TOP(x) (*(((mr_small *)&(x))+1))
172
#define MR_BOT(x) (*(((mr_small *)&(x))))
173
#endif
174
#ifdef MR_BIG_ENDIAN
175
#define MR_TOP(x) (*(((mr_small *)&(x))))
176
#define MR_BOT(x) (*(((mr_small *)&(x))+1))
177
#endif
178
 
179
/* chinese remainder theorem structures */
180
 
181
typedef struct {
182
big *C;
183
big *V;
184
big *M;
185
int NP;
186
} big_chinese;
187
 
188
typedef struct {
189
mr_utype *C;
190
mr_utype *V;
191
mr_utype *M;
192
int NP;
193
} small_chinese;
194
 
195
/* Cryptographically strong pseudo-random number generator */
196
 
197
typedef struct {
198
mr_unsign32 ira[NK];  /* random number...   */
199
int         rndptr;   /* ...array & pointer */
200
mr_unsign32 borrow;
201
int pool_ptr;
202
char pool[MR_HASH_BYTES];    /* random pool */
203
} csprng;
204
 
205
/* secure hash Algorithm structure */
206
 
207
typedef struct {
208
mr_unsign32 length[2];
209
mr_unsign32 h[8];
210
mr_unsign32 w[80];
211
} sha256;
212
 
213
typedef sha256 sha;
214
 
215
#ifdef mr_unsign64
216
 
217
typedef struct {
218
mr_unsign64 length[2];
219
mr_unsign64 h[8];
220
mr_unsign64 w[80];
221
} sha512;
222
 
223
typedef sha512 sha384;
224
 
225
#endif
226
 
227
/* advanced encryption algorithm structure */
228
 
229
#define MR_ECB   0
230
#define MR_CBC   1
231
#define MR_CFB1  2
232
#define MR_CFB2  3
233
#define MR_CFB4  5
234
#define MR_PCFB1 10
235
#define MR_PCFB2 11
236
#define MR_PCFB4 13
237
#define MR_OFB1  14
238
#define MR_OFB2  15
239
#define MR_OFB4  17
240
#define MR_OFB8  21
241
#define MR_OFB16 29
242
 
243
typedef struct {
244
int Nk,Nr;
245
int mode;
246
mr_unsign32 fkey[60];
247
mr_unsign32 rkey[60];
248
char f[16];
249
} aes;
250
 
251
 
252
               /* Elliptic curve point status */
253
 
254
#define MR_EPOINT_GENERAL    0
255
#define MR_EPOINT_NORMALIZED 1
256
#define MR_EPOINT_INFINITY   2
257
 
258
#define MR_PROJECTIVE 0
259
#define MR_AFFINE     1
260
 
261
 
262
/* Elliptic Curve epoint structure. Uses projective (X,Y,Z) co-ordinates */
263
 
264
typedef struct {
265
big X;
266
big Y;
267
big Z;
268
int marker;
269
} epoint;
270
 
271
 
272
/* Structure for Brickell method for finite *
273
   field exponentiation with precomputation */
274
 
275
typedef struct {
276
    big *table;
277
    big n;
278
    int base;
279
    int store;
280
} brick;
281
 
282
/* Structure for Brickell method for elliptic *
283
   curve  exponentiation with precomputation  */
284
 
285
typedef struct {
286
    epoint **table;
287
    big a,b,n;
288
    int base;
289
    int store;
290
} ebrick;
291
 
292
typedef struct {
293
    epoint **table;
294
    big a6,a2;
295
    int m,a,b,c;
296
    int base;
297
    int store;
298
} ebrick2;
299
 
300
/* main MIRACL instance structure */
301
 
302
typedef struct {
303
mr_small base;       /* number base     */
304
mr_small apbase;     /* apparent base   */
305
int   pack;          /* packing density */
306
int   lg2b;          /* bits in base    */
307
mr_small base2;      /* 2^mr_lg2b          */
308
BOOL (*user)(void);  /* pointer to user supplied function */
309
 
310
int   nib;           /* length of bigs  */
311
int   depth;                 /* error tracing ..*/
312
int   trace[MR_MAXDEPTH];    /* .. mechanism    */
313
BOOL  check;         /* overflow check  */
314
BOOL  fout;          /* Output to file   */
315
BOOL  fin;           /* Input from file  */
316
BOOL  active;
317
 
318
#ifndef MR_NO_FILE_IO
319
 
320
FILE  *infile;       /* Input file       */
321
FILE  *otfile;       /* Output file      */
322
 
323
#endif
324
 
325
mr_unsign32 ira[NK];  /* random number...   */
326
int         rndptr;   /* ...array & pointer */
327
mr_unsign32 borrow;
328
 
329
            /* Montgomery constants */
330
mr_small ndash;
331
big modulus;
332
BOOL ACTIVE;
333
BOOL MONTY;
334
                       /* Elliptic Curve details  */
335
BOOL SS;               /* True for Super-Singular */
336
big A,B,C;
337
int coord,Asize,Bsize;
338
 
339
int M,AA,BB,CC;     /* for GF(2^m) curves */
340
 
341
int logN;           /* constants for fast fourier fft multiplication */
342
int nprimes,degree;
343
mr_utype *prime,*cr;
344
mr_utype *inverse,**roots;
345
small_chinese chin;
346
mr_utype const1,const2,const3;
347
mr_small msw,lsw;
348
mr_utype **s1,**s2;   /* pre-computed tables for polynomial reduction */
349
mr_utype **t;         /* workspace */
350
mr_utype *wa;
351
mr_utype *wb;
352
mr_utype *wc;
353
BOOL same;
354
BOOL first_one;
355
BOOL debug;
356
 
357
big w0;            /* workspace bigs  */
358
big w1,w2,w3,w4;
359
big w5,w6,w7;
360
big w8,w9,w10,w11;
361
big w12,w13,w14,w15;
362
big w16,w17,w18;
363
 
364
/* User modifiables */
365
 
366
char *IOBUFF; /* i/o buffer    */
367
int  IOBSIZ;  /* size of i/o buffer */
368
BOOL ERCON;        /* error control   */
369
int  ERNUM;        /* last error code */
370
int  NTRY;         /* no. of tries for probablistic primality testing   */
371
int  IOBASE;       /* base for input and output */
372
BOOL EXACT;        /* exact flag      */
373
BOOL RPOINT;       /* =ON for radix point, =OFF for fractions in output */
374
BOOL TRACER;       /* turns trace tracker on/off */
375
int  INPLEN;       /* input length               */
376
int *PRIMES;       /* small primes array         */
377
 
378
#ifdef MR_FLASH
379
int   workprec;
380
int   stprec;        /* start precision */
381
 
382
int RS,RD;
383
double D;
384
 
385
double db,n,p;
386
int a,b,c,d,r,q,oldn,ndig;
387
mr_small u,v,ku,kv;
388
 
389
BOOL last,carryon;
390
flash pi;
391
 
392
 
393
#endif
394
 
395
#ifdef MR_KCM
396
big big_ndash;
397
big ws;
398
#endif
399
 
400
#ifdef MR_FP_ROUNDING
401
mr_large inverse_base;
402
#endif
403
int size;
404
char *workspace;
405
 
406
} miracl;
407
 
408
 
409
#ifndef MR_GENERIC_MT
410
 
411
#ifdef MR_WINDOWS_MT
412
#define MR_OS_THREADS
413
#endif
414
 
415
#ifdef MR_UNIX_MT
416
#define MR_OS_THREADS
417
#endif
418
 
419
#ifndef MR_OS_THREADS
420
 
421
extern miracl *mr_mip;  /* pointer to MIRACL's only global variable */
422
 
423
#endif
424
 
425
#endif
426
 
427
 
428
#ifdef MR_GENERIC_MT
429
 
430
#define _MIPT_  miracl *,
431
#define _MIPTO_ miracl *
432
#define _MIPD_  miracl *mr_mip,
433
#define _MIPDO_ miracl *mr_mip
434
#define _MIPP_  mr_mip,
435
#define _MIPPO_ mr_mip
436
 
437
#else
438
 
439
#define _MIPT_    
440
#define _MIPTO_  void  
441
#define _MIPD_    
442
#define _MIPDO_  void  
443
#define _MIPP_    
444
#define _MIPPO_    
445
 
446
#endif
447
 
448
/* Preamble and exit code for MIRACL routines. *
449
 * Not used if MR_STRIPPED_DOWN is defined     */
450
 
451
#ifdef MR_STRIPPED_DOWN
452
#define MR_OUT
453
#define MR_IN(N)
454
#else
455
#define MR_OUT  mr_mip->depth--;        
456
#define MR_IN(N) mr_mip->depth++; if (mr_mip->depth<MR_MAXDEPTH) {mr_mip->trace[mr_mip->depth]=(N); if (mr_mip->TRACER) mr_track(_MIPPO_); }
457
#endif
458
 
459
/* Function definitions  */
460
 
461
/* Group 0 - Internal routines */
462
 
463
extern void  mr_berror(_MIPT_ int);
464
extern mr_small mr_shiftbits(mr_small,int);
465
extern mr_small mr_setbase(_MIPT_ mr_small);
466
extern void  mr_track(_MIPTO_ );
467
extern void  mr_lzero(big);
468
extern BOOL  mr_notint(flash);
469
extern int   mr_lent(flash);
470
extern void  mr_padd(_MIPT_ big,big,big);
471
extern void  mr_psub(_MIPT_ big,big,big);
472
extern void  mr_pmul(_MIPT_ big,mr_small,big);
473
#ifdef MR_FP_ROUNDING
474
extern mr_large mr_invert(mr_small);
475
extern mr_small imuldiv(mr_small,mr_small,mr_small,mr_small,mr_large,mr_small *);
476
extern mr_small mr_sdiv(_MIPT_ big,mr_small,mr_large,big);
477
#else
478
extern mr_small mr_sdiv(_MIPT_ big,mr_small,big);
479
#endif
480
extern void  mr_shift(_MIPT_ big,int,big);
481
extern miracl *mr_first_alloc(void);
482
extern void  *mr_alloc(_MIPT_ int,int);
483
extern void  mr_free(void *);
484
extern void  set_user_function(_MIPT_ BOOL (*)(void));
485
extern void  set_io_buffer_size(_MIPT_ int);
486
extern int   mr_testbit(_MIPT_ big,int);
487
extern int   mr_window(_MIPT_ big,int,int *,int *);
488
extern int   mr_window2(_MIPT_ big,big,int,int *,int *);
489
extern int   mr_naf_window(_MIPT_ big,big,int,int *,int *);
490
 
491
extern int   mr_fft_init(_MIPT_ int,big,big,BOOL);
492
extern void  mr_dif_fft(_MIPT_ int,int,mr_utype *);
493
extern void  mr_dit_fft(_MIPT_ int,int,mr_utype *);
494
extern void  fft_reset(_MIPTO_);
495
 
496
extern int   mr_poly_mul(_MIPT_ int,big*,int,big*,big*);
497
extern int   mr_poly_sqr(_MIPT_ int,big*,big*);
498
extern void  mr_polymod_set(_MIPT_ int,big*,big*);
499
extern int   mr_poly_rem(_MIPT_ int,big *,big *);
500
 
501
extern int   mr_ps_big_mul(_MIPT_ int,big *,big *,big *);
502
extern int   mr_ps_zzn_mul(_MIPT_ int,big *,big *,big *);
503
 
504
extern mr_small muldiv(mr_small,mr_small,mr_small,mr_small,mr_small *);
505
extern mr_small muldvm(mr_small,mr_small,mr_small,mr_small *);
506
extern mr_small muldvd(mr_small,mr_small,mr_small,mr_small *);
507
extern void     muldvd2(mr_small,mr_small,mr_small *,mr_small *);
508
 
509
/* Group 1 - General purpose, I/O and basic arithmetic routines  */
510
 
511
extern int   igcd(int,int);
512
extern mr_small sgcd(mr_small,mr_small);
513
extern int   isqrt(int,int);
514
extern void  irand(_MIPT_ mr_unsign32);
515
extern mr_small brand(_MIPTO_ );
516
extern void  zero(flash);
517
extern void  convert(_MIPT_ int,big);
518
extern void  lgconv(_MIPT_ long,big);
519
extern flash mirvar(_MIPT_ int);
520
extern flash mirvar_mem(_MIPT_ char *,int);
521
extern void  mirkill(big);
522
extern void  *memalloc(_MIPT_ int);
523
extern void  memkill(_MIPT_ char *,int);
524
extern void  mr_init_threading(void);
525
extern void  mr_end_threading(void);
526
extern miracl *get_mip(_MIPTO_ );
527
extern miracl *mirsys(int,mr_small);
528
extern void  mirexit(_MIPTO_ );
529
extern int   exsign(flash);
530
extern void  insign(int,flash);
531
extern int   getdig(_MIPT_ big,int);
532
extern int   numdig(_MIPT_ big);
533
extern void  putdig(_MIPT_ int,big,int);
534
extern void  copy(flash,flash);
535
extern void  negify(flash,flash);
536
extern void  absol(flash,flash);
537
extern int   size(big);
538
extern int   compare(big,big);
539
extern void  add(_MIPT_ big,big,big);
540
extern void  subtract(_MIPT_ big,big,big);
541
extern void  incr(_MIPT_ big,int,big);
542
extern void  decr(_MIPT_ big,int,big);
543
extern void  premult(_MIPT_ big,int,big);
544
extern int   subdiv(_MIPT_ big,int,big);
545
extern BOOL  subdivisible(_MIPT_ big,int);
546
extern int   remain(_MIPT_ big,int);
547
extern void  bytes_to_big(_MIPT_ int,char *,big);
548
extern int   big_to_bytes(_MIPT_ int,big,char *,BOOL);
549
extern mr_small normalise(_MIPT_ big,big);
550
extern void  multiply(_MIPT_ big,big,big);
551
extern void  fft_mult(_MIPT_ big,big,big);
552
extern BOOL  fastmultop(_MIPT_ int,big,big,big);
553
extern void  divide(_MIPT_ big,big,big);
554
extern BOOL  divisible(_MIPT_ big,big);
555
extern void  mad(_MIPT_ big,big,big,big,big,big);
556
extern int   instr(_MIPT_ flash,char *);
557
extern int   otstr(_MIPT_ flash,char *);
558
extern int   cinstr(_MIPT_ flash,char *);
559
extern int   cotstr(_MIPT_ flash,char *);
560
 
561
#ifndef MR_NO_FILE_IO
562
 
563
extern int   innum(_MIPT_ flash,FILE *);
564
extern int   otnum(_MIPT_ flash,FILE *);
565
extern int   cinnum(_MIPT_ flash,FILE *);
566
extern int   cotnum(_MIPT_ flash,FILE *);
567
 
568
#endif
569
 
570
/* Group 2 - Advanced arithmetic routines */
571
 
572
extern mr_small smul(mr_small,mr_small,mr_small);
573
extern mr_small spmd(mr_small,mr_small,mr_small);
574
extern mr_small invers(mr_small,mr_small);
575
extern mr_small sqrmp(mr_small,mr_small);
576
extern int      jac(mr_small,mr_small);
577
 
578
extern void  gprime(_MIPT_ int);
579
extern int   jack(_MIPT_ big,big);
580
extern int   egcd(_MIPT_ big,big,big);
581
extern int   xgcd(_MIPT_ big,big,big,big,big);
582
extern int   logb2(_MIPT_ big);
583
extern void  expint(_MIPT_ int,int,big);
584
extern void  sftbit(_MIPT_ big,int,big);
585
extern void  power(_MIPT_ big,long,big,big);
586
extern void  powmod(_MIPT_ big,big,big,big);
587
extern void  powmod2(_MIPT_ big,big,big,big,big,big);
588
extern void  powmodn(_MIPT_ int,big *,big *,big,big);
589
extern int   powltr(_MIPT_ int,big,big,big);
590
extern BOOL  double_inverse(_MIPT_ big,big,big,big,big);
591
extern BOOL  multi_inverse(_MIPT_ int,big*,big,big*);
592
extern void  lucas(_MIPT_ big,big,big,big,big);
593
extern BOOL  nroot(_MIPT_ big,int,big);
594
extern BOOL  sqroot(_MIPT_ big,big,big);
595
extern void  bigrand(_MIPT_ big,big);
596
extern void  bigdig(_MIPT_ int,int,big);
597
extern int   trial_division(_MIPT_ big,big);
598
extern BOOL  isprime(_MIPT_ big);
599
extern BOOL  nxprime(_MIPT_ big,big);
600
extern BOOL  nxsafeprime(_MIPT_ int,int,big,big);
601
extern BOOL  crt_init(_MIPT_ big_chinese *,int,big *);
602
extern void  crt(_MIPT_ big_chinese *,big *,big);
603
extern void  crt_end(big_chinese *);
604
extern BOOL  scrt_init(_MIPT_ small_chinese *,int,mr_utype *);
605
extern void  scrt(_MIPT_ small_chinese*,mr_utype *,big);
606
extern void  scrt_end(small_chinese *);
607
extern BOOL  brick_init(_MIPT_ brick *,big,big,int);
608
extern void  pow_brick(_MIPT_ brick *,big,big);
609
extern void  brick_end(brick *);
610
extern BOOL  ebrick_init(_MIPT_ ebrick *,big,big,big,big,big,int);
611
extern void  ebrick_end(ebrick *);
612
extern int   mul_brick(_MIPT_ ebrick*,big,big,big);
613
extern BOOL  ebrick2_init(_MIPT_ ebrick2 *,big,big,big,big,int,int,int,int,int);
614
extern void  ebrick2_end(ebrick2 *);
615
extern int   mul2_brick(_MIPT_ ebrick2*,big,big,big);
616
 
617
/* Montgomery stuff */
618
 
619
extern mr_small prepare_monty(_MIPT_ big);
620
extern void  kill_monty(_MIPTO_ );
621
extern void  nres(_MIPT_ big,big);
622
extern void  redc(_MIPT_ big,big);
623
 
624
extern void  nres_negate(_MIPT_ big,big);
625
extern void  nres_modadd(_MIPT_ big,big,big);
626
extern void  nres_modsub(_MIPT_ big,big,big);
627
extern void  nres_premult(_MIPT_ big,int,big);
628
extern void  nres_modmult(_MIPT_ big,big,big);
629
extern int   nres_moddiv(_MIPT_ big,big,big);
630
extern void  nres_dotprod(_MIPT_ int,big *,big *,big);
631
extern void  nres_powmod(_MIPT_ big,big,big);
632
extern void  nres_powltr(_MIPT_ int,big,big);
633
extern void  nres_powmod2(_MIPT_ big,big,big,big,big);
634
extern void  nres_powmodn(_MIPT_ int,big *,big *,big);
635
extern BOOL  nres_sqroot(_MIPT_ big,big);
636
extern void  nres_lucas(_MIPT_ big,big,big,big);
637
extern BOOL  nres_double_inverse(_MIPT_ big,big,big,big);
638
extern BOOL  nres_multi_inverse(_MIPT_ int,big *,big *);
639
 
640
extern void  shs_init(sha *);
641
extern void  shs_process(sha *,int);
642
extern void  shs_hash(sha *,char *);
643
 
644
extern void  shs256_init(sha256 *);
645
extern void  shs256_process(sha256 *,int);
646
extern void  shs256_hash(sha256 *,char *);
647
 
648
#ifdef mr_unsign64
649
 
650
extern void  shs512_init(sha512 *);
651
extern void  shs512_process(sha512 *,int);
652
extern void  shs512_hash(sha512 *,char *);
653
 
654
extern void  shs384_init(sha384 *);
655
extern void  shs384_process(sha384 *,int);
656
extern void  shs384_hash(sha384 *,char *);
657
 
658
#endif
659
 
660
extern BOOL  aes_init(aes *,int,int,char *,char *);
661
extern void  aes_getreg(aes *,char *);
662
extern mr_unsign32 aes_encrypt(aes *,char *);
663
extern mr_unsign32 aes_decrypt(aes *,char *);
664
extern void  aes_reset(aes *,int,char *);
665
extern void  aes_end(aes *);
666
 
667
extern void  strong_init(csprng *,int,char *,mr_unsign32);
668
extern int   strong_rng(csprng *);
669
extern void  strong_bigrand(_MIPT_ csprng *,big,big);
670
extern void  strong_bigdig(_MIPT_ csprng *,int,int,big);
671
extern void  strong_kill(csprng *);
672
 
673
/* special modular multipliers */
674
 
675
extern void  comba_mult(_MIPT_ big,big,big);
676
extern void  comba_square(_MIPT_ big,big);
677
extern void  comba_redc(_MIPT_ big,big);
678
extern void  comba_add(_MIPT_ big,big,big);
679
extern void  comba_sub(_MIPT_ big,big,big);
680
 
681
extern void  fastmodmult(_MIPT_ big,big,big);
682
extern void  fastmodsquare(_MIPT_ big,big);
683
 
684
extern void  kcm_mul(_MIPT_ big,big,big);
685
extern void  kcm_sqr(_MIPT_ big,big);
686
extern void  kcm_redc(_MIPT_ big,big);
687
 
688
extern void  kcm_multiply(_MIPT_ int,big,big,big);
689
extern void  kcm_square(_MIPT_ int,big,big);
690
extern BOOL  kcm_top(_MIPT_ int,big,big,big);
691
 
692
/* elliptic curve stuff */
693
 
694
extern BOOL point_at_infinity(epoint *);
695
 
696
extern void ecurve_init(_MIPT_ big,big,big,int);
697
extern big  ecurve_add(_MIPT_ epoint *,epoint *);
698
extern big  ecurve_sub(_MIPT_ epoint *,epoint *);
699
extern void ecurve_double_add(_MIPT_ epoint *,epoint *,epoint *,epoint *,big *,big *);
700
extern void ecurve_multi_add(_MIPT_ int,epoint **,epoint **);
701
extern void ecurve_mult(_MIPT_ big,epoint *,epoint *);
702
extern void ecurve_mult2(_MIPT_ big,epoint *,big,epoint *,epoint *);
703
extern void ecurve_multn(_MIPT_ int,big *,epoint**,epoint *);
704
 
705
extern epoint* epoint_init(_MIPTO_ );
706
extern BOOL epoint_set(_MIPT_ big,big,int,epoint*);
707
extern int  epoint_get(_MIPT_ epoint*,big,big);
708
extern void epoint_getxyz(_MIPT_ epoint *,big,big,big);
709
extern int  epoint_norm(_MIPT_ epoint *);
710
extern void epoint_free(epoint *);
711
extern void epoint_copy(epoint *,epoint *);
712
extern BOOL epoint_comp(_MIPT_ epoint *,epoint *);
713
extern void epoint_negate(_MIPT_ epoint *);
714
 
715
extern BOOL ecurve2_init(_MIPT_ int,int,int,int,big,big,BOOL,int);
716
extern big  ecurve2_add(_MIPT_ epoint *,epoint *);
717
extern big  ecurve2_sub(_MIPT_ epoint *,epoint *);
718
extern void ecurve2_multi_add(_MIPT_ int,epoint **,epoint **);
719
extern void ecurve2_mult(_MIPT_ big,epoint *,epoint *);
720
extern void ecurve2_mult2(_MIPT_ big,epoint *,big,epoint *,epoint *);
721
extern void ecurve2_multn(_MIPT_ int,big *,epoint**,epoint *);
722
 
723
extern epoint* epoint2_init(_MIPTO_ );
724
extern BOOL epoint2_set(_MIPT_ big,big,int,epoint*);
725
extern int  epoint2_get(_MIPT_ epoint*,big,big);
726
extern void epoint2_getxyz(_MIPT_ epoint *,big,big,big);
727
extern int  epoint2_norm(_MIPT_ epoint *);
728
extern void epoint2_free(epoint *);
729
extern void epoint2_copy(epoint *,epoint *);
730
extern BOOL epoint2_comp(_MIPT_ epoint *,epoint *);
731
extern void epoint2_negate(_MIPT_ epoint *);
732
 
733
/* GF(2) stuff */
734
 
735
extern BOOL prepare_basis(_MIPT_ int,int,int,int,BOOL);
736
extern void add2(big,big,big);
737
extern void incr2(big,int,big);
738
extern void reduce2(_MIPT_ big,big);
739
extern void modmult2(_MIPT_ big,big,big);
740
extern void power2(_MIPT_ big,int,big);
741
extern void sqroot2(_MIPT_ big,big);
742
extern BOOL inverse2(_MIPT_ big,big);
743
extern void karmul2(int,mr_small *,mr_small *,mr_small *,mr_small *);
744
extern void karmul2_poly(_MIPT_ int,big *,big *,big *,big *);
745
extern void karmul2_poly_upper(_MIPT_ int,big *,big *,big *,big *);
746
extern void gf2m_dotprod(_MIPT_ int,big *,big *,big);
747
extern int  trace2(_MIPT_ big);
748
 
749
/* Group 3 - Floating-slash routines      */
750
 
751
#ifdef MR_FLASH
752
extern void  fpack(_MIPT_ big,big,flash);
753
extern void  numer(_MIPT_ flash,big);
754
extern void  denom(_MIPT_ flash,big);
755
extern BOOL  fit(big,big,int);
756
extern void  build(_MIPT_ flash,int (*)(_MIPT_ big,int));
757
extern void  mround(_MIPT_ big,big,flash);
758
extern void  flop(_MIPT_ flash,flash,int *,flash);
759
extern void  fmul(_MIPT_ flash,flash,flash);
760
extern void  fdiv(_MIPT_ flash,flash,flash);
761
extern void  fadd(_MIPT_ flash,flash,flash);
762
extern void  fsub(_MIPT_ flash,flash,flash);
763
extern int   fcomp(_MIPT_ flash,flash);
764
extern void  fconv(_MIPT_ int,int,flash);
765
extern void  frecip(_MIPT_ flash,flash);
766
extern void  ftrunc(_MIPT_ flash,big,flash);
767
extern void  fmodulo(_MIPT_ flash,flash,flash);
768
extern void  fpmul(_MIPT_ flash,int,int,flash);
769
extern void  fincr(_MIPT_ flash,int,int,flash);
770
extern void  dconv(_MIPT_ double,flash);
771
extern double fdsize(_MIPT_ flash);
772
extern void  frand(_MIPT_ flash);
773
 
774
/* Group 4 - Advanced Flash routines */
775
 
776
extern void  fpower(_MIPT_ flash,int,flash);
777
extern BOOL  froot(_MIPT_ flash,int,flash);
778
extern void  fpi(_MIPT_ flash);
779
extern void  fexp(_MIPT_ flash,flash);
780
extern void  flog(_MIPT_ flash,flash);
781
extern void  fpowf(_MIPT_ flash,flash,flash);
782
extern void  ftan(_MIPT_ flash,flash);
783
extern void  fatan(_MIPT_ flash,flash);
784
extern void  fsin(_MIPT_ flash,flash);
785
extern void  fasin(_MIPT_ flash,flash);
786
extern void  fcos(_MIPT_ flash,flash);
787
extern void  facos(_MIPT_ flash,flash);
788
extern void  ftanh(_MIPT_ flash,flash);
789
extern void  fatanh(_MIPT_ flash,flash);
790
extern void  fsinh(_MIPT_ flash,flash);
791
extern void  fasinh(_MIPT_ flash,flash);
792
extern void  fcosh(_MIPT_ flash,flash);
793
extern void  facosh(_MIPT_ flash,flash);
794
#endif
795
 
796
 
797
/* Test predefined Macros to determine compiler type, and hopefully
798
   selectively use fast in-line assembler (or other compiler specific
799
   optimisations. Note I am unsure of Microsoft version numbers. So I
800
   suspect are Microsoft.
801
 
802
   Note: It seems to be impossible to get the 16-bit Microsoft compiler
803
   to allow inline 32-bit op-codes. So I suspect that INLINE_ASM == 2 will
804
   never work with it. Pity.
805
 
806
#define INLINE_ASM 1    -> generates 8086 inline assembly
807
#define INLINE_ASM 2    -> generates mixed 8086 & 80386 inline assembly,
808
                           so you can get some benefit while running in a
809
                           16-bit environment on 32-bit hardware (DOS, Windows
810
                           3.1...)
811
#define INLINE_ASM 3    -> generate true 80386 inline assembly - (Using DOS
812
                           extender, Windows '95/Windows NT)
813
                           Actually optimised for Pentium
814
 
815
#define INLINE_ASM 4    -> 80386 code in the GNU style (for (DJGPP)
816
 
817
Small, medium, compact and large memory models are supported for the
818
first two of the above.
819
 
820
*/
821
 
822
#ifndef MR_NOASM
823
 
824
/* Itanium - inline the time-critical functions */
825
 
826
    #ifdef MR_ITANIUM
827
        #define muldvd(a,b,c,rp)  (tm=_m64_xmahu((a),(b),(c)),*(rp)=_m64_xmalu((a),(b),(c)),tm)
828
        #define muldvd2(a,b,c,rp) (tm=_m64_xmalu((a),(b),(*(c))),*(c)=_m64_xmahu((a),(b),(*(c))),tm+=*(rp),*(c)+=(tm<*(rp)),*(rp)=tm)
829
    #endif
830
 
831
 
832
/* Borland C/Turbo C */
833
 
834
    #ifdef __TURBOC__ 
835
    #ifndef __HUGE__
836
        #define ASM asm
837
        #if defined(__COMPACT__) || defined(__LARGE__)
838
            #define MR_LMM
839
        #endif
840
 
841
        #if MIRACL==16
842
            #define INLINE_ASM 1
843
        #endif
844
 
845
        #if __TURBOC__>=0x410
846
            #if MIRACL==32
847
#if defined(__SMALL__) || defined(__MEDIUM__) || defined(__LARGE__) || defined(__COMPACT__)
848
                    #define INLINE_ASM 2
849
                #else
850
                    #define INLINE_ASM 3
851
                #endif
852
            #endif
853
        #endif
854
    #endif
855
    #endif
856
 
857
/* Microsoft C */
858
 
859
    #ifdef _MSC_VER
860
    #ifndef M_I86HM
861
        #define ASM _asm
862
        #if defined(M_I86CM) || defined(M_I86LM)
863
            #define MR_LMM
864
        #endif
865
        #if _MSC_VER>=600
866
            #if MIRACL==16
867
                #define INLINE_ASM 1
868
            #endif
869
        #endif
870
        #if _MSC_VER>=1000
871
            #if MIRACL==32
872
                #define INLINE_ASM 3
873
            #endif
874
        #endif     
875
    #endif       
876
    #endif
877
 
878
/* DJGPP GNU C */
879
 
880
    #ifdef __GNUC__
881
    #ifdef i386
882
        #define ASM __asm__ __volatile__
883
        #if MIRACL==32
884
            #define INLINE_ASM 4
885
        #endif
886
    #endif
887
    #endif
888
 
889
#endif
890
 
891
/*
892
   The following contribution is from Tielo Jongmans, Netherlands
893
   These inline assembler routines are suitable for Watcom 10.0 and up
894
 
895
   Added into miracl.h.  Notice the override of the original declarations
896
   of these routines, which should be removed.
897
 
898
   The following pragma is optional, it is dangerous, but it saves a
899
   calling sequence
900
*/
901
 
902
/*
903
 
904
#pragma off (check_stack);
905
 
906
extern unsigned int muldiv(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int *);
907
#pragma aux muldiv=                 \
908
       "mul     edx"                \
909
       "add     eax,ebx"            \
910
       "adc     edx,0"              \
911
       "div     ecx"                \
912
       "mov     [esi],edx"          \
913
    parm [eax] [edx] [ebx] [ecx] [esi]   \
914
    value [eax]                     \
915
    modify [eax edx];
916
 
917
extern unsigned int muldvm(unsigned int, unsigned int, unsigned int, unsigned int *);
918
#pragma aux muldvm=                 \
919
        "div     ebx"               \
920
        "mov     [ecx],edx"         \
921
    parm [edx] [eax] [ebx] [ecx]    \
922
    value [eax]                     \
923
    modify [eax edx];
924
 
925
extern unsigned int muldvd(unsigned int, unsigned int, unsigned int, unsigned int *);
926
#pragma aux muldvd=                 \
927
        "mul     edx"               \
928
        "add     eax,ebx"           \
929
        "adc     edx,0"             \
930
        "mov     [ecx],eax"         \
931
        "mov     eax,edx"           \
932
    parm [eax] [edx] [ebx] [ecx]    \
933
    value [eax]                     \
934
    modify [eax edx];
935
 
936
*/
937
 
938
 
939
#endif
940
 
941
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.