f_impl.c 7.08 KB
Newer Older
Michael Hamburg's avatar
Michael Hamburg committed
1 2 3 4
/* Copyright (c) 2014 Cryptography Research, Inc.
 * Released under the MIT License.  See LICENSE.txt for license information.
 */

5
#include "f_field.h"
Michael Hamburg's avatar
Michael Hamburg committed
6

7
void gf_mul (gf *__restrict__ cs, const gf *as, const gf *bs) {
Michael Hamburg's avatar
Michael Hamburg committed
8 9 10 11
    const uint64_t *a = as->limb, *b = bs->limb;
    uint64_t *c = cs->limb;

    __uint128_t accum0 = 0, accum1 = 0, accum2;
12
    uint64_t mask = (1ull<<60) - 1;  
Michael Hamburg's avatar
Michael Hamburg committed
13

14
    uint64_t aa[4] __attribute__((aligned(32))), bb[4] __attribute__((aligned(32))), bbb[4] __attribute__((aligned(32)));
Michael Hamburg's avatar
Michael Hamburg committed
15 16 17 18 19

    /* For some reason clang doesn't vectorize this without prompting? */
    unsigned int i;
    for (i=0; i<sizeof(aa)/sizeof(uint64xn_t); i++) {
        ((uint64xn_t*)aa)[i] = ((const uint64xn_t*)a)[i] + ((const uint64xn_t*)(&a[4]))[i];
20 21
        ((uint64xn_t*)bb)[i] = ((const uint64xn_t*)b)[i] + ((const uint64xn_t*)(&b[4]))[i]; 
        ((uint64xn_t*)bbb)[i] = ((const uint64xn_t*)bb)[i] + ((const uint64xn_t*)(&b[4]))[i];     
Michael Hamburg's avatar
Michael Hamburg committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    }
    /*
    for (int i=0; i<4; i++) {
    aa[i] = a[i] + a[i+4];
    bb[i] = b[i] + b[i+4];
    }
    */

    accum2  = widemul(&a[0],&b[3]);
    accum0  = widemul(&aa[0],&bb[3]);
    accum1  = widemul(&a[4],&b[7]);

    mac(&accum2, &a[1], &b[2]);
    mac(&accum0, &aa[1], &bb[2]);
    mac(&accum1, &a[5], &b[6]);

    mac(&accum2, &a[2], &b[1]);
    mac(&accum0, &aa[2], &bb[1]);
    mac(&accum1, &a[6], &b[5]);

    mac(&accum2, &a[3], &b[0]);
    mac(&accum0, &aa[3], &bb[0]);
    mac(&accum1, &a[7], &b[4]);

    accum0 -= accum2;
    accum1 += accum2;

    c[3] = ((uint64_t)(accum1)) & mask;
    c[7] = ((uint64_t)(accum0)) & mask;

52 53
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    
    mac(&accum0, &aa[1],&bb[3]);
    mac(&accum1, &a[5], &b[7]);
    mac(&accum0, &aa[2], &bb[2]);
    mac(&accum1, &a[6], &b[6]);
    mac(&accum0, &aa[3], &bb[1]);
    accum1 += accum0;

    accum2 = widemul(&a[0],&b[0]);
    accum1 -= accum2;
    accum0 += accum2;
    
    msb(&accum0, &a[1], &b[3]);
    msb(&accum0, &a[2], &b[2]);
    mac(&accum1, &a[7], &b[5]);
    msb(&accum0, &a[3], &b[1]);
    mac(&accum1, &aa[0], &bb[0]);
    mac(&accum0, &a[4], &b[4]);

    c[0] = ((uint64_t)(accum0)) & mask;
    c[4] = ((uint64_t)(accum1)) & mask;

76 77
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
78

79 80 81
    accum2  = widemul(&a[2],&b[7]);
    mac(&accum0, &a[6], &bb[3]);
    mac(&accum1, &aa[2], &bbb[3]);
Michael Hamburg's avatar
Michael Hamburg committed
82

83 84 85
    mac(&accum2, &a[3], &b[6]);
    mac(&accum0, &a[7], &bb[2]);
    mac(&accum1, &aa[3], &bbb[2]);
Michael Hamburg's avatar
Michael Hamburg committed
86

87
    mac(&accum2, &a[0],&b[1]);
Michael Hamburg's avatar
Michael Hamburg committed
88 89 90 91 92 93 94 95 96 97 98 99 100
    mac(&accum1, &aa[0], &bb[1]);
    mac(&accum0, &a[4], &b[5]);

    mac(&accum2, &a[1], &b[0]);
    mac(&accum1, &aa[1], &bb[0]);
    mac(&accum0, &a[5], &b[4]);

    accum1 -= accum2;
    accum0 += accum2;

    c[1] = ((uint64_t)(accum0)) & mask;
    c[5] = ((uint64_t)(accum1)) & mask;

101 102
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
103

104 105 106
    accum2  = widemul(&a[3],&b[7]);
    mac(&accum0, &a[7], &bb[3]);
    mac(&accum1, &aa[3], &bbb[3]);
Michael Hamburg's avatar
Michael Hamburg committed
107

108
    mac(&accum2, &a[0],&b[2]);
Michael Hamburg's avatar
Michael Hamburg committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    mac(&accum1, &aa[0], &bb[2]);
    mac(&accum0, &a[4], &b[6]);

    mac(&accum2, &a[1], &b[1]);
    mac(&accum1, &aa[1], &bb[1]);
    mac(&accum0, &a[5], &b[5]);

    mac(&accum2, &a[2], &b[0]);
    mac(&accum1, &aa[2], &bb[0]);
    mac(&accum0, &a[6], &b[4]);

    accum1 -= accum2;
    accum0 += accum2;

    c[2] = ((uint64_t)(accum0)) & mask;
    c[6] = ((uint64_t)(accum1)) & mask;

126 127
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
128 129 130 131 132 133 134 135

    accum0 += c[3];
    accum1 += c[7];
    c[3] = ((uint64_t)(accum0)) & mask;
    c[7] = ((uint64_t)(accum1)) & mask;

    /* we could almost stop here, but it wouldn't be stable, so... */

136 137
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
138 139 140 141
    c[4] += ((uint64_t)(accum0)) + ((uint64_t)(accum1));
    c[0] += ((uint64_t)(accum1));
}

