Coverage Report

Created: 2025-11-16 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/relic/src/relic_util.c
Line
Count
Source
1
/*
2
 * RELIC is an Efficient LIbrary for Cryptography
3
 * Copyright (c) 2009 RELIC Authors
4
 *
5
 * This file is part of RELIC. RELIC is legal property of its developers,
6
 * whose names are not listed here. Please refer to the COPYRIGHT file
7
 * for contact information.
8
 *
9
 * RELIC is free software; you can redistribute it and/or modify it under the
10
 * terms of the version 2.1 (or later) of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; or version 2.0 of the Apache
12
 * License as published by the Apache Software Foundation. See the LICENSE files
13
 * for more details.
14
 *
15
 * RELIC is distributed in the hope that it will be useful, but WITHOUT ANY
16
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17
 * A PARTICULAR PURPOSE. See the LICENSE files for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public or the
20
 * Apache License along with RELIC. If not, see <https://www.gnu.org/licenses/>
21
 * or <https://www.apache.org/licenses/>.
22
 */
23
24
/**
25
 * @file
26
 *
27
 * Implementation of useful configuration routines.
28
 *
29
 * @ingroup relic
30
 */
31
32
#include <stdio.h>
33
#include <stdarg.h>
34
#include <ctype.h>
35
#include <inttypes.h>
36
37
#include "relic_core.h"
38
#include "relic_conf.h"
39
#include "relic_util.h"
40
#include "relic_types.h"
41
42
#if ARCH == ARM && OPSYS == DROID
43
#include <android/log.h>
44
#endif
45
46
/*============================================================================*/
47
/* Private definitions                                                        */
48
/*============================================================================*/
49
50
/**
51
 * Buffer to hold printed messages.
52
 */
53
#if ARCH == AVR
54
55
#ifndef QUIET
56
volatile char print_buf[128 + 1];
57
volatile char *util_print_ptr;
58
59
#if OPSYS == DUINO
60
/**
61
 * Send byte to serial port.
62
 */
63
void uart_putchar(char c, FILE *stream) {
64
  if (c == '\n') {
65
    uart_putchar('\r', stream);
66
  }
67
  loop_until_bit_is_set(UCSR0A, UDRE0);
68
  UDR0 = c;
69
}
70
71
/**
72
 * Stream for serial port.
73
 */
74
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
75
76
#endif
77
#endif
78
79
#endif /* QUIET */
80
81
/*============================================================================*/
82
/* Public definitions                                                         */
83
/*============================================================================*/
84
85
2.63k
uint32_t util_conv_endian(uint32_t i) {
86
2.63k
  uint32_t i1, i2, i3, i4;
87
2.63k
  i1 = i & 0xFF;
88
2.63k
  i2 = (i >> 8) & 0xFF;
89
2.63k
  i3 = (i >> 16) & 0xFF;
90
2.63k
  i4 = (i >> 24) & 0xFF;
91
92
2.63k
  return ((uint32_t) i1 << 24) | ((uint32_t) i2 << 16) | ((uint32_t) i3 << 8)
93
2.63k
      | i4;
94
2.63k
}
95
96
2.63k
uint32_t util_conv_big(uint32_t i) {
97
#ifdef BIGED
98
  return i;
99
#else
100
2.63k
  return util_conv_endian(i);
101
2.63k
#endif
102
2.63k
}
103
104
0
uint32_t util_conv_little(uint32_t i) {
105
0
#ifndef BIGED
106
0
  return util_conv_endian(i);
107
#else
108
  return i;
109
#endif
110
0
}
111
112
38.3M
char util_conv_char(dig_t i) {
113
#if WSIZE == 8 || WSIZE == 16
114
  /* Avoid tables to save up some memory. This is not performance-critical. */
115
  if (i < 10) {
116
    return i + '0';
117
  }
118
  if (i < 36) {
119
    return (i - 10) + 'A';
120
  }
121
  if (i < 62) {
122
    return (i - 36) + 'a';
123
  }
124
  if (i == 62) {
125
    return '+';
126
  } else {
127
    return '/';
128
  }
129
#else
130
  /* Use a table. */
131
38.3M
  static const char conv_table[] =
132
38.3M
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
133
38.3M
  return conv_table[i];
134
38.3M
#endif
135
38.3M
}
136
137
50.1M
size_t util_bits_dig(dig_t a) {
138
50.1M
    return RLC_DIG - arch_lzcnt(a);
139
50.1M
}
140
141
0
int util_cmp_sec(const void *a, const void *b, size_t size) {
142
0
  const uint8_t *_a = (const uint8_t *)a;
143
0
  const uint8_t *_b = (const uint8_t *)b;
144
0
  uint8_t result = 0;
145
0
  int i;
146
147
0
  for (i = 0; i < size; i++) {
148
0
    result |= _a[i] ^ _b[i];
149
0
  }
150
151
0
  return (result == 0 ? RLC_EQ : RLC_NE);
152
0
}
153
154
0
void util_perm(uint_t p[], size_t n) {
155
0
  size_t i, j, k;
156
157
0
  for (i = 0; i < n; i++) {
158
0
    rand_bytes((uint8_t *)&k, sizeof(size_t));
159
0
    j = k % (i+1);
160
0
    p[i] = p[j];
161
0
    p[j] = i;
162
0
  }
163
0
}
164
165
#ifndef QUIET
166
void util_print(const char *format, ...) {
167
#if ARCH == AVR && !defined(OPSYS)
168
  util_print_ptr = print_buf + 1;
169
  va_list list;
170
  va_start(list, format);
171
  vsnprintf_P((char *)util_print_ptr, sizeof(print_buf) - 1, format, list);
172
  va_end(list);
173
  print_buf[0] = (uint8_t)2;
174
#elif ARCH == AVR && OPSYS == DUINO
175
  stdout = &uart_output;
176
  va_list list;
177
  va_start(list, format);
178
  vsnprintf_P((char *)print_buf, sizeof(print_buf), format, list);
179
  printf("%s", (char *)print_buf);
180
  va_end(list);
181
#elif ARCH == MSP && !defined(OPSYS)
182
  va_list list;
183
  va_start(list, format);
184
  vprintf(format, list);
185
  va_end(list);
186
#elif ARCH == ARM && OPSYS == DROID
187
  va_list list;
188
  va_start(list, format);
189
  __android_log_vprint(ANDROID_LOG_INFO, "relic-toolkit", format, list);
190
  va_end(list);
191
#else
192
  va_list list;
193
  va_start(list, format);
194
  vprintf(format, list);
195
  fflush(stdout);
196
  va_end(list);
197
#endif
198
}
199
#endif
200
201
0
void util_print_dig(dig_t a, int pad) {
202
0
#if RLC_DIG == 64
203
0
  if (pad) {
204
0
    util_print("%.16" PRIX64, (uint64_t) a);
205
0
  } else {
206
0
    util_print("%" PRIX64, (uint64_t) a);
207
0
  }
208
#elif RLC_DIG == 32
209
  if (pad) {
210
    util_print("%.8" PRIX32, (uint32_t) a);
211
  } else {
212
    util_print("%" PRIX32, (uint32_t) a);
213
  }
214
#elif RLC_DIG == 16
215
  if (pad) {
216
    util_print("%.4" PRIX16, (uint16_t) a);
217
  } else {
218
    util_print("%" PRIX16, (uint16_t) a);
219
  }
220
#else
221
  if (pad) {
222
    util_print("%.2" PRIX8, (uint8_t)a);
223
  } else {
224
    util_print("%" PRIX8, (uint8_t)a);
225
  }
226
#endif
227
0
}