1 /* Copyright (c) 2014 Cryptography Research, Inc.
2  * Released under the MIT License.  See LICENSE.txt for license information.
3  */
4 
5 #include "f_field.h"
6 
7 #if (defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) && !I_HATE_UNROLLED_LOOPS) \
8      || defined(CRYPTONITE_DECAF_FORCE_UNROLL)
9 #define REPEAT8(_x) _x _x _x _x _x _x _x _x
10 #define FOR_LIMB(_i,_start,_end,_x) do { _i=_start; REPEAT8( if (_i<_end) { _x; } _i++;) } while (0)
11 #else
12 #define FOR_LIMB(_i,_start,_end,_x) do { for (_i=_start; _i<_end; _i++) _x; } while (0)
13 #endif
14 
cryptonite_gf_mul(cryptonite_gf_s * __restrict__ cs,const gf as,const gf bs)15 void cryptonite_gf_mul (cryptonite_gf_s *__restrict__ cs, const gf as, const gf bs) {
16     const uint32_t *a = as->limb, *b = bs->limb;
17     uint32_t *c = cs->limb;
18 
19     uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
20     uint32_t mask = (1<<28) - 1;
21 
22     uint32_t aa[8], bb[8];
23 
24     int i,j;
25     for (i=0; i<8; i++) {
26         aa[i] = a[i] + a[i+8];
27         bb[i] = b[i] + b[i+8];
28     }
29 
30     FOR_LIMB(j,0,8,{
31         accum2 = 0;
32 
33         FOR_LIMB (i,0,j+1,{
34             accum2 += widemul(a[j-i],b[i]);
35             accum1 += widemul(aa[j-i],bb[i]);
36             accum0 += widemul(a[8+j-i], b[8+i]);
37         });
38 
39         accum1 -= accum2;
40         accum0 += accum2;
41         accum2 = 0;
42 
43         FOR_LIMB (i,j+1,8,{
44             accum0 -= widemul(a[8+j-i], b[i]);
45             accum2 += widemul(aa[8+j-i], bb[i]);
46             accum1 += widemul(a[16+j-i], b[8+i]);
47         });
48 
49         accum1 += accum2;
50         accum0 += accum2;
51 
52         c[j] = ((uint32_t)(accum0)) & mask;
53         c[j+8] = ((uint32_t)(accum1)) & mask;
54 
55         accum0 >>= 28;
56         accum1 >>= 28;
57     });
58 
59     accum0 += accum1;
60     accum0 += c[8];
61     accum1 += c[0];
62     c[8] = ((uint32_t)(accum0)) & mask;
63     c[0] = ((uint32_t)(accum1)) & mask;
64 
65     accum0 >>= 28;
66     accum1 >>= 28;
67     c[9] += ((uint32_t)(accum0));
68     c[1] += ((uint32_t)(accum1));
69 }
70 
cryptonite_gf_mulw_unsigned(cryptonite_gf_s * __restrict__ cs,const gf as,uint32_t b)71 void cryptonite_gf_mulw_unsigned (cryptonite_gf_s *__restrict__ cs, const gf as, uint32_t b) {
72     assert(b<1<<28);
73 
74     const uint32_t *a = as->limb;
75     uint32_t *c = cs->limb;
76 
77     uint64_t accum0 = 0, accum8 = 0;
78     uint32_t mask = (1ull<<28)-1;
79 
80     int i;
81     FOR_LIMB(i,0,8,{
82         accum0 += widemul(b, a[i]);
83         accum8 += widemul(b, a[i+8]);
84 
85         c[i] = accum0 & mask; accum0 >>= 28;
86         c[i+8] = accum8 & mask; accum8 >>= 28;
87     });
88 
89     accum0 += accum8 + c[8];
90     c[8] = accum0 & mask;
91     c[9] += accum0 >> 28;
92 
93     accum8 += c[0];
94     c[0] = accum8 & mask;
95     c[1] += accum8 >> 28;
96 }
97 
cryptonite_gf_sqr(cryptonite_gf_s * __restrict__ cs,const gf as)98 void cryptonite_gf_sqr (cryptonite_gf_s *__restrict__ cs, const gf as) {
99     cryptonite_gf_mul(cs,as,as); /* Performs better with a dedicated square */
100 }
101 
102