Skip to content

Commit

Permalink
[bit_converter|hash] replaced hash common functions with equivalents.
Browse files Browse the repository at this point in the history
Functions hash_algorithm_uint32_t_to_uint8_t_array,
hash_algorithm_uint64_t_to_uint8_t_array,
hash_algorithm_uint8_t_array_to_uint32_t and
hash_algorithm_uint8_t_array_to_uint64_t have equivalents from the
bit_converter unit.
  • Loading branch information
TheVice committed Mar 26, 2024
1 parent 7d840cc commit c237d84
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 132 deletions.
5 changes: 3 additions & 2 deletions hash.blake3.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 - 2022 TheVice
* Copyright (c) 2020 - 2022, 2024 TheVice
*
*/

Expand All @@ -14,6 +14,7 @@
#include "stdc_secure_api.h"

#include "hash.h"
#include "bit_converter.h"
#include "buffer.h"

#include <stddef.h>
Expand Down Expand Up @@ -342,7 +343,7 @@ uint8_t MERGE(uint8_t* stack, uint8_t* stack_length, const uint32_t* t, uint8_t
{
uint8_t t_[sizeof(uint32_t)];

if (!hash_algorithm_uint32_t_to_uint8_t_array(t[0], t_))
if (!bit_converter_get_bytes_from_uint32_t(t[0], t_))
{
return 0;
}
Expand Down
110 changes: 6 additions & 104 deletions hash.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 - 2022 TheVice
* Copyright (c) 2019 - 2022, 2024 TheVice
*
*/

#include "hash.h"

#include "bit_converter.h"
#include "buffer.h"
#include "conversion.h"

Expand Down Expand Up @@ -66,7 +67,7 @@ uint8_t hash_algorithm_uint32_t_array_to_uint8_t_array(

while (start < finish)
{
if (!hash_algorithm_uint32_t_to_uint8_t_array(*start, output))
if (!bit_converter_get_bytes_from_uint32_t(*start, output))
{
return 0;
}
Expand All @@ -78,33 +79,6 @@ uint8_t hash_algorithm_uint32_t_array_to_uint8_t_array(
return 1;
}

uint8_t hash_algorithm_uint32_t_to_uint8_t_array(
uint32_t input, uint8_t* output)
{
if (NULL == output)
{
return 0;
}

uint8_t i = 0;

do
{
output[i] = (uint8_t)(input % 16);
input /= 16;
output[i++] += (uint8_t)((input % 16) * 16);
}
while (15 < (input /= 16));

while (i < sizeof(uint32_t))
{
output[i++] = (uint8_t)input;
input = 0;
}

return 1;
}

uint8_t hash_algorithm_uint64_t_array_to_uint8_t_array(
const uint64_t* start, const uint64_t* finish, uint8_t* output)
{
Expand All @@ -118,7 +92,7 @@ uint8_t hash_algorithm_uint64_t_array_to_uint8_t_array(

while (start < finish)
{
if (!hash_algorithm_uint64_t_to_uint8_t_array(*start, output))
if (!bit_converter_get_bytes_from_uint64_t(*start, output))
{
return 0;
}
Expand All @@ -130,55 +104,6 @@ uint8_t hash_algorithm_uint64_t_array_to_uint8_t_array(
return 1;
}

uint8_t hash_algorithm_uint64_t_to_uint8_t_array(uint64_t input, uint8_t* output)
{
if (NULL == output)
{
return 0;
}

uint8_t i = 0;

do
{
output[i] = (uint8_t)(input % 16);
input /= 16;
output[i++] += (uint8_t)((input % 16) * 16);
}
while (15 < (input /= 16));

while (i < sizeof(uint64_t))
{
output[i++] = (uint8_t)input;
input = 0;
}

return 1;
}

uint8_t hash_algorithm_uint8_t_array_to_uint32_t(
const uint8_t* start, const uint8_t* finish, uint32_t* output)
{
if (NULL == start ||
NULL == finish ||
finish < start ||
NULL == output)
{
return 0;
}

uint8_t j = 0;
*output = 0;

while ((--finish) > (start - 1) && j < 8)
{
*output += (uint32_t)(((*finish) & 0xF0) >> 4) * ((uint32_t)1 << (4 * (7 - (j++))));
*output += (uint32_t)((*finish) & 0x0F) * ((uint32_t)1 << (4 * (7 - (j++))));
}

return 1;
}

uint8_t hash_algorithm_uint8_t_array_to_uint32_t_array(
const uint8_t* input, ptrdiff_t input_size, uint32_t* output)
{
Expand All @@ -191,7 +116,7 @@ uint8_t hash_algorithm_uint8_t_array_to_uint32_t_array(

for (ptrdiff_t i = 0, j = 0; i < input_size; i += sizeof(uint32_t), ++j)
{
if (!hash_algorithm_uint8_t_array_to_uint32_t(input + i, input + i + sizeof(uint32_t), output + j))
if (!bit_converter_to_uint32_t(input + i, input + i + sizeof(uint32_t), output + j))
{
return 0;
}
Expand All @@ -200,29 +125,6 @@ uint8_t hash_algorithm_uint8_t_array_to_uint32_t_array(
return 1;
}

uint8_t hash_algorithm_uint8_t_array_to_uint64_t(
const uint8_t* start, const uint8_t* finish, uint64_t* output)
{
if (NULL == start ||
NULL == finish ||
finish < start ||
NULL == output)
{
return 0;
}

uint8_t j = 0;
*output = 0;

while ((--finish) > (start - 1) && j < 16)
{
*output += (uint64_t)(((*finish) & 0xF0) >> 4) * ((uint64_t)1 << (4 * (15 - (j++))));
*output += (uint64_t)((*finish) & 0x0F) * ((uint64_t)1 << (4 * (15 - (j++))));
}

return 1;
}

uint8_t hash_algorithm_uint8_t_array_to_uint64_t_array(
const uint8_t* input, ptrdiff_t input_size, uint64_t* output)
{
Expand All @@ -235,7 +137,7 @@ uint8_t hash_algorithm_uint8_t_array_to_uint64_t_array(

for (ptrdiff_t i = 0, j = 0; i < input_size; i += sizeof(uint64_t), ++j)
{
if (!hash_algorithm_uint8_t_array_to_uint64_t(input + i, input + i + sizeof(uint64_t), output + j))
if (!bit_converter_to_uint64_t(input + i, input + i + sizeof(uint64_t), output + j))
{
return 0;
}
Expand Down
13 changes: 7 additions & 6 deletions hash.crc32.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 - 2020, 2022 TheVice
* Copyright (c) 2019 - 2020, 2022, 2024 TheVice
*
*/

Expand All @@ -12,13 +12,14 @@
*/

#include "hash.h"
#include "bit_converter.h"
#include "common.h"

#include <stddef.h>

uint8_t hash_algorithm_crc32_init(uint8_t* output)
{
return hash_algorithm_uint32_t_to_uint8_t_array(UINT32_MAX, output);
return bit_converter_get_bytes_from_uint32_t(UINT32_MAX, output);
}

uint8_t hash_algorithm_crc32_core(const uint8_t* start, const uint8_t* finish, uint8_t* output)
Expand Down Expand Up @@ -101,7 +102,7 @@ uint8_t hash_algorithm_crc32_core(const uint8_t* start, const uint8_t* finish, u
/**/
uint32_t out;

if (!hash_algorithm_uint8_t_array_to_uint32_t(output, output + sizeof(uint32_t), &out))
if (!bit_converter_to_uint32_t(output, output + sizeof(uint32_t), &out))
{
return 0;
}
Expand All @@ -111,7 +112,7 @@ uint8_t hash_algorithm_crc32_core(const uint8_t* start, const uint8_t* finish, u
out = (out >> sizeof(uint64_t)) ^ table[((out ^ (*start)) & 0xFF)];
}

return hash_algorithm_uint32_t_to_uint8_t_array(out, output);
return bit_converter_get_bytes_from_uint32_t(out, output);
}

uint8_t hash_algorithm_crc32_final(uint8_t* output, uint8_t order)
Expand All @@ -123,14 +124,14 @@ uint8_t hash_algorithm_crc32_final(uint8_t* output, uint8_t order)

uint32_t out;

if (!hash_algorithm_uint8_t_array_to_uint32_t(output, output + sizeof(uint32_t), &out))
if (!bit_converter_to_uint32_t(output, output + sizeof(uint32_t), &out))
{
return 0;
}

out ^= UINT32_MAX;

if (!hash_algorithm_uint32_t_to_uint8_t_array(out, output))
if (!bit_converter_get_bytes_from_uint32_t(out, output))
{
return 0;
}
Expand Down
10 changes: 1 addition & 9 deletions hash.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 - 2022 TheVice
* Copyright (c) 2019 - 2022, 2024 TheVice
*
*/

Expand All @@ -16,18 +16,10 @@ uint8_t hash_algorithm_bytes_to_string(

uint8_t hash_algorithm_uint32_t_array_to_uint8_t_array(
const uint32_t* start, const uint32_t* finish, uint8_t* output);
uint8_t hash_algorithm_uint32_t_to_uint8_t_array(
uint32_t input, uint8_t* output);
uint8_t hash_algorithm_uint64_t_array_to_uint8_t_array(
const uint64_t* start, const uint64_t* finish, uint8_t* output);
uint8_t hash_algorithm_uint64_t_to_uint8_t_array(
uint64_t input, uint8_t* output);
uint8_t hash_algorithm_uint8_t_array_to_uint32_t(
const uint8_t* start, const uint8_t* finish, uint32_t* output);
uint8_t hash_algorithm_uint8_t_array_to_uint32_t_array(
const uint8_t* input, ptrdiff_t input_size, uint32_t* output);
uint8_t hash_algorithm_uint8_t_array_to_uint64_t(
const uint8_t* start, const uint8_t* finish, uint64_t* output);
uint8_t hash_algorithm_uint8_t_array_to_uint64_t_array(
const uint8_t* input, ptrdiff_t input_size, uint64_t* output);

Expand Down
5 changes: 3 additions & 2 deletions hash.sha3.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2012 - 2022 TheVice
* Copyright (c) 2012 - 2021, 2024 TheVice
*
*/

Expand All @@ -26,6 +26,7 @@
#include "stdc_secure_api.h"

#include "hash.h"
#include "bit_converter.h"
#include "buffer.h"
#include "common.h"

Expand Down Expand Up @@ -199,7 +200,7 @@ uint8_t Keccak_absorption_bytes(const uint8_t* data, uint64_t* S, uint8_t rate_o
const uint8_t* start = data + 8 * i;
const uint8_t* finish = start + 8;

if (!hash_algorithm_uint8_t_array_to_uint64_t(start, finish, &data_[i]))
if (!bit_converter_to_uint64_t(start, finish, &data_[i]))
{
return 0;
}
Expand Down
13 changes: 7 additions & 6 deletions hash.xxhash.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 TheVice
* Copyright (c) 2021, 2024 TheVice
*
*/

Expand All @@ -14,6 +14,7 @@
#include "stdc_secure_api.h"

#include "hash.h"
#include "bit_converter.h"
#include "common.h"

#include <stddef.h>
Expand Down Expand Up @@ -76,7 +77,7 @@ static const uint32_t PRIME32_5 = 0x165667B1U;
{\
uint32_t result; \
\
if (!hash_algorithm_uint8_t_array_to_uint32_t((START), (START) + 4, &result)) \
if (!bit_converter_to_uint32_t((START), (START) + 4, &result)) \
{ \
return 0; \
} \
Expand All @@ -99,7 +100,7 @@ static const uint32_t PRIME32_5 = 0x165667B1U;
{ \
uint32_t result; \
\
if (!hash_algorithm_uint8_t_array_to_uint32_t((START), (START) + 4, &result)) \
if (!bit_converter_to_uint32_t((START), (START) + 4, &result)) \
{ \
return 0; \
} \
Expand Down Expand Up @@ -355,7 +356,7 @@ static const uint64_t PRIME64_5 = 0x27D4EB2F165667C5ULL;
{\
uint64_t result; \
\
if (!hash_algorithm_uint8_t_array_to_uint64_t((START), (START) + 8, &result)) \
if (!bit_converter_to_uint64_t((START), (START) + 8, &result)) \
{ \
return 0; \
} \
Expand Down Expand Up @@ -384,7 +385,7 @@ static const uint64_t PRIME64_5 = 0x27D4EB2F165667C5ULL;
{ \
uint64_t result; \
\
if (!hash_algorithm_uint8_t_array_to_uint64_t((START), (START) + 8, &result)) \
if (!bit_converter_to_uint64_t((START), (START) + 8, &result)) \
{ \
return 0; \
} \
Expand All @@ -399,7 +400,7 @@ static const uint64_t PRIME64_5 = 0x27D4EB2F165667C5ULL;
{ \
uint32_t result; \
\
if (!hash_algorithm_uint8_t_array_to_uint32_t((START), (START) + 4, &result)) \
if (!bit_converter_to_uint32_t((START), (START) + 4, &result)) \
{ \
return 0; \
} \
Expand Down
Loading

0 comments on commit c237d84

Please sign in to comment.