Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
test_utils.h
Go to the documentation of this file.
1#ifndef SSEC_TEST_UTILS_H
2#define SSEC_TEST_UTILS_H
3
4#include "munit.h"
5#include <fp2.h>
6#include <utilities.h>
7#include <stdio.h>
8
9static inline __attribute__((always_inline)) void escape(void *p) { __asm__ __volatile__("" : "+m,r"(p) : : "memory"); }
10
11#define TEST_END \
12 { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
13#define SUITE_END \
14 { NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE }
15#define PARAM_END \
16 { NULL, NULL }
17
18
19#define TEST_CASE(test_func) \
20 { #test_func, test_func, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
21
22#define TEST_CASE_SETUP(test_func, setup, teardown) \
23 { #test_func, test_func, setup, teardown, MUNIT_TEST_OPTION_NONE, NULL }
24
25#define BENCHMARK(test_func, setup, teardown, params) \
26 { #test_func, test_func, setup, teardown, MUNIT_TEST_OPTION_SINGLE_ITERATION, params }
27
28#define SUITE(suite_name, test_cases) \
29 { suite_name, test_cases, NULL, 0, MUNIT_SUITE_OPTION_NONE }
30
31#define SUITE_IT(suite_name, test_cases, iterations) \
32 { suite_name, test_cases, NULL, iterations, MUNIT_SUITE_OPTION_NONE }
33
34#define SUITE_OF_SUITE(suite_name, nested_suite) \
35 { suite_name, NULL, nested_suite, 0, MUNIT_SUITE_OPTION_NONE }
36
37
38#define RANDOM_FP_ELEMENT(out) \
39{ \
40 munit_rand_memory(FIELD_BYTES, (munit_uint8_t *) (out)); \
41 (out)[FIELD_64BITS_WORDS - 1] &= MASK_FIELD_ELEMENT; \
42 while (!multiprecision_is_smaller((out), FIELD_CHARACTERISTIC, FIELD_64BITS_WORDS)) { \
43 munit_rand_memory(FIELD_BYTES, (munit_uint8_t *) (out)); \
44 (out)[FIELD_64BITS_WORDS - 1] &= MASK_FIELD_ELEMENT; \
45 } \
46}
47
48#define RANDOM_SEED(out) \
49{ \
50 munit_rand_memory(SECURITY_BITS / 8, (munit_uint8_t *) (out)); \
51}
52
53static void RANDOM_FP2_ELEMENT(fp2_t *output) {
54 RANDOM_FP_ELEMENT(output->re);
55 RANDOM_FP_ELEMENT(output->im);
56}
57
58#define RANDOM_BIT_STRING(out) \
59{ \
60 munit_rand_memory(BIT_LENGTH_PATH / 8, (munit_uint8_t *) (out)); \
61}
62
63// Next function should be implemented with is_smaller (in ct)
64#define RANDOM_TRIT_STRING(out) \
65{ \
66 uint8_t bound = 0xF3; \
67 for(int i = 0; i < (TRITLENGTH_PATH / 5); i++) { \
68 uint8_t trit_str; \
69 munit_rand_memory(1, (munit_uint8_t *) (&trit_str)); \
70 while (issmaller(bound, trit_str)) { \
71 munit_rand_memory(1, (munit_uint8_t *) (&trit_str)); \
72 } \
73 out[i] = trit_str; \
74 } \
75}
76
77// KAT API
78#define MAX_MARKER_LEN 50
79
80static inline int FindMarker(FILE *infile, const char *marker) {
81 char line[MAX_MARKER_LEN] = {0};
82 int i, len;
83 int curr_line;
84
85 len = (int) strlen(marker);
86 if (len > MAX_MARKER_LEN - 1) {
87 len = MAX_MARKER_LEN - 1;
88 }
89
90 for (i = 0; i < len; i++) {
91 curr_line = fgetc(infile);
92 line[i] = curr_line;
93 if (curr_line == EOF) {
94 return 0;
95 }
96 }
97 line[len] = '\0';
98
99 while (1) {
100 if (!strncmp(line, marker, len)) {
101 return 1;
102 }
103
104 for (i = 0; i < len - 1; i++) {
105 line[i] = line[i + 1];
106 }
107 curr_line = fgetc(infile);
108 line[len - 1] = curr_line;
109 if (curr_line == EOF) {
110 return 0;
111 }
112 line[len] = '\0';
113 }
114}
115
116static inline int convert_to_hex(int ch) {
117 if (ch >= 48 && ch <= 57) {
118 return (ch - 48);
119 } else if (ch >= 64 && ch <= 70) {
120 return (ch - 55);
121 }
122 return 0;
123}
124
125static inline int ReadHex(FILE *infile, unsigned char *A, int Length, char *str) {
126 int count, ch1, ch2;
127
128 if (Length == 0) {
129 A[0] = 0x00;
130 return 1;
131 }
132 memset(A, 0x00, Length);
133 count = 0;
134 if (FindMarker(infile, str)) {
135 while (count < Length) {
136 ch1 = fgetc(infile);
137 ch2 = fgetc(infile);
138 ch1 = convert_to_hex(ch1);
139 ch2 = convert_to_hex(ch2);
140 A[count] = ((unsigned char) (ch1 << 4) | (unsigned char) ch2);
141 count++;
142 }
143 } else {
144 return 0;
145 }
146 return 1;
147}
148
149static inline void file_print_bytes(FILE *fp, const char *txt, const uint8_t *data_bytes, uint32_t data_bytes_length) {
150 fprintf(fp, "%s", txt);
151
152 for (uint32_t i = 0; i < data_bytes_length; i++) {
153 fprintf(fp, "%02X", data_bytes[i]);
154 }
155 fprintf(fp, "\n");
156}
157
158#endif // SSEC_TEST_UTILS_H
#define p
Definition fp-gmp.h:44
uint64_t fp[NUMBER_OF_WORDS]
Definition fp-gmp.h:22
A
Definition tests.py:29
for i
Definition fp2.h:10
#define RANDOM_FP_ELEMENT(out)
Definition test_utils.h:38
#define MAX_MARKER_LEN
Definition test_utils.h:78