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 ge->infinity = 0;
100}
101
103 secp256k1_fe z2, z3;
105 secp256k1_fe_sqr(&z2, &gej->z);
106 secp256k1_fe_mul(&z3, &z2, &gej->z);
107 secp256k1_fe_mul(&gej->x, &ge->x, &z2);
108 secp256k1_fe_mul(&gej->y, &ge->y, &z3);
109 gej->infinity = ge->infinity;
110}
111
113 secp256k1_ge ge;
116}
117
119 secp256k1_ge ge;
122}
123
125 do {
126 unsigned char b32[32];
127 int overflow = 0;
128 testrand256_test(b32);
129 secp256k1_scalar_set_b32(num, b32, &overflow);
130 if (overflow || secp256k1_scalar_is_zero(num)) {
131 continue;
132 }
133 break;
134 } while(1);
135}
136
138 do {
139 unsigned char b32[32];
140 int overflow = 0;
141 testrand256(b32);
142 secp256k1_scalar_set_b32(num, b32, &overflow);
143 if (overflow || secp256k1_scalar_is_zero(num)) {
144 continue;
145 }
146 break;
147 } while(1);
148}
149
150static void testutil_random_scalar_order_b32(unsigned char *b32) {
153 secp256k1_scalar_get_b32(b32, &num);
154}
155
156#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:54
static void testutil_random_pubkey_test(secp256k1_pubkey *pk)
Definition: testutil.h:118
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:137
static void testutil_random_gej_test(secp256k1_gej *gej)
Definition: testutil.h:112
static void testutil_random_scalar_order_test(secp256k1_scalar *num)
Definition: testutil.h:124
static void testutil_random_scalar_order_b32(unsigned char *b32)
Definition: testutil.h:150
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:102