Bitcoin Core  27.99.0
P2P Digital Currency
field.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5  ***********************************************************************/
6 
7 #ifndef SECP256K1_FIELD_H
8 #define SECP256K1_FIELD_H
9 
10 #include "util.h"
11 
12 /* This file defines the generic interface for working with secp256k1_fe
13  * objects, which represent field elements (integers modulo 2^256 - 2^32 - 977).
14  *
15  * The actual definition of the secp256k1_fe type depends on the chosen field
16  * implementation; see the field_5x52.h and field_10x26.h files for details.
17  *
18  * All secp256k1_fe objects have implicit properties that determine what
19  * operations are permitted on it. These are purely a function of what
20  * secp256k1_fe_ operations are applied on it, generally (implicitly) fixed at
21  * compile time, and do not depend on the chosen field implementation. Despite
22  * that, what these properties actually entail for the field representation
23  * values depends on the chosen field implementation. These properties are:
24  * - magnitude: an integer in [0,32]
25  * - normalized: 0 or 1; normalized=1 implies magnitude <= 1.
26  *
27  * In VERIFY mode, they are materialized explicitly as fields in the struct,
28  * allowing run-time verification of these properties. In that case, the field
29  * implementation also provides a secp256k1_fe_verify routine to verify that
30  * these fields match the run-time value and perform internal consistency
31  * checks. */
32 #ifdef VERIFY
33 # define SECP256K1_FE_VERIFY_FIELDS \
34  int magnitude; \
35  int normalized;
36 #else
37 # define SECP256K1_FE_VERIFY_FIELDS
38 #endif
39 
40 #if defined(SECP256K1_WIDEMUL_INT128)
41 #include "field_5x52.h"
42 #elif defined(SECP256K1_WIDEMUL_INT64)
43 #include "field_10x26.h"
44 #else
45 #error "Please select wide multiplication implementation"
46 #endif
47 
48 #ifdef VERIFY
49 /* Magnitude and normalized value for constants. */
50 #define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0) \
51  /* Magnitude is 0 for constant 0; 1 otherwise. */ \
52  , (((d7) | (d6) | (d5) | (d4) | (d3) | (d2) | (d1) | (d0)) != 0) \
53  /* Normalized is 1 unless sum(d_i<<(32*i) for i=0..7) exceeds field modulus. */ \
54  , (!(((d7) & (d6) & (d5) & (d4) & (d3) & (d2)) == 0xfffffffful && ((d1) == 0xfffffffful || ((d1) == 0xfffffffe && (d0 >= 0xfffffc2f)))))
55 #else
56 #define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
57 #endif
58 
66 #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)) }
67 
68 static const secp256k1_fe secp256k1_fe_one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
70  0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
71  0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
72 );
73 
74 #ifndef VERIFY
75 /* In non-VERIFY mode, we #define the fe operations to be identical to their
76  * internal field implementation, to avoid the potential overhead of a
77  * function call (even though presumably inlinable). */
78 # define secp256k1_fe_normalize secp256k1_fe_impl_normalize
79 # define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
80 # define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
81 # define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
82 # define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
83 # define secp256k1_fe_set_int secp256k1_fe_impl_set_int
84 # define secp256k1_fe_clear secp256k1_fe_impl_clear
85 # define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
86 # define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
87 # define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
88 # define secp256k1_fe_set_b32_mod secp256k1_fe_impl_set_b32_mod
89 # define secp256k1_fe_set_b32_limit secp256k1_fe_impl_set_b32_limit
90 # define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
91 # define secp256k1_fe_negate_unchecked secp256k1_fe_impl_negate_unchecked
92 # define secp256k1_fe_mul_int_unchecked secp256k1_fe_impl_mul_int_unchecked
93 # define secp256k1_fe_add secp256k1_fe_impl_add
94 # define secp256k1_fe_mul secp256k1_fe_impl_mul
95 # define secp256k1_fe_sqr secp256k1_fe_impl_sqr
96 # define secp256k1_fe_cmov secp256k1_fe_impl_cmov
97 # define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
98 # define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
99 # define secp256k1_fe_inv secp256k1_fe_impl_inv
100 # define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
101 # define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
102 # define secp256k1_fe_half secp256k1_fe_impl_half
103 # define secp256k1_fe_add_int secp256k1_fe_impl_add_int
104 # define secp256k1_fe_is_square_var secp256k1_fe_impl_is_square_var
105 #endif /* !defined(VERIFY) */
106 
113 
120 
126 
133 
139 
145 static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
146 
153 
162 static int secp256k1_fe_is_zero(const secp256k1_fe *a);
163 
169 static int secp256k1_fe_is_odd(const secp256k1_fe *a);
170 
177 static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b);
178 
185 static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
186 
193 static void secp256k1_fe_set_b32_mod(secp256k1_fe *r, const unsigned char *a);
194 
201 static int secp256k1_fe_set_b32_limit(secp256k1_fe *r, const unsigned char *a);
202 
207 static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
208 
216 #define secp256k1_fe_negate(r, a, m) ASSERT_INT_CONST_AND_DO(m, secp256k1_fe_negate_unchecked(r, a, m))
217 
222 static void secp256k1_fe_negate_unchecked(secp256k1_fe *r, const secp256k1_fe *a, int m);
223 
229 static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
230 
238 #define secp256k1_fe_mul_int(r, a) ASSERT_INT_CONST_AND_DO(a, secp256k1_fe_mul_int_unchecked(r, a))
239 
245 
253 static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
254 
264 
272 static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a);
273 
284 
292 static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a);
293 
299 
306 
314 
317 
326 static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
327 
335 
339 static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m);
340 
346 
348 static void secp256k1_fe_verify(const secp256k1_fe *a);
349 #define SECP256K1_FE_VERIFY(a) secp256k1_fe_verify(a)
350 
352 static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m);
353 #define SECP256K1_FE_VERIFY_MAGNITUDE(a, m) secp256k1_fe_verify_magnitude(a, m)
354 
355 #endif /* SECP256K1_FIELD_H */
#define secp256k1_fe_cmov
Definition: field.h:96
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
#define secp256k1_fe_cmp_var
Definition: field.h:87
static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m)
Check that magnitude of a is at most m (no-op unless VERIFY is enabled).
#define secp256k1_fe_normalize_weak
Definition: field.h:79
static const secp256k1_fe secp256k1_const_beta
Definition: field.h:69
static void secp256k1_fe_verify(const secp256k1_fe *a)
Check invariants on a field element (no-op unless VERIFY is enabled).
#define secp256k1_fe_is_odd
Definition: field.h:86
#define secp256k1_fe_mul
Definition: field.h:94
static const secp256k1_fe secp256k1_fe_one
Definition: field.h:68
static int secp256k1_fe_sqrt(secp256k1_fe *SECP256K1_RESTRICT r, const secp256k1_fe *SECP256K1_RESTRICT a)
Compute a square root of a field element.
#define secp256k1_fe_add
Definition: field.h:93
#define secp256k1_fe_clear
Definition: field.h:84
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_half
Definition: field.h:102
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
This expands to an initializer for a secp256k1_fe valued sum((i*32) * d_i, i=0..7) mod p.
Definition: field.h:66
#define secp256k1_fe_to_storage
Definition: field.h:97
#define secp256k1_fe_inv_var
Definition: field.h:100
#define secp256k1_fe_is_zero
Definition: field.h:85
#define secp256k1_fe_mul_int_unchecked
Definition: field.h:92
#define secp256k1_fe_set_b32_limit
Definition: field.h:89
#define secp256k1_fe_is_square_var
Definition: field.h:104
#define secp256k1_fe_get_bounds
Definition: field.h:101
#define secp256k1_fe_from_storage
Definition: field.h:98
#define secp256k1_fe_set_b32_mod
Definition: field.h:88
#define secp256k1_fe_negate_unchecked
Definition: field.h:91
#define secp256k1_fe_get_b32
Definition: field.h:90
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
#define secp256k1_fe_inv
Definition: field.h:99
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize
Definition: field.h:78
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
#define secp256k1_fe_add_int
Definition: field.h:103
#define secp256k1_fe_set_int
Definition: field.h:83
#define SECP256K1_RESTRICT
Definition: util.h:171
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
Definition: field_10x26.h:14