2020-12-17 08:33:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013, 2014 Pieter Wuille *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2013-05-09 15:24:32 +02:00
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#ifndef SECP256K1_FIELD_H
|
|
|
|
#define SECP256K1_FIELD_H
|
2013-03-08 01:20:41 +01:00
|
|
|
|
2020-08-09 10:58:40 -07:00
|
|
|
#include "util.h"
|
|
|
|
|
2022-01-28 17:39:30 -05:00
|
|
|
/* This file defines the generic interface for working with secp256k1_fe
|
|
|
|
* objects, which represent field elements (integers modulo 2^256 - 2^32 - 977).
|
|
|
|
*
|
|
|
|
* The actual definition of the secp256k1_fe type depends on the chosen field
|
|
|
|
* implementation; see the field_5x52.h and field_10x26.h files for details.
|
|
|
|
*
|
|
|
|
* All secp256k1_fe objects have implicit properties that determine what
|
|
|
|
* operations are permitted on it. These are purely a function of what
|
|
|
|
* secp256k1_fe_ operations are applied on it, generally (implicitly) fixed at
|
|
|
|
* compile time, and do not depend on the chosen field implementation. Despite
|
|
|
|
* that, what these properties actually entail for the field representation
|
|
|
|
* values depends on the chosen field implementation. These properties are:
|
|
|
|
* - magnitude: an integer in [0,32]
|
|
|
|
* - normalized: 0 or 1; normalized=1 implies magnitude <= 1.
|
|
|
|
*
|
|
|
|
* In VERIFY mode, they are materialized explicitly as fields in the struct,
|
|
|
|
* allowing run-time verification of these properties. In that case, the field
|
|
|
|
* implementation also provides a secp256k1_fe_verify routine to verify that
|
|
|
|
* these fields match the run-time value and perform internal consistency
|
|
|
|
* checks. */
|
|
|
|
#ifdef VERIFY
|
|
|
|
# define SECP256K1_FE_VERIFY_FIELDS \
|
|
|
|
int magnitude; \
|
|
|
|
int normalized;
|
|
|
|
#else
|
|
|
|
# define SECP256K1_FE_VERIFY_FIELDS
|
|
|
|
#endif
|
|
|
|
|
2020-08-09 10:58:40 -07:00
|
|
|
#if defined(SECP256K1_WIDEMUL_INT128)
|
2013-03-30 21:49:09 +01:00
|
|
|
#include "field_5x52.h"
|
2020-08-09 10:58:40 -07:00
|
|
|
#elif defined(SECP256K1_WIDEMUL_INT64)
|
|
|
|
#include "field_10x26.h"
|
2013-04-07 00:37:06 +02:00
|
|
|
#else
|
2020-08-09 10:58:40 -07:00
|
|
|
#error "Please select wide multiplication implementation"
|
2013-04-03 03:43:14 +02:00
|
|
|
#endif
|
2013-03-08 01:20:41 +01:00
|
|
|
|
2022-02-01 11:15:09 -05:00
|
|
|
#ifdef VERIFY
|
|
|
|
/* Magnitude and normalized value for constants. */
|
|
|
|
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0) \
|
|
|
|
/* Magnitude is 0 for constant 0; 1 otherwise. */ \
|
|
|
|
, (((d7) | (d6) | (d5) | (d4) | (d3) | (d2) | (d1) | (d0)) != 0) \
|
|
|
|
/* Normalized is 1 unless sum(d_i<<(32*i) for i=0..7) exceeds field modulus. */ \
|
|
|
|
, (!(((d7) & (d6) & (d5) & (d4) & (d3) & (d2)) == 0xfffffffful && ((d1) == 0xfffffffful || ((d1) == 0xfffffffe && (d0 >= 0xfffffc2f)))))
|
|
|
|
#else
|
|
|
|
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** This expands to an initializer for a secp256k1_fe valued sum((i*32) * d_i, i=0..7) mod p.
|
|
|
|
*
|
|
|
|
* It has magnitude 1, unless d_i are all 0, in which case the magnitude is 0.
|
|
|
|
* It is normalized, unless sum(2^(i*32) * d_i, i=0..7) >= p.
|
|
|
|
*
|
|
|
|
* SECP256K1_FE_CONST_INNER is provided by the implementation.
|
|
|
|
*/
|
|
|
|
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) SECP256K1_FE_VERIFY_CONST((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) }
|
|
|
|
|
2022-01-19 09:53:02 -05:00
|
|
|
static const secp256k1_fe secp256k1_fe_one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
|
2021-02-27 07:57:16 -05:00
|
|
|
static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
|
|
|
|
0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
|
|
|
|
0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
|
|
|
|
);
|
2022-01-19 09:53:02 -05:00
|
|
|
|
2022-01-28 17:39:30 -05:00
|
|
|
#ifndef VERIFY
|
|
|
|
/* In non-VERIFY mode, we #define the fe operations to be identical to their
|
|
|
|
* internal field implementation, to avoid the potential overhead of a
|
|
|
|
* function call (even though presumably inlinable). */
|
2022-01-28 17:51:12 -05:00
|
|
|
# define secp256k1_fe_normalize secp256k1_fe_impl_normalize
|
2022-01-28 17:54:22 -05:00
|
|
|
# define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
|
2022-01-28 17:59:36 -05:00
|
|
|
# define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
|
2022-01-31 16:51:10 -05:00
|
|
|
# define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
|
|
|
|
# define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
|
2022-01-28 18:04:15 -05:00
|
|
|
# define secp256k1_fe_set_int secp256k1_fe_impl_set_int
|
2022-01-31 17:15:41 -05:00
|
|
|
# define secp256k1_fe_clear secp256k1_fe_impl_clear
|
2022-01-28 18:07:55 -05:00
|
|
|
# define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
|
2022-01-28 18:11:21 -05:00
|
|
|
# define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
|
2022-01-28 18:16:16 -05:00
|
|
|
# define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
|
2022-01-28 18:19:00 -05:00
|
|
|
# define secp256k1_fe_set_b32 secp256k1_fe_impl_set_b32
|
2022-01-28 18:23:54 -05:00
|
|
|
# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
|
2022-01-28 18:27:38 -05:00
|
|
|
# define secp256k1_fe_negate secp256k1_fe_impl_negate
|
2022-01-28 18:33:45 -05:00
|
|
|
# define secp256k1_fe_mul_int secp256k1_fe_impl_mul_int
|
2022-01-28 18:36:13 -05:00
|
|
|
# define secp256k1_fe_add secp256k1_fe_impl_add
|
2022-01-28 18:40:33 -05:00
|
|
|
# define secp256k1_fe_mul secp256k1_fe_impl_mul
|
2022-01-28 18:42:47 -05:00
|
|
|
# define secp256k1_fe_sqr secp256k1_fe_impl_sqr
|
2022-01-28 18:45:42 -05:00
|
|
|
# define secp256k1_fe_cmov secp256k1_fe_impl_cmov
|
2022-01-28 18:56:54 -05:00
|
|
|
# define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
|
2022-01-28 19:02:03 -05:00
|
|
|
# define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
|
2022-01-28 19:20:51 -05:00
|
|
|
# define secp256k1_fe_inv secp256k1_fe_impl_inv
|
|
|
|
# define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
|
2022-06-08 15:04:49 -04:00
|
|
|
# define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
|
2022-11-17 11:28:49 -05:00
|
|
|
# define secp256k1_fe_half secp256k1_fe_impl_half
|
2022-01-28 17:39:30 -05:00
|
|
|
#endif /* !defined(VERIFY) */
|
|
|
|
|
2022-01-28 17:51:12 -05:00
|
|
|
/** Normalize a field element.
|
|
|
|
*
|
|
|
|
* On input, r must be a valid field element.
|
|
|
|
* On output, r represents the same value but has normalized=1 and magnitude=1.
|
2020-01-16 16:52:09 +01:00
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_normalize(secp256k1_fe *r);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 17:54:22 -05:00
|
|
|
/** Give a field element magnitude 1.
|
|
|
|
*
|
|
|
|
* On input, r must be a valid field element.
|
|
|
|
* On output, r represents the same value but has magnitude=1. Normalized is unchanged.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_normalize_weak(secp256k1_fe *r);
|
2014-12-10 14:34:25 +01:00
|
|
|
|
2022-01-28 17:59:36 -05:00
|
|
|
/** Normalize a field element, without constant-time guarantee.
|
|
|
|
*
|
|
|
|
* Identical in behavior to secp256k1_fe_normalize, but not constant time in r.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_normalize_var(secp256k1_fe *r);
|
2014-12-05 03:37:42 +01:00
|
|
|
|
2022-01-31 16:51:10 -05:00
|
|
|
/** Determine whether r represents field element 0.
|
|
|
|
*
|
|
|
|
* On input, r must be a valid field element.
|
|
|
|
* Returns whether r = 0 (mod p).
|
|
|
|
*/
|
2021-03-12 10:31:54 -08:00
|
|
|
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
|
2014-12-12 12:55:01 +07:00
|
|
|
|
2022-01-31 16:51:10 -05:00
|
|
|
/** Determine whether r represents field element 0, without constant-time guarantee.
|
|
|
|
*
|
|
|
|
* Identical in behavior to secp256k1_normalizes_to_zero, but not constant time in r.
|
|
|
|
*/
|
2021-03-12 10:31:54 -08:00
|
|
|
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r);
|
2014-12-13 17:14:26 +07:00
|
|
|
|
2022-01-28 18:04:15 -05:00
|
|
|
/** Set a field element to an integer in range [0,0x7FFF].
|
|
|
|
*
|
|
|
|
* On input, r does not need to be initialized, a must be in [0,0x7FFF].
|
|
|
|
* On output, r represents value a, is normalized and has magnitude (a!=0).
|
2021-05-13 10:40:50 -04:00
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-31 17:15:41 -05:00
|
|
|
/** Set a field element to 0.
|
|
|
|
*
|
|
|
|
* On input, a does not need to be initialized.
|
|
|
|
* On output, a represents 0, is normalized and has magnitude 0.
|
|
|
|
*/
|
2015-09-17 18:54:52 -05:00
|
|
|
static void secp256k1_fe_clear(secp256k1_fe *a);
|
|
|
|
|
2022-01-28 18:07:55 -05:00
|
|
|
/** Determine whether a represents field element 0.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid normalized field element.
|
|
|
|
* Returns whether a = 0 (mod p).
|
|
|
|
*
|
|
|
|
* This behaves identical to secp256k1_normalizes_to_zero{,_var}, but requires
|
|
|
|
* normalized input (and is much faster).
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_fe_is_zero(const secp256k1_fe *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:11:21 -05:00
|
|
|
/** Determine whether a (mod p) is odd.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid normalized field element.
|
|
|
|
* Returns (int(a) mod p) & 1.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_fe_is_odd(const secp256k1_fe *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-31 17:34:36 -05:00
|
|
|
/** Determine whether two field elements are equal.
|
|
|
|
*
|
|
|
|
* On input, a and b must be valid field elements with magnitudes not exceeding
|
|
|
|
* 1 and 31, respectively.
|
|
|
|
* Returns a = b (mod p).
|
|
|
|
*/
|
2016-07-09 14:23:44 +02:00
|
|
|
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b);
|
|
|
|
|
2022-01-31 17:34:36 -05:00
|
|
|
/** Determine whether two field elements are equal, without constant-time guarantee.
|
|
|
|
*
|
|
|
|
* Identical in behavior to secp256k1_fe_equal, but not constant time in either a or b.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:16:16 -05:00
|
|
|
/** Compare the values represented by 2 field elements, without constant-time guarantee.
|
|
|
|
*
|
|
|
|
* On input, a and b must be valid normalized field elements.
|
|
|
|
* Returns 1 if a > b, -1 if a < b, and 0 if a = b (comparisons are done as integers
|
|
|
|
* in range 0..p-1).
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
|
2014-11-26 17:26:39 +01:00
|
|
|
|
2022-01-28 18:19:00 -05:00
|
|
|
/** Set a field element equal to a provided 32-byte big endian value.
|
|
|
|
*
|
|
|
|
* On input, r does not need to be initalized. a must be a pointer to an initialized 32-byte array.
|
|
|
|
* On output, r = a (mod p). It will have magnitude 1, and if (a < p), it will be normalized.
|
|
|
|
* If not, it will only be weakly normalized. Returns whether (a < p).
|
|
|
|
*
|
|
|
|
* Note that this function is unusual in that the normalization of the output depends on the
|
|
|
|
* run-time value of a.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:23:54 -05:00
|
|
|
/** Convert a field element to 32-byte big endian byte array.
|
|
|
|
* On input, a must be a valid normalized field element, and r a pointer to a 32-byte array.
|
|
|
|
* On output, r = a (mod p).
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:27:38 -05:00
|
|
|
/** Negate a field element.
|
|
|
|
*
|
|
|
|
* On input, r does not need to be initialized. a must be a valid field element with
|
|
|
|
* magnitude not exceeding m. m must be an integer in [0,31].
|
|
|
|
* Performs {r = -a}.
|
|
|
|
* On output, r will not be normalized, and will have magnitude m+1.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2023-03-01 16:19:35 -05:00
|
|
|
/** Adds a small integer (up to 0x7FFF) to r. The resulting magnitude increases by one. */
|
|
|
|
static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
|
|
|
|
|
2022-01-28 18:33:45 -05:00
|
|
|
/** Multiply a field element with a small integer.
|
|
|
|
*
|
|
|
|
* On input, r must be a valid field element. a must be an integer in [0,32].
|
|
|
|
* The magnitude of r times a must not exceed 32.
|
|
|
|
* Performs {r *= a}.
|
|
|
|
* On output, r's magnitude is multiplied by a, and r will not be normalized.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_mul_int(secp256k1_fe *r, int a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:36:13 -05:00
|
|
|
/** Increment a field element by another.
|
|
|
|
*
|
|
|
|
* On input, r and a must be valid field elements, not necessarily normalized.
|
|
|
|
* The sum of their magnitudes must not exceed 32.
|
|
|
|
* Performs {r += a}.
|
|
|
|
* On output, r will not be normalized, and will have magnitude incremented by a's.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:40:33 -05:00
|
|
|
/** Multiply two field elements.
|
|
|
|
*
|
|
|
|
* On input, a and b must be valid field elements; r does not need to be initialized.
|
|
|
|
* r and a may point to the same object, but neither can be equal to b. The magnitudes
|
|
|
|
* of a and b must not exceed 8.
|
|
|
|
* Performs {r = a * b}
|
|
|
|
* On output, r will have magnitude 1, but won't be normalized.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 18:42:47 -05:00
|
|
|
/** Square a field element.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid field element; r does not need to be initialized. The magnitude
|
|
|
|
* of a must not exceed 8.
|
|
|
|
* Performs {r = a**2}
|
|
|
|
* On output, r will have magnitude 1, but won't be normalized.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-31 18:19:45 -05:00
|
|
|
/** Compute a square root of a field element.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid field element with magnitude<=8; r need not be initialized.
|
|
|
|
* Performs {r = sqrt(a)} or {r = sqrt(-a)}, whichever exists. The resulting value
|
|
|
|
* represented by r will be a square itself. Variables r and a must not point to the same object.
|
|
|
|
* On output, r will have magnitude 1 but will not be normalized.
|
|
|
|
*/
|
|
|
|
static int secp256k1_fe_sqrt(secp256k1_fe * SECP256K1_RESTRICT r, const secp256k1_fe * SECP256K1_RESTRICT a);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2022-01-28 19:20:51 -05:00
|
|
|
/** Compute the modular inverse of a field element.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid field element; r need not be initialized.
|
|
|
|
* Performs {r = a**(p-2)} (which maps 0 to 0, and every other element to its
|
|
|
|
* inverse).
|
|
|
|
* On output, r will have magnitude (a.magnitude != 0) and be normalized.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a);
|
2013-03-08 01:20:41 +01:00
|
|
|
|
2022-01-28 19:20:51 -05:00
|
|
|
/** Compute the modular inverse of a field element, without constant-time guarantee.
|
|
|
|
*
|
|
|
|
* Behaves identically to secp256k1_fe_inv, but is not constant-time in a.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
|
2013-03-09 22:47:40 +01:00
|
|
|
|
2022-01-28 18:56:54 -05:00
|
|
|
/** Convert a field element to secp256k1_fe_storage.
|
|
|
|
*
|
|
|
|
* On input, a must be a valid normalized field element.
|
|
|
|
* Performs {r = a}.
|
|
|
|
*/
|
2015-09-24 08:42:37 +00:00
|
|
|
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
|
2015-01-24 23:04:48 -04:00
|
|
|
|
2022-01-28 19:02:03 -05:00
|
|
|
/** Convert a field element back from secp256k1_fe_storage.
|
|
|
|
*
|
|
|
|
* On input, r need not be initialized.
|
|
|
|
* Performs {r = a}.
|
|
|
|
* On output, r will be normalized and will have magnitude 1.
|
|
|
|
*/
|
2015-09-24 08:42:37 +00:00
|
|
|
static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
|
2015-01-24 23:04:48 -04:00
|
|
|
|
2020-05-20 15:09:13 +03:00
|
|
|
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag);
|
2014-12-02 20:20:13 +01:00
|
|
|
|
2022-01-28 18:45:42 -05:00
|
|
|
/** Conditionally move a field element in constant time.
|
|
|
|
*
|
|
|
|
* On input, both r and a must be valid field elements. Flag must be 0 or 1.
|
|
|
|
* Performs {r = flag ? a : r}.
|
|
|
|
* On output, r's magnitude and normalized will equal a's in case of flag=1, unchanged otherwise.
|
|
|
|
*/
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
|
2015-04-22 00:20:54 +00:00
|
|
|
|
2022-11-17 11:28:49 -05:00
|
|
|
/** Halve the value of a field element modulo the field prime in constant-time.
|
|
|
|
*
|
|
|
|
* On input, r must be a valid field element.
|
|
|
|
* On output, r will be normalized and have magnitude floor(m/2) + 1 where m is
|
|
|
|
* the magnitude of r on input.
|
|
|
|
*/
|
2021-12-06 01:24:30 +07:00
|
|
|
static void secp256k1_fe_half(secp256k1_fe *r);
|
|
|
|
|
2022-06-08 15:04:49 -04:00
|
|
|
/** Sets r to a field element with magnitude m, normalized if (and only if) m==0.
|
|
|
|
* The value is chosen so that it is likely to trigger edge cases related to
|
|
|
|
* internal overflows. */
|
2021-12-23 16:41:10 +07:00
|
|
|
static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m);
|
|
|
|
|
2023-01-04 16:05:34 -05:00
|
|
|
/** Determine whether a is a square (modulo p). */
|
|
|
|
static int secp256k1_fe_is_square_var(const secp256k1_fe *a);
|
|
|
|
|
2023-05-09 12:46:00 -04:00
|
|
|
/** Check invariants on a field element (no-op unless VERIFY is enabled). */
|
2020-09-01 16:33:22 -07:00
|
|
|
static void secp256k1_fe_verify(const secp256k1_fe *a);
|
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#endif /* SECP256K1_FIELD_H */
|