Skip to content

Commit

Permalink
BLS WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
guidovranken committed Apr 19, 2021
1 parent 744dc46 commit f658bbb
Show file tree
Hide file tree
Showing 34 changed files with 3,466 additions and 188 deletions.
120 changes: 120 additions & 0 deletions components.cpp
Expand Up @@ -262,6 +262,29 @@ bool Bignum::IsNegative(void) const {
return data.GetSize() && data.GetConstVectorPtr()[0] == '-';
}

bool Bignum::IsGreaterThan(const std::string& other) const {
CF_ASSERT(IsNegative() == false, "IsGreaterThan on negative numbers not supported");
const auto s = ToTrimmedString();
if ( s.size() > other.size() ) {
return true;
} else if ( s.size() < other.size() ) {
return false;
} else {
for (size_t i = 0; i < s.size(); i++) {
const int a = s[i];
const int b = other[i];
if ( a > b ) {
return true;
} else if ( a < b ) {
return false;
}
}
}

CF_ASSERT(s == other, "Logic error");
return false;
}

bool Bignum::IsLessThan(const std::string& other) const {
boost::multiprecision::cpp_int A(ToTrimmedString());
boost::multiprecision::cpp_int B(other);
Expand Down Expand Up @@ -492,13 +515,110 @@ void MACType::Serialize(Datasource& ds) const {
type.Serialize(ds);
}

G2::G2(nlohmann::json json) :
first(json[0]),
second(json[1]) {
}

nlohmann::json G2::ToJSON(void) const {
return std::vector<nlohmann::json>{
first.first.ToJSON(), first.second.ToJSON(),
second.first.ToJSON(), second.second.ToJSON()
};
}

void G2::Serialize(Datasource& ds) const {
first.Serialize(ds);
second.Serialize(ds);
}

/* BLS_Signature */
BLS_Signature::BLS_Signature(Datasource& ds) :
signature(ds),
pub(ds)
{ }

BLS_Signature::BLS_Signature(G2 signature, ECC_PublicKey pub) :
signature(signature),
pub(pub)
{ }

BLS_Signature::BLS_Signature(nlohmann::json json) :
signature(json["signature"]),
pub(json["pub"])
{ }

bool BLS_Signature::operator==(const BLS_Signature& rhs) const {
return
(signature == rhs.signature) &&
(pub == rhs.pub);
}

void BLS_Signature::Serialize(Datasource& ds) const {
signature.Serialize(ds);
pub.Serialize(ds);
}

nlohmann::json BLS_Signature::ToJSON(void) const {
return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
}

/* BLS_KeyPair */

BLS_KeyPair::BLS_KeyPair(Datasource& ds) :
priv(ds),
pub(ds)
{ }

BLS_KeyPair::BLS_KeyPair(BLS_PrivateKey priv, BignumPair pub) :
priv(priv),
pub(pub)
{ }

bool BLS_KeyPair::operator==(const BLS_KeyPair& rhs) const {
return
(priv == rhs.priv) &&
(pub == rhs.pub);
}

void BLS_KeyPair::Serialize(Datasource& ds) const {
priv.Serialize(ds);
pub.Serialize(ds);
}

nlohmann::json BLS_KeyPair::ToJSON(void) const {
return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()};
}

/* BLS_PairingComponents */

BLS_PairingComponents::BLS_PairingComponents(Datasource& ds) {
const auto num = ds.Get<uint32_t>(0);
for (size_t i = 0; i < num; i++) {
c.push_back( Component{{ds}, {ds}, {ds}, {ds}} );
}
}

BLS_PairingComponents::BLS_PairingComponents(nlohmann::json json) {
for (const auto& j : json) {
c.push_back( Component{
{j["sig_v"], j["sig_w"], j["sig_x"], j["sig_y"]},
{j["pub_x"], j["pub_y"]},
{j["msg"]},
{j["aug"]}});
}
}

void BLS_PairingComponents::Serialize(Datasource& ds) const {
ds.Put<uint32_t>(c.size());
for (const auto& component : c) {
component.sig.Serialize(ds);
component.pub.Serialize(ds);
component.msg.Serialize(ds);
component.aug.Serialize(ds);
}
}

