[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[2dd068] | 2 | |
---|
[b52d27] | 3 | /** |
---|
| 4 | * @file ffops.h |
---|
| 5 | * |
---|
| 6 | * operations in a finite prime field F_p. |
---|
| 7 | * The largest supported p is 536870909, i.e. the largest prime less than 2^29. |
---|
| 8 | * |
---|
| 9 | **/ |
---|
| 10 | |
---|
[2dd068] | 11 | #ifndef INCL_FFOPS_H |
---|
| 12 | #define INCL_FFOPS_H |
---|
| 13 | |
---|
[e4fe2b] | 14 | // #include "config.h" |
---|
[84250a6] | 15 | |
---|
[2dd068] | 16 | #include "cf_globals.h" |
---|
[27b3cf] | 17 | #ifdef HAVE_NTL |
---|
| 18 | #include <NTL/config.h> |
---|
| 19 | #endif |
---|
[2dd068] | 20 | |
---|
[e76d7a6] | 21 | /* define type of your compilers 64 bit integer type */ |
---|
[fd68165] | 22 | #ifndef FACTORY_INT64 |
---|
[10f279] | 23 | #if SIZEOF_LONG == 8 |
---|
| 24 | #define FACTORY_INT64 long int |
---|
| 25 | #else |
---|
[fd68165] | 26 | #define FACTORY_INT64 long long int |
---|
[e76d7a6] | 27 | #endif |
---|
[10f279] | 28 | #endif |
---|
[e76d7a6] | 29 | |
---|
[a3f0fea] | 30 | EXTERN_VAR int ff_prime; |
---|
| 31 | EXTERN_VAR int ff_halfprime; |
---|
| 32 | EXTERN_VAR short * ff_invtab; |
---|
| 33 | EXTERN_VAR bool ff_big; |
---|
[2dd068] | 34 | |
---|
| 35 | int ff_newinv ( const int ); |
---|
[02af91] | 36 | int ff_biginv ( const int ); |
---|
[2dd068] | 37 | void ff_setprime ( const int ); |
---|
| 38 | |
---|
| 39 | inline int ff_norm ( const int a ) |
---|
| 40 | { |
---|
[02af91] | 41 | int n = a % ff_prime; |
---|
[8145ac] | 42 | #if defined(NTL_AVOID_BRANCHING) |
---|
[806c18] | 43 | n += (n >> 31) & ff_prime; |
---|
[27b3cf] | 44 | return n; |
---|
| 45 | #else |
---|
[2a289e] | 46 | if (n < 0) n += ff_prime; |
---|
[27b3cf] | 47 | return n; |
---|
| 48 | #endif |
---|
[2dd068] | 49 | } |
---|
| 50 | |
---|
[8710ff0] | 51 | inline long ff_norm ( const long a ) |
---|
| 52 | { |
---|
[8145ac] | 53 | long n = a % (long)ff_prime; |
---|
| 54 | #if defined(NTL_AVOID_BRANCHING) |
---|
[5eeb4f] | 55 | #if SIZEOF_LONG==8 |
---|
| 56 | n += (n >> 63) & ff_prime; |
---|
| 57 | #else |
---|
[8710ff0] | 58 | n += (n >> 31) & ff_prime; |
---|
[5eeb4f] | 59 | #endif |
---|
[8710ff0] | 60 | return n; |
---|
| 61 | #else |
---|
[8145ac] | 62 | if (n < 0) n += (long)ff_prime; |
---|
[8710ff0] | 63 | return n; |
---|
| 64 | #endif |
---|
| 65 | } |
---|
| 66 | |
---|
[2dd068] | 67 | inline int ff_symmetric( const int a ) |
---|
| 68 | { |
---|
| 69 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
[806c18] | 70 | return ( a > ff_halfprime ) ? a - ff_prime : a; |
---|
[2dd068] | 71 | else |
---|
[806c18] | 72 | return a; |
---|
[2dd068] | 73 | } |
---|
| 74 | |
---|
[8710ff0] | 75 | inline long ff_symmetric( const long a ) |
---|
| 76 | { |
---|
| 77 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
| 78 | return ( a > ff_halfprime ) ? a - ff_prime : a; |
---|
| 79 | else |
---|
| 80 | return a; |
---|
| 81 | } |
---|
| 82 | |
---|
[8145ac] | 83 | #if SIZEOF_LONG==4 |
---|
[fd68165] | 84 | inline int ff_bignorm ( const FACTORY_INT64 a ) |
---|
[02af91] | 85 | { |
---|
[fd68165] | 86 | int n = (int)(a % (FACTORY_INT64)ff_prime); |
---|
[8145ac] | 87 | #if defined(NTL_AVOID_BRANCHING) |
---|
[806c18] | 88 | n += (n >> 31) & ff_prime; |
---|
[2a289e] | 89 | return n; |
---|
| 90 | #else |
---|
| 91 | if (n < 0) n += ff_prime; |
---|
| 92 | return n; |
---|
| 93 | #endif |
---|
[2dd068] | 94 | } |
---|
[8145ac] | 95 | #endif |
---|
[2dd068] | 96 | |
---|
| 97 | inline int ff_add ( const int a, const int b ) |
---|
| 98 | { |
---|
[79cf7d0] | 99 | //return ff_norm( a + b ); |
---|
[8145ac] | 100 | #if defined(NTL_AVOID_BRANCHING) |
---|
[09171bd] | 101 | int r=( a + b ); |
---|
| 102 | r -= ff_prime; |
---|
[806c18] | 103 | r += (r >> 31) & ff_prime; |
---|
[09171bd] | 104 | return r; |
---|
| 105 | #else |
---|
[79cf7d0] | 106 | int r=( a + b ); |
---|
| 107 | if (r >= ff_prime) r -= ff_prime; |
---|
| 108 | return r; |
---|
[09171bd] | 109 | #endif |
---|
[2dd068] | 110 | } |
---|
| 111 | |
---|
| 112 | inline int ff_sub ( const int a, const int b ) |
---|
| 113 | { |
---|
[79cf7d0] | 114 | //return ff_norm( a - b ); |
---|
[8145ac] | 115 | #if defined(NTL_AVOID_BRANCHING) |
---|
[09171bd] | 116 | int r=( a - b ); |
---|
[806c18] | 117 | r += (r >> 31) & ff_prime; |
---|
[09171bd] | 118 | return r; |
---|
| 119 | #else |
---|
[79cf7d0] | 120 | int r=( a - b ); |
---|
[2a289e] | 121 | if (r < 0) r += ff_prime; |
---|
[79cf7d0] | 122 | return r; |
---|
[09171bd] | 123 | #endif |
---|
[2dd068] | 124 | } |
---|
| 125 | |
---|
| 126 | inline int ff_neg ( const int a ) |
---|
| 127 | { |
---|
[79cf7d0] | 128 | //return ff_norm( -a ); |
---|
[2a289e] | 129 | // EXPERIMENT |
---|
[8145ac] | 130 | #if defined(NTL_AVOID_BRANCHING) |
---|
[2a289e] | 131 | int r= -a; |
---|
[806c18] | 132 | r += (r >> 31) & ff_prime; |
---|
[2a289e] | 133 | return r; |
---|
| 134 | #else |
---|
[09171bd] | 135 | return ( a == 0 ? 0 : ff_prime-a ); |
---|
[2a289e] | 136 | #endif |
---|
[2dd068] | 137 | } |
---|
| 138 | |
---|
| 139 | inline int ff_mul ( const int a, const int b ) |
---|
| 140 | { |
---|
[8145ac] | 141 | #if SIZEOF_LONG==4 |
---|
[02af91] | 142 | if ( ff_big ) |
---|
[fd68165] | 143 | return ff_bignorm( (FACTORY_INT64)a * (FACTORY_INT64)b ); |
---|
[02af91] | 144 | else |
---|
[8145ac] | 145 | #endif /* else: FACTORY_INT64 is long, i.e. ff_bignorm is the same as ff_norm(long) */ |
---|
| 146 | return ff_norm ( (long)a * (long)b ); |
---|
[2dd068] | 147 | } |
---|
| 148 | |
---|
| 149 | inline int ff_inv ( const int a ) |
---|
| 150 | { |
---|
[02af91] | 151 | if ( ff_big ) |
---|
[806c18] | 152 | return ff_biginv( a ); |
---|
[02af91] | 153 | else { |
---|
[5e92a9] | 154 | int b; |
---|
[806c18] | 155 | if ( (b = (int)(ff_invtab[a])) ) |
---|
| 156 | return b; |
---|
| 157 | else |
---|
| 158 | return ff_newinv( a ); |
---|
[02af91] | 159 | } |
---|
[84250a6] | 160 | |
---|
[2dd068] | 161 | } |
---|
| 162 | |
---|
| 163 | inline int ff_div ( const int a, const int b ) |
---|
| 164 | { |
---|
[02af91] | 165 | return ff_mul( a, ff_inv( b ) ); |
---|
[2dd068] | 166 | } |
---|
| 167 | |
---|
[493c477] | 168 | #endif /* ! INCL_FFOPS_H */ |
---|