2 * @file p448/f_generic.c
6 * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
7 * Released under the MIT License. See LICENSE.txt for license information.
9 * @brief Generic arithmetic which has to be compiled per field.
11 * @warning This file was automatically generated in Python.
12 * Please do not edit it.
16 static const gf MODULUS = {FIELD_LITERAL(
17 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff, 0xfffffffffffffe, 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff
20 /** Serialize to wire format. */
21 void gf_serialize (uint8_t serial[SER_BYTES], const gf x, int with_hibit) {
22 unsigned int j=0, fill=0;
28 gf_strong_reduce(red);
29 if (!with_hibit) { assert(gf_hibit(red) == 0); }
31 UNROLL for (i=0; i<(with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
32 if (fill < 8 && j < NLIMBS) {
33 buffer |= ((dword_t)red->limb[LIMBPERM(j)]) << fill;
34 fill += LIMB_PLACE_VALUE(LIMBPERM(j));
43 /** Return high bit of x = low bit of 2x mod p */
44 mask_t gf_hibit(const gf x) {
48 return -(y->limb[0]&1);
51 /** Return high bit of x = low bit of 2x mod p */
52 mask_t gf_lobit(const gf x) {
56 return -(y->limb[0]&1);
59 /** Deserialize from wire format; return -1 on success and 0 on failure. */
60 mask_t gf_deserialize (gf x, const uint8_t serial[SER_BYTES], int with_hibit, uint8_t hi_nmask) {
61 unsigned int j=0, fill=0;
64 const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
68 UNROLL for (i=0; i<NLIMBS; i++) {
69 UNROLL while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
70 uint8_t sj = serial[j];
71 if (j==nbytes-1) sj &= ~hi_nmask;
72 buffer |= ((dword_t)sj) << fill;
76 x->limb[LIMBPERM(i)] = (i<NLIMBS-1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer;
77 fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
78 buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
79 scarry = (scarry + x->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)]) >> (8*sizeof(word_t));
81 succ = with_hibit ? -(mask_t)1 : ~gf_hibit(x);
82 return succ & word_is_zero(buffer) & ~word_is_zero(scarry);
85 /** Reduce to canonical form. */
86 void gf_strong_reduce (gf a) {
92 /* first, clear high */
93 gf_weak_reduce(a); /* Determined to have negligible perf impact. */
95 /* now the total is less than 2p */
97 /* compute total_value - p. No need to reduce mod p. */
99 for (i=0; i<NLIMBS; i++) {
100 scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
101 a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
102 scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
105 /* uncommon case: it was >= p, so now scarry = 0 and this = x
106 * common case: it was < p, so now scarry = -1 and this = x - p + 2^255
107 * so let's add back in p. will carry back off the top for 2^255.
109 assert(word_is_zero(scarry) | word_is_zero(scarry+1));
114 for (i=0; i<NLIMBS; i++) {
115 carry = carry + a->limb[LIMBPERM(i)] + (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
116 a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
117 carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
120 assert(word_is_zero(carry + scarry_0));
123 /** Subtract two gf elements d=a-b */
124 void gf_sub (gf d, const gf a, const gf b) {
125 gf_sub_RAW ( d, a, b );
127 gf_weak_reduce ( d );
130 /** Add two field elements d = a+b */
131 void gf_add (gf d, const gf a, const gf b) {
132 gf_add_RAW ( d, a, b );
133 gf_weak_reduce ( d );
137 mask_t gf_eq(const gf a, const gf b) {
145 for (i=0; i<NLIMBS; i++) {
146 ret |= c->limb[LIMBPERM(i)];
149 return word_is_zero(ret);