Bitcoin Core  26.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 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  VERIFY_CHECK(bit < 32);
65  /* Verify that adding (1 << bit) will not overflow any in-range scalar *r by overflowing the underlying uint32_t. */
66  VERIFY_CHECK(((uint32_t)1 << bit) - 1 <= UINT32_MAX - EXHAUSTIVE_TEST_ORDER);
67 }
68 
69 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
70  int i;
71  int over = 0;
72  *r = 0;
73  for (i = 0; i < 32; i++) {
74  *r = (*r * 0x100) + b32[i];
75  if (*r >= EXHAUSTIVE_TEST_ORDER) {
76  over = 1;
78  }
79  }
80  if (overflow) *overflow = over;
81 
83 }
84 
85 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
87 
88  memset(bin, 0, 32);
89  bin[28] = *a >> 24; bin[29] = *a >> 16; bin[30] = *a >> 8; bin[31] = *a;
90 }
91 
94 
95  return *a == 0;
96 }
97 
100 
101  if (*a == 0) {
102  *r = 0;
103  } else {
104  *r = EXHAUSTIVE_TEST_ORDER - *a;
105  }
106 
108 }
109 
112 
113  return *a == 1;
114 }
115 
118 
119  return *a > EXHAUSTIVE_TEST_ORDER / 2;
120 }
121 
124 
125  if (flag) secp256k1_scalar_negate(r, r);
126 
128  return flag ? -1 : 1;
129 }
130 
134 
135  *r = (*a * *b) % EXHAUSTIVE_TEST_ORDER;
136 
138 }
139 
142 
143  *r1 = *a;
144  *r2 = 0;
145 
148 }
149 
153 
154  return *a == *b;
155 }
156 
158  uint32_t mask0, mask1;
159  volatile int vflag = flag;
161  SECP256K1_CHECKMEM_CHECK_VERIFY(r, sizeof(*r));
162 
163  mask0 = vflag + ~((uint32_t)0);
164  mask1 = ~mask0;
165  *r = (*r & mask0) | (*a & mask1);
166 
168 }
169 
171  int i;
172  *r = 0;
174 
175  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++)
176  if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
177  *r = i;
178 
180  /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
181  * have a composite group order; fix it in exhaustive_tests.c). */
182  VERIFY_CHECK(*r != 0);
183 }
184 
187 
189 
191 }
192 
195 
196  *r = (*a + ((-(uint32_t)(*a & 1)) & EXHAUSTIVE_TEST_ORDER)) >> 1;
197 
199 }
200 
201 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:92
#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 unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
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 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 SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a)
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:139
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count
#define EXHAUSTIVE_TEST_ORDER