From d29be80759aa8b16c206b4c48e641caba4af0dd1 Mon Sep 17 00:00:00 2001 From: rtjk <47841774+rtjk@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:46:29 +0100 Subject: [PATCH] imported fix from CROSS upstream: endianness-aware csprng Signed-off-by: rtjk <47841774+rtjk@users.noreply.github.com> --- docs/algorithms/sig/cross.md | 4 +- docs/algorithms/sig/cross.yml | 4 +- .../copy_from_upstream/copy_from_upstream.yml | 2 +- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 47 ++++++++++++++--- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- .../csprng_hash.h | 51 +++++++++++++++---- 39 files changed, 1445 insertions(+), 329 deletions(-) diff --git a/docs/algorithms/sig/cross.md b/docs/algorithms/sig/cross.md index 91de3bb82..8fa97e174 100644 --- a/docs/algorithms/sig/cross.md +++ b/docs/algorithms/sig/cross.md @@ -5,9 +5,9 @@ - **Principal submitters**: Marco Baldi, Alessandro Barenghi, Sebastian Bitzer, Patrick Karl, Felice Manganiello, Alessio Pavoni, Gerardo Pelosi, Paolo Santini, Jonas Schupp, Freeman Slaughter, Antonia Wachter-Zeh, Violetta Weger. - **Auxiliary submitters**: Marco Gianvecchio. - **Authors' website**: https://www.cross-crypto.com/ -- **Specification version**: 1.2 + Keccak_x4 + PQClean fixes. +- **Specification version**: 1.2 + Keccak_x4 + PQClean fixes + endianness fix. - **Primary Source**: - - **Source**: https://github.com/rtjk/CROSS-PQClean/commit/577d7c761c684637923c8648644cf2f4d7b41954 + - **Source**: https://github.com/rtjk/CROSS-PQClean/commit/82938638cb6bcb680d9472e544a61be38db806cc - **Implementation license (SPDX-Identifier)**: CC0-1.0 diff --git a/docs/algorithms/sig/cross.yml b/docs/algorithms/sig/cross.yml index 616da6602..6cbc0d629 100644 --- a/docs/algorithms/sig/cross.yml +++ b/docs/algorithms/sig/cross.yml @@ -19,9 +19,9 @@ crypto-assumption: hardness of the restricted syndrome decoding problem for rand linear codes on a finite field website: https://www.cross-crypto.com/ nist-round: 1 -spec-version: 1.2 + Keccak_x4 + PQClean fixes +spec-version: 1.2 + Keccak_x4 + PQClean fixes + endianness fix primary-upstream: - source: https://github.com/rtjk/CROSS-PQClean/commit/577d7c761c684637923c8648644cf2f4d7b41954 + source: https://github.com/rtjk/CROSS-PQClean/commit/82938638cb6bcb680d9472e544a61be38db806cc spdx-license-identifier: CC0-1.0 parameter-sets: - name: cross-rsdp-128-balanced diff --git a/scripts/copy_from_upstream/copy_from_upstream.yml b/scripts/copy_from_upstream/copy_from_upstream.yml index 1362a407e..cf3ea5596 100644 --- a/scripts/copy_from_upstream/copy_from_upstream.yml +++ b/scripts/copy_from_upstream/copy_from_upstream.yml @@ -66,7 +66,7 @@ upstreams: name: upcross git_url: https://github.com/rtjk/CROSS-PQClean.git git_branch: master - git_commit: 577d7c761c684637923c8648644cf2f4d7b41954 + git_commit: 82938638cb6bcb680d9472e544a61be38db806cc sig_meta_path: 'generate/crypto_sign/{pqclean_scheme}/META.yml' sig_scheme_path: 'generate/crypto_sign/{pqclean_scheme}' kems: diff --git a/src/sig/cross/upcross_cross-rsdp-128-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-balanced_avx2/csprng_hash.h index 7892cfde2..738ab30fe 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-128-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-balanced_clean/csprng_hash.h index c9c14549f..00999a95e 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-128-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-fast_avx2/csprng_hash.h index 0aae75c2f..083c69f43 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-128-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-fast_clean/csprng_hash.h index c0b9a5420..9e79d7972 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-128-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-small_avx2/csprng_hash.h index ddddf21d4..d3a025ddc 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-128-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-128-small_clean/csprng_hash.h index 5acaa2452..b288b826f 100644 --- a/src/sig/cross/upcross_cross-rsdp-128-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-128-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-balanced_avx2/csprng_hash.h index 2bc154c1d..bd09c81fc 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-balanced_clean/csprng_hash.h index c0c30a66e..fda519b19 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-fast_avx2/csprng_hash.h index c3bc2489c..382b892f3 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-fast_clean/csprng_hash.h index 1849cf2b3..f4ac46ccf 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-small_avx2/csprng_hash.h index 5071313b9..5dfa722e9 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-192-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-192-small_clean/csprng_hash.h index 3685f99b8..d6397fe20 100644 --- a/src/sig/cross/upcross_cross-rsdp-192-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-192-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-balanced_avx2/csprng_hash.h index 57eb0aadf..b234b8aa8 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-balanced_clean/csprng_hash.h index 10e8b10c8..98df725fc 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-fast_avx2/csprng_hash.h index 89f17f2f1..180d0f12b 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-fast_clean/csprng_hash.h index 293874625..6ab13a977 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-small_avx2/csprng_hash.h index c3eaa95c0..29890cddf 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdp-256-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdp-256-small_clean/csprng_hash.h index b55084025..489bb060b 100644 --- a/src/sig/cross/upcross_cross-rsdp-256-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdp-256-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_vec(FZ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-balanced_avx2/csprng_hash.h index e0c67a478..06d5689ed 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-balanced_clean/csprng_hash.h index 884bfe955..7dd4bca16 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-fast_avx2/csprng_hash.h index 4874b381a..11d2dff18 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-fast_clean/csprng_hash.h index 03bbe70fb..3f8527952 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-small_avx2/csprng_hash.h index 13225a12b..a4f5332f4 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-128-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-128-small_clean/csprng_hash.h index 841d87dd5..2b42231e7 100644 --- a/src/sig/cross/upcross_cross-rsdpg-128-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-128-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-balanced_avx2/csprng_hash.h index 42274791e..09f9e911c 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-balanced_clean/csprng_hash.h index f5fb444a4..e2f423ade 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-fast_avx2/csprng_hash.h index e7dbffc55..3664fdf70 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-fast_clean/csprng_hash.h index 194e3fc37..6b554a94f 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-small_avx2/csprng_hash.h index 75aef4d2c..75778f98d 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-192-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-192-small_clean/csprng_hash.h index 96b409f08..ffc040f87 100644 --- a/src/sig/cross/upcross_cross-rsdpg-192-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-192-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-balanced_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-balanced_avx2/csprng_hash.h index cde3e4784..05dbf36d3 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-balanced_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-balanced_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-balanced_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-balanced_clean/csprng_hash.h index e4cf7194c..825fcd3fb 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-balanced_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-balanced_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-fast_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-fast_avx2/csprng_hash.h index c30cc648a..6d9584b7d 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-fast_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-fast_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-fast_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-fast_clean/csprng_hash.h index 95c827533..9875d33c4 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-fast_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-fast_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-small_avx2/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-small_avx2/csprng_hash.h index e3f83d36d..eaf2e9ca4 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-small_avx2/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-small_avx2/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; diff --git a/src/sig/cross/upcross_cross-rsdpg-256-small_clean/csprng_hash.h b/src/sig/cross/upcross_cross-rsdpg-256-small_clean/csprng_hash.h index 1a5748204..30a1703f0 100644 --- a/src/sig/cross/upcross_cross-rsdpg-256-small_clean/csprng_hash.h +++ b/src/sig/cross/upcross_cross-rsdpg-256-small_clean/csprng_hash.h @@ -214,6 +214,37 @@ FQ_ELEM fq_star_rnd_state(CSPRNG_STATE_T *const csprng_state) { return rnd_value + 1; } /* end fq_star_rnd_state */ +/********************* Endianness detection and handling **********************/ + +#define BIG_ENDIAN_SYSTEM 0 // assume little-endian by default + +/* Check if we are on a big-endian system: + * the __BYTE_ORDER__ macro is defined by the compiler + * recent versions of GCC and Clang define it + * other methods to detect endianness might be needed when using different compilers */ +#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#undef BIG_ENDIAN_SYSTEM +#define BIG_ENDIAN_SYSTEM 1 +#endif + +static inline uint64_t to_little_endian64(uint64_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap64(x); +#else + return x; +#endif +} + +static inline uint32_t to_little_endian32(uint32_t x) { + /* When compiling on a big-endian system, swap the bytes */ +#if BIG_ENDIAN_SYSTEM + return __builtin_bswap32(x); +#else + return x; +#endif +} + /***************** Specialized CSPRNGs for non binary domains *****************/ /* CSPRNG sampling fixed weight strings */ @@ -249,14 +280,14 @@ void CSPRNG_fq_vec(FQ_ELEM res[N], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < N) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -298,14 +329,14 @@ void CSPRNG_fq_vec_beta(FQ_ELEM res[T], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_VEC_BETA, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < T) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -345,7 +376,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FQ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; @@ -353,7 +384,7 @@ void CSPRNG_fq_mat(FQ_ELEM res[K][N - K], while (placed < K * (N - K)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -393,14 +424,14 @@ void CSPRNG_zz_inf_w(FZ_ELEM res[M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_ZZ_INF_W, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32; @@ -440,14 +471,14 @@ void CSPRNG_fz_mat(FZ_ELEM res[M][N - M], * in from the left end */ csprng_randombytes(CSPRNG_buffer, BUFSIZE_FZ_MAT, csprng_state); int placed = 0; - uint64_t sub_buffer = *(uint64_t *)CSPRNG_buffer; + uint64_t sub_buffer = to_little_endian64(*(uint64_t *)CSPRNG_buffer); int bits_in_sub_buf = 64; /* position of the next fresh byte in CSPRNG_buffer*/ int pos_in_buf = 8; while (placed < M * (N - M)) { if (bits_in_sub_buf <= 32) { /* get 32 fresh bits from main buffer with a single load */ - uint32_t refresh_buf = *(uint32_t *) (CSPRNG_buffer + pos_in_buf); + uint32_t refresh_buf = to_little_endian32(*(uint32_t *) (CSPRNG_buffer + pos_in_buf)); pos_in_buf += 4; sub_buffer |= ((uint64_t) refresh_buf) << bits_in_sub_buf; bits_in_sub_buf += 32;