refactor(crypto): Apply formatting to Monero code.

pull/2386/head
Andrew Kozlik 2 years ago committed by Andrew Kozlik
parent e0358c7f24
commit d19af9896b

@ -2,75 +2,93 @@
// //
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, are // Redistribution and use in source and binary forms, with or without
// permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, this list of // 1. Redistributions of source code must retain the above copyright notice,
// conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, this list // 2. Redistributions in binary form must reproduce the above copyright notice,
// of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation
// materials provided with the distribution. // and/or other materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be // 3. Neither the name of the copyright holder nor the names of its contributors
// used to endorse or promote products derived from this software without specific // may be used to endorse or promote products derived from this software
// prior written permission. // without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// //
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote
// developers
#include "base58.h"
#include <assert.h> #include <assert.h>
#include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "base58.h"
#include "int-util.h"
#include "sha2.h"
#include "../base58.h" #include "../base58.h"
#include "../byte_order.h" #include "../byte_order.h"
#include "int-util.h"
#include "sha2.h"
const size_t alphabet_size = 58; // sizeof(b58digits_ordered) - 1; const size_t alphabet_size = 58; // sizeof(b58digits_ordered) - 1;
const size_t encoded_block_sizes[] = {0, 2, 3, 5, 6, 7, 9, 10, 11}; const size_t full_encoded_block_size = 11;
const size_t full_block_size = sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1; const size_t encoded_block_sizes[] = {
const size_t full_encoded_block_size = 11; // encoded_block_sizes[full_block_size]; 0, 2, 3, 5, 6, 7, 9, 10, full_encoded_block_size};
const size_t full_block_size =
sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1;
const size_t addr_checksum_size = 4; const size_t addr_checksum_size = 4;
const size_t max_bin_data_size = 64; const size_t max_bin_data_size = 64;
const int decoded_block_sizes[] = {0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8}; const int decoded_block_sizes[] = {0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8};
#define reverse_alphabet(letter) ((int8_t) b58digits_map[(int)letter]) #define reverse_alphabet(letter) ((int8_t)b58digits_map[(int)letter])
uint64_t uint_8be_to_64(const uint8_t *data, size_t size) {
uint64_t uint_8be_to_64(const uint8_t* data, size_t size)
{
assert(1 <= size && size <= sizeof(uint64_t)); assert(1 <= size && size <= sizeof(uint64_t));
uint64_t res = 0; uint64_t res = 0;
switch (9 - size) switch (9 - size) {
{ case 1:
case 1: res |= *data++; /* FALLTHRU */ res |= *data++; /* FALLTHRU */
case 2: res <<= 8; res |= *data++; /* FALLTHRU */ case 2:
case 3: res <<= 8; res |= *data++; /* FALLTHRU */ res <<= 8;
case 4: res <<= 8; res |= *data++; /* FALLTHRU */ res |= *data++; /* FALLTHRU */
case 5: res <<= 8; res |= *data++; /* FALLTHRU */ case 3:
case 6: res <<= 8; res |= *data++; /* FALLTHRU */ res <<= 8;
case 7: res <<= 8; res |= *data++; /* FALLTHRU */ res |= *data++; /* FALLTHRU */
case 8: res <<= 8; res |= *data; break; case 4:
default: assert(false); res <<= 8;
res |= *data++; /* FALLTHRU */
case 5:
res <<= 8;
res |= *data++; /* FALLTHRU */
case 6:
res <<= 8;
res |= *data++; /* FALLTHRU */
case 7:
res <<= 8;
res |= *data++; /* FALLTHRU */
case 8:
res <<= 8;
res |= *data;
break;
default:
assert(false);
} }
return res; return res;
} }
void uint_64_to_8be(uint64_t num, size_t size, uint8_t* data) void uint_64_to_8be(uint64_t num, size_t size, uint8_t *data) {
{
assert(1 <= size && size <= sizeof(uint64_t)); assert(1 <= size && size <= sizeof(uint64_t));
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
@ -78,17 +96,15 @@ void uint_64_to_8be(uint64_t num, size_t size, uint8_t* data)
#else #else
uint64_t num_be = num; uint64_t num_be = num;
#endif #endif
memcpy(data, (uint8_t*)(&num_be) + sizeof(uint64_t) - size, size); memcpy(data, (uint8_t *)(&num_be) + sizeof(uint64_t) - size, size);
} }
void encode_block(const char* block, size_t size, char* res) void encode_block(const char *block, size_t size, char *res) {
{
assert(1 <= size && size <= full_block_size); assert(1 <= size && size <= full_block_size);
uint64_t num = uint_8be_to_64((uint8_t*)(block), size); uint64_t num = uint_8be_to_64((uint8_t *)(block), size);
int i = ((int)(encoded_block_sizes[size])) - 1; int i = ((int)(encoded_block_sizes[size])) - 1;
while (0 <= i) while (0 <= i) {
{
uint64_t remainder = num % alphabet_size; uint64_t remainder = num % alphabet_size;
num /= alphabet_size; num /= alphabet_size;
res[i] = b58digits_ordered[remainder]; res[i] = b58digits_ordered[remainder];
@ -96,28 +112,30 @@ void encode_block(const char* block, size_t size, char* res)
} }
} }
bool decode_block(const char* block, size_t size, char* res) bool decode_block(const char *block, size_t size, char *res) {
{
assert(1 <= size && size <= full_encoded_block_size); assert(1 <= size && size <= full_encoded_block_size);
int res_size = decoded_block_sizes[size]; int res_size = decoded_block_sizes[size];
if (res_size <= 0) if (res_size <= 0) {
return false; // Invalid block size return false; // Invalid block size
}
uint64_t res_num = 0; uint64_t res_num = 0;
uint64_t order = 1; uint64_t order = 1;
for (size_t i = size - 1; i < size; --i) for (size_t i = size - 1; i < size; --i) {
{ if (block[i] & 0x80) {
if (block[i] & 0x80)
return false; // Invalid symbol return false; // Invalid symbol
}
int digit = reverse_alphabet(block[i]); int digit = reverse_alphabet(block[i]);
if (digit < 0) if (digit < 0) {
return false; // Invalid symbol return false; // Invalid symbol
}
uint64_t product_hi = 0; uint64_t product_hi = 0;
uint64_t tmp = res_num + mul128(order, (uint64_t) digit, &product_hi); uint64_t tmp = res_num + mul128(order, (uint64_t)digit, &product_hi);
if (tmp < res_num || 0 != product_hi) if (tmp < res_num || 0 != product_hi) {
return false; // Overflow return false; // Overflow
}
res_num = tmp; res_num = tmp;
// The original code comment for the order multiplication says // The original code comment for the order multiplication says
@ -127,51 +145,52 @@ bool decode_block(const char* block, size_t size, char* res)
order *= alphabet_size; order *= alphabet_size;
} }
if ((size_t)res_size < full_block_size && (UINT64_C(1) << (8 * res_size)) <= res_num) if ((size_t)res_size < full_block_size &&
(UINT64_C(1) << (8 * res_size)) <= res_num)
return false; // Overflow return false; // Overflow
uint_64_to_8be(res_num, res_size, (uint8_t*)(res)); uint_64_to_8be(res_num, res_size, (uint8_t *)(res));
return true; return true;
} }
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data,
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz) size_t binsz) {
{ if (binsz == 0) {
if (binsz==0) {
if (b58sz) { if (b58sz) {
*b58sz = 0; *b58sz = 0;
} }
return true; return true;
} }
const char * data_bin = data; const char *data_bin = data;
size_t full_block_count = binsz / full_block_size; size_t full_block_count = binsz / full_block_size;
size_t last_block_size = binsz % full_block_size; size_t last_block_size = binsz % full_block_size;
size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size]; size_t res_size = full_block_count * full_encoded_block_size +
encoded_block_sizes[last_block_size];
if (b58sz){ if (b58sz) {
if (res_size > *b58sz){ if (res_size > *b58sz) {
return false; return false;
} }
*b58sz = res_size; *b58sz = res_size;
} }
for (size_t i = 0; i < full_block_count; ++i) for (size_t i = 0; i < full_block_count; ++i) {
{ encode_block(data_bin + i * full_block_size, full_block_size,
encode_block(data_bin + i * full_block_size, full_block_size, b58 + i * full_encoded_block_size); b58 + i * full_encoded_block_size);
} }
if (0 < last_block_size) if (0 < last_block_size) {
{ encode_block(data_bin + full_block_count * full_block_size, last_block_size,
encode_block(data_bin + full_block_count * full_block_size, last_block_size, b58 + full_block_count * full_encoded_block_size); b58 + full_block_count * full_encoded_block_size);
} }
return true; return true;
} }
bool xmr_base58_decode(const char *b58, size_t b58sz, void *data, size_t *binsz) bool xmr_base58_decode(const char *b58, size_t b58sz, void *data,
{ size_t *binsz) {
if (b58sz == 0) { if (b58sz == 0) {
*binsz = 0; *binsz = 0;
return true; return true;
@ -185,24 +204,26 @@ bool xmr_base58_decode(const char *b58, size_t b58sz, void *data, size_t *binsz)
return false; // Invalid enc length return false; // Invalid enc length
} }
size_t data_size = full_block_count * full_block_size + last_block_decoded_size; size_t data_size =
if (*binsz < data_size){ full_block_count * full_block_size + last_block_decoded_size;
if (*binsz < data_size) {
*binsz = 0; *binsz = 0;
return false; return false;
} }
char * data_bin = data; char *data_bin = data;
for (size_t i = 0; i < full_block_count; ++i) for (size_t i = 0; i < full_block_count; ++i) {
{ if (!decode_block(b58 + i * full_encoded_block_size,
if (!decode_block(b58 + i * full_encoded_block_size, full_encoded_block_size, data_bin + i * full_block_size)) { full_encoded_block_size,
data_bin + i * full_block_size)) {
*binsz = 0; *binsz = 0;
return false; return false;
} }
} }
if (0 < last_block_size) if (0 < last_block_size) {
{ if (!decode_block(b58 + full_block_count * full_encoded_block_size,
if (!decode_block(b58 + full_block_count * full_encoded_block_size, last_block_size, last_block_size,
data_bin + full_block_count * full_block_size)) { data_bin + full_block_count * full_block_size)) {
*binsz = 0; *binsz = 0;
return false; return false;
@ -213,8 +234,8 @@ bool xmr_base58_decode(const char *b58, size_t b58sz, void *data, size_t *binsz)
return true; return true;
} }
int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data, size_t binsz, char *b58, size_t b58sz) int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data,
{ size_t binsz, char *b58, size_t b58sz) {
if (binsz > max_bin_data_size || tag > 127) { // tag varint if (binsz > max_bin_data_size || tag > 127) { // tag varint
return false; return false;
} }
@ -223,22 +244,23 @@ int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data, size_t binsz
uint8_t buf[(binsz + 1) + HASHER_DIGEST_LENGTH]; uint8_t buf[(binsz + 1) + HASHER_DIGEST_LENGTH];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
uint8_t *hash = buf + binsz + 1; uint8_t *hash = buf + binsz + 1;
buf[0] = (uint8_t) tag; buf[0] = (uint8_t)tag;
memcpy(buf + 1, data, binsz); memcpy(buf + 1, data, binsz);
hasher_Raw(HASHER_SHA3K, buf, binsz + 1, hash); hasher_Raw(HASHER_SHA3K, buf, binsz + 1, hash);
bool r = xmr_base58_encode(b58, &b58size, buf, binsz + 1 + addr_checksum_size); bool r =
return (int) (!r ? 0 : b58size); xmr_base58_encode(b58, &b58size, buf, binsz + 1 + addr_checksum_size);
return (int)(!r ? 0 : b58size);
} }
int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag, void *data, size_t datalen) int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag,
{ void *data, size_t datalen) {
size_t buflen = 1 + max_bin_data_size + addr_checksum_size; size_t buflen = 1 + max_bin_data_size + addr_checksum_size;
uint8_t buf[buflen]; uint8_t buf[buflen];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
uint8_t hash[HASHER_DIGEST_LENGTH] = {0}; uint8_t hash[HASHER_DIGEST_LENGTH] = {0};
if (!xmr_base58_decode(addr, sz, buf, &buflen)){ if (!xmr_base58_decode(addr, sz, buf, &buflen)) {
return 0; return 0;
} }
@ -247,20 +269,21 @@ int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag, voi
} }
size_t res_size = buflen - addr_checksum_size - 1; size_t res_size = buflen - addr_checksum_size - 1;
if (datalen < res_size){ if (datalen < res_size) {
return 0; return 0;
} }
hasher_Raw(HASHER_SHA3K, buf, buflen - addr_checksum_size, hash); hasher_Raw(HASHER_SHA3K, buf, buflen - addr_checksum_size, hash);
if (memcmp(hash, buf + buflen - addr_checksum_size, addr_checksum_size) != 0){ if (memcmp(hash, buf + buflen - addr_checksum_size, addr_checksum_size) !=
0) {
return 0; return 0;
} }
*tag = buf[0]; *tag = buf[0];
if (*tag > 127){ if (*tag > 127) {
return false; // varint return false; // varint
} }
memcpy(data, buf+1, res_size); memcpy(data, buf + 1, res_size);
return (int) res_size; return (int)res_size;
} }

