2015-04-14 12:07:44 -07:00
|
|
|
/* Copyright (c) 2015, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2016-07-04 14:03:08 -10:00
|
|
|
#ifndef OPENSSL_HEADER_EC_ECP_NISTZ_H
|
|
|
|
#define OPENSSL_HEADER_EC_ECP_NISTZ_H
|
|
|
|
|
2017-04-19 14:56:44 -10:00
|
|
|
#include <GFp/base.h>
|
2015-04-14 12:07:44 -07:00
|
|
|
|
2018-05-16 12:51:05 -10:00
|
|
|
#include "../../limbs/limbs.h"
|
2015-04-14 12:07:44 -07:00
|
|
|
|
2019-01-17 18:09:25 -10:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wconversion"
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
#endif
|
2015-04-14 12:07:44 -07:00
|
|
|
|
2018-04-29 10:59:44 -10:00
|
|
|
// This function looks at `w + 1` scalar bits (`w` current, 1 adjacent less
|
2017-08-18 14:06:02 -04:00
|
|
|
// significant bit), and recodes them into a signed digit for use in fast point
|
|
|
|
// multiplication: the use of signed rather than unsigned digits means that
|
|
|
|
// fewer points need to be precomputed, given that point inversion is easy (a
|
|
|
|
// precomputed point dP makes -dP available as well).
|
|
|
|
//
|
|
|
|
// BACKGROUND:
|
|
|
|
//
|
|
|
|
// Signed digits for multiplication were introduced by Booth ("A signed binary
|
|
|
|
// multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
|
|
|
|
// pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
|
|
|
|
// Booth's original encoding did not generally improve the density of nonzero
|
|
|
|
// digits over the binary representation, and was merely meant to simplify the
|
|
|
|
// handling of signed factors given in two's complement; but it has since been
|
|
|
|
// shown to be the basis of various signed-digit representations that do have
|
|
|
|
// further advantages, including the wNAF, using the following general
|
|
|
|
// approach:
|
|
|
|
//
|
|
|
|
// (1) Given a binary representation
|
|
|
|
//
|
|
|
|
// b_k ... b_2 b_1 b_0,
|
|
|
|
//
|
|
|
|
// of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
|
|
|
|
// by using bit-wise subtraction as follows:
|
|
|
|
//
|
|
|
|
// b_k b_(k-1) ... b_2 b_1 b_0
|
|
|
|
// - b_k ... b_3 b_2 b_1 b_0
|
|
|
|
// -------------------------------------
|
|
|
|
// s_k b_(k-1) ... s_3 s_2 s_1 s_0
|
|
|
|
//
|
|
|
|
// A left-shift followed by subtraction of the original value yields a new
|
|
|
|
// representation of the same value, using signed bits s_i = b_(i+1) - b_i.
|
|
|
|
// This representation from Booth's paper has since appeared in the
|
|
|
|
// literature under a variety of different names including "reversed binary
|
|
|
|
// form", "alternating greedy expansion", "mutual opposite form", and
|
|
|
|
// "sign-alternating {+-1}-representation".
|
|
|
|
//
|
|
|
|
// An interesting property is that among the nonzero bits, values 1 and -1
|
|
|
|
// strictly alternate.
|
|
|
|
//
|
|
|
|
// (2) Various window schemes can be applied to the Booth representation of
|
|
|
|
// integers: for example, right-to-left sliding windows yield the wNAF
|
|
|
|
// (a signed-digit encoding independently discovered by various researchers
|
|
|
|
// in the 1990s), and left-to-right sliding windows yield a left-to-right
|
|
|
|
// equivalent of the wNAF (independently discovered by various researchers
|
|
|
|
// around 2004).
|
|
|
|
//
|
|
|
|
// To prevent leaking information through side channels in point multiplication,
|
|
|
|
// we need to recode the given integer into a regular pattern: sliding windows
|
|
|
|
// as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
|
|
|
|
// decades older: we'll be using the so-called "modified Booth encoding" due to
|
|
|
|
// MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
|
2018-04-29 10:59:44 -10:00
|
|
|
// (1961), pp. 67-91), in a radix-2**w setting. That is, we always combine `w`
|
|
|
|
// signed bits into a signed digit, e.g. (for `w == 5`):
|
2017-08-18 14:06:02 -04:00
|
|
|
//
|
|
|
|
// s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
|
|
|
|
//
|
|
|
|
// The sign-alternating property implies that the resulting digit values are
|
2018-04-29 10:59:44 -10:00
|
|
|
// integers from `-2**(w-1)` to `2**(w-1)`, e.g. -16 to 16 for `w == 5`.
|
2017-08-18 14:06:02 -04:00
|
|
|
//
|
|
|
|
// Of course, we don't actually need to compute the signed digits s_i as an
|
|
|
|
// intermediate step (that's just a nice way to see how this scheme relates
|
|
|
|
// to the wNAF): a direct computation obtains the recoded digit from the
|
|
|
|
// six bits b_(4j + 4) ... b_(4j - 1).
|
|
|
|
//
|
2018-04-29 10:59:44 -10:00
|
|
|
// This function takes those `w` bits as an integer, writing the recoded digit
|
|
|
|
// to |*is_negative| (a mask for `constant_time_select_s`) and |*digit|
|
|
|
|
// (absolute value, in the range 0 .. 2**(w-1). Note that this integer
|
|
|
|
// essentially provides the input bits "shifted to the left" by one position.
|
|
|
|
// For example, the input to compute the least significant recoded digit, given
|
|
|
|
// that there's no bit b_-1, has to be b_4 b_3 b_2 b_1 b_0 0.
|
2018-05-16 12:51:05 -10:00
|
|
|
static inline void booth_recode(Limb *is_negative, unsigned *digit,
|
2016-07-04 14:03:08 -10:00
|
|
|
unsigned in, unsigned w) {
|
|
|
|
assert(w >= 2);
|
|
|
|
assert(w <= 7);
|
2015-04-14 12:07:44 -07:00
|
|
|
|
2018-04-29 10:59:44 -10:00
|
|
|
// Set all bits of `s` to MSB(in), similar to |constant_time_msb_s|,
|
|
|
|
// but 'in' seen as (`w+1`)-bit value.
|
2018-05-16 12:51:05 -10:00
|
|
|
Limb s = ~((in >> w) - 1);
|
2016-07-04 14:03:08 -10:00
|
|
|
unsigned d;
|
|
|
|
d = (1 << (w + 1)) - in - 1;
|
2015-04-14 12:07:44 -07:00
|
|
|
d = (d & s) | (in & ~s);
|
|
|
|
d = (d >> 1) + (d & 1);
|
|
|
|
|
2018-04-26 16:49:26 -10:00
|
|
|
*is_negative = constant_time_is_nonzero_w(s & 1);
|
2015-04-14 12:07:44 -07:00
|
|
|
*digit = d;
|
|
|
|
}
|
|
|
|
|
2019-01-17 18:09:25 -10:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2016-07-04 14:03:08 -10:00
|
|
|
|
2016-07-07 21:13:23 -10:00
|
|
|
void gfp_little_endian_bytes_from_scalar(uint8_t str[], size_t str_len,
|
2018-05-16 12:51:05 -10:00
|
|
|
const Limb scalar[],
|
2016-07-07 21:13:23 -10:00
|
|
|
size_t num_limbs);
|
|
|
|
|
2018-04-29 10:59:44 -10:00
|
|
|
#endif // OPENSSL_HEADER_EC_ECP_NISTZ_H
|