2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2014 Cryptography Research, Inc.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
10 * Originally written by Mike Hamburg
16 #include "constant_time.h"
20 /** Square x, n times. */
21 static ossl_inline void gf_sqrn (
42 #define gf_add_nr gf_add_RAW
44 /** Subtract mod p. Bias by 2 and don't reduce */
45 static ossl_inline void gf_sub_nr ( gf c, const gf a, const gf b ) {
48 if (GF_HEADROOM < 3) gf_weak_reduce(c);
51 /** Subtract mod p. Bias by amt but don't reduce. */
52 static ossl_inline void gf_subx_nr ( gf c, const gf a, const gf b, int amt ) {
55 if (GF_HEADROOM < amt+1) gf_weak_reduce(c);
58 /** Mul by signed int. Not constant-time WRT the sign of that int. */
59 static ossl_inline void gf_mulw(gf c, const gf a, int32_t w) {
61 gf_mulw_unsigned(c, a, w);
63 gf_mulw_unsigned(c, a, -w);
68 /** Constant time, x = is_z ? z : y */
69 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z) {
70 constant_time_select(x,y,z,sizeof(gf),is_z,0);
73 /** Constant time, if (neg) x=-x; */
74 static ossl_inline void gf_cond_neg(gf x, mask_t neg) {
77 gf_cond_sel(x,x,y,neg);
80 /** Constant time, if (swap) (x,y) = (y,x); */
81 static ossl_inline void
82 gf_cond_swap(gf x, gf_s *__restrict__ y, mask_t swap) {
83 constant_time_cond_swap(x,y,sizeof(gf_s),swap);
86 static ossl_inline void gf_mul_qnr(gf_s *__restrict__ out, const gf x) {
90 static ossl_inline void gf_div_qnr(gf_s *__restrict__ out, const gf x) {