2017-12-01 18:12:25 +00:00
|
|
|
/*
|
2018-01-18 13:12:46 +00:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-12-01 18:12:25 +00:00
|
|
|
* Copyright 2015-2016 Cryptography Research, Inc.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Originally written by Mike Hamburg
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
|
|
|
#include "field.h"
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
static const gf MODULUS = {
|
|
|
|
FIELD_LITERAL(0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff,
|
|
|
|
0xffffffffffffff, 0xfffffffffffffe, 0xffffffffffffff,
|
|
|
|
0xffffffffffffff, 0xffffffffffffff)
|
2017-12-04 11:38:58 +00:00
|
|
|
};
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-05 16:14:11 +00:00
|
|
|
/* Serialize to wire format. */
|
2017-12-04 11:38:58 +00:00
|
|
|
void gf_serialize(uint8_t serial[SER_BYTES], const gf x, int with_hibit)
|
|
|
|
{
|
|
|
|
unsigned int j = 0, fill = 0;
|
2018-01-18 12:55:23 +00:00
|
|
|
dword_t buffer = 0;
|
2017-12-12 14:17:40 +00:00
|
|
|
int i;
|
2017-11-15 15:27:21 +00:00
|
|
|
gf red;
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_copy(red, x);
|
|
|
|
gf_strong_reduce(red);
|
2017-12-04 13:30:53 +00:00
|
|
|
if (!with_hibit)
|
2017-12-04 11:38:58 +00:00
|
|
|
assert(gf_hibit(red) == 0);
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2018-01-31 13:37:42 +00:00
|
|
|
for (i = 0; i < (with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
if (fill < 8 && j < NLIMBS) {
|
2017-12-04 11:38:58 +00:00
|
|
|
buffer |= ((dword_t) red->limb[LIMBPERM(j)]) << fill;
|
2017-11-15 15:27:21 +00:00
|
|
|
fill += LIMB_PLACE_VALUE(LIMBPERM(j));
|
|
|
|
j++;
|
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
serial[i] = (uint8_t)buffer;
|
2017-11-15 15:27:21 +00:00
|
|
|
fill -= 8;
|
|
|
|
buffer >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Return high bit of x = low bit of 2x mod p */
|
2017-12-04 11:38:58 +00:00
|
|
|
mask_t gf_hibit(const gf x)
|
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
gf y;
|
2018-02-12 14:27:02 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_add(y, x, x);
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_strong_reduce(y);
|
2017-12-12 14:17:40 +00:00
|
|
|
return 0 - (y->limb[0] & 1);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Return high bit of x = low bit of 2x mod p */
|
2017-12-04 11:38:58 +00:00
|
|
|
mask_t gf_lobit(const gf x)
|
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
gf y;
|
2018-02-12 14:27:02 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_copy(y, x);
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_strong_reduce(y);
|
2017-12-12 14:17:40 +00:00
|
|
|
return 0 - (y->limb[0] & 1);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Deserialize from wire format; return -1 on success and 0 on failure. */
|
2017-12-04 11:38:58 +00:00
|
|
|
mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
|
|
|
|
uint8_t hi_nmask)
|
|
|
|
{
|
|
|
|
unsigned int j = 0, fill = 0;
|
2017-11-15 15:27:21 +00:00
|
|
|
dword_t buffer = 0;
|
|
|
|
dsword_t scarry = 0;
|
|
|
|
const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
|
2018-01-18 12:55:23 +00:00
|
|
|
unsigned int i;
|
|
|
|
mask_t succ;
|
|
|
|
|
2018-01-31 13:37:42 +00:00
|
|
|
for (i = 0; i < NLIMBS; i++) {
|
|
|
|
while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
|
|
|
|
uint8_t sj;
|
|
|
|
|
|
|
|
sj = serial[j];
|
2017-12-04 11:38:58 +00:00
|
|
|
if (j == nbytes - 1)
|
|
|
|
sj &= ~hi_nmask;
|
|
|
|
buffer |= ((dword_t) sj) << fill;
|
2017-11-15 15:27:21 +00:00
|
|
|
fill += 8;
|
|
|
|
j++;
|
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
x->limb[LIMBPERM(i)] = (word_t)
|
|
|
|
((i < NLIMBS - 1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer);
|
2017-11-15 15:27:21 +00:00
|
|
|
fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
|
|
|
|
buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
|
2017-12-04 11:38:58 +00:00
|
|
|
scarry =
|
|
|
|
(scarry + x->limb[LIMBPERM(i)] -
|
|
|
|
MODULUS->limb[LIMBPERM(i)]) >> (8 * sizeof(word_t));
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
succ = with_hibit ? 0 - (mask_t) 1 : ~gf_hibit(x);
|
|
|
|
return succ & word_is_zero((word_t)buffer) & ~word_is_zero((word_t)scarry);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Reduce to canonical form. */
|
2017-12-04 11:38:58 +00:00
|
|
|
void gf_strong_reduce(gf a)
|
|
|
|
{
|
2018-01-18 12:55:23 +00:00
|
|
|
dsword_t scarry;
|
|
|
|
word_t scarry_0;
|
|
|
|
dword_t carry = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
/* first, clear high */
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_weak_reduce(a); /* Determined to have negligible perf impact. */
|
2017-11-15 15:27:21 +00:00
|
|
|
|
|
|
|
/* now the total is less than 2p */
|
|
|
|
|
|
|
|
/* compute total_value - p. No need to reduce mod p. */
|
2018-01-18 12:55:23 +00:00
|
|
|
scarry = 0;
|
2017-12-04 11:38:58 +00:00
|
|
|
for (i = 0; i < NLIMBS; i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
|
|
|
|
a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
|
|
|
|
scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
/*
|
|
|
|
* uncommon case: it was >= p, so now scarry = 0 and this = x common case:
|
|
|
|
* it was < p, so now scarry = -1 and this = x - p + 2^255 so let's add
|
|
|
|
* back in p. will carry back off the top for 2^255.
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
2017-12-04 11:38:58 +00:00
|
|
|
assert(word_is_zero(scarry) | word_is_zero(scarry + 1));
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-12 14:17:40 +00:00
|
|
|
scarry_0 = (word_t)scarry;
|
2017-11-15 15:27:21 +00:00
|
|
|
|
|
|
|
/* add it back */
|
2017-12-04 11:38:58 +00:00
|
|
|
for (i = 0; i < NLIMBS; i++) {
|
|
|
|
carry =
|
|
|
|
carry + a->limb[LIMBPERM(i)] +
|
|
|
|
(scarry_0 & MODULUS->limb[LIMBPERM(i)]);
|
2017-11-15 15:27:21 +00:00
|
|
|
a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
|
|
|
|
carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(word_is_zero(carry + scarry_0));
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Subtract two gf elements d=a-b */
|
2017-12-04 11:38:58 +00:00
|
|
|
void gf_sub(gf d, const gf a, const gf b)
|
|
|
|
{
|
|
|
|
gf_sub_RAW(d, a, b);
|
|
|
|
gf_bias(d, 2);
|
|
|
|
gf_weak_reduce(d);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Add two field elements d = a+b */
|
2017-12-04 11:38:58 +00:00
|
|
|
void gf_add(gf d, const gf a, const gf b)
|
|
|
|
{
|
|
|
|
gf_add_RAW(d, a, b);
|
|
|
|
gf_weak_reduce(d);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Compare a==b */
|
2017-12-04 11:38:58 +00:00
|
|
|
mask_t gf_eq(const gf a, const gf b)
|
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
gf c;
|
2017-12-04 11:38:58 +00:00
|
|
|
mask_t ret = 0;
|
2018-01-18 12:55:23 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_sub(c, a, b);
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_strong_reduce(c);
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2018-02-12 14:03:36 +00:00
|
|
|
for (i = 0; i < NLIMBS; i++)
|
2017-11-15 15:27:21 +00:00
|
|
|
ret |= c->limb[LIMBPERM(i)];
|
|
|
|
|
|
|
|
return word_is_zero(ret);
|
|
|
|
}
|
2017-12-05 16:59:42 +00:00
|
|
|
|
|
|
|
mask_t gf_isr(gf a, const gf x)
|
|
|
|
{
|
|
|
|
gf L0, L1, L2;
|
2018-02-12 14:27:02 +00:00
|
|
|
|
2017-12-05 16:59:42 +00:00
|
|
|
gf_sqr(L1, x);
|
|
|
|
gf_mul(L2, x, L1);
|
|
|
|
gf_sqr(L1, L2);
|
|
|
|
gf_mul(L2, x, L1);
|
|
|
|
gf_sqrn(L1, L2, 3);
|
|
|
|
gf_mul(L0, L2, L1);
|
|
|
|
gf_sqrn(L1, L0, 3);
|
|
|
|
gf_mul(L0, L2, L1);
|
|
|
|
gf_sqrn(L2, L0, 9);
|
|
|
|
gf_mul(L1, L0, L2);
|
|
|
|
gf_sqr(L0, L1);
|
|
|
|
gf_mul(L2, x, L0);
|
|
|
|
gf_sqrn(L0, L2, 18);
|
|
|
|
gf_mul(L2, L1, L0);
|
|
|
|
gf_sqrn(L0, L2, 37);
|
|
|
|
gf_mul(L1, L2, L0);
|
|
|
|
gf_sqrn(L0, L1, 37);
|
|
|
|
gf_mul(L1, L2, L0);
|
|
|
|
gf_sqrn(L0, L1, 111);
|
|
|
|
gf_mul(L2, L1, L0);
|
|
|
|
gf_sqr(L0, L2);
|
|
|
|
gf_mul(L1, x, L0);
|
|
|
|
gf_sqrn(L0, L1, 223);
|
|
|
|
gf_mul(L1, L2, L0);
|
|
|
|
gf_sqr(L2, L1);
|
|
|
|
gf_mul(L0, L2, x);
|
|
|
|
gf_copy(a, L1);
|
|
|
|
return gf_eq(L0, ONE);
|
|
|
|
}
|