Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ziaptos committed May 9, 2024
1 parent 59d6245 commit 3566813
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
12 changes: 8 additions & 4 deletions src/exp.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <iostream>
#include <stdint.h>
#include <cstdint>
#include <vector>

#include "naf.hpp"
Expand All @@ -14,16 +14,20 @@ void nafMulByScalar(BaseGroup& G, BaseGroupElementOut& r,
{
BaseGroupElementIn baseCopy;
int nBits = (scalarSize * 8) + 2;
std::vector<uint8_t> naf((scalarSize + 2) * 8);
buildNaf(naf.data(), scalar, scalarSize);
// std::vector<uint8_t> naf((scalarSize + 2) * 8);
std::vector<std::int64_t> naf_as_i64(scalarSize + 2);

buildNaf(naf_as_i64.data(), scalar, scalarSize);

auto naf = reinterpret_cast<std::uint8_t*>(naf_as_i64.data());

G.copy(baseCopy, base); // base and result can be the same
G.copy(r, G.zero());
int i = nBits - 1;

while ((i >= 0) && (naf[i] == 0))
{
i--;
--i;
}

while (i >= 0)
Expand Down
4 changes: 4 additions & 0 deletions src/fullprover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ struct ProverResponse
ProverResponse(ProverError _error);
ProverResponse(const char* _raw_json, ProverResponseMetrics _metrics);

ProverResponse() = delete;
ProverResponse(ProverResponse const&) = delete;
ProverResponse& operator=(ProverResponse const&) = delete;

~ProverResponse();
};

Expand Down
15 changes: 8 additions & 7 deletions src/groth16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ template <typename Engine>
std::unique_ptr<Proof<Engine>>
Prover<Engine>::prove(typename Engine::FrElement* wtns)
{
#define USE_FUTURES

#if 1 // defined(USE_OPENMP) || 1
#ifndef USE_FUTURES // 1 // defined(USE_OPENMP) || 1
std::cout << "using openmp" << std::endl;
std::cout << "num variables: " << nVars << std::endl;
std::cout << "domain size: " << domainSize << std::endl;
Expand Down Expand Up @@ -303,12 +304,12 @@ Prover<Engine>::prove(typename Engine::FrElement* wtns)
fill_with_random_bytes(r);
fill_with_random_bytes(s);

// # ifndef USE_OPENMP
// pA_future.get();
// pB1_future.get();
// pB2_future.get();
// pC_future.get();
// # endif
# ifdef USE_FUTURES
pA_future.get();
pB1_future.get();
pB2_future.get();
pC_future.get();
# endif

typename Engine::G1Point p1;
typename Engine::G2Point p2;
Expand Down
42 changes: 29 additions & 13 deletions src/naf.hpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
#pragma once

#include <stdint.h>
#include <cstdint>

static inline uint64_t NAFTable[1024];
static inline std::uint64_t NAFTable[1024];

inline void buildNaf(uint8_t* r, uint8_t* scalar, unsigned int scalarSize)
inline void buildNaf(std::int64_t* r64, std::uint8_t* scalar,
unsigned int scalarSize)
{
int64_t* r64 = (int64_t*)r;
// r was uint8_t* which is not necessarly aligned same s int64_t
// int64_t* r64 = (int64_t*)r;

bool carry = false;
bool last = (scalar[0] & 1);
int st;
int64_t rs;
bool carry = false;
bool last = (scalar[0] & 1);
std::int64_t rs;

for (unsigned int i = 0; i < scalarSize + 2; i++)
{
st = last ? 1 : 0;
int st = last ? 1 : 0;

if (i < scalarSize)
{
st += scalar[i] & 0xFE;
}

if (i < scalarSize - 1)
{
st += (scalar[i + 1] & 1) << 8;
}

if (carry)
{
st += 0x200;
}

rs = NAFTable[st];
carry = rs & 4;
Expand All @@ -34,9 +44,9 @@ inline bool buildNafTable()
{
for (int in = 0; in < 1024; in++)
{
bool carry = (in & 0x200);
bool last = (in & 1);
uint8_t res[8];
bool carry = (in & 0x200);
bool last = (in & 1);
std::uint8_t res[8];
for (int i = 0; i < 8; i++)
{
bool cur = in & (1 << (i + 1));
Expand Down Expand Up @@ -109,11 +119,17 @@ inline bool buildNafTable()
}
}

uint64_t r64 = (*((int64_t*)(res)));
std::uint64_t r64;
std::memcpy(&r64, res, sizeof(r64)); // = (*((int64_t*)(res)));
if (carry)
{
r64 |= 0x4;
}

if (last)
{
r64 |= 0x8;
}

NAFTable[in] = r64;
}
Expand Down

0 comments on commit 3566813

Please sign in to comment.