Skip to content

Commit

Permalink
Add a signature based attester
Browse files Browse the repository at this point in the history
  • Loading branch information
roshanr95 committed Dec 1, 2020
1 parent 49c56d5 commit f4bcfc7
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions pubsub/include/marlin/pubsub/attestation/SigAttester.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef MARLIN_PUBSUB_ATTESTATION_SIGATTESTER_HPP
#define MARLIN_PUBSUB_ATTESTATION_SIGATTESTER_HPP

#include <stdint.h>
#include <marlin/core/WeakBuffer.hpp>
#include <ctime>
#include <optional>

#include <secp256k1_recovery.h>
#include <cryptopp/keccak.h>


namespace marlin {
namespace pubsub {

struct SigAttester {
secp256k1_context* ctx_signer = nullptr;
secp256k1_context* ctx_verifier = nullptr;
uint8_t key[32];

SigAttester() {
ctx_signer = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
ctx_verifier = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);

do {
CryptoPP::OS_GenerateRandomBlock(false, key, 32);
} while(
secp256k1_ec_seckey_verify(ctx_verifier, key) != 1
);
}

~SigAttester() {
secp256k1_context_destroy(ctx_signer);
secp256k1_context_destroy(ctx_verifier);
}

template<typename HeaderType>
constexpr uint64_t attestation_size(
uint64_t,
uint16_t,
uint8_t const*,
uint64_t,
HeaderType
) {
return 67;
}

template<typename HeaderType>
int attest(
uint64_t message_id,
uint16_t channel,
uint8_t const* message_data,
uint64_t message_size,
HeaderType prev_header,
core::Buffer& out,
uint64_t offset = 0
) {
if(prev_header.attestation_size != 0) {
// FIXME: should probably add _unsafe to function
out.write_unsafe(offset, prev_header.attestation_data, prev_header.attestation_size);
return 1;
}

out.write_uint16_be_unsafe(offset, 67);

uint8_t hash[32];
CryptoPP::Keccak_256 hasher;
// Hash message
hasher.CalculateTruncatedDigest(hash, 32, message_data, message_size);

// Get key
if(key == nullptr) {
return -2;
}

// Sign
secp256k1_ecdsa_recoverable_signature sig;
auto res = secp256k1_ecdsa_sign_recoverable(
ctx_signer,
&sig,
hash,
key,
nullptr,
nullptr
);

if(res == 0) {
// Sign failed
return -3;
}

// Output
int recid;
secp256k1_ecdsa_recoverable_signature_serialize_compact(
ctx_signer,
out.data()+offset+2,
&recid,
&sig
);

out.data()[offset+66] = (uint8_t)recid;

return 0;
}

template<typename HeaderType>
bool verify(
uint64_t,
uint16_t,
uint8_t const*,
uint64_t,
HeaderType
) {
return true;
}

std::optional<uint64_t> parse_size(core::Buffer&, uint64_t = 0) {
return 67;
}
};

} // namespace pubsub
} // namespace marlin

#endif // MARLIN_PUBSUB_ATTESTATION_SIGATTESTER_HPP

0 comments on commit f4bcfc7

Please sign in to comment.