Bitcoin ABC 0.33.3
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
17 return !(*a & 1);
18}
19
21SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v) { *r = v; }
22
23SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
24 if (offset < 32)
25 return ((*a >> offset) & ((((uint32_t)1) << count) - 1));
26 else
27 return 0;
28}
29
30SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
31 return secp256k1_scalar_get_bits(a, offset, count);
32}
33
35
37 *r = (*a + *b) % EXHAUSTIVE_TEST_ORDER;
38 return *r < *b;
39}
40
41static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
42 if (flag && bit < 32)
43 *r += ((uint32_t)1 << bit);
44#ifdef VERIFY
45 VERIFY_CHECK(bit < 32);
46 /* Verify that adding (1 << bit) will not overflow any in-range scalar *r by overflowing the underlying uint32_t. */
47 VERIFY_CHECK(((uint32_t)1 << bit) - 1 <= UINT32_MAX - EXHAUSTIVE_TEST_ORDER);
49#endif
50}
51
52static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
53 int i;
54 int over = 0;
55 *r = 0;
56 for (i = 0; i < 32; i++) {
57 *r = (*r * 0x100) + b32[i];
58 if (*r >= EXHAUSTIVE_TEST_ORDER) {
59 over = 1;
61 }
62 }
63 if (overflow) *overflow = over;
64}
65
66static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
67 memset(bin, 0, 32);
68 bin[28] = *a >> 24; bin[29] = *a >> 16; bin[30] = *a >> 8; bin[31] = *a;
69}
70
72 return *a == 0;
73}
74
76 if (*a == 0) {
77 *r = 0;
78 } else {
79 *r = EXHAUSTIVE_TEST_ORDER - *a;
80 }
81}
82
84 return *a == 1;
85}
86
88 return *a > EXHAUSTIVE_TEST_ORDER / 2;
89}
90
92 if (flag) secp256k1_scalar_negate(r, r);
93 return flag ? -1 : 1;
94}
95
97 *r = (*a * *b) % EXHAUSTIVE_TEST_ORDER;
98}
99
101 int ret;
102 VERIFY_CHECK(n > 0);
103 VERIFY_CHECK(n < 16);
104 ret = *r & ((1 << n) - 1);
105 *r >>= n;
106 return ret;
107}
108
110 *r1 = *a;
111 *r2 = 0;
112}
113
115 return *a == *b;
116}
117
119 uint32_t mask0, mask1;
120 volatile int vflag = flag;
121 SECP256K1_CHECKMEM_CHECK_VERIFY(r, sizeof(*r));
122 mask0 = vflag + ~((uint32_t)0);
123 mask1 = ~mask0;
124 *r = (*r & mask0) | (*a & mask1);
125}
126
128 int i;
129 *r = 0;
130 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++)
131 if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
132 *r = i;
133 /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
134 * have a composite group order; fix it in exhaustive_tests.c). */
135 VERIFY_CHECK(*r != 0);
136}
137
140}
141
142#endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:85
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 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)
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
#define SECP256K1_INLINE
Definition: util.h:48
#define VERIFY_CHECK(cond)
Definition: util.h:130
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count
#define EXHAUSTIVE_TEST_ORDER