-
Notifications
You must be signed in to change notification settings - Fork 26
/
io.c
46 lines (38 loc) · 1.32 KB
/
io.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
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "io.h"
#include <string.h>
#include "compat.h"
void mzd_to_char_array(uint8_t* dst, const mzd_local_t* data, size_t len) {
const size_t word_count = (len + 7) / sizeof(uint64_t);
const block_t* block = CONST_BLOCK(data, 0);
for (size_t i = word_count; i; --i, dst += sizeof(uint64_t), len -= sizeof(uint64_t)) {
const uint64_t tmp = htobe64(block->w64[i - 1]);
memcpy(dst, &tmp, MIN(sizeof(tmp), len));
}
}
void mzd_from_char_array(mzd_local_t* result, const uint8_t* data, size_t len) {
const size_t word_count = (len + 7) / sizeof(uint64_t);
block_t* block = BLOCK(result, 0);
for (size_t i = word_count; i; --i, data += sizeof(uint64_t), len -= sizeof(uint64_t)) {
uint64_t tmp = 0;
memcpy(&tmp, data, MIN(sizeof(tmp), len));
block->w64[i - 1] = be64toh(tmp);
}
}
#if defined(PICNIC_STATIC) || !defined(NDEBUG)
void print_hex(FILE* out, const uint8_t* data, size_t len) {
for (size_t i = len; i; --i, ++data) {
fprintf(out, "%02X", *data);
}
}
#endif