#ifndef SIGSUM_CRYPTO_H_INCLUDED
#define SIGSUM_CRYPTO_H_INCLUDED

#include <stdint.h>
#include <stdlib.h>

typedef uint64_t sigsum_size_t;

/* Sigsum uses SHA256 hashing. */
#define SIGSUM_DIGEST_SIZE 32

struct sigsum_hash
{
  uint8_t h[SIGSUM_DIGEST_SIZE];
};

/* Sigsum uses Ed25519 signatures. */
#define SIGSUM_PUBKEY_SIZE 32
#define SIGSUM_SIGNATURE_SIZE 64

struct sigsum_pubkey
{
  uint8_t k[SIGSUM_PUBKEY_SIZE];
};

struct sigsum_signature
{
  uint8_t s[SIGSUM_SIGNATURE_SIZE];
};

struct sigsum_cosignature
{
  uint64_t timestamp; /* At most 63 bits. */
  struct sigsum_hash key_hash;
  struct sigsum_signature signature;
};

/* A public key and its hash. Intended to aid lookup of keys by hash,
   e.g., to match a cosignature key hash to the corresponding witness
   public key. */
struct sigsum_key_with_hash
{
  struct sigsum_pubkey key;
  struct sigsum_hash hash;
};

struct sigsum_key_map
{
  size_t num_keys;
  /* Entries must be sorted lexicographically by key hash. */
  const struct sigsum_key_with_hash *keys;
};

/* Looks up a public key by key hash. Returns NULL if not found. If
   idx is non-NULL, matching index is stored there on success. */
const struct sigsum_pubkey *
sigsum_key_map_lookup (const struct sigsum_key_map *map,
		       const struct sigsum_hash *key_hash,
		       size_t *idx);

/* Computes the key hash for a public key. */
void
sigsum_hash_key (struct sigsum_hash *kh, const struct sigsum_pubkey *key);

#endif /* SIGSUM_CRYPTO_H_INCLUDED */
