2016-05-04 17:25:42 -07:00
|
|
|
#include "Note.hpp"
|
|
|
|
#include "prf.h"
|
|
|
|
#include "crypto/sha256.h"
|
|
|
|
|
|
|
|
#include "version.h"
|
|
|
|
#include "streams.h"
|
|
|
|
|
2016-05-12 12:19:13 -07:00
|
|
|
#include "zcash/util.h"
|
|
|
|
|
2016-05-04 17:25:42 -07:00
|
|
|
namespace libzcash {
|
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
SproutNote::SproutNote() {
|
2016-05-04 17:25:42 -07:00
|
|
|
a_pk = random_uint256();
|
|
|
|
rho = random_uint256();
|
|
|
|
r = random_uint256();
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
uint256 SproutNote::cm() const {
|
2016-05-04 17:25:42 -07:00
|
|
|
unsigned char discriminant = 0xb0;
|
|
|
|
|
|
|
|
CSHA256 hasher;
|
|
|
|
hasher.Write(&discriminant, 1);
|
|
|
|
hasher.Write(a_pk.begin(), 32);
|
|
|
|
|
2018-04-25 13:32:13 -07:00
|
|
|
auto value_vec = convertIntToVectorLE(value_);
|
2016-05-04 17:25:42 -07:00
|
|
|
|
|
|
|
hasher.Write(&value_vec[0], value_vec.size());
|
|
|
|
hasher.Write(rho.begin(), 32);
|
|
|
|
hasher.Write(r.begin(), 32);
|
|
|
|
|
|
|
|
uint256 result;
|
|
|
|
hasher.Finalize(result.begin());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
uint256 SproutNote::nullifier(const SpendingKey& a_sk) const {
|
2016-05-04 17:25:42 -07:00
|
|
|
return PRF_nf(a_sk, rho);
|
|
|
|
}
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
SproutNotePlaintext::SproutNotePlaintext(
|
2018-04-24 21:20:01 -07:00
|
|
|
const SproutNote& note,
|
2016-05-11 11:58:49 -07:00
|
|
|
boost::array<unsigned char, ZC_MEMO_SIZE> memo) : memo(memo)
|
2016-05-04 17:25:42 -07:00
|
|
|
{
|
2018-04-25 13:32:13 -07:00
|
|
|
value = note.value();
|
2016-05-04 17:25:42 -07:00
|
|
|
rho = note.rho;
|
|
|
|
r = note.r;
|
|
|
|
}
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
SproutNote SproutNotePlaintext::note(const PaymentAddress& addr) const
|
2016-05-04 17:25:42 -07:00
|
|
|
{
|
2018-04-24 21:20:01 -07:00
|
|
|
return SproutNote(addr.a_pk, value, rho, r);
|
2016-05-04 17:25:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
SproutNotePlaintext SproutNotePlaintext::decrypt(const ZCNoteDecryption& decryptor,
|
2016-05-04 17:25:42 -07:00
|
|
|
const ZCNoteDecryption::Ciphertext& ciphertext,
|
|
|
|
const uint256& ephemeralKey,
|
|
|
|
const uint256& h_sig,
|
|
|
|
unsigned char nonce
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto plaintext = decryptor.decrypt(ciphertext, ephemeralKey, h_sig, nonce);
|
|
|
|
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ss << plaintext;
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
SproutNotePlaintext ret;
|
2016-05-04 17:25:42 -07:00
|
|
|
ss >> ret;
|
|
|
|
|
|
|
|
assert(ss.size() == 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
ZCNoteEncryption::Ciphertext SproutNotePlaintext::encrypt(ZCNoteEncryption& encryptor,
|
2016-05-04 17:25:42 -07:00
|
|
|
const uint256& pk_enc
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ss << (*this);
|
|
|
|
|
|
|
|
ZCNoteEncryption::Plaintext pt;
|
|
|
|
|
|
|
|
assert(pt.size() == ss.size());
|
|
|
|
|
|
|
|
memcpy(&pt[0], &ss[0], pt.size());
|
|
|
|
|
|
|
|
return encryptor.encrypt(pk_enc, pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|