Bitcoin Core 31.99.0
P2P Digital Currency
testutil.h
Go to the documentation of this file.
1/***********************************************************************
2 * Distributed under the MIT software license, see the accompanying *
3 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4 ***********************************************************************/
5
6#ifndef SECP256K1_TESTUTIL_H
7#define SECP256K1_TESTUTIL_H
8
9#include "field.h"
10#include "group.h"
11#include "testrand.h"
12#include "util.h"
13
14/* Helper for when we need to check that the ctx-provided sha256 compression was called */
15#define DEFINE_SHA256_TRANSFORM_PROBE(name) \
16 static int name##_called = 0; \
17 static void name(uint32_t *s, const unsigned char *msg, size_t rounds) { \
18 name##_called = 1; \
19 secp256k1_sha256_transform(s, msg, rounds); \
20 s[0] ^= 0xdeadbeef; /* intentional perturbation for testing */ \
21 }
22
23/* group order of the secp256k1 curve in 32-byte big endian representation */
24static const unsigned char secp256k1_group_order_bytes[32] = {
25 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
27 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
28 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
29};
30
32 unsigned char bin[32];
33 do {
34 testrand256(bin);
35 if (secp256k1_fe_set_b32_limit(x, bin)) {
36 return;
37 }
38 } while(1);
39}
40
42 do {
44 } while (secp256k1_fe_is_zero(nz));
45}
46
48 secp256k1_fe zero;
49 int n = testrand_int(m + 1);
51 if (n == 0) {
52 return;
53 }
54 secp256k1_fe_set_int(&zero, 0);
55 secp256k1_fe_negate(&zero, &zero, 0);
57 secp256k1_fe_add(fe, &zero);
58#ifdef VERIFY
59 CHECK(fe->magnitude == n);
60#endif
61}
62
64 unsigned char bin[32];
65 do {
67 if (secp256k1_fe_set_b32_limit(x, bin)) {
68 return;
69 }
70 } while(1);
71}
72
74 do {
76 } while(secp256k1_fe_is_zero(fe));
77}
78
81}
82
85}
86
89}
90
93}
94
97}
98
100 secp256k1_fe fe;
101 do {
103 if (secp256k1_ge_set_xo_var(ge, &fe, testrand_bits(1))) {
105 break;
106 }
107 } while(1);
108}
109
111 secp256k1_fe z;
113 secp256k1_gej_set_ge(gej, ge);
114 secp256k1_gej_rescale(gej, &z);
115}
116
118 secp256k1_ge ge;
121}
122
124 secp256k1_ge ge;
127}
128
130 do {
131 unsigned char b32[32];
132 int overflow = 0;
133 testrand256_test(b32);
134 secp256k1_scalar_set_b32(num, b32, &overflow);
135 if (overflow || secp256k1_scalar_is_zero(num)) {
136 continue;
137 }
138 break;
139 } while(1);
140}
141
143 do {
144 unsigned char b32[32];
145 int overflow = 0;
146 testrand256(b32);
147 secp256k1_scalar_set_b32(num, b32, &overflow);
148 if (overflow || secp256k1_scalar_is_zero(num)) {
149 continue;
150 }
151 break;
152 } while(1);
153}
154
155static void testutil_random_scalar_order_b32(unsigned char *b32) {
158 secp256k1_scalar_get_b32(b32, &num);
159}
160
161#endif /* SECP256K1_TESTUTIL_H */
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:211
#define secp256k1_fe_add
Definition: field.h:92
#define secp256k1_fe_is_zero
Definition: field.h:84
#define secp256k1_fe_mul_int_unchecked
Definition: field.h:91
#define secp256k1_fe_set_b32_limit
Definition: field.h:88
#define secp256k1_fe_normalize
Definition: field.h:78
#define secp256k1_fe_set_int
Definition: field.h:83
#define SECP256K1_GE_X_MAGNITUDE_MAX
Maximum allowed magnitudes for group element coordinates in affine (x, y) and jacobian (x,...
Definition: group.h:49
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Set a group element (affine) equal to the point with the given X coordinate, and given oddness for Y.
#define SECP256K1_GEJ_Y_MAGNITUDE_MAX
Definition: group.h:52
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
Rescale a jacobian point by b which must be non-zero.
#define SECP256K1_GE_Y_MAGNITUDE_MAX
Definition: group.h:50
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
#define SECP256K1_GEJ_Z_MAGNITUDE_MAX
Definition: group.h:53
#define SECP256K1_GEJ_X_MAGNITUDE_MAX
Definition: group.h:51
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:265
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
Definition: field_10x26.h:14
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
secp256k1_fe y
Definition: group.h:30
secp256k1_fe x
Definition: group.h:29
secp256k1_fe z
Definition: group.h:31
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static void testrand256_test(unsigned char *b32)
Generate a pseudorandom 32-byte array with long sequences of zero and one bits.
static void testrand256(unsigned char *b32)
Generate a pseudorandom 32-byte array.
static SECP256K1_INLINE uint64_t testrand_bits(int bits)
Generate a pseudorandom number in the range [0..2**bits-1].
static uint32_t testrand_int(uint32_t range)
Generate a pseudorandom number in the range [0..range-1].
static void testutil_random_fe_test(secp256k1_fe *x)
Definition: testutil.h:63
static void testutil_random_pubkey_test(secp256k1_pubkey *pk)
Definition: testutil.h:123
static void testutil_random_gej_y_magnitude(secp256k1_gej *gej)
Definition: testutil.h:91
static const unsigned char secp256k1_group_order_bytes[32]
Definition: testutil.h:24
static void testutil_random_fe_non_zero(secp256k1_fe *nz)
Definition: testutil.h:41
static void testutil_random_scalar_order(secp256k1_scalar *num)
Definition: testutil.h:142
static void testutil_random_gej_test(secp256k1_gej *gej)
Definition: testutil.h:117
static void testutil_random_scalar_order_test(secp256k1_scalar *num)
Definition: testutil.h:129
static void testutil_random_scalar_order_b32(unsigned char *b32)
Definition: testutil.h:155
static void testutil_random_fe(secp256k1_fe *x)
Definition: testutil.h:31
static void testutil_random_fe_non_zero_test(secp256k1_fe *fe)
Definition: testutil.h:73
static void testutil_random_gej_x_magnitude(secp256k1_gej *gej)
Definition: testutil.h:87
static void testutil_random_fe_magnitude(secp256k1_fe *fe, int m)
Definition: testutil.h:47
static void testutil_random_ge_x_magnitude(secp256k1_ge *ge)
Definition: testutil.h:79
static void testutil_random_gej_z_magnitude(secp256k1_gej *gej)
Definition: testutil.h:95
static void testutil_random_ge_test(secp256k1_ge *ge)
Definition: testutil.h:99
static void testutil_random_ge_y_magnitude(secp256k1_ge *ge)
Definition: testutil.h:83
static void testutil_random_ge_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge)
Definition: testutil.h:110