Skip to content

Commit

Permalink
FEAT: implemented optional XXH3, XXH32, XXH64 and XXH128 chec…
Browse files Browse the repository at this point in the history
…ksums
  • Loading branch information
Oldes committed Feb 5, 2024
1 parent a674fa6 commit f25ddfa
Show file tree
Hide file tree
Showing 10 changed files with 7,439 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Credits for Non-REBOL orginated C files and modules
Copyright (c) 2021, Dominic Szablewski https://phoboslab.org
The MIT License(MIT) - https://github.com/phoboslab/qoi

* xxHash (optional):
Copyright (C) 2012-2023 Yann Collet - https://github.com/Cyan4973/xxHash
BSD 2-Clause License

Credits for currently deprecated and or unused code
---------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions make/rebol3.nest
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ include-base36-encoding: [
config: INCLUDE_BASE36
]

include-xxhash: [
config: INCLUDE_XXHASH
file: %core/u-xxhash.c
]

;- native additional compressions:
include-lzma-compression: [
config: INCLUDE_LZMA
Expand Down Expand Up @@ -930,6 +935,7 @@ include-rebol-core: [
:include-cryptography
:include-bincode
:include-iconv
:include-xxhash
:include-mail
:include-prot-https

Expand Down
4 changes: 4 additions & 0 deletions src/boot/words.reb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ sha256
sha384
sha512
ripemd160
xxh3
xxh32
xxh64
xxh128
sha3-224
sha3-256
sha3-384
Expand Down
6 changes: 6 additions & 0 deletions src/core/n-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ static mbedtls_ctr_drbg_context ctr_drbg;
add_ec_word(SYM_SHA3_256)
add_ec_word(SYM_SHA3_384)
add_ec_word(SYM_SHA3_512)
#endif
#ifdef INCLUDE_XXHASH
add_ec_word(SYM_XXH3)
add_ec_word(SYM_XXH32)
add_ec_word(SYM_XXH64)
add_ec_word(SYM_XXH128)
#endif
add_ec_word(SYM_TCP)
}
Expand Down
24 changes: 24 additions & 0 deletions src/core/n-strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ static struct digest {
{SHA3_256, SHA3_256_Starts, SHA3_Update, SHA3_256_Finish, SHA3_CtxSize, SYM_SHA3_256, 32, 64},
{SHA3_384, SHA3_384_Starts, SHA3_Update, SHA3_384_Finish, SHA3_CtxSize, SYM_SHA3_384, 48, 128},
{SHA3_512, SHA3_512_Starts, SHA3_Update, SHA3_512_Finish, SHA3_CtxSize, SYM_SHA3_512, 64, 128},
#endif
#ifdef INCLUDE_MD4
{HashXXH3, XXH3_Starts, XXH3_Update, XXH3_Finish, XXH3_CtxSize, SYM_XXH3, 8, 64},
{HashXXH32, XXH32_Starts, XXH32_Update, XXH32_Finish, XXH32_CtxSize, SYM_XXH32, 4, 64},
{HashXXH64, XXH64_Starts, XXH64_Update, XXH64_Finish, XXH64_CtxSize, SYM_XXH64, 8, 64},
{HashXXH128, XXH128_Starts, XXH128_Update, XXH128_Finish, XXH128_CtxSize, SYM_XXH128, 16, 64},
#endif
{0}

Expand Down Expand Up @@ -138,6 +144,24 @@ static struct digest {
MD4(input, length, output);
*olen = 16;
break;
#endif
#ifdef INCLUDE_XXHASH
case SYM_XXH3:
HashXXH3(input, length, output);
*olen = 8;
break;
case SYM_XXH32:
HashXXH32(input, length, output);
*olen = 4;
break;
case SYM_XXH64:
HashXXH64(input, length, output);
*olen = 8;
break;
case SYM_XXH128:
HashXXH128(input, length, output);
*olen = 16;
break;
#endif
default:
return FALSE;
Expand Down
62 changes: 61 additions & 1 deletion src/core/p-checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#include REBOL_OPTIONS_FILE
#endif

#ifdef INCLUDE_MBEDTLS
#include "sys-core.h"
#include "sys-checksum.h"
#ifdef INCLUDE_MBEDTLS
#include "reb-net.h"
#include "mbedtls/md4.h"
#include "mbedtls/md5.h"
Expand Down Expand Up @@ -140,6 +140,24 @@
*ctx = sizeof(SHA512_CTX);
*blk = SHA512_DIGEST_LENGTH;
return;
#endif
#ifdef INCLUDE_XXHASH
case SYM_XXH3:
*ctx = sizeof(XXH3_CTX);
*blk = 8;
break;
case SYM_XXH32:
*ctx = sizeof(XXH32_CTX);
*blk = 4;
break;
case SYM_XXH64:
*ctx = sizeof(XXH64_CTX);
*blk = 8;
break;
case SYM_XXH128:
*ctx = sizeof(XXH128_CTX);
*blk = 16;
break;
#endif
default:
*ctx = *blk = 0;
Expand Down Expand Up @@ -225,6 +243,20 @@
case SYM_SHA512:
SHA512_Starts((SHA512_CTX*)VAL_BIN(ctx));
return TRUE;
#endif
#ifdef INCLUDE_XXHASH
case SYM_XXH3:
XXH3_Starts((XXH3_CTX*)VAL_BIN(ctx));
return TRUE;
case SYM_XXH32:
XXH32_Starts((XXH32_CTX*)VAL_BIN(ctx));
return TRUE;
case SYM_XXH64:
XXH64_Starts((XXH64_CTX*)VAL_BIN(ctx));
return TRUE;
case SYM_XXH128:
XXH128_Starts((XXH128_CTX*)VAL_BIN(ctx));
return TRUE;
#endif
}
return FALSE;
Expand Down Expand Up @@ -348,6 +380,20 @@
case SYM_SHA512:
SHA512_Update((SHA512_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
#endif
#ifdef INCLUDE_XXHASH
case SYM_XXH3:
XXH3_Update((XXH3_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
case SYM_XXH32:
XXH32_Update((XXH32_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
case SYM_XXH64:
XXH64_Update((XXH64_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
case SYM_XXH128:
XXH128_Update((XXH128_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
#endif
}
break;
Expand Down Expand Up @@ -430,6 +476,20 @@
case SYM_SHA512:
SHA512_Finish((SHA512_CTX*)DS_TOP, VAL_BIN_DATA(data));
break;
#endif
#ifdef INCLUDE_XXHASH
case SYM_XXH3:
XXH3_Finish((XXH3_CTX*)DS_TOP, VAL_BIN_DATA(data));
break;
case SYM_XXH32:
XXH32_Finish((XXH32_CTX*)DS_TOP, VAL_BIN_DATA(data));
break;
case SYM_XXH64:
XXH64_Finish((XXH64_CTX*)DS_TOP, VAL_BIN_DATA(data));
break;
case SYM_XXH128:
XXH128_Finish((XXH128_CTX*)DS_TOP, VAL_BIN_DATA(data));
break;
#endif
}
if(action == A_READ) *D_RET = *data;
Expand Down
165 changes: 165 additions & 0 deletions src/core/u-xxhash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* xxHash - Extremely Fast Hash algorithm
* Copyright (C) 2012-2023 Yann Collet
*
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN 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.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/

/*
* xxhash.c instantiates functions defined in xxhash.h
*/

#include "sys-core.h"

#ifdef INCLUDE_XXHASH
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */

#include "sys-xxhash.h"

/***********************************************************************
**
*/ REBYTE *HashXXH3(REBYTE *d, REBCNT n, REBYTE *md)
/*
***********************************************************************/
{
// d is data, n is length
XXH64_hash_t const hash = XXH3_64bits(d, n);
XXH64_canonicalFromHash(md, hash);
return md;
}
/***********************************************************************
**
*/ REBYTE *HashXXH32(REBYTE *d, REBCNT n, REBYTE *md)
/*
***********************************************************************/
{
XXH32_hash_t const hash = XXH32(d, n, 0);
XXH32_canonicalFromHash(md, hash);
return md;
}
/***********************************************************************
**
*/ REBYTE *HashXXH64(REBYTE *d, REBCNT n, REBYTE *md)
/*
***********************************************************************/
{
// d is data, n is length
XXH64_hash_t const hash = XXH64(d, n, 0);
XXH64_canonicalFromHash(md, hash);
return md;
}
/***********************************************************************
**
*/ REBYTE* HashXXH128(REBYTE* d, REBCNT n, REBYTE* md)
/*
***********************************************************************/
{
// d is data, n is length
XXH128_hash_t const hash = XXH128(d, n, 0);
XXH128_canonicalFromHash(md, hash);
return md;
}
int XXH3_CtxSize(void) { return sizeof(XXH3_state_t); }
int XXH32_CtxSize(void) { return sizeof(XXH32_state_t); }
int XXH64_CtxSize(void) { return sizeof(XXH64_state_t); }
int XXH128_CtxSize(void) { return sizeof(XXH3_state_t); }

void XXH3_Starts( XXH3_state_t *ctx )
{
XXH3_64bits_reset(ctx);
}
void XXH3_Update( XXH3_state_t *ctx,
const unsigned char *input,
size_t ilen )
{
XXH3_64bits_update( ctx, input, ilen );
}
void XXH3_Finish(XXH3_state_t*ctx,
unsigned char output[64] )
{
XXH64_hash_t const hash = XXH3_64bits_digest(ctx);
XXH64_canonicalFromHash(output, hash);
}

void XXH32_Starts( XXH32_state_t *ctx )
{
ctx = XXH32_createState();
XXH32_reset(ctx, 0);
}
void XXH32_Update( XXH32_state_t *ctx,
const unsigned char *input,
size_t ilen )
{
XXH32_update( ctx, input, ilen );
}
void XXH32_Finish(XXH32_state_t*ctx,
unsigned char output[64] )
{
XXH32_hash_t const hash = XXH32_digest(ctx);
XXH32_canonicalFromHash(output, hash);
}

void XXH64_Starts( XXH64_state_t *ctx )
{
ctx = XXH64_createState();
XXH64_reset(ctx, 0);
}
void XXH64_Update( XXH64_state_t *ctx,
const unsigned char *input,
size_t ilen )
{
XXH64_update( ctx, input, ilen );
}
void XXH64_Finish(XXH64_state_t*ctx,
unsigned char output[64] )
{
XXH64_hash_t const hash = XXH64_digest(ctx);
XXH64_canonicalFromHash(output, hash);
}

void XXH128_Starts( XXH3_state_t *ctx )
{
XXH3_128bits_reset(ctx);
}
void XXH128_Update( XXH3_state_t *ctx,
const unsigned char *input,
size_t ilen )
{
XXH3_128bits_update( ctx, input, ilen );
}
void XXH128_Finish(XXH3_state_t*ctx,
unsigned char output[16] )
{
XXH128_hash_t const hash = XXH3_128bits_digest(ctx);
XXH128_canonicalFromHash((XXH128_canonical_t*)output, hash);
}

#endif
35 changes: 35 additions & 0 deletions src/include/sys-checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,39 @@ void SHA3_224_Finish(void* c, REBYTE* md);
void SHA3_256_Finish(void* c, REBYTE* md);
void SHA3_384_Finish(void* c, REBYTE* md);
void SHA3_512_Finish(void* c, REBYTE* md);
#endif

#ifdef INCLUDE_XXHASH
#define XXH_STATIC_LINKING_ONLY
#include "sys-xxhash.h"

#define XXH3_CTX XXH3_state_t
#define XXH32_CTX XXH32_state_t
#define XXH64_CTX XXH64_state_t
#define XXH128_CTX XXH3_state_t
REBYTE* HashXXH3(REBYTE*, REBCNT, REBYTE*);
REBYTE* HashXXH32(REBYTE*, REBCNT, REBYTE*);
REBYTE* HashXXH64(REBYTE*, REBCNT, REBYTE*);
REBYTE* HashXXH128(REBYTE*, REBCNT, REBYTE*);

void XXH3_Starts(void* c);
void XXH3_Update(void* c, REBYTE* data, REBCNT len);
void XXH3_Finish(void* c, REBYTE* md);
int XXH3_CtxSize(void);

void XXH32_Starts(void* c);
void XXH32_Update(void* c, REBYTE* data, REBCNT len);
void XXH32_Finish(void* c, REBYTE* md);
int XXH32_CtxSize(void);

void XXH64_Starts(void* c);
void XXH64_Update(void* c, REBYTE* data, REBCNT len);
void XXH64_Finish(void* c, REBYTE* md);
int XXH64_CtxSize(void);

void XXH128_Starts(void* c);
void XXH128_Update(void* c, REBYTE* data, REBCNT len);
void XXH128_Finish(void* c, REBYTE* md);
int XXH128_CtxSize(void);

#endif
Loading

0 comments on commit f25ddfa

Please sign in to comment.