Bitcoin Core  26.1.0
P2P Digital Currency
scalar_low_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2015 Andrew Poelstra *
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_SCALAR_REPR_IMPL_H
8 #define SECP256K1_SCALAR_REPR_IMPL_H
9 
10 #include "checkmem.h"
11 #include "scalar.h"
12 #include "util.h"
13 
14 #include <string.h>
15 
18 
19  return !(*a & 1);
20 }
21 
23 
25  *r = v % EXHAUSTIVE_TEST_ORDER;
26 
28 }
29 
30 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
32 
33  if (offset < 32)
34  return ((*a >> offset) & ((((uint32_t)1) << count) - 1));
35  else
36  return 0;
37 }
38 
39 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
41 
42  return secp256k1_scalar_get_bits(a, offset, count);
43 }
44 
46 
50 
51  *r = (*a + *b) % EXHAUSTIVE_TEST_ORDER;
52 
54  return *r < *b;
55 }
56 
57 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
59 
60  if (flag && bit < 32)
61  *r += ((uint32_t)1 << bit);
62 
64 #ifdef VERIFY
65  VERIFY_CHECK(bit < 32);
66  /* Verify that adding (1 << bit) will not overflow any in-range scalar *r by overflowing the underlying uint32_t. */
67  VERIFY_CHECK(((uint32_t)1 << bit) - 1 <= UINT32_MAX - EXHAUSTIVE_TEST_ORDER);
68 #endif
69 }
70 
71 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
72  int i;
73  int over = 0;
74  *r = 0;
75  for (i = 0; i < 32; i++) {
76  *r = (*r * 0x100) + b32[i];
77  if (*r >= EXHAUSTIVE_TEST_ORDER) {
78  over = 1;
80  }
81  }
82  if (overflow) *overflow = over;
83 
85 }
86 
87 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
89 
90  memset(bin, 0, 32);
91  bin[28] = *a >> 24; bin[29] = *a >> 16; bin[30] = *a >> 8; bin[31] = *a;
92 }
93 
96 
97  return *a == 0;
98 }
99 
102 
103  if (*a == 0) {
104  *r = 0;
105  } else {
106  *r = EXHAUSTIVE_TEST_ORDER - *a;
107  }
108 
110 }
111 
114 
115  return *a == 1;
116 }
117 
120 
121  return *a > EXHAUSTIVE_TEST_ORDER / 2;
122 }
123 
126 
127  if (flag) secp256k1_scalar_negate(r, r);
128 
130  return flag ? -1 : 1;
131 }
132 
136 
137  *r = (*a * *b) % EXHAUSTIVE_TEST_ORDER;
138 
140 }
141 
143  int ret;
145  VERIFY_CHECK(n > 0);
146  VERIFY_CHECK(n < 16);
147 
148  ret = *r & ((1 << n) - 1);
149  *r >>= n;
150 
152  return ret;
153 }
154 
157 
158  *r1 = *a;
159  *r2 = 0;
160 
163 }
164 
168 
169  return *a == *b;
170 }
171 
173  uint32_t mask0, mask1;
174  volatile int vflag = flag;
176  SECP256K1_CHECKMEM_CHECK_VERIFY(r, sizeof(*r));
177 
178  mask0 = vflag + ~((uint32_t)0);
179  mask1 = ~mask0;
180  *r = (*r & mask0) | (*a & mask1);
181 
183 }
184 
186  int i;
187  *r = 0;
189 
190  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++)
191  if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
192  *r = i;
193 
195  /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
196  * have a composite group order; fix it in exhaustive_tests.c). */
197  VERIFY_CHECK(*r != 0);
198 }
199 
202 
204 
206 }
207 
208 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define VERIFY_CHECK(cond)
Definition: util.h:143
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow)
int ret
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
static SECP256K1_INLINE void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
static SECP256K1_INLINE int secp256k1_scalar_is_even(const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
#define SECP256K1_INLINE
Definition: util.h:48
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_is_one(const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a)
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
#define EXHAUSTIVE_TEST_ORDER
static SECP256K1_INLINE void secp256k1_scalar_clear(secp256k1_scalar *r)
static SECP256K1_INLINE int secp256k1_scalar_check_overflow(const secp256k1_scalar *a)
static int count
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag)
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:92
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
static void secp256k1_scalar_verify(const secp256k1_scalar *r)
Check invariants on a scalar (no-op unless VERIFY is enabled).