Bitcoin Core 30.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/* group order of the secp256k1 curve in 32-byte big endian representation */
15static const unsigned char secp256k1_group_order_bytes[32] = {
16 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
18 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
19 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
20};
21
23 unsigned char bin[32];
24 do {
25 testrand256(bin);
26 if (secp256k1_fe_set_b32_limit(x, bin)) {
27 return;
28 }
29 } while(1);
30}
31
33 do {
35 } while (secp256k1_fe_is_zero(nz));
36}
37
39 secp256k1_fe zero;
40 int n = testrand_int(m + 1);
42 if (n == 0) {
43 return;
44 }
45 secp256k1_fe_set_int(&zero, 0);
46 secp256k1_fe_negate(&zero, &zero, 0);
48 secp256k1_fe_add(fe, &zero);
49#ifdef VERIFY
50 CHECK(fe->magnitude == n);
51#endif
52}
53
55 unsigned char bin[32];
56 do {
58 if (secp256k1_fe_set_b32_limit(x, bin)) {
59 return;
60 }
61 } while(1);
62}
63
65 do {
67 } while(secp256k1_fe_is_zero(fe));
68}
69
72}
73
76}
77
80}
81
84}
85
88}
89
91 secp256k1_fe fe;
92 do {
94 if (secp256k1_ge_set_xo_var(ge, &fe, testrand_bits(1))) {
96 break;
97 }
98 } while(1);
99}
100
102 secp256k1_fe z;
104 secp256k1_gej_set_ge(gej, ge);
105 secp256k1_gej_rescale(gej, &z);
106}
107
109 secp256k1_ge ge;
112}
113
115 secp256k1_ge ge;
118}
119
121 do {
122 unsigned char b32[32];
123 int overflow = 0;
124 testrand256_test(b32);
125 secp256k1_scalar_set_b32(num, b32, &overflow);
126 if (overflow || secp256k1_scalar_is_zero(num)) {
127 continue;
128 }
129 break;
130 } while(1);
131}
132
134 do {
135 unsigned char b32[32];
136 int overflow = 0;
137 testrand256(b32);
138 secp256k1_scalar_set_b32(num, b32, &overflow);
139 if (overflow || secp256k1_scalar_is_zero(num)) {
140 continue;
141 }
142 break;
143 } while(1);
144}
145
146static void testutil_random_scalar_order_b32(unsigned char *b32) {
149 secp256k1_scalar_get_b32(b32, &num);
150}
151
152#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:246
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:61
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:54
static void testutil_random_pubkey_test(secp256k1_pubkey *pk)
Definition: testutil.h:114
static void testutil_random_gej_y_magnitude(secp256k1_gej *gej)
Definition: testutil.h:82
static const unsigned char secp256k1_group_order_bytes[32]
Definition: testutil.h:15
static void testutil_random_fe_non_zero(secp256k1_fe *nz)
Definition: testutil.h:32
static void testutil_random_scalar_order(secp256k1_scalar *num)
Definition: testutil.h:133
static void testutil_random_gej_test(secp256k1_gej *gej)
Definition: testutil.h:108
static void testutil_random_scalar_order_test(secp256k1_scalar *num)
Definition: testutil.h:120
static void testutil_random_scalar_order_b32(unsigned char *b32)
Definition: testutil.h:146
static void testutil_random_fe(secp256k1_fe *x)
Definition: testutil.h:22
static void testutil_random_fe_non_zero_test(secp256k1_fe *fe)
Definition: testutil.h:64
static void testutil_random_gej_x_magnitude(secp256k1_gej *gej)
Definition: testutil.h:78
static void testutil_random_fe_magnitude(secp256k1_fe *fe, int m)
Definition: testutil.h:38
static void testutil_random_ge_x_magnitude(secp256k1_ge *ge)
Definition: testutil.h:70
static void testutil_random_gej_z_magnitude(secp256k1_gej *gej)
Definition: testutil.h:86
static void testutil_random_ge_test(secp256k1_ge *ge)
Definition: testutil.h:90
static void testutil_random_ge_y_magnitude(secp256k1_ge *ge)
Definition: testutil.h:74
static void testutil_random_ge_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge)
Definition: testutil.h:101