Bitcoin Core  27.99.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 uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
32 
33  VERIFY_CHECK(count > 0 && count <= 32);
34  if (offset < 32) {
35  return (*a >> offset) & (0xFFFFFFFF >> (32 - count));
36  } else {
37  return 0;
38  }
39 }
40 
41 SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
43 
44  return secp256k1_scalar_get_bits_limb32(a, offset, count);
45 }
46 
48 
52 
53  *r = (*a + *b) % EXHAUSTIVE_TEST_ORDER;
54 
56  return *r < *b;
57 }
58 
59 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
61 
62  if (flag && bit < 32)
63  *r += ((uint32_t)1 << bit);
64 
66  VERIFY_CHECK(bit < 32);
67  /* Verify that adding (1 << bit) will not overflow any in-range scalar *r by overflowing the underlying uint32_t. */
68  VERIFY_CHECK(((uint32_t)1 << bit) - 1 <= UINT32_MAX - EXHAUSTIVE_TEST_ORDER);
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 
144 
145  *r1 = *a;
146  *r2 = 0;
147 
150 }
151 
155 
156  return *a == *b;
157 }
158 
160  uint32_t mask0, mask1;
161  volatile int vflag = flag;
163  SECP256K1_CHECKMEM_CHECK_VERIFY(r, sizeof(*r));
164 
165  mask0 = vflag + ~((uint32_t)0);
166  mask1 = ~mask0;
167  *r = (*r & mask0) | (*a & mask1);
168 
170 }
171 
173  int i;
174  uint32_t res = 0;
176 
177  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
178  if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1) {
179  res = i;
180  break;
181  }
182  }
183 
184  /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
185  * have a composite group order; fix it in exhaustive_tests.c). */
186  VERIFY_CHECK(res != 0);
187  *r = res;
188 
190 }
191 
194 
196 
198 }
199 
202 
203  *r = (*a + ((-(uint32_t)(*a & 1)) & EXHAUSTIVE_TEST_ORDER)) >> 1;
204 
206 }
207 
208 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:99
#define SECP256K1_SCALAR_VERIFY(r)
Definition: scalar.h:103
static SECP256K1_INLINE int secp256k1_scalar_is_even(const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_check_overflow(const secp256k1_scalar *a)
static void secp256k1_scalar_half(secp256k1_scalar *r, const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_clear(secp256k1_scalar *r)
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow)
static SECP256K1_INLINE uint32_t 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 void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x)
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag)
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a)
static SECP256K1_INLINE uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
static SECP256K1_INLINE int secp256k1_scalar_is_one(const secp256k1_scalar *a)
#define SECP256K1_INLINE
Definition: util.h:48
#define VERIFY_CHECK(cond)
Definition: util.h:153
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count
#define EXHAUSTIVE_TEST_ORDER