blob: 7af9d8ceb874ccce57cd8069cc9bd87857647408 (
plain) (
tree)
|
|
/**
* Copyright 2018 VMware
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _HOTSTUFF_CRYPTO_H
#define _HOTSTUFF_CRYPTO_H
#include <openssl/rand.h>
#include "secp256k1.h"
#include "salticidae/crypto.h"
#include "hotstuff/type.h"
#include "hotstuff/task.h"
namespace hotstuff {
using salticidae::SHA256;
class PubKey: public Serializable, Cloneable {
public:
virtual ~PubKey() = default;
virtual PubKey *clone() override = 0;
};
using pubkey_bt = BoxObj<PubKey>;
class PrivKey: public Serializable {
public:
virtual ~PrivKey() = default;
virtual pubkey_bt get_pubkey() const = 0;
virtual void from_rand() = 0;
};
using privkey_bt = BoxObj<PrivKey>;
class PartCert: public Serializable, public Cloneable {
public:
virtual ~PartCert() = default;
virtual promise_t verify(const PubKey &pubkey, VeriPool &vpool) = 0;
virtual bool verify(const PubKey &pubkey) = 0;
virtual const uint256_t &get_blk_hash() const = 0;
virtual PartCert *clone() override = 0;
};
class ReplicaConfig;
class QuorumCert: public Serializable, public Cloneable {
public:
virtual ~QuorumCert() = default;
virtual void add_part(ReplicaID replica, const PartCert &pc) = 0;
virtual void compute() = 0;
virtual promise_t verify(const ReplicaConfig &config, VeriPool &vpool) = 0;
virtual bool verify(const ReplicaConfig &config) = 0;
virtual const uint256_t &get_blk_hash() const = 0;
virtual QuorumCert *clone() override = 0;
};
using part_cert_bt = BoxObj<PartCert>;
using quorum_cert_bt = BoxObj<QuorumCert>;
class PubKeyDummy: public PubKey {
PubKeyDummy *clone() override { return new PubKeyDummy(*this); }
void serialize(DataStream &) const override {}
void unserialize(DataStream &) override {}
};
class PrivKeyDummy: public PrivKey {
pubkey_bt get_pubkey() const override { return new PubKeyDummy(); }
void serialize(DataStream &) const override {}
void unserialize(DataStream &) override {}
void from_rand() override {}
};
class PartCertDummy: public PartCert {
uint256_t blk_hash;
public:
PartCertDummy() {}
PartCertDummy(const uint256_t &blk_hash):
blk_hash(blk_hash) {}
void serialize(DataStream &s) const override {
s << (uint32_t)0 << blk_hash;
}
void unserialize(DataStream &s) override {
uint32_t tmp;
s >> tmp >> blk_hash;
}
PartCert *clone() override {
return new PartCertDummy(blk_hash);
}
bool verify(const PubKey &) override { return true; }
promise_t verify(const PubKey &, VeriPool &) override {
return promise_t([](promise_t &pm){ pm.resolve(true); });
}
const uint256_t &get_blk_hash() const override { return blk_hash; }
};
class QuorumCertDummy: public QuorumCert {
uint256_t blk_hash;
public:
QuorumCertDummy() {}
QuorumCertDummy(const ReplicaConfig &, const uint256_t &blk_hash):
blk_hash(blk_hash) {}
void serialize(DataStream &s) const override {
s << (uint32_t)1 << blk_hash;
}
void unserialize(DataStream &s) override {
uint32_t tmp;
s >> tmp >> blk_hash;
}
QuorumCert *clone() override {
return new QuorumCertDummy(*this);
}
void add_part(ReplicaID, const PartCert &) override {}
void compute() override {}
bool verify(const ReplicaConfig &) override { return true; }
promise_t verify(const ReplicaConfig &, VeriPool &) override {
return promise_t([](promise_t &pm) { pm.resolve(true); });
}
const uint256_t &get_blk_hash() const override { return blk_hash; }
};
class Secp256k1Context {
secp256k1_context *ctx;
friend class PubKeySecp256k1;
friend class SigSecp256k1;
public:
Secp256k1Context(bool sign = false
|