Bitcoin Core 28.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
15 unsigned char bin[32];
16 do {
17 testrand256(bin);
18 if (secp256k1_fe_set_b32_limit(x, bin)) {
19 return;
20 }
21 } while(1);
22}
23
25 do {
27 } while (secp256k1_fe_is_zero(nz));
28}
29
31 secp256k1_fe zero;
32 int n = testrand_int(m + 1);
34 if (n == 0) {
35 return;
36 }
37 secp256k1_fe_set_int(&zero, 0);
38 secp256k1_fe_negate(&zero, &zero, 0);
40 secp256k1_fe_add(fe, &zero);
41#ifdef VERIFY
42 CHECK(fe->magnitude == n);
43#endif
44}
45
47 unsigned char bin[32];
48 do {
50 if (secp256k1_fe_set_b32_limit(x, bin)) {
51 return;
52 }
53 } while(1);
54}
55
57 do {
59 } while(secp256k1_fe_is_zero(fe));
60}
61
64}
65
68}
69
72}
73
76}
77
80}
81
83 secp256k1_fe fe;
84 do {
86 if (secp256k1_ge_set_xo_var(ge, &fe, testrand_bits(1))) {
88 break;
89 }
90 } while(1);
91 ge->infinity = 0;
92}
93
95 secp256k1_fe z2, z3;
97 secp256k1_fe_sqr(&z2, &gej->z);
98 secp256k1_fe_mul(&z3, &z2, &gej->z);
99 secp256k1_fe_mul(&gej->x, &ge->x, &z2);
100 secp256k1_fe_mul(&gej->y, &ge->y, &z3);
101 gej->infinity = ge->infinity;
102}
103
105 secp256k1_ge ge;
108}
109
111 secp256k1_ge ge;
114}
115
117 do {
118 unsigned char b32[32];
119 int overflow = 0;
120 testrand256_test(b32);
121 secp256k1_scalar_set_b32(num, b32, &overflow);
122 if (overflow || secp256k1_scalar_is_zero(num)) {
123 continue;
124 }
125 break;
126 } while(1);
127}
128
130 do {
131 unsigned char b32[32];
132 int overflow = 0;
133 testrand256(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
142static void testutil_random_scalar_order_b32(unsigned char *b32) {
145 secp256k1_scalar_get_b32(b32, &num);
146}
147
148#endif /* SECP256K1_TESTUTIL_H */
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:211
#define secp256k1_fe_mul
Definition: field.h:93
#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_sqr
Definition: field.h:94
#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
#define SECP256K1_GE_Y_MAGNITUDE_MAX
Definition: group.h:50
#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
int infinity
Definition: group.h:19
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
int infinity
Definition: group.h:32
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:46
static void testutil_random_pubkey_test(secp256k1_pubkey *pk)
Definition: testutil.h:110
static void testutil_random_gej_y_magnitude(secp256k1_gej *gej)
Definition: testutil.h:74
static void testutil_random_fe_non_zero(secp256k1_fe *nz)
Definition: testutil.h:24
static void testutil_random_scalar_order(secp256k1_scalar *num)
Definition: testutil.h:129
static void testutil_random_gej_test(secp256k1_gej *gej)
Definition: testutil.h:104
static void testutil_random_scalar_order_test(secp256k1_scalar *num)
Definition: testutil.h:116
static void testutil_random_scalar_order_b32(unsigned char *b32)
Definition: testutil.h:142
static void testutil_random_fe(secp256k1_fe *x)
Definition: testutil.h:14
static void testutil_random_fe_non_zero_test(secp256k1_fe *fe)
Definition: testutil.h:56
static void testutil_random_gej_x_magnitude(secp256k1_gej *gej)
Definition: testutil.h:70
static void testutil_random_fe_magnitude(secp256k1_fe *fe, int m)
Definition: testutil.h:30
static void testutil_random_ge_x_magnitude(secp256k1_ge *ge)
Definition: testutil.h:62
static void testutil_random_gej_z_magnitude(secp256k1_gej *gej)
Definition: testutil.h:78
static void testutil_random_ge_test(secp256k1_ge *ge)
Definition: testutil.h:82
static void testutil_random_ge_y_magnitude(secp256k1_ge *ge)
Definition: testutil.h:66
static void testutil_random_ge_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge)
Definition: testutil.h:94