/* SR25519_Signature */
SR25519_Signature::SR25519_Signature(Datasource& ds) :
signature(ds),
Expand Down
1 change: 1 addition & 0 deletions config.h
Expand Up @@ -4,5 +4,6 @@ namespace cryptofuzz {
namespace config {
constexpr size_t kMaxBignumSize = 4000;
constexpr bool kNegativeIntegers = false;
constexpr size_t kMutatorPoolSize = 64;
} /* namespace config */
} /* namespace cryptofuzz */
8 changes: 8 additions & 0 deletions docs/blst.md
@@ -0,0 +1,8 @@
```
git clone --depth 1 https://github.com/supranational/blst
cd blst/
./build.sh
export BLST_LIBBLST_A_PATH=$(realpath libblst.a)
export BLST_INCLUDE_PATH=$(realpath bindings/)
export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_BLST"
```
2 changes: 1 addition & 1 deletion docs/chia_bls.md
Expand Up @@ -4,7 +4,7 @@ cd bls-signatures/
mkdir build/
cd build/
cmake ..
make -j$(nproc) bls/
make -j$(nproc)
export CHIA_BLS_LIBBLS_A_PATH=$(realpath libbls.a)
export CHIA_BLS_INCLUDE_PATH=$(realpath ../src/)
export CHIA_BLS_RELIC_INCLUDE_PATH_1=$(realpath _deps/relic-build/include/)
Expand Down
13 changes: 13 additions & 0 deletions docs/mcl.md
@@ -0,0 +1,13 @@
```
git clone --depth 1 https://github.com/herumi/mcl.git
cd mcl/
mkdir build/
cd build/
cmake .. -DMCL_STATIC_LIB=on
make
export MCL_INCLUDE_PATH=$(realpath ../include/)
export MCL_LIBMCL_A_PATH=$(realpath lib/libmcl.a)
export MCL_LIBMCLBN384_A_PATH=$(realpath lib/libmclbn384.a)
export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_MCL"
export LINK_FLAGS="$LINK_FLAGS -lgmp"
```
24 changes: 24 additions & 0 deletions driver.cpp
Expand Up @@ -43,12 +43,18 @@ void Driver::Run(const uint8_t* data, const size_t size) const {
static ExecutorDH_GenerateKeyPair executorDH_GenerateKeyPair(CF_OPERATION("DH_GenerateKeyPair"), modules, options);
static ExecutorDH_Derive executorDH_Derive(CF_OPERATION("DH_Derive"), modules, options);
static ExecutorBignumCalc executorBignumCalc(CF_OPERATION("BignumCalc"), modules, options);
static ExecutorBignumCalc_Mod_BLS12_381_R executorBignumCalc_mod_bls12_381_r(CF_OPERATION("BignumCalc_Mod_BLS12_381_R"), modules, options);
static ExecutorBignumCalc_Mod_BLS12_381_P executorBignumCalc_mod_bls12_381_p(CF_OPERATION("BignumCalc_Mod_BLS12_381_P"), modules, options);
static ExecutorBLS_PrivateToPublic executorBLS_PrivateToPublic(CF_OPERATION("BLS_PrivateToPublic"), modules, options);
static ExecutorBLS_Sign executorBLS_Sign(CF_OPERATION("BLS_Sign"), modules, options);
static ExecutorBLS_Verify executorBLS_Verify(CF_OPERATION("BLS_Verify"), modules, options);
static ExecutorBLS_Pairing executorBLS_Pairing(CF_OPERATION("BLS_Pairing"), modules, options);
static ExecutorBLS_HashToG1 executorBLS_HashToG1(CF_OPERATION("BLS_HashToG1"), modules, options);
static ExecutorBLS_HashToG2 executorBLS_HashToG2(CF_OPERATION("BLS_HashToG2"), modules, options);
static ExecutorBLS_IsG1OnCurve executorBLS_IsG1OnCurve(CF_OPERATION("BLS_IsG1OnCurve"), modules, options);
static ExecutorBLS_IsG2OnCurve executorBLS_IsG2OnCurve(CF_OPERATION("BLS_IsG2OnCurve"), modules, options);
static ExecutorBLS_GenerateKeyPair executorBLS_GenerateKeyPair(CF_OPERATION("BLS_GenerateKeyPair"), modules, options);
static ExecutorMisc executorMisc(CF_OPERATION("Misc"), modules, options);
static ExecutorSR25519_Verify executorSR25519_Verify(CF_OPERATION("SR25519_Verify"), modules, options);

try {
Expand Down Expand Up @@ -145,6 +151,12 @@ void Driver::Run(const uint8_t* data, const size_t size) const {
case CF_OPERATION("BignumCalc"):
executorBignumCalc.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BignumCalc_Mod_BLS12_381_R"):
executorBignumCalc_mod_bls12_381_r.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BignumCalc_Mod_BLS12_381_P"):
executorBignumCalc_mod_bls12_381_p.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BLS_PrivateToPublic"):
executorBLS_PrivateToPublic.Run(ds, payload.data(), payload.size());
break;
Expand All @@ -163,6 +175,18 @@ void Driver::Run(const uint8_t* data, const size_t size) const {
case CF_OPERATION("BLS_HashToG2"):
executorBLS_HashToG2.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BLS_IsG1OnCurve"):
executorBLS_IsG1OnCurve.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BLS_IsG2OnCurve"):
executorBLS_IsG2OnCurve.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("BLS_GenerateKeyPair"):
executorBLS_GenerateKeyPair.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("Misc"):
executorMisc.Run(ds, payload.data(), payload.size());
break;
case CF_OPERATION("SR25519_Verify"):
executorSR25519_Verify.Run(ds, payload.data(), payload.size());
break;
Expand Down
16 changes: 16 additions & 0 deletions entry.cpp
Expand Up @@ -190,6 +190,14 @@
#include <modules/schnorrkel/module.h>
#endif

#if defined(CRYPTOFUZZ_BLST)
#include <modules/blst/module.h>
#endif

#if defined(CRYPTOFUZZ_MCL)
#include <modules/mcl/module.h>
#endif

std::shared_ptr<cryptofuzz::Driver> driver = nullptr;

const cryptofuzz::Options* cryptofuzz_options = nullptr;
Expand Down Expand Up @@ -402,6 +410,14 @@ extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
driver->LoadModule( std::make_shared<cryptofuzz::module::schnorrkel>() );
#endif

#if defined(CRYPTOFUZZ_BLST)
driver->LoadModule( std::make_shared<cryptofuzz::module::blst>() );
#endif

#if defined(CRYPTOFUZZ_MCL)
driver->LoadModule( std::make_shared<cryptofuzz::module::mcl>() );
#endif

/* TODO check if options.forceModule (if set) refers to a module that is
* actually loaded, warn otherwise.
*/
Expand Down

0 comments on commit f658bbb

Please sign in to comment.