142
void gf_mulw (gf *__restrict__ cs, const gf *as, uint64_t b) {
Michael Hamburg's avatar
Michael Hamburg committed
143 144 145 146
    const uint64_t *a = as->limb;
    uint64_t *c = cs->limb;

    __uint128_t accum0, accum4;
147
    uint64_t mask = (1ull<<60) - 1;  
Michael Hamburg's avatar
Michael Hamburg committed
148 149 150 151

    accum0 = widemul_rm(b, &a[0]);
    accum4 = widemul_rm(b, &a[4]);

152 153
    c[0] = accum0 & mask; accum0 >>= 60;
    c[4] = accum4 & mask; accum4 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
154 155 156 157

    mac_rm(&accum0, b, &a[1]);
    mac_rm(&accum4, b, &a[5]);

158 159
    c[1] = accum0 & mask; accum0 >>= 60;
    c[5] = accum4 & mask; accum4 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
160 161 162 163

    mac_rm(&accum0, b, &a[2]);
    mac_rm(&accum4, b, &a[6]);

164 165
    c[2] = accum0 & mask; accum0 >>= 60;
    c[6] = accum4 & mask; accum4 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
166 167 168 169

    mac_rm(&accum0, b, &a[3]);
    mac_rm(&accum4, b, &a[7]);

170 171
    c[3] = accum0 & mask; accum0 >>= 60;
    c[7] = accum4 & mask; accum4 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
172 173 174
    
    accum0 += accum4 + c[4];
    c[4] = accum0 & mask;
175
    c[5] += accum0 >> 60;
Michael Hamburg's avatar
Michael Hamburg committed
176 177 178

    accum4 += c[0];
    c[0] = accum4 & mask;
179
    c[1] += accum4 >> 60;
Michael Hamburg's avatar
Michael Hamburg committed
180 181
}

