Bitcoin Core  29.1.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_is_zero secp256k1_fe_impl_is_zero
85 # define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
86 # define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
87 # define secp256k1_fe_set_b32_mod secp256k1_fe_impl_set_b32_mod
88 # define secp256k1_fe_set_b32_limit secp256k1_fe_impl_set_b32_limit
89 # define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
90 # define secp256k1_fe_negate_unchecked secp256k1_fe_impl_negate_unchecked
91 # define secp256k1_fe_mul_int_unchecked secp256k1_fe_impl_mul_int_unchecked
92 # define secp256k1_fe_add secp256k1_fe_impl_add
93 # define secp256k1_fe_mul secp256k1_fe_impl_mul
94 # define secp256k1_fe_sqr secp256k1_fe_impl_sqr
95 # define secp256k1_fe_cmov secp256k1_fe_impl_cmov
96 # define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
97 # define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
98 # define secp256k1_fe_inv secp256k1_fe_impl_inv
99 # define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
100 # define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
101 # define secp256k1_fe_half secp256k1_fe_impl_half
102 # define secp256k1_fe_add_int secp256k1_fe_impl_add_int
103 # define secp256k1_fe_is_square_var secp256k1_fe_impl_is_square_var
104 #endif /* !defined(VERIFY) */
105 
111 static void secp256k1_fe_normalize(secp256k1_fe *r);
112 
119 
125 
131 static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
132 
138 
144 static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
145 
147 static void secp256k1_fe_clear(secp256k1_fe *a);
148 
157 static int secp256k1_fe_is_zero(const secp256k1_fe *a);
158 
164 static int secp256k1_fe_is_odd(const secp256k1_fe *a);
165 
172 static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b);
173 
180 static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
181 
188 static void secp256k1_fe_set_b32_mod(secp256k1_fe *r, const unsigned char *a);
189 
196 static int secp256k1_fe_set_b32_limit(secp256k1_fe *r, const unsigned char *a);
197 
202 static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
203 
211 #define secp256k1_fe_negate(r, a, m) ASSERT_INT_CONST_AND_DO(m, secp256k1_fe_negate_unchecked(r, a, m))
212 
217 static void secp256k1_fe_negate_unchecked(secp256k1_fe *r, const secp256k1_fe *a, int m);
218 
224 static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
225 
233 #define secp256k1_fe_mul_int(r, a) ASSERT_INT_CONST_AND_DO(a, secp256k1_fe_mul_int_unchecked(r, a))
234 
239 static void secp256k1_fe_mul_int_unchecked(secp256k1_fe *r, int a);
240 
248 static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
249 
258 static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
259 
267 static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a);
268 
279 
287 static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a);
288 
293 static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
294 
301 
309 
311 static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag);
312 
321 static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
322 
329 static void secp256k1_fe_half(secp256k1_fe *r);
330 
334 static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m);
335 
340 static int secp256k1_fe_is_square_var(const secp256k1_fe *a);
341 
343 static void secp256k1_fe_verify(const secp256k1_fe *a);
344 #define SECP256K1_FE_VERIFY(a) secp256k1_fe_verify(a)
345 
347 static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m);
348 #define SECP256K1_FE_VERIFY_MAGNITUDE(a, m) secp256k1_fe_verify_magnitude(a, m)
349 
350 #endif /* SECP256K1_FIELD_H */
#define secp256k1_fe_set_b32_mod
Definition: field.h:87
This field implementation represents the value as 10 uint32_t limbs in base 2^26. ...
Definition: field_10x26.h:14
#define secp256k1_fe_add_int
Definition: field.h:102
#define secp256k1_fe_negate_unchecked
Definition: field.h:90
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_set_b32_limit
Definition: field.h:88
#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_is_zero
Definition: field.h:84
#define secp256k1_fe_inv_var
Definition: field.h:99
#define secp256k1_fe_mul
Definition: field.h:93
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
#define secp256k1_fe_mul_int_unchecked
Definition: field.h:91
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
#define secp256k1_fe_is_square_var
Definition: field.h:103
#define secp256k1_fe_half
Definition: field.h:101
#define secp256k1_fe_sqr
Definition: field.h:94
#define secp256k1_fe_normalize_weak
Definition: field.h:79
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
Definition: field.h:92
#define secp256k1_fe_cmov
Definition: field.h:95
static void secp256k1_fe_clear(secp256k1_fe *a)
Clear a field element to prevent leaking sensitive information.
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
static void secp256k1_fe_verify(const secp256k1_fe *a)
Check invariants on a field element (no-op unless VERIFY is enabled).
static const secp256k1_fe secp256k1_fe_one
Definition: field.h:68
#define secp256k1_fe_inv
Definition: field.h:98
#define SECP256K1_RESTRICT
Definition: util.h:194
#define secp256k1_fe_is_odd
Definition: field.h:85
#define secp256k1_fe_get_bounds
Definition: field.h:100
#define secp256k1_fe_set_int
Definition: field.h:83
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_to_storage
Definition: field.h:96
static const secp256k1_fe secp256k1_const_beta
Definition: field.h:69
#define secp256k1_fe_normalize
Definition: field.h:78
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_get_b32
Definition: field.h:89
#define secp256k1_fe_cmp_var
Definition: field.h:86
#define secp256k1_fe_from_storage
Definition: field.h:97