Skip to content

Commit c76be9e

Browse files
committed
Remove unused num functions
1 parent 4285a98 commit c76be9e

File tree

3 files changed

+0
-221
lines changed

3 files changed

+0
-221
lines changed

src/num.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#error "Please select num implementation"
1818
#endif
1919

20-
/** Clear a number to prevent the leak of sensitive data. */
21-
static void secp256k1_num_clear(secp256k1_num_t *r);
22-
2320
/** Copy a number. */
2421
static void secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a);
2522

@@ -30,15 +27,9 @@ static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const sec
3027
/** Set a number to the value of a binary big-endian string. */
3128
static void secp256k1_num_set_bin(secp256k1_num_t *r, const unsigned char *a, unsigned int alen);
3229

33-
/** Set a number equal to a (signed) integer. */
34-
static void secp256k1_num_set_int(secp256k1_num_t *r, int a);
35-
3630
/** Compute a modular inverse. The input must be less than the modulus. */
3731
static void secp256k1_num_mod_inverse(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *m);
3832

39-
/** Multiply two numbers modulo another. */
40-
static void secp256k1_num_mod_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b, const secp256k1_num_t *m);
41-
4233
/** Compare the absolute value of two numbers. */
4334
static int secp256k1_num_cmp(const secp256k1_num_t *a, const secp256k1_num_t *b);
4435

