| 1 |
/* |
| 2 |
This file is for functions for field arithmetic |
| 3 |
*/ |
| 4 |
// 20240810 djb: even more cryptoint usage |
| 5 |
// 20240809 djb: restructuring |
| 6 |
// 20240805 djb: more cryptoint usage |
| 7 |
// 20221231 djb: const for GF_mul |
| 8 |
// 20221230 djb: add linker line |
| 9 |
|
| 10 |
// linker define gf_iszero gf_inv |
| 11 |
// linker use gf_mul |
| 12 |
|
| 13 |
#include "gf.h" |
| 14 |
|
| 15 |
#include "crypto_int32.h" |
| 16 |
|
| 17 |
/* check if a == 0 */ |
| 18 |
gf gf_iszero(gf a) |
| 19 |
{ |
| 20 |
return crypto_int32_zero_mask(a) & GFMASK; |
| 21 |
} |
| 22 |
|
| 23 |
/* input: field element in */ |
| 24 |
/* return: in^2 */ |
| 25 |
static gf gf_sq(gf in) |
| 26 |
{ |
| 27 |
const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF}; |
| 28 |
|
| 29 |
uint32_t x = in; |
| 30 |
uint32_t t; |
| 31 |
|
| 32 |
x = (x | crypto_int32_shlmod(x,8)) & B[3]; |
| 33 |
x = (x | crypto_int32_shlmod(x,4)) & B[2]; |
| 34 |
x = (x | crypto_int32_shlmod(x,2)) & B[1]; |
| 35 |
x = (x | crypto_int32_shlmod(x,1)) & B[0]; |
| 36 |
|
| 37 |
t = x & 0x7FC000; |
| 38 |
x ^= t >> 9; |
| 39 |
x ^= t >> 12; |
| 40 |
|
| 41 |
t = x & 0x3000; |
| 42 |
x ^= t >> 9; |
| 43 |
x ^= t >> 12; |
| 44 |
|
| 45 |
return x & GFMASK; |
| 46 |
} |
| 47 |
|
| 48 |
gf gf_inv(gf in) |
| 49 |
{ |
| 50 |
gf tmp_11; |
| 51 |
gf tmp_1111; |
| 52 |
|
| 53 |
gf out = in; |
| 54 |
|
| 55 |
out = gf_sq(out); |
| 56 |
tmp_11 = gf_mul(out, in); // 11 |
| 57 |
|
| 58 |
out = gf_sq(tmp_11); |
| 59 |
out = gf_sq(out); |
| 60 |
tmp_1111 = gf_mul(out, tmp_11); // 1111 |
| 61 |
|
| 62 |
out = gf_sq(tmp_1111); |
| 63 |
out = gf_sq(out); |
| 64 |
out = gf_sq(out); |
| 65 |
out = gf_sq(out); |
| 66 |
out = gf_mul(out, tmp_1111); // 11111111 |
| 67 |
|
| 68 |
out = gf_sq(out); |
| 69 |
out = gf_sq(out); |
| 70 |
out = gf_mul(out, tmp_11); // 1111111111 |
| 71 |
|
| 72 |
out = gf_sq(out); |
| 73 |
out = gf_mul(out, in); // 11111111111 |
| 74 |
|
| 75 |
return gf_sq(out); // 111111111110 |
| 76 |
} |
| 77 |
|