Skip to content

Commit

Permalink
xxHash64 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Mar 1, 2024
1 parent e9f918d commit 7df6bd4
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: secretbase
Type: Package
Title: Cryptographic Hash and Extendable-Output Functions
Version: 0.3.0.1
Version: 0.3.0.2
Description: SHA-256, SHA-3 cryptographic hash and SHAKE256 extendable-output
functions (XOF). The SHA-3 Secure Hash Standard was published by the
National Institute of Standards and Technology (NIST) in 2015 at
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

export(sha256)
export(sha3)
export(xxh64)
useDynLib(secretbase, .registration = TRUE)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# secretbase 0.3.0.2

* Adds 'xxHash64' as a fast non-cryptographic hash function.

# secretbase 0.3.0.1

* CRAN release correcting for Clang-UBSAN checks.
Expand Down
30 changes: 30 additions & 0 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,33 @@ sha3 <- function(x, bits = 256L, convert = TRUE, file)
sha256 <- function(x, convert = TRUE, file)
if (missing(file)) .Call(secretbase_sha256, x, convert) else
.Call(secretbase_sha256_file, file, convert)

#' Fast Non-Cryptographic Hashing Using xxHash64
#'
#' Returns the 'xxHash64' of the supplied R object or file. This is an extremely
#' fast hash, processing at RAM speed limits.
#'
#' @inheritParams sha3
#'
#' @return A character string, raw or integer vector depending on 'convert'.
#'
#' @details This implementation uses the algorithm released by Yann Collet at
#' \url{https://xxhash.com/}.
#'
#' @examples
#' # xxHash64 as character string:
#' xxh64("secret base")
#'
#' # xxHash64 as raw vector:
#' xxh64("secret base", convert = FALSE)
#'
#' # xxHash64 a file:
#' file <- tempfile(); cat("secret base", file = file)
#' xxh64(file = file)
#' unlink(file)
#'
#' @export
#'
xxh64 <- function(x, convert = TRUE, file)
if (missing(file)) .Call(secretbase_xxh64, x, convert) else
.Call(secretbase_xxh64_file, file, convert)
47 changes: 47 additions & 0 deletions man/xxh64.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static const R_CallMethodDef callMethods[] = {
{"secretbase_sha3_file", (DL_FUNC) &secretbase_sha3_file, 3},
{"secretbase_sha256", (DL_FUNC) &secretbase_sha256, 2},
{"secretbase_sha256_file", (DL_FUNC) &secretbase_sha256_file, 2},
{"secretbase_xxh64", (DL_FUNC) &secretbase_xxh64, 2},
{"secretbase_xxh64_file", (DL_FUNC) &secretbase_xxh64_file, 2},
{NULL, NULL, 0}
};

Expand Down
21 changes: 19 additions & 2 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
#define SB_SERIAL_HEADERS 6
#define SB_BUF_SIZE 4096


#ifdef WORDS_BIGENDIAN
# define MBEDTLS_IS_BIG_ENDIAN 1
# define SB_IS_BIG_ENDIAN 1
#else
# define MBEDTLS_IS_BIG_ENDIAN 0
# define SB_IS_BIG_ENDIAN 0
#endif

typedef enum {
Expand Down Expand Up @@ -67,6 +68,15 @@ typedef struct mbedtls_sha256_context {
uint32_t state[8];
} mbedtls_sha256_context;

typedef struct XXH64_state_s {
uint64_t total_len;
uint64_t v[4];
uint64_t mem64[4];
uint32_t memsize;
uint32_t reserved32;
uint64_t reserved64;
} XXH64_state_t;

typedef struct secretbase_sha3_context {
int skip;
mbedtls_sha3_context *ctx;
Expand All @@ -77,11 +87,18 @@ typedef struct secretbase_sha256_context {
mbedtls_sha256_context *ctx;
} secretbase_sha256_context;

typedef struct secretbase_xxh64_context {
int skip;
XXH64_state_t *ctx;
} secretbase_xxh64_context;

SEXP hash_to_sexp(unsigned char *, size_t, int);

SEXP secretbase_sha3(SEXP, SEXP, SEXP);
SEXP secretbase_sha3_file(SEXP, SEXP, SEXP);
SEXP secretbase_sha256(SEXP, SEXP);
SEXP secretbase_sha256_file(SEXP, SEXP);
SEXP secretbase_xxh64(SEXP, SEXP);
SEXP secretbase_xxh64_file(SEXP, SEXP);

#endif
4 changes: 2 additions & 2 deletions src/secret2.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ static inline uint64_t mbedtls_bswap64(uint64_t x) {
#endif /* !defined(MBEDTLS_BSWAP64) */

#define MBEDTLS_GET_UINT32_BE(data, offset) \
((MBEDTLS_IS_BIG_ENDIAN) \
((SB_IS_BIG_ENDIAN) \
? mbedtls_get_unaligned_uint32((data) + (offset)) \
: MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
)

#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
{ \
if (MBEDTLS_IS_BIG_ENDIAN) \
if (SB_IS_BIG_ENDIAN) \
{ \
mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
} \
Expand Down
Loading

0 comments on commit 7df6bd4

Please sign in to comment.