@ -2,31 +2,34 @@
// //
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, are // Redistribution and use in source and binary forms, with or without
// permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, this list of // 1. Redistributions of source code must retain the above copyright notice,
// conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, this list // 2. Redistributions in binary form must reproduce the above copyright notice,
// of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation
// materials provided with the distribution. // and/or other materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be // 3. Neither the name of the copyright holder nor the names of its contributors
// used to endorse or promote products derived from this software without specific // may be used to endorse or promote products derived from this software
// prior written permission. // without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// //
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote
// developers
#ifndef __XMR_BASE58_H__ #ifndef __XMR_BASE58_H__
#define __XMR_BASE58_H__ #define __XMR_BASE58_H__
@ -35,9 +38,13 @@
#include "hasher.h" #include "hasher.h"
#include "options.h" #include "options.h"
int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data, size_t binsz, char *b58, size_t b58sz); int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data,
int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag, void *data, size_t datalen); size_t binsz, char *b58, size_t b58sz);
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz); int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag,
bool xmr_base58_decode(const char *b58, size_t b58sz, void *data, size_t *binsz); void *data, size_t datalen);
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data,
size_t binsz);
bool xmr_base58_decode(const char *b58, size_t b58sz, void *data,
size_t *binsz);
#endif #endif