@@ -61,40 +52,16 @@ static void secp256k1_num_div(secp256k1_num_t *r, const secp256k1_num_t *a, cons
6152
even if r was negative. */
6253
static void secp256k1_num_mod(secp256k1_num_t *r, const secp256k1_num_t *m);
6354

64-
/** Calculate the number of bits in (the absolute value of) a number. */
65-
static int secp256k1_num_bits(const secp256k1_num_t *a);
66-
6755
/** Right-shift the passed number by bits bits, and return those bits. */
6856
static int secp256k1_num_shift(secp256k1_num_t *r, int bits);
6957

7058
/** Check whether a number is zero. */
7159
static int secp256k1_num_is_zero(const secp256k1_num_t *a);
7260

73-
/** Check whether a number is odd. */
74-
static int secp256k1_num_is_odd(const secp256k1_num_t *a);
75-
7661
/** Check whether a number is strictly negative. */
7762
static int secp256k1_num_is_neg(const secp256k1_num_t *a);
7863

79-
/** Check whether a particular bit is set in a number. */
80-
static int secp256k1_num_get_bit(const secp256k1_num_t *a, int pos);
81-
82-
/** Increase a number by 1. */
83-
static void secp256k1_num_inc(secp256k1_num_t *r);
84-
85-
/** Set a number equal to the value of a hex string (unsigned). */
86-
static void secp256k1_num_set_hex(secp256k1_num_t *r, const char *a, int alen);
87-
88-
/** Convert (the absolute value of) a number to a hexadecimal string. */
89-
static void secp256k1_num_get_hex(char *r, int rlen, const secp256k1_num_t *a);
90-
91-
/** Split a number into a low and high part. */
92-
static void secp256k1_num_split(secp256k1_num_t *rl, secp256k1_num_t *rh, const secp256k1_num_t *a, int bits);
93-
9464
/** Change a number's sign. */
9565
static void secp256k1_num_negate(secp256k1_num_t *r);
9666

97-
/** Get a bunch of bits from a number. */
98-
static int secp256k1_num_get_bits(const secp256k1_num_t *a, int offset, int count);
99-
10067
#endif

src/num_gmp_impl.h

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,10 @@ static void secp256k1_num_sanity(const secp256k1_num_t *a) {
2222
#define secp256k1_num_sanity(a) do { } while(0)
2323
#endif
2424

25-
static void secp256k1_num_init(secp256k1_num_t *r) {
26-
r->neg = 0;
27-
r->limbs = 1;
28-
r->data[0] = 0;
29-
}
30-
31-
static void secp256k1_num_clear(secp256k1_num_t *r) {
32-
memset(r, 0, sizeof(*r));
33-
}
34-
35-
static void secp256k1_num_free(secp256k1_num_t *r) {
36-
(void)r;
37-
}
38-
3925
static void secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a) {
4026
*r = *a;
4127
}
4228

43-
static int secp256k1_num_bits(const secp256k1_num_t *a) {
44-
int ret=(a->limbs-1)*GMP_NUMB_BITS;
45-
mp_limb_t x=a->data[a->limbs-1];
46-
while (x) {
47-
x >>= 1;
48-
ret++;
49-
}
50-
return ret;
51-
}
52-
53-
5429
static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num_t *a) {
5530
unsigned char tmp[65];
5631
int len = 0;
@@ -81,12 +56,6 @@ static void secp256k1_num_set_bin(secp256k1_num_t *r, const unsigned char *a, un
8156
while (r->limbs > 1 && r->data[r->limbs-1]==0) r->limbs--;
8257
}
8358

84-
static void secp256k1_num_set_int(secp256k1_num_t *r, int a) {
85-
r->limbs = 1;
86-
r->neg = (a < 0);
87-
r->data[0] = (a < 0) ? -a : a;
88-
}
89-
9059
static void secp256k1_num_add_abs(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
9160
mp_limb_t c = mpn_add(r->data, a->data, a->limbs, b->data, b->limbs);
9261
r->limbs = a->limbs;
@@ -165,10 +134,6 @@ static int secp256k1_num_is_zero(const secp256k1_num_t *a) {
165134
return (a->limbs == 1 && a->data[0] == 0);
166135
}
167136

168-
static int secp256k1_num_is_odd(const secp256k1_num_t *a) {
169-
return a->data[0] & 1;
170-
}
171-
172137
static int secp256k1_num_is_neg(const secp256k1_num_t *a) {
173138
return (a->limbs > 1 || a->data[0] != 0) && a->neg;
174139
}
@@ -260,12 +225,6 @@ static void secp256k1_num_div(secp256k1_num_t *r, const secp256k1_num_t *a, cons
260225
r->neg = a->neg ^ b->neg;
261226
}
262227

263-
static void secp256k1_num_mod_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b, const secp256k1_num_t *m) {
264-
secp256k1_num_mul(r, a, b);
265-
secp256k1_num_mod(r, m);
266-
}
267-
268-
269228
static int secp256k1_num_shift(secp256k1_num_t *r, int bits) {
270229
VERIFY_CHECK(bits <= GMP_NUMB_BITS);
271230
mp_limb_t ret = mpn_rshift(r->data, r->data, r->limbs, bits);
@@ -274,107 +233,8 @@ static int secp256k1_num_shift(secp256k1_num_t *r, int bits) {
274233
return ret;
275234
}
276235

277-
static int secp256k1_num_get_bit(const secp256k1_num_t *a, int pos) {
278-
return (a->limbs*GMP_NUMB_BITS > pos) && ((a->data[pos/GMP_NUMB_BITS] >> (pos % GMP_NUMB_BITS)) & 1);
279-
}
280-
281-
static void secp256k1_num_inc(secp256k1_num_t *r) {
282-
mp_limb_t ret = mpn_add_1(r->data, r->data, r->limbs, (mp_limb_t)1);
283-
if (ret) {
284-
VERIFY_CHECK(r->limbs < 2*NUM_LIMBS);
285-
r->data[r->limbs++] = ret;
286-
}
287-
}
288-
289-
static void secp256k1_num_set_hex(secp256k1_num_t *r, const char *a, int alen) {
290-
static const unsigned char cvt[256] = {
291-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
292-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
293-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
294-
0, 1, 2, 3, 4, 5, 6,7,8,9,0,0,0,0,0,0,
295-
0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
296-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
297-
0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
298-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
299-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
300-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
301-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
302-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
303-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
304-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
305-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
306-
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0
307-
};
308-
unsigned char num[257] = {};
309-
for (int i=0; i<alen; i++) {
310-
num[i] = cvt[(unsigned char)a[i]];
311-
}
312-
r->limbs = mpn_set_str(r->data, num, alen, 16);
313-
r->neg = 0;
314-
while (r->limbs > 1 && r->data[r->limbs-1] == 0) r->limbs--;
315-
}
316-
317-
static void secp256k1_num_get_hex(char *r, int rlen, const secp256k1_num_t *a) {
318-
static const unsigned char cvt[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
319-
unsigned char *tmp = malloc(257);
320-
mp_size_t len = mpn_get_str(tmp, 16, (mp_limb_t*)a->data, a->limbs);
321-
VERIFY_CHECK(len <= rlen);
322-
for (int i=0; i<len; i++) {
323-
VERIFY_CHECK(rlen-len+i >= 0);
324-
VERIFY_CHECK(rlen-len+i < rlen);
325-
VERIFY_CHECK(tmp[i] < 16);
326-
r[rlen-len+i] = cvt[tmp[i]];
327-
}
328-
for (int i=0; i<rlen-len; i++) {
329-
VERIFY_CHECK(i >= 0);
330-
VERIFY_CHECK(i < rlen);
331-
r[i] = cvt[0];
332-
}
333-
free(tmp);
334-
}
335-
336-
static void secp256k1_num_split(secp256k1_num_t *rl, secp256k1_num_t *rh, const secp256k1_num_t *a, int bits) {
337-
VERIFY_CHECK(bits > 0);
338-
rh->neg = a->neg;
339-
if (bits >= a->limbs * GMP_NUMB_BITS) {
340-
*rl = *a;
341-
rh->limbs = 1;
342-
rh->data[0] = 0;
343-
return;
344-
}
345-
rl->limbs = 0;
346-
rl->neg = a->neg;
347-
int left = bits;
348-
while (left >= GMP_NUMB_BITS) {
349-
rl->data[rl->limbs] = a->data[rl->limbs];
350-
rl->limbs++;
351-
left -= GMP_NUMB_BITS;
352-
}
353-
if (left == 0) {
354-
mpn_copyi(rh->data, a->data + rl->limbs, a->limbs - rl->limbs);
355-
rh->limbs = a->limbs - rl->limbs;
356-
} else {
357-
mpn_rshift(rh->data, a->data + rl->limbs, a->limbs - rl->limbs, left);
358-
rh->limbs = a->limbs - rl->limbs;
359-
while (rh->limbs>1 && rh->data[rh->limbs-1]==0) rh->limbs--;
360-
}
361-
if (left > 0) {
362-
rl->data[rl->limbs] = a->data[rl->limbs] & ((((mp_limb_t)1) << left) - 1);
363-
rl->limbs++;
364-
}
365-
while (rl->limbs>1 && rl->data[rl->limbs-1]==0) rl->limbs--;
366-
}
367-
368236
static void secp256k1_num_negate(secp256k1_num_t *r) {
369237
r->neg ^= 1;
370238
}
371239

372-
static int secp256k1_num_get_bits(const secp256k1_num_t *a, int offset, int count) {
373-
int ret = 0;
374-
for (int i = 0; i < count; i++) {
375-
ret |= ((a->data[(offset + i) / GMP_NUMB_BITS] >> ((offset + i) % GMP_NUMB_BITS)) & 1) << i;
376-
}
377-
return ret;
378-
}
379-
380240
#endif

src/tests.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -125,39 +125,6 @@ void random_num_order(secp256k1_num_t *num) {
125125
} while(1);
126126
}
127127

128-
void test_num_copy_inc_cmp(void) {
129-
secp256k1_num_t n1,n2;
130-
random_num_order(&n1);
131-
secp256k1_num_copy(&n2, &n1);
132-
CHECK(secp256k1_num_eq(&n1, &n2));
133-
CHECK(secp256k1_num_eq(&n2, &n1));
134-
secp256k1_num_inc(&n2);
135-
CHECK(!secp256k1_num_eq(&n1, &n2));
136-
CHECK(!secp256k1_num_eq(&n2, &n1));
137-
}
138-
139-
140-
void test_num_get_set_hex(void) {
141-
secp256k1_num_t n1,n2;
142-
random_num_order_test(&n1);
143-
char c[64];
144-
secp256k1_num_get_hex(c, 64, &n1);
145-
secp256k1_num_set_hex(&n2, c, 64);
146-
CHECK(secp256k1_num_eq(&n1, &n2));
147-
for (int i=0; i<64; i++) {
148-
/* check whether the lower 4 bits correspond to the last hex character */
149-
int low1 = secp256k1_num_shift(&n1, 4);
150-
int lowh = c[63];
151-
int low2 = ((lowh>>6)*9+(lowh-'0'))&15;
152-
CHECK(low1 == low2);
153-
/* shift bits off the hex representation, and compare */
154-
memmove(c+1, c, 63);
155-
c[0] = '0';
156-
secp256k1_num_set_hex(&n2, c, 64);
157-
CHECK(secp256k1_num_eq(&n1, &n2));
158-
}
159-
}
160-
161128
void test_num_get_set_bin(void) {
162129
secp256k1_num_t n1,n2;
163130
random_num_order_test(&n1);
@@ -178,18 +145,6 @@ void test_num_get_set_bin(void) {
178145
}
179146
}
180147

181-
void run_num_int(void) {
182-
secp256k1_num_t n1;
183-
for (int i=-255; i<256; i++) {
184-
unsigned char c1[3] = {};
185-
c1[2] = abs(i);
186-
unsigned char c2[3] = {0x11,0x22,0x33};
187-
secp256k1_num_set_int(&n1, i);
188-
secp256k1_num_get_bin(c2, 3, &n1);
189-
CHECK(memcmp(c1, c2, 3) == 0);
190-
}
191-
}
192-
193148
void test_num_negate(void) {
194149
secp256k1_num_t n1;
195150
secp256k1_num_t n2;
@@ -241,13 +196,10 @@ void test_num_add_sub(void) {
241196

242197
void run_num_smalltests(void) {
243198
for (int i=0; i<100*count; i++) {
244-
test_num_copy_inc_cmp();
245-
test_num_get_set_hex();
246199
test_num_get_set_bin();
247200
test_num_negate();
248201
test_num_add_sub();
249202
}
250-
run_num_int();
251203
}
252204

253205
/***** SCALAR TESTS *****/

0 commit comments

Comments
 (0)