Skip to content

Commit

Permalink
Merge branch 'main' into cmake-build
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkh-fb authored Dec 21, 2023
2 parents 7947170 + cb64940 commit 4941a86
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 46 deletions.
6 changes: 4 additions & 2 deletions include/blockchain/mpc/hd_derive.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const unsigned int EDDSA_PUBLIC_KEY_SIZE = 32;
const unsigned int PRIVATE_KEY_SIZE = 32;
const unsigned int CHAIN_CODE_SIZE_BYTES = 32;
const unsigned int BIP44_PATH_LENGTH = 5;

typedef unsigned char HDChaincode[CHAIN_CODE_SIZE_BYTES];
typedef unsigned char PubKey[COMPRESSED_PUBLIC_KEY_SIZE];
typedef unsigned char PrivKey[PRIVATE_KEY_SIZE];
typedef uint32_t Bip44Path[BIP44_PATH_LENGTH];

typedef enum
{
Expand All @@ -41,12 +43,12 @@ inline uint32_t bip32_hardened_index(uint32_t idx)
return (1U<<31) + idx;
}

// TODO: Refactor receive allocated int[BIP44_PATH_LENGTH] path
hd_derive_status build_bip44_path(uint32_t** path, uint32_t* path_len, uint32_t asset_num, uint32_t account, uint32_t change = 0, uint32_t addr_index = 0);
hd_derive_status derive_public_key_generic(const elliptic_curve256_algebra_ctx_t *ctx, PubKey derived_key, const PubKey pubkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);
hd_derive_status derive_private_key_generic(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);
hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, PubKey derived_pubkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);

hd_derive_status build_bip44_path(Bip44Path path, uint32_t asset_num, uint32_t account, uint32_t change = 0, uint32_t addr_index = 0);

#ifdef __cplusplus
}
#endif //__cplusplus
Expand Down
41 changes: 29 additions & 12 deletions include/logging/logging_t.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
// This file can be replaced with other logging system integration
#pragma once

#ifndef LOGGING_T_H_
#define LOGGING_T_H_
typedef enum {
COSIGNER_LOG_LEVEL_FATAL = 50000,
COSIGNER_LOG_LEVEL_ERROR = 40000,
COSIGNER_LOG_LEVEL_WARN = 30000,
COSIGNER_LOG_LEVEL_INFO = 20000,
COSIGNER_LOG_LEVEL_DEBUG = 10000,
COSIGNER_LOG_LEVEL_TRACE = 5000,
} COSIGNER_LOG_LEVEL;

#include <stdio.h>
typedef void (*cosigner_log_callback)(int level, const char* file, int line, const char* func, const char* message, void* userp);

#define LOG(level, message, ...) do {printf((message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_DEBUG(message, ...) do {printf((message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_TRACE(message, ...) do {printf((message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_INFO(message, ...) do {printf((message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_WARN(message, ...) do {printf((message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_ERROR(message, ...) do {fprintf(stderr, (message), ##__VA_ARGS__);putchar('\n');} while(0)
#define LOG_FATAL(message, ...) do {fprintf(stderr, (message), ##__VA_ARGS__);putchar('\n');} while(0)
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus

#endif
void cosigner_log_init(cosigner_log_callback cb, void* userp);

void cosigner_log_msg(int level, const char* file, int line, const char* func, const char* message, ...)
__attribute__ ((format (printf, 5, 6)));

#ifdef __cplusplus
}
#endif //__cplusplus

#define LOG(level, message, ...) cosigner_log_msg((level), __FILE__, __LINE__, __func__, (message), ##__VA_ARGS__)
#define LOG_TRACE(message, ...) LOG(COSIGNER_LOG_LEVEL_TRACE, message, ##__VA_ARGS__)
#define LOG_DEBUG(message, ...) LOG(COSIGNER_LOG_LEVEL_DEBUG, message, ##__VA_ARGS__)
#define LOG_INFO(message, ...) LOG(COSIGNER_LOG_LEVEL_INFO, message, ##__VA_ARGS__)
#define LOG_WARN(message, ...) LOG(COSIGNER_LOG_LEVEL_WARN, message, ##__VA_ARGS__)
#define LOG_ERROR(message, ...) LOG(COSIGNER_LOG_LEVEL_ERROR, message, ##__VA_ARGS__)
#define LOG_FATAL(message, ...) LOG(COSIGNER_LOG_LEVEL_FATAL, message, ##__VA_ARGS__)
6 changes: 3 additions & 3 deletions src/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ endif
COMMON_CFLAGS := $(COMMON_CFLAGS)