@ -2,46 +2,46 @@
// //
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, are // Redistribution and use in source and binary forms, with or without
// permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, this list of // 1. Redistributions of source code must retain the above copyright notice,
// conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, this list // 2. Redistributions in binary form must reproduce the above copyright notice,
// of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation
// materials provided with the distribution. // and/or other materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be // 3. Neither the name of the copyright holder nor the names of its contributors
// used to endorse or promote products derived from this software without specific // may be used to endorse or promote products derived from this software
// prior written permission. // without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// //
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote
// developers
#pragma once #pragma once
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
static inline uint64_t hi_dword(uint64_t val) { static inline uint64_t hi_dword(uint64_t val) { return val >> 32; }
return val >> 32;
}
static inline uint64_t lo_dword(uint64_t val) { static inline uint64_t lo_dword(uint64_t val) { return val & 0xFFFFFFFF; }
return val & 0xFFFFFFFF;
}
static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) { static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand,
uint64_t* product_hi) {
// multiplier = ab = a * 2^32 + b // multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d // multiplicand = cd = c * 2^32 + d
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
@ -67,11 +67,12 @@ static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64
return product_lo; return product_lo;
} }
#define SWAP64(x) ((((uint64_t) (x) & 0x00000000000000ff) << 56) | \ #define SWAP64(x) \
(((uint64_t) (x) & 0x000000000000ff00) << 40) | \ ((((uint64_t)(x)&0x00000000000000ff) << 56) | \
(((uint64_t) (x) & 0x0000000000ff0000) << 24) | \ (((uint64_t)(x)&0x000000000000ff00) << 40) | \
(((uint64_t) (x) & 0x00000000ff000000) << 8) | \ (((uint64_t)(x)&0x0000000000ff0000) << 24) | \
(((uint64_t) (x) & 0x000000ff00000000) >> 8) | \ (((uint64_t)(x)&0x00000000ff000000) << 8) | \
(((uint64_t) (x) & 0x0000ff0000000000) >> 24) | \ (((uint64_t)(x)&0x000000ff00000000) >> 8) | \
(((uint64_t) (x) & 0x00ff000000000000) >> 40) | \ (((uint64_t)(x)&0x0000ff0000000000) >> 24) | \
(((uint64_t) (x) & 0xff00000000000000) >> 56)) (((uint64_t)(x)&0x00ff000000000000) >> 40) | \
(((uint64_t)(x)&0xff00000000000000) >> 56))

@ -3,8 +3,6 @@
^\./crypto/chacha20poly1305/ ^\./crypto/chacha20poly1305/
^\./crypto/ed25519-donna/ ^\./crypto/ed25519-donna/
^\./crypto/gui/ ^\./crypto/gui/
^\./crypto/monero/base58
^\./crypto/monero/int-util
^\./crypto/blake2 ^\./crypto/blake2
^\./crypto/check_mem ^\./crypto/check_mem
^\./crypto/groestl ^\./crypto/groestl

Loading…
Cancel
Save