Skip to content

Commit

Permalink
update after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mschoenebeck committed Jun 7, 2023
1 parent edffafc commit bcf16d6
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 128 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,40 @@ Chia Network's [bls-signatures](https://github.com/Chia-Network/bls-signatures)
```
Chia Network's library: This library (cpu-ext-ct, asm): This library (cpu-ext-rt, asm): This library (no cpu-ext, asm): This library (no cpu-ext, cpp):
Signing Signing Signing Signing Signing
Total: 5000 runs in 5447 ms Total: 5000 runs in 4623 ms Total: 5000 runs in 4806 ms Total: 5000 runs in 6266 ms Total: 5000 runs in 15713 ms
Avg: 1.0894 ms Avg: 0.9246 ms Avg: 0.9612 ms Avg: 1.2532 ms Avg: 3.1426 ms
Total: 5000 runs in 5447 ms Total: 5000 runs in 4535 ms Total: 5000 runs in 4726 ms Total: 5000 runs in 6160 ms Total: 5000 runs in 8344 ms
Avg: 1.0894 ms Avg: 0.907 ms Avg: 0.9452 ms Avg: 1.232 ms Avg: 1.6688 ms
Verification Verification Verification Verification Verification
Total: 10000 runs in 32285 ms Total: 10000 runs in 24605 ms Total: 10000 runs in 24773 ms Total: 10000 runs in 30844 ms Total: 10000 runs in 77720 ms
Avg: 3.2285 ms Avg: 2.4605 ms Avg: 2.4773 ms Avg: 3.0844 ms Avg: 7.772 ms
Total: 10000 runs in 32285 ms Total: 10000 runs in 19259 ms Total: 10000 runs in 19727 ms Total: 10000 runs in 24561 ms Total: 10000 runs in 33982 ms
Avg: 3.2285 ms Avg: 1.9259 ms Avg: 1.9727 ms Avg: 2.4561 ms Avg: 3.3982 ms
Public key validation Public key validation Public key validation Public key validation Public key validation
Total: 100000 runs in 26464 ms Total: 100000 runs in 2066 ms Total: 100000 runs in 2092 ms Total: 100000 runs in 3071 ms Total: 100000 runs in 8318 ms
Avg: 0.26464 ms Avg: 0.02066 ms Avg: 0.02092 ms Avg: 0.03071 ms Avg: 0.08318 ms
Total: 100000 runs in 26464 ms Total: 100000 runs in 2066 ms Total: 100000 runs in 2092 ms Total: 100000 runs in 3071 ms Total: 100000 runs in 4146 ms
Avg: 0.26464 ms Avg: 0.02066 ms Avg: 0.02092 ms Avg: 0.03071 ms Avg: 0.04146 ms
Signature validation Signature validation Signature validation Signature validation Signature validation
Total: 100000 runs in 28818 ms Total: 100000 runs in 12575 ms Total: 100000 runs in 12665 ms Total: 100000 runs in 17622 ms Total: 100000 runs in 45040 ms
Avg: 0.28818 ms Avg: 0.12575 ms Avg: 0.12665 ms Avg: 0.17622 ms Avg: 0.4504 ms
Total: 100000 runs in 28818 ms Total: 100000 runs in 12474 ms Total: 100000 runs in 12665 ms Total: 100000 runs in 17622 ms Total: 100000 runs in 24255 ms
Avg: 0.28818 ms Avg: 0.12474 ms Avg: 0.12665 ms Avg: 0.17622 ms Avg: 0.24255 ms
Aggregation Aggregation Aggregation Aggregation Aggregation
Total: 100000 runs in 287 ms Total: 100000 runs in 224 ms Total: 100000 runs in 226 ms Total: 100000 runs in 299 ms Total: 100000 runs in 746 ms
Avg: 0.00287 ms Avg: 0.00224 ms Avg: 0.00226 ms Avg: 0.00299 ms Avg: 0.00746 ms
Total: 100000 runs in 287 ms Total: 100000 runs in 224 ms Total: 100000 runs in 226 ms Total: 100000 runs in 299 ms Total: 100000 runs in 410 ms
Avg: 0.00287 ms Avg: 0.00224 ms Avg: 0.00226 ms Avg: 0.00299 ms Avg: 0.0041 ms
Batch verification Batch verification Batch verification Batch verification Batch verification
Total: 100000 runs in 138664 ms Total: 100000 runs in 101991 ms Total: 100000 runs in 105088 ms Total: 100000 runs in 132992 ms Total: 100000 runs in 313394 ms
Avg: 1.38664 ms Avg: 1.01991 ms Avg: 1.05088 ms Avg: 1.32992 ms Avg: 3.13394 ms
Total: 100000 runs in 138664 ms Total: 100000 runs in 91959 ms Total: 100000 runs in 95714 ms Total: 100000 runs in 121560 ms Total: 100000 runs in 164145 ms
Avg: 1.38664 ms Avg: 0.91959 ms Avg: 0.95714 ms Avg: 1.2156 ms Avg: 1.64145 ms
PopScheme Aggregation PopScheme Aggregation PopScheme Aggregation PopScheme Aggregation PopScheme Aggregation
Total: 5000 runs in 14 ms Total: 5000 runs in 11 ms Total: 5000 runs in 11 ms Total: 5000 runs in 14 ms Total: 5000 runs in 37 ms
Avg: 0.0028 ms Avg: 0.0022 ms Avg: 0.0022 ms Avg: 0.0028 ms Avg: 0.0074 ms
Total: 5000 runs in 14 ms Total: 5000 runs in 11 ms Total: 5000 runs in 11 ms Total: 5000 runs in 14 ms Total: 5000 runs in 21 ms
Avg: 0.0028 ms Avg: 0.0022 ms Avg: 0.0022 ms Avg: 0.0028 ms Avg: 0.0042 ms
PopScheme Proofs verification PopScheme Proofs verification PopScheme Proofs verification PopScheme Proofs verification PopScheme Proofs verification
Total: 5000 runs in 16567 ms Total: 5000 runs in 13017 ms Total: 5000 runs in 13231 ms Total: 5000 runs in 16729 ms Total: 5000 runs in 41583 ms
Avg: 3.3134 ms Avg: 2.6034 ms Avg: 2.6462 ms Avg: 3.3458 ms Avg: 8.3166 ms
Total: 5000 runs in 16567 ms Total: 5000 runs in 10315 ms Total: 5000 runs in 10531 ms Total: 5000 runs in 13400 ms Total: 5000 runs in 18593 ms
Avg: 3.3134 ms Avg: 2.0630 ms Avg: 2.1062 ms Avg: 2.6800 ms Avg: 3.7186 ms
PopScheme verification PopScheme verification PopScheme verification PopScheme verification PopScheme verification
Total: 5000 runs in 10 ms Total: 5000 runs in 5 ms Total: 5000 runs in 6 ms Total: 5000 runs in 8 ms Total: 5000 runs in 20 ms
Avg: 0.0020 ms Avg: 0.0010 ms Avg: 0.0012 ms Avg: 0.0016 ms Avg: 0.0040 ms
Total: 5000 runs in 10 ms Total: 5000 runs in 5 ms Total: 5000 runs in 6 ms Total: 5000 runs in 7 ms Total: 5000 runs in 10 ms
Avg: 0.0020 ms Avg: 0.0010 ms Avg: 0.0012 ms Avg: 0.0014 ms Avg: 0.0020 ms
```

