Skip to content

Commit

Permalink
Use portable byteswap functions
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkh-fb committed Dec 20, 2023
1 parent ebff055 commit fec6133
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/common/crypto/GFp_curve_algebra/GFp_curve_algebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/common/crypto/ed25519_algebra/ed25519_algebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/common/crypto/zero_knowledge_proof/diffie_hellman_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions test/crypto/ed25519_algebra/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <openssl/rand.h>
#include <openssl/bn.h>

#include <byteswap.h>

#define CATCH_CONFIG_MAIN
#include <tests/catch.hpp>

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions test/crypto/secp256k1_algebra/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <openssl/objects.h>
#include <openssl/rand.h>

#include <byteswap.h>

#define CATCH_CONFIG_MAIN
#include <tests/catch.hpp>

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit fec6133

Please sign in to comment.