-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptopals_gmp_private.c
75 lines (67 loc) · 2.39 KB
/
cryptopals_gmp_private.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "cryptopals_gmp_private.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
void byte_array_to_mpz_init(mpz_t out, const byte_array in) {
byte_array hex = byte_array_to_hex_byte_array(in);
mpz_init_set_str(out, (const char *)hex.bytes, 16);
free_byte_array(hex);
}
void byte_array_to_mpz(mpz_t out, const byte_array in) {
byte_array hex = byte_array_to_hex_byte_array(in);
mpz_set_str(out, (const char *)hex.bytes, 16);
free_byte_array(hex);
}
byte_array mpz_to_byte_array(const mpz_t in) {
// Normally need 2 more than mpz_sizeinbase, for possible negative sign
// and null byte. We're only dealing with positive integers, so probably
// could just use 1 more, but just playing it safe.
size_t size_needed = 2 + mpz_sizeinbase(in, 16);
byte_array hex = alloc_byte_array(size_needed);
size_t len = gmp_snprintf((char *)hex.bytes, size_needed, "%Zx", in);
if (len >= size_needed) {
fprintf(stderr, "%s: mpz_sizeinbase incorrectly determined size of mpz\n", __func__);
free_byte_array(hex);
return NO_BA;
}
byte_array out = hex_to_bytes((const char *)hex.bytes);
free_byte_array(hex);
return out;
}
byte_array mpz_to_hex(const mpz_t in) {
size_t size_needed = 2 + mpz_sizeinbase(in, 16);
byte_array hex = alloc_byte_array(size_needed);
size_t len = gmp_snprintf((char *)hex.bytes, size_needed, "%Zx", in);
if (len >= size_needed) {
fprintf(stderr, "%s: mpz_sizeinbase incorrectly determined size of mpz\n", __func__);
free_byte_array(hex);
return NO_BA;
}
hex.len = strlen(hex.bytes);
return hex;
}
void test_conversion_functions(const char * hex) {
printf("%-11s = %s\n", "input", hex);
mpz_t in;
mpz_init_set_str(in, hex, 16);
gmp_printf("%-11s = %Zx\n", "as mpz", in);
byte_array bytes = mpz_to_byte_array(in);
printf("%-11s = ", "as bytes");
print_byte_array(bytes);
mpz_t copy;
byte_array_to_mpz_init(copy, bytes);
gmp_printf("%-11s = %Zx\n", "back to mpz", copy);
assert(!mpz_cmp(in, copy));
printf("tests passed!\n");
mpz_clears(in, copy, (mpz_ptr)NULL);
free_byte_array(bytes);
}
void test_zero_conversion() {
mpz_t zero;
mpz_init(zero);
byte_array zero_mpz_as_ba = mpz_to_byte_array(zero);
assert(zero_mpz_as_ba.len == 1);
assert(zero_mpz_as_ba.bytes[0] == 0);
free_byte_array(zero_mpz_as_ba);
mpz_clear(zero);
}