## Clean
Expand Down
7 changes: 6 additions & 1 deletion include/fp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class fp
uint64_t mul2();
fp toMont() const;
fp fromMont() const;
fp phi() const;
template<size_t N> fp exp(const std::array<uint64_t, N>& s) const;
fp inverse() const;
bool sqrt(fp& c) const;
Expand All @@ -53,7 +54,8 @@ class fp
static const fp R2; // fp identity squared: R2 = 2^(384*2) mod p
static const fp B; // B coefficient from cure equation: y^2 = x^3 + B
static const fp twoInv;
static const std::array<uint64_t, 4> Q; // scalar field modulus: q = 52435875175126190479447740508185965837690552500527637822603658699938581184513 or 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001
static const fp glvPhi1; // glvPhi1 ^ 3 = 1
static const std::array<uint64_t, 4> Q; // scalar field modulus: q = 52435875175126190479447740508185965837690552500527637822603658699938581184513 or 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001
static const std::array<uint64_t, 6> pPlus1Over4;
static const std::array<uint64_t, 6> pMinus1Over2;
static const std::array<uint64_t, 6> pMinus3Over4;
Expand Down Expand Up @@ -92,6 +94,7 @@ class fp2
fp2 sub(const fp2& e) const;
void subAssign(const fp2& e);
fp2 neg() const;
fp2 conj() const;
fp2 mul(const fp2& e) const;
void mulAssign(const fp2& e);
fp2 square() const;
Expand All @@ -109,6 +112,8 @@ class fp2

