Skip to content

Commit

Permalink
Merge pull request #23 from AntelopeIO/cleanup_options
Browse files Browse the repository at this point in the history
Cleanup conversion option parameters.
  • Loading branch information
greg7mdp authored Apr 2, 2024
2 parents e159d6e + 7f992aa commit 382cb5f
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 243 deletions.
36 changes: 18 additions & 18 deletions bench/eth_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,25 @@ void benchG1Add2() {
constexpr int numIters = 10000;
g1 pbak = random_g1();

auto performTest = [pbak](bool check, bool raw) {
auto performTest = [pbak](conv_opt opt) {
array<uint8_t, 144> pRaw = {};
g1 p = pbak;
p.toJacobianBytesLE(pRaw, raw);
p.toJacobianBytesLE(pRaw, opt.to_mont ? from_mont::yes : from_mont::no);

auto start = startStopwatch();

for (int i = 0; i < numIters; i++) {
p = *g1::fromJacobianBytesLE(pRaw, check, raw);
p = *g1::fromJacobianBytesLE(pRaw, opt);
p.addAssign(p);
p.toJacobianBytesLE(pRaw, raw);
p.toJacobianBytesLE(pRaw, opt.to_mont ? from_mont::yes : from_mont::no);
}
endStopwatch(string("check=") + std::to_string(check) + string(", raw=") + std::to_string(raw), start, numIters);
endStopwatch(string("check_valid=") + std::to_string(opt.check_valid) + string(", to_mont=") + std::to_string(opt.to_mont), start, numIters);
};

performTest(true, true);
performTest(true, false);
performTest(false, true);
performTest(false, false);
performTest({ .check_valid = true, .to_mont = true });
performTest({ .check_valid = true, .to_mont = false });
performTest({ .check_valid = false, .to_mont = true });
performTest({ .check_valid = false, .to_mont = false });

}

Expand All @@ -232,25 +232,25 @@ void benchG2Add2() {
constexpr int numIters = 10000;
g2 pbak = random_g2();

auto performTest = [pbak](bool check, bool raw) {
auto performTest = [pbak](conv_opt opt) {
array<uint8_t, 288> pRaw = {};
g2 p = pbak;
p.toJacobianBytesLE(pRaw, raw);
p.toJacobianBytesLE(pRaw, opt.to_mont ? from_mont::yes : from_mont::no);

auto start = startStopwatch();

for (int i = 0; i < numIters; i++) {
p = *g2::fromJacobianBytesLE(pRaw, check, raw);
p = *g2::fromJacobianBytesLE(pRaw, opt);
p.addAssign(p);
p.toJacobianBytesLE(pRaw, raw);
p.toJacobianBytesLE(pRaw, opt.to_mont ? from_mont::yes : from_mont::no);
}
endStopwatch(string("check=") + std::to_string(check) + string(", raw=") + std::to_string(raw), start, numIters);
endStopwatch(string("check_valid=") + std::to_string(opt.check_valid) + string(", to_mont=") + std::to_string(opt.to_mont), start, numIters);
};

performTest(true, true);
performTest(true, false);
performTest(false, true);
performTest(false, false);
performTest({ .check_valid = true, .to_mont = true });
performTest({ .check_valid = true, .to_mont = false });
performTest({ .check_valid = false, .to_mont = true });
performTest({ .check_valid = false, .to_mont = false });

}

Expand Down
68 changes: 44 additions & 24 deletions include/bls12-381/fp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
namespace bls12_381
{

// config when converting from bytes
// ---------------------------------
struct conv_opt {
bool check_valid; // check bytes hold a valid encoding
bool to_mont; // convert to montgomery form
};

// config when converting to bytes
// -------------------------------
enum class from_mont : uint8_t { no = 0, yes };

// element representation of 'fp' field which is the base field
// ------------------------------------------------------------
class fp
{

Expand All @@ -21,12 +33,14 @@ class fp
fp();
explicit fp(const std::array<uint64_t, 6>& d);
fp(const fp& e);
static std::optional<fp> fromBytesBE(const std::span<const uint8_t, 48> in, const bool check = true, const bool raw = false);
static std::optional<fp> fromBytesLE(const std::span<const uint8_t, 48> in, const bool check = true, const bool raw = false);
void toBytesBE(const std::span<uint8_t, 48> out, const bool raw = false) const;
void toBytesLE(const std::span<uint8_t, 48> out, const bool raw = false) const;
std::array<uint8_t, 48> toBytesBE(const bool raw = false) const;
std::array<uint8_t, 48> toBytesLE(const bool raw = false) const;
static std::optional<fp> fromBytesBE(const std::span<const uint8_t, 48> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
static std::optional<fp> fromBytesLE(const std::span<const uint8_t, 48> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
void toBytesBE(const std::span<uint8_t, 48> out, const from_mont fm = from_mont::yes) const;
void toBytesLE(const std::span<uint8_t, 48> out, const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 48> toBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 48> toBytesLE(const from_mont fm = from_mont::yes) const;
static fp zero();
static fp one();
bool isValid() const;
Expand Down Expand Up @@ -107,12 +121,14 @@ class fp2
fp2();
explicit fp2(const std::array<fp, 2>& e2);
fp2(const fp2& e);
static std::optional<fp2> fromBytesBE(const std::span<const uint8_t, 96> in, const bool check = true, const bool raw = false);
static std::optional<fp2> fromBytesLE(const std::span<const uint8_t, 96> in, const bool check = true, const bool raw = false);
void toBytesBE(const std::span<uint8_t, 96> out, const bool raw = false) const;
void toBytesLE(const std::span<uint8_t, 96> out, const bool raw = false) const;
std::array<uint8_t, 96> toBytesBE(const bool raw = false) const;
std::array<uint8_t, 96> toBytesLE(const bool raw = false) const;
static std::optional<fp2> fromBytesBE(const std::span<const uint8_t, 96> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
static std::optional<fp2> fromBytesLE(const std::span<const uint8_t, 96> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
void toBytesBE(const std::span<uint8_t, 96> out, const from_mont fm = from_mont::yes) const;
void toBytesLE(const std::span<uint8_t, 96> out, const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 96> toBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 96> toBytesLE(const from_mont fm = from_mont::yes) const;
static fp2 zero();
static fp2 one();
bool isZero() const;
Expand Down Expand Up @@ -166,12 +182,14 @@ class fp6
fp6();
explicit fp6(const std::array<fp2, 3>& e3);
fp6(const fp6& e);
static std::optional<fp6> fromBytesBE(const std::span<const uint8_t, 288> in, const bool check = true, const bool raw = false);
static std::optional<fp6> fromBytesLE(const std::span<const uint8_t, 288> in, const bool check = true, const bool raw = false);
void toBytesBE(const std::span<uint8_t, 288> out, const bool raw = false) const;
void toBytesLE(const std::span<uint8_t, 288> out, const bool raw = false) const;
std::array<uint8_t, 288> toBytesBE(const bool raw = false) const;
std::array<uint8_t, 288> toBytesLE(const bool raw = false) const;
static std::optional<fp6> fromBytesBE(const std::span<const uint8_t, 288> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
static std::optional<fp6> fromBytesLE(const std::span<const uint8_t, 288> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
void toBytesBE(const std::span<uint8_t, 288> out, const from_mont fm = from_mont::yes) const;
void toBytesLE(const std::span<uint8_t, 288> out, const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 288> toBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 288> toBytesLE(const from_mont fm = from_mont::yes) const;
static fp6 zero();
static fp6 one();
bool isZero() const;
Expand Down Expand Up @@ -214,12 +232,14 @@ class fp12
fp12();
explicit fp12(const std::array<fp6, 2>& e2);
fp12(const fp12& e);
static std::optional<fp12> fromBytesBE(const std::span<const uint8_t, 576> in, const bool check = true, const bool raw = false);
static std::optional<fp12> fromBytesLE(const std::span<const uint8_t, 576> in, const bool check = true, const bool raw = false);
void toBytesBE(const std::span<uint8_t, 576> out, const bool raw = false) const;
void toBytesLE(const std::span<uint8_t, 576> out, const bool raw = false) const;
std::array<uint8_t, 576> toBytesBE(const bool raw = false) const;
std::array<uint8_t, 576> toBytesLE(const bool raw = false) const;
static std::optional<fp12> fromBytesBE(const std::span<const uint8_t, 576> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
static std::optional<fp12> fromBytesLE(const std::span<const uint8_t, 576> in,
const conv_opt opt = { .check_valid = true, .to_mont = true });
void toBytesBE(const std::span<uint8_t, 576> out, const from_mont fm = from_mont::yes) const;
void toBytesLE(const std::span<uint8_t, 576> out, const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 576> toBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 576> toBytesLE(const from_mont fm = from_mont::yes) const;
static fp12 zero();
static fp12 one();
bool isZero() const;
Expand Down
62 changes: 33 additions & 29 deletions include/bls12-381/g.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
#include <functional>
#include <optional>
#include <span>
#include <bls12-381/fp.hpp>

namespace bls12_381
{

class fp;
class fp2;
class fp6;
class fp12;

// g1 is type for point in G1.
// g1 is both used for Affine and Jacobian point representation.
// If z is equal to one the point is considered as in affine form.
Expand All @@ -27,20 +23,24 @@ class g1
g1();
explicit g1(const std::array<fp, 3>& e3);
g1(const g1& e);
static std::optional<g1> fromJacobianBytesBE(const std::span<const uint8_t, 144> in, const bool check = false, const bool raw = false);
static std::optional<g1> fromJacobianBytesLE(const std::span<const uint8_t, 144> in, const bool check = false, const bool raw = false);
static std::optional<g1> fromAffineBytesBE(const std::span<const uint8_t, 96> in, const bool check = false, const bool raw = false);
static std::optional<g1> fromAffineBytesLE(const std::span<const uint8_t, 96> in, const bool check = false, const bool raw = false);
static std::optional<g1> fromJacobianBytesBE(const std::span<const uint8_t, 144> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g1> fromJacobianBytesLE(const std::span<const uint8_t, 144> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g1> fromAffineBytesBE(const std::span<const uint8_t, 96> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g1> fromAffineBytesLE(const std::span<const uint8_t, 96> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g1> fromCompressedBytesBE(const std::span<const uint8_t, 48> in);
void toJacobianBytesBE(const std::span<uint8_t, 144> out, const bool raw = false) const;
void toJacobianBytesLE(const std::span<uint8_t, 144> out, const bool raw = false) const;
void toAffineBytesBE(const std::span<uint8_t, 96> out, const bool raw = false) const;
void toAffineBytesLE(const std::span<uint8_t, 96> out, const bool raw = false) const;
void toJacobianBytesBE(const std::span<uint8_t, 144> out, const from_mont fm = from_mont::yes) const;
void toJacobianBytesLE(const std::span<uint8_t, 144> out, const from_mont fm = from_mont::yes) const;
void toAffineBytesBE(const std::span<uint8_t, 96> out, const from_mont fm = from_mont::yes) const;
void toAffineBytesLE(const std::span<uint8_t, 96> out, const from_mont fm = from_mont::yes) const;
void toCompressedBytesBE(const std::span<uint8_t, 48> out) const;
std::array<uint8_t, 144> toJacobianBytesBE(const bool raw = false) const;
std::array<uint8_t, 144> toJacobianBytesLE(const bool raw = false) const;
std::array<uint8_t, 96> toAffineBytesBE(const bool raw = false) const;
std::array<uint8_t, 96> toAffineBytesLE(const bool raw = false) const;
std::array<uint8_t, 144> toJacobianBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 144> toJacobianBytesLE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 96> toAffineBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 96> toAffineBytesLE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 48> toCompressedBytesBE() const;
static g1 zero();
static g1 one();
Expand Down Expand Up @@ -89,20 +89,24 @@ class g2
g2();
explicit g2(const std::array<fp2, 3>& e3);
g2(const g2& e);
static std::optional<g2> fromJacobianBytesBE(const std::span<const uint8_t, 288> in, const bool check = false, const bool raw = false);
static std::optional<g2> fromJacobianBytesLE(const std::span<const uint8_t, 288> in, const bool check = false, const bool raw = false);
static std::optional<g2> fromAffineBytesBE(const std::span<const uint8_t, 192> in, const bool check = false, const bool raw = false);
static std::optional<g2> fromAffineBytesLE(const std::span<const uint8_t, 192> in, const bool check = false, const bool raw = false);
static std::optional<g2> fromJacobianBytesBE(const std::span<const uint8_t, 288> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g2> fromJacobianBytesLE(const std::span<const uint8_t, 288> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g2> fromAffineBytesBE(const std::span<const uint8_t, 192> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g2> fromAffineBytesLE(const std::span<const uint8_t, 192> in,
conv_opt opt = { .check_valid = false, .to_mont = true });
static std::optional<g2> fromCompressedBytesBE(const std::span<const uint8_t, 96> in);
void toJacobianBytesBE(const std::span<uint8_t, 288> out, const bool raw = false) const;
void toJacobianBytesLE(const std::span<uint8_t, 288> out, const bool raw = false) const;
void toAffineBytesBE(const std::span<uint8_t, 192> out, const bool raw = false) const;
void toAffineBytesLE(const std::span<uint8_t, 192> out, const bool raw = false) const;
void toJacobianBytesBE(const std::span<uint8_t, 288> out, const from_mont fm = from_mont::yes) const;
void toJacobianBytesLE(const std::span<uint8_t, 288> out, const from_mont fm = from_mont::yes) const;
void toAffineBytesBE(const std::span<uint8_t, 192> out, const from_mont fm = from_mont::yes) const;
void toAffineBytesLE(const std::span<uint8_t, 192> out, const from_mont fm = from_mont::yes) const;
void toCompressedBytesBE(const std::span<uint8_t, 96> out) const;
std::array<uint8_t, 288> toJacobianBytesBE(const bool raw = false) const;
std::array<uint8_t, 288> toJacobianBytesLE(const bool raw = false) const;
std::array<uint8_t, 192> toAffineBytesBE(const bool raw = false) const;
std::array<uint8_t, 192> toAffineBytesLE(const bool raw = false) const;
std::array<uint8_t, 288> toJacobianBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 288> toJacobianBytesLE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 192> toAffineBytesBE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 192> toAffineBytesLE(const from_mont fm = from_mont::yes) const;
std::array<uint8_t, 96> toCompressedBytesBE() const;
static g2 zero();
static g2 one();
Expand Down
Loading

0 comments on commit 382cb5f

Please sign in to comment.