From fec613384cbb4dbf03a0fef8959af403f6d21529 Mon Sep 17 00:00:00 2001 From: Igor Khanin Date: Wed, 20 Dec 2023 17:40:50 +0200 Subject: [PATCH] Use portable byteswap functions --- .../crypto/GFp_curve_algebra/GFp_curve_algebra.c | 4 ++-- src/common/crypto/ed25519_algebra/ed25519_algebra.c | 12 ++++++------ .../crypto/zero_knowledge_proof/diffie_hellman_log.c | 4 ++-- test/crypto/ed25519_algebra/main.cpp | 6 ++++-- test/crypto/secp256k1_algebra/main.cpp | 6 ++++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/common/crypto/GFp_curve_algebra/GFp_curve_algebra.c b/src/common/crypto/GFp_curve_algebra/GFp_curve_algebra.c index e4a0f36..7c77a83 100644 --- a/src/common/crypto/GFp_curve_algebra/GFp_curve_algebra.c +++ b/src/common/crypto/GFp_curve_algebra/GFp_curve_algebra.c @@ -1019,8 +1019,8 @@ static uint8_t in_field(const elliptic_curve256_scalar_t val, const uint8_t *fie const uint32_t *ptr2 = (const uint32_t*)field; for (size_t i = sizeof(elliptic_curve256_scalar_t) / sizeof(uint32_t); i > 0; i --) { - uint64_t v1 = __bswap_32(ptr1[i - 1]); - uint64_t v2 = __bswap_32(ptr2[i - 1]); + uint64_t v1 = bswap_32(ptr1[i - 1]); + uint64_t v2 = bswap_32(ptr2[i - 1]); cc = ((v1 - v2 - cc) >> 32) & 1; } return cc; diff --git a/src/common/crypto/ed25519_algebra/ed25519_algebra.c b/src/common/crypto/ed25519_algebra/ed25519_algebra.c index 1dbe233..cb9b115 100644 --- a/src/common/crypto/ed25519_algebra/ed25519_algebra.c +++ b/src/common/crypto/ed25519_algebra/ed25519_algebra.c @@ -51,10 +51,10 @@ static inline void bswap_256(const ed25519_scalar_t in, ed25519_scalar_t out) { uint64_t *inptr = (uint64_t*)in; uint64_t *outptr = (uint64_t*)out; - outptr[0] = __bswap_64(inptr[3]); - outptr[1] = __bswap_64(inptr[2]); - outptr[2] = __bswap_64(inptr[1]); - outptr[3] = __bswap_64(inptr[0]); + outptr[0] = bswap_64(inptr[3]); + outptr[1] = bswap_64(inptr[2]); + outptr[2] = bswap_64(inptr[1]); + outptr[3] = bswap_64(inptr[0]); } static inline int ed25519_to_scalar(const ed25519_scalar_t in, ed25519_scalar_t out) @@ -852,8 +852,8 @@ static uint8_t in_field(const elliptic_curve256_scalar_t val) const uint32_t *ptr2 = (const uint32_t*)ED25519_FIELD; for (size_t i = sizeof(elliptic_curve256_scalar_t) / sizeof(uint32_t); i > 0; i --) { - uint64_t v1 = __bswap_32(ptr1[i - 1]); - uint64_t v2 = __bswap_32(ptr2[i - 1]); + uint64_t v1 = bswap_32(ptr1[i - 1]); + uint64_t v2 = bswap_32(ptr2[i - 1]); cc = ((v1 - v2 - cc) >> 32) & 1; } return cc; diff --git a/src/common/crypto/zero_knowledge_proof/diffie_hellman_log.c b/src/common/crypto/zero_knowledge_proof/diffie_hellman_log.c index 699d5cb..8eefd5d 100644 --- a/src/common/crypto/zero_knowledge_proof/diffie_hellman_log.c +++ b/src/common/crypto/zero_knowledge_proof/diffie_hellman_log.c @@ -13,8 +13,8 @@ static inline int cmp_uint256(const uint8_t *a, const uint8_t *b) for (size_t i = 0; i < sizeof(elliptic_curve256_scalar_t) / sizeof(uint64_t); i++) { - uint64_t n1 = __bswap_64(*aptr); // elliptic_curve256_scalar_t is represented as big endian number - uint64_t n2 = __bswap_64(*bptr); + uint64_t n1 = bswap_64(*aptr); // elliptic_curve256_scalar_t is represented as big endian number + uint64_t n2 = bswap_64(*bptr); if (n1 > n2) return 1; else if (n1 < n2) diff --git a/test/crypto/ed25519_algebra/main.cpp b/test/crypto/ed25519_algebra/main.cpp index 293b2f3..f988734 100644 --- a/test/crypto/ed25519_algebra/main.cpp +++ b/test/crypto/ed25519_algebra/main.cpp @@ -4,6 +4,8 @@ #include #include +#include + #define CATCH_CONFIG_MAIN #include @@ -264,7 +266,7 @@ TEST_CASE( "ed25519_algebra_add_points", "zkp") { status = ed25519_algebra_add_points(ctx, &res, &pa, &pb); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); - uint32_t val = __bswap_32(__bswap_32(a)+__bswap_32(b)); // sum in big endian + uint32_t val = bswap_32(bswap_32(a)+bswap_32(b)); // sum in big endian status = ed25519_algebra_generator_mul_data(ctx, (uint8_t*)&val, sizeof(val), &sum); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); @@ -284,7 +286,7 @@ TEST_CASE( "ed25519_algebra_add_points", "zkp") { REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); REQUIRE(memcmp(pa, res, sizeof(ed25519_point_t)) == 0); - uint32_t val = __bswap_32(__bswap_32(a)+__bswap_32(b)); // sum in big endian + uint32_t val = bswap_32(bswap_32(a)+bswap_32(b)); // sum in big endian status = ed25519_algebra_generator_mul_data(ctx, (uint8_t*)&val, sizeof(val), &sum); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); diff --git a/test/crypto/secp256k1_algebra/main.cpp b/test/crypto/secp256k1_algebra/main.cpp index cfd166c..447b2ae 100644 --- a/test/crypto/secp256k1_algebra/main.cpp +++ b/test/crypto/secp256k1_algebra/main.cpp @@ -5,6 +5,8 @@ #include #include +#include + #define CATCH_CONFIG_MAIN #include @@ -358,7 +360,7 @@ TEST_CASE( "secp256k1_algebra_add_points", "zkp") { status = GFp_curve_algebra_add_points(ctx, &res, &pa, &pb); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); - uint32_t val = __bswap_32(__bswap_32(a)+__bswap_32(b)); // sum in big endian + uint32_t val = bswap_32(bswap_32(a)+bswap_32(b)); // sum in big endian status = GFp_curve_algebra_generator_mul_data(ctx, (uint8_t*)&val, sizeof(val), &sum); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); @@ -378,7 +380,7 @@ TEST_CASE( "secp256k1_algebra_add_points", "zkp") { REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS); REQUIRE(memcmp(pa, res, sizeof(elliptic_curve256_point_t)) == 0); - uint32_t val = __bswap_32(__bswap_32(a)+__bswap_32(b)); // sum in big endian + uint32_t val = bswap_32(bswap_32(a)+bswap_32(b)); // sum in big endian status = GFp_curve_algebra_generator_mul_data(ctx, (uint8_t*)&val, sizeof(val), &sum); REQUIRE(status == ELLIPTIC_CURVE_ALGEBRA_SUCCESS);