static const fp2 negativeOne2;
static const fp2 B;
static const fp2 psiX;
static const fp2 psiY;
};

// element representation of 'fp6' field which is cubic extension of 'fp2' field
Expand Down
3 changes: 2 additions & 1 deletion include/g.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class g1
g1 sub(const g1& e) const;
template<size_t N> g1 mulScalar(const std::array<uint64_t, N>& s) const;
g1 clearCofactor() const;
g1 glvEndomorphism() const;
static g1 multiExp(const std::vector<g1>& points, const std::vector<std::array<uint64_t, 4>>& scalars);
static g1 mapToCurve(const fp& e);
static std::tuple<fp, fp> swuMapG1(const fp& e);
Expand Down Expand Up @@ -104,12 +105,12 @@ class g2
g2 dbl() const;
g2 neg() const;
g2 sub(const g2& e) const;
g2 psi() const;
template<size_t N> g2 mulScalar(const std::array<uint64_t, N>& s) const;
g2 clearCofactor() const;
g2 frobeniusMap(int64_t power) const;
static g2 multiExp(const std::vector<g2>& points, const std::vector<std::array<uint64_t, 4>>& scalars);
static g2 mapToCurve(const fp2& e);
static g2 fromMessage(const std::vector<uint8_t>& msg, const std::string& dst);
static std::tuple<fp2, fp2> swuMapG2(const fp2& e);
//static void isogenyMapG2(fp2& x, fp2& y);
g2 isogenyMap() const;
Expand Down
14 changes: 14 additions & 0 deletions include/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ int64_t cmp(const std::array<uint64_t, N>& a, const std::array<uint64_t, N>& b)
return 0;
}

// checks two std::arrays for equality: returns true if a == b, false otherwise.
template<size_t N>
bool equal(const std::array<uint64_t, N>& a, const std::array<uint64_t, N>& b)
{
for(uint64_t i = 0; i < N; i++)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}

// returns the length of the absolute value of s in bits. The bit length of 0 is 0.
template<size_t N>
uint64_t bitLength(const std::array<uint64_t, N>& s)
Expand Down
7 changes: 6 additions & 1 deletion include/signatures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ std::array<uint64_t, 4> derive_child_sk_unhardened(
uint32_t index
);

g1 derive_shild_g1_unhardened(
g1 derive_child_g1_unhardened(
const g1& pk,
uint32_t index
);
Expand All @@ -105,6 +105,11 @@ std::array<uint64_t, 4> secret_key(const std::vector<uint8_t>& seed);
// Derive public key from a BLS private key
g1 public_key(const std::array<uint64_t, 4>& sk);

g2 fromMessage(
const std::vector<uint8_t>& msg,
const std::string& dst
);

// Sign message with a private key
g2 sign(
const std::array<uint64_t, 4>& sk,
Expand Down
17 changes: 7 additions & 10 deletions src/arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "../include/bls12_381.hpp"
#include <bls12_381.hpp>
#ifdef __x86_64__
#include <cpuid.h>
#endif

using namespace std;

Expand Down Expand Up @@ -2054,11 +2056,8 @@ tuple<uint64_t, uint64_t> Add64(
const uint64_t& carry
)
{
uint64_t sum = x + y + carry;
// The sum will overflow if both top bits are set (x & y) or if one of them
// is (x | y), and a carry from the lower place happened. If such a carry
// happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum).
uint64_t carryOut = ((x & y) | ((x | y) & ~sum)) >> 63;
uint64_t sum = (x + y) + carry;
uint64_t carryOut = (sum < x);
return {sum, carryOut};
}

Expand All @@ -2073,10 +2072,8 @@ tuple<uint64_t, uint64_t> Sub64(
const uint64_t& borrow
)
{
uint64_t diff, borrowOut;
diff = x - y - borrow;
// See Sub32 for the bit logic.
borrowOut = ((~x & y) | (~(x ^ y) & diff)) >> 63;
uint64_t diff = x - y - borrow;
uint64_t borrowOut = (diff > x);
return {diff, borrowOut};
}

Expand Down
Loading

0 comments on commit bcf16d6

Please sign in to comment.