C_Files := crypto/shamir_secret_sharing/verifiable_secret_sharing.c crypto/paillier/paillier.c crypto/paillier/paillier_zkp.c crypto/GFp_curve_algebra/GFp_curve_algebra.c \
crypto/commitments/commitments.c crypto/zero_knowledge_proof/schnorr.c crypto/zero_knowledge_proof/range_proofs.c crypto/zero_knowledge_proof/diffie_hellman_log.c \
crypto/commitments/ring_pedersen.c crypto/ed25519_algebra/ed25519_algebra.c crypto/drng/drng.c crypto/keccak1600/keccak1600.c
crypto/commitments/commitments.c crypto/zero_knowledge_proof/schnorr.c crypto/zero_knowledge_proof/range_proofs.c crypto/zero_knowledge_proof/diffie_hellman_log.c \
crypto/commitments/ring_pedersen.c crypto/ed25519_algebra/ed25519_algebra.c crypto/drng/drng.c crypto/keccak1600/keccak1600.c logging/logging_t.c

C_Objects := $(C_Files:.c=.o)

Cpp_Files := cosigner/cosigner_exception.cpp cosigner/cmp_setup_service.cpp cosigner/cmp_offline_refresh_service.cpp cosigner/utils.cpp \
Expand Down
23 changes: 8 additions & 15 deletions src/common/blockchain/mpc/hd_derive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static hd_derive_status BIP32Hash(hmac_sha_result output, const HDChaincode chai
if (1 != HMAC_Update(ctx, num, 4)) {
goto end_bip32_hash;
}

if (1 != HMAC_Final(ctx, output, &output_len)) {
goto end_bip32_hash;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ hd_derive_status derive_private_key_generic(const elliptic_curve256_algebra_ctx_
hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, PubKey derived_pubkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode,
const uint32_t* path, const uint32_t path_len) {
PubKey temp_pubkey;

if (!path || !path_len)
{
memcpy(derived_privkey, privkey, PRIVATE_KEY_SIZE);
Expand Down Expand Up @@ -170,18 +170,11 @@ hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_
return HD_DERIVE_SUCCESS;
}

hd_derive_status build_bip44_path(uint32_t** path, uint32_t* path_len, uint32_t asset_num, uint32_t account, uint32_t change, uint32_t addr_index) {
*path_len = 5;
*path = (uint32_t*)malloc(BIP44_PATH_LENGTH * sizeof(uint32_t));
if (NULL == *path){
return HD_DERIVE_ERROR_OUT_OF_MEMORY;
}

(*path)[0] = BIP44; //purpose
(*path)[1] = asset_num;
(*path)[2] = account;
(*path)[3] = change;
(*path)[4] = addr_index;

hd_derive_status build_bip44_path(Bip44Path path, uint32_t asset_num, uint32_t account, uint32_t change, uint32_t addr_index) {
path[0] = BIP44; //purpose
path[1] = asset_num;
path[2] = account;
path[3] = change;
path[4] = addr_index;
return HD_DERIVE_SUCCESS;
}
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
1 change: 1 addition & 0 deletions src/common/lib.lds
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ libcosigner.so
range_proof_*;
schnorr_zkp_*;
derive_*;
cosigner_log_*;
_ZN10fireblocks6common8cosigner17*;
_ZN10fireblocks6common8cosigner2*;
_ZN10fireblocks6common8cosigner32*;
Expand Down
45 changes: 45 additions & 0 deletions src/common/logging/logging_t.c
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
#include "logging/logging_t.h"

#include <stdarg.h>
#include <stdio.h>

#define MAX_LOG_SIZE 4096

static void default_log_callback(int level, const char* file, int line, const char* func, const char* message, void* userp)
{
(void)file;
(void)line;
(void)func;
(void)userp;

if (level >= COSIGNER_LOG_LEVEL_ERROR)
fprintf(stderr, "%s\n", message);
else
printf("%s\n", message);
}

static cosigner_log_callback log_callback = default_log_callback;
static void* log_callback_user_data_pointer = NULL;

void cosigner_log_init(cosigner_log_callback cb, void* userp)
{
log_callback = cb;
log_callback_user_data_pointer = userp;
}

void cosigner_log_msg(int level, const char* file, int line, const char* func, const char* message, ...)
{
va_list args;
char buffer[MAX_LOG_SIZE] = { '\0' };

if (log_callback == NULL)
return;

if (message != NULL)
{
va_start(args, message);
vsnprintf(buffer, MAX_LOG_SIZE, message, args);
va_end(args);
}

log_callback(level, file, line, func, buffer, log_callback_user_data_pointer);
}
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 4941a86

Please sign in to comment.