2016-03-31 21:18:36 -07:00
|
|
|
/*
|
|
|
|
See the Zcash protocol specification for more information.
|
|
|
|
https://github.com/zcash/zips/blob/master/protocol/protocol.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZC_NOTE_ENCRYPTION_H_
|
|
|
|
#define ZC_NOTE_ENCRYPTION_H_
|
|
|
|
|
|
|
|
#include <boost/array.hpp>
|
|
|
|
#include "uint256.h"
|
2016-05-16 08:50:31 -07:00
|
|
|
#include "uint252.h"
|
2016-03-31 21:18:36 -07:00
|
|
|
|
2016-05-04 17:25:38 -07:00
|
|
|
#include "zcash/Zcash.h"
|
2016-03-31 21:18:36 -07:00
|
|
|
|
|
|
|
namespace libzcash {
|
|
|
|
|
|
|
|
#define NOTEENCRYPTION_AUTH_BYTES 16
|
|
|
|
|
|
|
|
template<size_t MLEN>
|
|
|
|
class NoteEncryption {
|
|
|
|
protected:
|
|
|
|
enum { CLEN=MLEN+NOTEENCRYPTION_AUTH_BYTES };
|
|
|
|
uint256 epk;
|
|
|
|
uint256 esk;
|
|
|
|
unsigned char nonce;
|
|
|
|
uint256 hSig;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef boost::array<unsigned char, CLEN> Ciphertext;
|
|
|
|
typedef boost::array<unsigned char, MLEN> Plaintext;
|
|
|
|
|
|
|
|
NoteEncryption(uint256 hSig);
|
|
|
|
|
|
|
|
// Gets the ephemeral public key
|
|
|
|
uint256 get_epk() {
|
|
|
|
return epk;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypts `message` with `pk_enc` and returns the ciphertext.
|
2016-08-18 14:51:15 -07:00
|
|
|
// This is only called ZC_NUM_JS_OUTPUTS times for a given instantiation;
|
2016-08-11 11:48:47 -07:00
|
|
|
// but can be called 255 times before the nonce-space runs out.
|
2016-03-31 21:18:36 -07:00
|
|
|
Ciphertext encrypt(const uint256 &pk_enc,
|
|
|
|
const Plaintext &message
|
|
|
|
);
|
|
|
|
|
|
|
|
// Creates a NoteEncryption private key
|
2016-05-16 08:50:31 -07:00
|
|
|
static uint256 generate_privkey(const uint252 &a_sk);
|
2016-03-31 21:18:36 -07:00
|
|
|
|
|
|
|
// Creates a NoteEncryption public key from a private key
|
|
|
|
static uint256 generate_pubkey(const uint256 &sk_enc);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t MLEN>
|
|
|
|
class NoteDecryption {
|
|
|
|
protected:
|
|
|
|
enum { CLEN=MLEN+NOTEENCRYPTION_AUTH_BYTES };
|
|
|
|
uint256 sk_enc;
|
|
|
|
uint256 pk_enc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef boost::array<unsigned char, CLEN> Ciphertext;
|
|
|
|
typedef boost::array<unsigned char, MLEN> Plaintext;
|
|
|
|
|
2016-08-23 20:50:45 -07:00
|
|
|
// Unused default constructor to make allocators happy
|
|
|
|
NoteDecryption() { }
|
2016-03-31 21:18:36 -07:00
|
|
|
NoteDecryption(uint256 sk_enc);
|
|
|
|
|
|
|
|
Plaintext decrypt(const Ciphertext &ciphertext,
|
|
|
|
const uint256 &epk,
|
|
|
|
const uint256 &hSig,
|
|
|
|
unsigned char nonce
|
|
|
|
) const;
|
2016-08-23 20:50:45 -07:00
|
|
|
|
2016-08-30 23:57:32 -07:00
|
|
|
friend inline bool operator==(const NoteDecryption& a, const NoteDecryption& b) { return a.sk_enc == b.sk_enc && a.pk_enc == b.pk_enc; }
|
2016-08-23 20:50:45 -07:00
|
|
|
friend inline bool operator<(const NoteDecryption& a, const NoteDecryption& b) { return a.pk_enc < b.pk_enc; }
|
2016-03-31 21:18:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
uint256 random_uint256();
|
2016-05-16 08:50:31 -07:00
|
|
|
uint252 random_uint252();
|
2016-03-31 21:18:36 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:54:42 -07:00
|
|
|
typedef libzcash::NoteEncryption<ZC_NOTEPLAINTEXT_SIZE> ZCNoteEncryption;
|
|
|
|
typedef libzcash::NoteDecryption<ZC_NOTEPLAINTEXT_SIZE> ZCNoteDecryption;
|
2016-03-31 21:18:36 -07:00
|
|
|
|
2016-05-04 17:25:38 -07:00
|
|
|
#endif /* ZC_NOTE_ENCRYPTION_H_ */
|