Skip to content

Commit

Permalink
Add support for using the 32 bit optimized HACL code. (#279)
Browse files Browse the repository at this point in the history
* Add support for using the 32 bit optimized HACL code.

* Remove comment.

* fixed allocation for alloca inside of loops

* A bit of cleanup.

Co-authored-by: Steve Maier <ysfred4@hotmail.com>
  • Loading branch information
jeffspel-crypto and YsFred4 authored May 16, 2022
1 parent 32b5589 commit 0ea2ffd
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 461 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ set(PROJECT_SOURCE_FILES
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_Bignum4096.h
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_Bignum4096_32.c
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_Bignum4096_32.h
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_GenericField32.c
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_GenericField32.h
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_GenericField64.c
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_GenericField64.h
${PROJECT_SOURCE_DIR}/src/karamel/Hacl_Hash.c
Expand Down
90 changes: 89 additions & 1 deletion src/electionguard/facades/Hacl_Bignum4096.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,102 @@
#include "Hacl_Bignum4096.hpp"

#include "../../karamel/Hacl_Bignum4096.h"
#ifdef _WIN32
#include "../../karamel/Hacl_GenericField32.h"
#endif // _WIN32
#include "../../karamel/Hacl_GenericField64.h"
#include "../log.hpp"

using electionguard::Log;

namespace hacl
{
#ifdef _WIN32
Bignum4096::Bignum4096(uint32_t *elem)
{
HaclBignumContext4096 ctx{Hacl_Bignum4096_32_mont_ctx_init(elem)};
context = std::move(ctx);
}
#else
Bignum4096::Bignum4096(uint64_t *elem)
{
HaclBignumContext4096 ctx{Hacl_Bignum4096_mont_ctx_init(elem)};
context = std::move(ctx);
}
#endif // _WIN32
Bignum4096::~Bignum4096() {}

uint64_t Bignum4096::add(uint64_t *a, uint64_t *b, uint64_t *res)
{
#ifdef _WIN32
return Hacl_Bignum4096_32_add(reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_add(a, b, res);
#endif // _WIN32
}

uint64_t Bignum4096::sub(uint64_t *a, uint64_t *b, uint64_t *res)
{
#ifdef _WIN32
return Hacl_Bignum4096_32_sub(reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_sub(a, b, res);
#endif // _WIN32
}

void Bignum4096::mul(uint64_t *a, uint64_t *b, uint64_t *res)
{
#ifdef _WIN32
Hacl_Bignum4096_32_mul(reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
Hacl_Bignum4096_mul(a, b, res);
#endif // _WIN32
}

bool Bignum4096::mod(uint64_t *n, uint64_t *a, uint64_t *res)
{

#ifdef _WIN32
return Hacl_Bignum4096_32_mod(reinterpret_cast<uint32_t *>(n),
reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_mod(n, a, res);
#endif // _WIN32
}

bool Bignum4096::modExp(uint64_t *n, uint64_t *a, uint32_t bBits, uint64_t *b, uint64_t *res,
bool useConstTime /* = true */)
{
{
if (bBits <= 0) {
Log::trace("Bignum4096::modExp:: bbits <= 0");
return false;
}
if (useConstTime) {
#ifdef _WIN32
return Hacl_Bignum4096_32_mod_exp_consttime(reinterpret_cast<uint32_t *>(n),
reinterpret_cast<uint32_t *>(a), bBits,
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_mod_exp_consttime(n, a, bBits, b, res);
#endif // WIN32
}
#ifdef _WIN32
return Hacl_Bignum4096_32_mod_exp_vartime(reinterpret_cast<uint32_t *>(n),
reinterpret_cast<uint32_t *>(a),
bBits,
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_mod_exp_vartime(n, a, bBits, b, res);
#endif // _WIN32
}

uint64_t *Bignum4096::fromBytes(uint32_t len, uint8_t *bytes)
Expand All @@ -65,7 +116,12 @@ namespace hacl

void Bignum4096::mod(uint64_t *a, uint64_t *res) const
{
#ifdef _WIN32
Hacl_Bignum4096_32_mod_precomp(context.get(), reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(res));
#else
Hacl_Bignum4096_mod_precomp(context.get(), a, res);
#endif // _WIN32
}

void Bignum4096::modExp(uint64_t *a, uint32_t bBits, uint64_t *b, uint64_t *res,
Expand All @@ -76,29 +132,61 @@ namespace hacl
return throw;
}
if (useConstTime) {
#ifdef _WIN32
return Hacl_Bignum4096_32_mod_exp_consttime_precomp(context.get(),
reinterpret_cast<uint32_t *>(a), bBits,
reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_mod_exp_consttime_precomp(context.get(), a, bBits, b, res);
#endif // _WIN32
}
#ifdef _WIN32
return Hacl_Bignum4096_32_mod_exp_vartime_precomp(context.get(), reinterpret_cast<uint32_t *>(a),
bBits, reinterpret_cast<uint32_t *>(b),
reinterpret_cast<uint32_t *>(res));
#else
return Hacl_Bignum4096_mod_exp_vartime_precomp(context.get(), a, bBits, b, res);
#endif // _WIN32
}

void Bignum4096::to_montgomery_form(uint64_t *a, uint64_t *aM) const
{
#ifdef _WIN32
Hacl_GenericField32_to_field(context.get(), reinterpret_cast<uint32_t *>(a),
reinterpret_cast<uint32_t *>(aM));
#else
Hacl_GenericField64_to_field(context.get(), a, aM);
#endif // _WIN32
}

void Bignum4096::from_montgomery_form(uint64_t *aM, uint64_t *a) const
{
#ifdef _WIN32
Hacl_GenericField32_from_field(context.get(), reinterpret_cast<uint32_t *>(aM),
reinterpret_cast<uint32_t *>(a));
#else
Hacl_GenericField64_from_field(context.get(), aM, a);
#endif // _WIN32
}

void Bignum4096::montgomery_mod_mul_stay_in_mont_form(uint64_t *aM, uint64_t *bM, uint64_t *cM) const
{
#ifdef _WIN32
Hacl_GenericField32_mul(context.get(), reinterpret_cast<uint32_t *>(aM),
reinterpret_cast<uint32_t *>(bM), reinterpret_cast<uint32_t *>(cM));
#else
Hacl_GenericField64_mul(context.get(), aM, bM, cM);
#endif // _WIN32
}

const Bignum4096 &CONTEXT_P()
{
#ifdef _WIN32
static Bignum4096 instance{(uint32_t*)(P_ARRAY_REVERSE)};
#else
static Bignum4096 instance{const_cast<uint64_t *>(P_ARRAY_REVERSE)};
#endif // _WIN32
return instance;
}
} // namespace hacl
19 changes: 19 additions & 0 deletions src/electionguard/facades/Hacl_Bignum4096.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef __FACADES__Hacl_Bignum4096_H_INCLUDED__
#define __FACADES__Hacl_Bignum4096_H_INCLUDED__

#ifdef _WIN32
#include "../../karamel/Hacl_Bignum4096_32.h"
#endif // _WIN32
#include "../../karamel/Hacl_Bignum4096.h"
#include "electionguard/export.h"

Expand All @@ -18,7 +21,11 @@ namespace hacl
class EG_INTERNAL_API Bignum4096
{
public:
#ifdef _WIN32
explicit Bignum4096(uint32_t *elem);
#else
explicit Bignum4096(uint64_t *elem);
#endif // _WIN32
~Bignum4096();

static uint64_t add(uint64_t *a, uint64_t *b, uint64_t *res);
Expand Down Expand Up @@ -63,13 +70,25 @@ namespace hacl

private:
struct handle_destructor {
#ifdef _WIN32
void operator()(Hacl_Bignum_MontArithmetic_bn_mont_ctx_u32 *handle) const
{
Hacl_Bignum4096_32_mont_ctx_free(handle);
}
#else
void operator()(Hacl_Bignum_MontArithmetic_bn_mont_ctx_u64 *handle) const
{
Hacl_Bignum4096_mont_ctx_free(handle);
}
#endif // _WIN32
};
#ifdef _WIN32
typedef std::unique_ptr<Hacl_Bignum_MontArithmetic_bn_mont_ctx_u32, handle_destructor>
HaclBignumContext4096;
#else
typedef std::unique_ptr<Hacl_Bignum_MontArithmetic_bn_mont_ctx_u64, handle_destructor>
HaclBignumContext4096;
#endif // _WIN32
HaclBignumContext4096 context;
};

Expand Down
Loading

0 comments on commit 0ea2ffd

Please sign in to comment.