2 *****************************************************************************
4 Implementation of interfaces for the classes Address and PublicAddress.
8 *****************************************************************************
9 * @author This file is part of libzerocash, developed by the Zerocash
10 * project and contributors (see AUTHORS).
11 * @copyright MIT license (see LICENSE file)
12 *****************************************************************************/
14 #include <cryptopp/osrng.h>
15 using CryptoPP::AutoSeededRandomPool;
17 #include <cryptopp/eccrypto.h>
19 using CryptoPP::ECIES;
21 #include <cryptopp/oids.h>
22 namespace ASN1 = CryptoPP::ASN1;
24 #include <cryptopp/filters.h>
25 using CryptoPP::StringSink;
26 using CryptoPP::StringStore;
31 namespace libzerocash {
33 PrivateAddress::PrivateAddress(const std::vector<unsigned char> a_sk, const std::string sk_enc) {
35 this->sk_enc = sk_enc;
38 PrivateAddress::PrivateAddress() {
42 bool PrivateAddress::operator==(const PrivateAddress& rhs) const {
43 return ((this->a_sk == rhs.a_sk) && (this->sk_enc == rhs.sk_enc));
46 bool PrivateAddress::operator!=(const PrivateAddress& rhs) const {
47 return !(*this == rhs);
50 const std::string PrivateAddress::getEncryptionSecretKey() const {
54 const std::vector<unsigned char>& PrivateAddress::getAddressSecret() const {
58 PublicAddress::PublicAddress(): a_pk(ZC_A_PK_SIZE) {
62 PublicAddress::PublicAddress(const std::vector<unsigned char>& a_pk, std::string& pk_enc) : a_pk(a_pk), pk_enc(pk_enc) {}
64 PublicAddress::PublicAddress(const PrivateAddress& addr_sk): a_pk(ZC_A_PK_SIZE) {
65 std::vector<bool> a_sk_bool(ZC_A_SK_SIZE * 8);
66 convertBytesVectorToVector(addr_sk.getAddressSecret(), a_sk_bool);
68 std::vector<bool> zeros_256(256, 0);
70 std::vector<bool> a_pk_internal;
71 concatenateVectors(a_sk_bool, zeros_256, a_pk_internal);
73 std::vector<bool> a_pk_bool(ZC_A_PK_SIZE * 8);
74 hashVector(a_pk_internal, a_pk_bool);
76 convertVectorToBytesVector(a_pk_bool, this->a_pk);
78 ECIES<ECP>::PublicKey publicKey;
80 ECIES<ECP>::PrivateKey decodedPrivateKey;
81 decodedPrivateKey.Load(StringStore(addr_sk.getEncryptionSecretKey()).Ref());
83 decodedPrivateKey.MakePublicKey(publicKey);
85 std::string encodedPublicKey;
86 publicKey.Save(StringSink(encodedPublicKey).Ref());
88 this->pk_enc = encodedPublicKey;
91 const std::string PublicAddress::getEncryptionPublicKey() const {
95 const std::vector<unsigned char>& PublicAddress::getPublicAddressSecret() const {
99 bool PublicAddress::operator==(const PublicAddress& rhs) const {
100 return ((this->a_pk == rhs.a_pk) && (this->pk_enc == rhs.pk_enc));
103 bool PublicAddress::operator!=(const PublicAddress& rhs) const {
104 return !(*this == rhs);
107 Address::Address(PrivateAddress& priv) : addr_pk(priv), addr_sk(priv) {
111 Address::Address() : addr_pk(), addr_sk() {
115 const PublicAddress& Address::getPublicAddress() const {
116 return this->addr_pk;
119 const PrivateAddress& Address::getPrivateAddress() const {
120 return this->addr_sk;
123 bool Address::operator==(const Address& rhs) const {
124 return ((this->addr_sk == rhs.addr_sk) && (this->addr_pk == rhs.addr_pk));
127 bool Address::operator!=(const Address& rhs) const {
128 return !(*this == rhs);
131 Address Address::CreateNewRandomAddress() {
132 std::vector<unsigned char> a_sk(ZC_A_SK_SIZE);
134 unsigned char a_sk_bytes[ZC_A_SK_SIZE];
135 getRandBytes(a_sk_bytes, ZC_A_SK_SIZE);
136 convertBytesToBytesVector(a_sk_bytes, a_sk);
138 AutoSeededRandomPool prng;
140 ECIES<ECP>::PrivateKey privateKey;
141 privateKey.Initialize(prng, ASN1::secp256r1());
143 std::string encodedPrivateKey;
145 privateKey.Save(StringSink(encodedPrivateKey).Ref());
147 PrivateAddress addr_sk(a_sk, encodedPrivateKey);
148 return Address(addr_sk);
151 } /* namespace libzerocash */