182
void gf_sqr (gf *__restrict__ cs, const gf *as) {
Michael Hamburg's avatar
Michael Hamburg committed
183 184 185 186
    const uint64_t *a = as->limb;
    uint64_t *c = cs->limb;

    __uint128_t accum0 = 0, accum1 = 0, accum2;
187
    uint64_t mask = (1ull<<60) - 1;  
Michael Hamburg's avatar
Michael Hamburg committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    uint64_t aa[4] __attribute__((aligned(32)));

    /* For some reason clang doesn't vectorize this without prompting? */
    unsigned int i;
    for (i=0; i<sizeof(aa)/sizeof(uint64xn_t); i++) {
      ((uint64xn_t*)aa)[i] = ((const uint64xn_t*)a)[i] + ((const uint64xn_t*)(&a[4]))[i];
    }

    accum2  = widemul(&a[0],&a[3]);
    accum0  = widemul(&aa[0],&aa[3]);
    accum1  = widemul(&a[4],&a[7]);

    mac(&accum2, &a[1], &a[2]);
    mac(&accum0, &aa[1], &aa[2]);
    mac(&accum1, &a[5], &a[6]);

    accum0 -= accum2;
    accum1 += accum2;

    c[3] = ((uint64_t)(accum1))<<1 & mask;
    c[7] = ((uint64_t)(accum0))<<1 & mask;

211 212
    accum0 >>= 59;
    accum1 >>= 59;
Michael Hamburg's avatar
Michael Hamburg committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    mac2(&accum0, &aa[1],&aa[3]);
    mac2(&accum1, &a[5], &a[7]);
    mac(&accum0, &aa[2], &aa[2]);
    accum1 += accum0;

    msb2(&accum0, &a[1], &a[3]);
    mac(&accum1, &a[6], &a[6]);
    
    accum2 = widemul(&a[0],&a[0]);
    accum1 -= accum2;
    accum0 += accum2;

    msb(&accum0, &a[2], &a[2]);
    mac(&accum1, &aa[0], &aa[0]);
    mac(&accum0, &a[4], &a[4]);

    c[0] = ((uint64_t)(accum0)) & mask;
    c[4] = ((uint64_t)(accum1)) & mask;

233 234
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

    accum2  = widemul2(&aa[2],&aa[3]);
    msb2(&accum0, &a[2], &a[3]);
    mac2(&accum1, &a[6], &a[7]);

    accum1 += accum2;
    accum0 += accum2;

    accum2  = widemul2(&a[0],&a[1]);
    mac2(&accum1, &aa[0], &aa[1]);
    mac2(&accum0, &a[4], &a[5]);

    accum1 -= accum2;
    accum0 += accum2;

    c[1] = ((uint64_t)(accum0)) & mask;
    c[5] = ((uint64_t)(accum1)) & mask;

253 254
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    accum2  = widemul(&aa[3],&aa[3]);
    msb(&accum0, &a[3], &a[3]);
    mac(&accum1, &a[7], &a[7]);

    accum1 += accum2;
    accum0 += accum2;

    accum2  = widemul2(&a[0],&a[2]);
    mac2(&accum1, &aa[0], &aa[2]);
    mac2(&accum0, &a[4], &a[6]);

    mac(&accum2, &a[1], &a[1]);
    mac(&accum1, &aa[1], &aa[1]);
    mac(&accum0, &a[5], &a[5]);

    accum1 -= accum2;
    accum0 += accum2;

    c[2] = ((uint64_t)(accum0)) & mask;
    c[6] = ((uint64_t)(accum1)) & mask;

277 278
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
279 280 281 282 283 284 285 286

    accum0 += c[3];
    accum1 += c[7];
    c[3] = ((uint64_t)(accum0)) & mask;
    c[7] = ((uint64_t)(accum1)) & mask;

    /* we could almost stop here, but it wouldn't be stable, so... */

287 288
    accum0 >>= 60;
    accum1 >>= 60;
Michael Hamburg's avatar
Michael Hamburg committed
289 290 291
    c[4] += ((uint64_t)(accum0)) + ((uint64_t)(accum1));
    c[0] += ((uint64_t)(accum1));
}