Bitcoin Core 28.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 *r = v % EXHAUSTIVE_TEST_ORDER;
24
26}
27
28SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
30
31 VERIFY_CHECK(count > 0 && count <= 32);
32 if (offset < 32) {
33 return (*a >> offset) & (0xFFFFFFFF >> (32 - count));
34 } else {
35 return 0;
36 }
37}
38
39SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
41
42 return secp256k1_scalar_get_bits_limb32(a, offset, count);
43}
44
46
50
51 *r = (*a + *b) % EXHAUSTIVE_TEST_ORDER;
52
54 return *r < *b;
55}
56
57static 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
69static 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
85static 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 uint32_t res = 0;
174
175 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
176 if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1) {
177 res = i;
178 break;
179 }
180 }
181
182 /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
183 * have a composite group order; fix it in exhaustive_tests.c). */
184 VERIFY_CHECK(res != 0);
185 *r = res;
186
188}
189
192
194
196}
197
200
201 *r = (*a + ((-(uint32_t)(*a & 1)) & EXHAUSTIVE_TEST_ORDER)) >> 1;
202
204}
205
206#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 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:54
#define VERIFY_CHECK(cond)
Definition: util.h:159
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count
#define EXHAUSTIVE_TEST_ORDER