Skip to content

Commit f05427a

Browse files
committed
Boolberry support
1 parent f90249d commit f05427a

17 files changed

+1869
-5
lines changed

binding.gyp

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"shavite3.c",
2020
"cryptonight.c",
2121
"x13.c",
22+
"boolberry.cc",
2223
"sha3/sph_hefty1.c",
2324
"sha3/sph_fugue.c",
2425
"sha3/aes_helper.c",
@@ -41,8 +42,15 @@
4142
"crypto/c_jh.c",
4243
"crypto/c_skein.c",
4344
"crypto/hash.c",
44-
"crypto/aesb.c"
45-
]
45+
"crypto/aesb.c",
46+
"crypto/wild_keccak.cpp"
47+
],
48+
"include_dirs": [
49+
"crypto",
50+
],
51+
"cflags_cc": [
52+
"-std=c++0x"
53+
],
4654
}
4755
]
4856
}

boolberry.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "boolberry.h"
2+
#include "crypto/cryptonote_core/cryptonote_format_utils.h"
3+
4+
#include <iostream>
5+
6+
void boolberry_hash(const char* input, uint32_t input_len, const char* scratchpad, uint64_t spad_length, char* output, uint64_t height) {
7+
crypto::hash* spad = (crypto::hash*) scratchpad;
8+
cryptonote::get_blob_longhash_bb(std::string(input, input_len), *((crypto::hash*)output), height, [&](uint64_t index) -> crypto::hash& {
9+
return spad[index%(spad_length / HASH_SIZE)];
10+
});
11+
}

boolberry.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <string>
5+
6+
void boolberry_hash(const char* input, uint32_t input_len, const char* scratchpad, uint64_t spad_length, char* output, uint64_t height);

crypto/c_jh.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ HashReturn jh_hash(int hashbitlen, const BitSequence *data,DataLength databitlen
110110
/*swapping bits 32i||32i+1||......||32i+15 with bits 32i+16||32i+17||......||32i+31 of 64-bit x*/
111111
#define SWAP16(x) (x) = ((((x) & 0x0000ffff0000ffffULL) << 16) | (((x) & 0xffff0000ffff0000ULL) >> 16));
112112
/*swapping bits 64i||64i+1||......||64i+31 with bits 64i+32||64i+33||......||64i+63 of 64-bit x*/
113-
#define SWAP32(x) (x) = (((x) << 32) | ((x) >> 32));
113+
#define SWAP32_JH(x) (x) = (((x) << 32) | ((x) >> 32));
114114

115115
/*The MDS transform*/
116116
#define L(m0,m1,m2,m3,m4,m5,m6,m7) \
@@ -194,7 +194,7 @@ static void E8(hashState *state)
194194
for (i = 0; i < 2; i++) {
195195
SS(state->x[0][i],state->x[2][i],state->x[4][i],state->x[6][i],state->x[1][i],state->x[3][i],state->x[5][i],state->x[7][i],((uint64*)E8_bitslice_roundconstant[roundnumber+5])[i],((uint64*)E8_bitslice_roundconstant[roundnumber+5])[i+2] );
196196
L(state->x[0][i],state->x[2][i],state->x[4][i],state->x[6][i],state->x[1][i],state->x[3][i],state->x[5][i],state->x[7][i]);
197-
SWAP32(state->x[1][i]); SWAP32(state->x[3][i]); SWAP32(state->x[5][i]); SWAP32(state->x[7][i]);
197+
SWAP32_JH(state->x[1][i]); SWAP32_JH(state->x[3][i]); SWAP32_JH(state->x[5][i]); SWAP32_JH(state->x[7][i]);
198198
}
199199

200200
/*round 7*roundnumber+6: Sbox and MDS layers*/

crypto/crypto.h

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Copyright (c) 2012-2013 The Cryptonote developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#pragma once
6+
7+
#include <cstddef>
8+
#include <mutex>
9+
#include <vector>
10+
11+
#include "common/pod-class.h"
12+
#include "generic-ops.h"
13+
#include "hash.h"
14+
15+
namespace crypto {
16+
17+
extern "C" {
18+
#include "random.h"
19+
}
20+
21+
extern std::mutex random_lock;
22+
23+
#pragma pack(push, 1)
24+
POD_CLASS ec_point {
25+
char data[32];
26+
};
27+
28+
POD_CLASS ec_scalar {
29+
char data[32];
30+
};
31+
32+
POD_CLASS public_key: ec_point {
33+
friend class crypto_ops;
34+
};
35+
36+
POD_CLASS secret_key: ec_scalar {
37+
friend class crypto_ops;
38+
};
39+
40+
POD_CLASS key_derivation: ec_point {
41+
friend class crypto_ops;
42+
};
43+
44+
POD_CLASS key_image: ec_point {
45+
friend class crypto_ops;
46+
};
47+
48+
POD_CLASS signature {
49+
ec_scalar c, r;
50+
friend class crypto_ops;
51+
};
52+
#pragma pack(pop)
53+
54+
static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 &&
55+
sizeof(public_key) == 32 && sizeof(secret_key) == 32 &&
56+
sizeof(key_derivation) == 32 && sizeof(key_image) == 32 &&
57+
sizeof(signature) == 64, "Invalid structure size");
58+
59+
class crypto_ops {
60+
crypto_ops();
61+
crypto_ops(const crypto_ops &);
62+
void operator=(const crypto_ops &);
63+
~crypto_ops();
64+
65+
static void generate_keys(public_key &, secret_key &);
66+
friend void generate_keys(public_key &, secret_key &);
67+
static bool check_key(const public_key &);
68+
friend bool check_key(const public_key &);
69+
static bool secret_key_to_public_key(const secret_key &, public_key &);
70+
friend bool secret_key_to_public_key(const secret_key &, public_key &);
71+
static bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
72+
friend bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
73+
static bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
74+
friend bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
75+
static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
76+
friend void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
77+
static void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
78+
friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
79+
static bool check_signature(const hash &, const public_key &, const signature &);
80+
friend bool check_signature(const hash &, const public_key &, const signature &);
81+
static void generate_key_image(const public_key &, const secret_key &, key_image &);
82+
friend void generate_key_image(const public_key &, const secret_key &, key_image &);
83+
static void generate_ring_signature(const hash &, const key_image &,
84+
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
85+
friend void generate_ring_signature(const hash &, const key_image &,
86+
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
87+
static bool check_ring_signature(const hash &, const key_image &,
88+
const public_key *const *, std::size_t, const signature *);
89+
friend bool check_ring_signature(const hash &, const key_image &,
90+
const public_key *const *, std::size_t, const signature *);
91+
};
92+
93+
/* Generate a value filled with random bytes.
94+
*/
95+
template<typename T>
96+
typename std::enable_if<std::is_pod<T>::value, T>::type rand() {
97+
typename std::remove_cv<T>::type res;
98+
std::lock_guard<std::mutex> lock(random_lock);
99+
generate_random_bytes(sizeof(T), &res);
100+
return res;
101+
}
102+
103+
/* Generate a new key pair
104+
*/
105+
inline void generate_keys(public_key &pub, secret_key &sec) {
106+
crypto_ops::generate_keys(pub, sec);
107+
}
108+
109+
/* Check a public key. Returns true if it is valid, false otherwise.
110+
*/
111+
inline bool check_key(const public_key &key) {
112+
return crypto_ops::check_key(key);
113+
}
114+
115+
/* Checks a private key and computes the corresponding public key.
116+
*/
117+
inline bool secret_key_to_public_key(const secret_key &sec, public_key &pub) {
118+
return crypto_ops::secret_key_to_public_key(sec, pub);
119+
}
120+
121+
/* To generate an ephemeral key used to send money to:
122+
* * The sender generates a new key pair, which becomes the transaction key. The public transaction key is included in "extra" field.
123+
* * Both the sender and the receiver generate key derivation from the transaction key, the receivers' "view" key and the output index.
124+
* * The sender uses key derivation and the receivers' "spend" key to derive an ephemeral public key.
125+
* * The receiver can either derive the public key (to check that the transaction is addressed to him) or the private key (to spend the money).
126+
*/
127+
inline bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation) {
128+
return crypto_ops::generate_key_derivation(key1, key2, derivation);
129+
}
130+
inline bool derive_public_key(const key_derivation &derivation, std::size_t output_index,
131+
const public_key &base, public_key &derived_key) {
132+
return crypto_ops::derive_public_key(derivation, output_index, base, derived_key);
133+
}
134+
inline void derive_secret_key(const key_derivation &derivation, std::size_t output_index,
135+
const secret_key &base, secret_key &derived_key) {
136+
crypto_ops::derive_secret_key(derivation, output_index, base, derived_key);
137+
}
138+
139+
/* Generation and checking of a standard signature.
140+
*/
141+
inline void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) {
142+
crypto_ops::generate_signature(prefix_hash, pub, sec, sig);
143+
}
144+
inline bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig) {
145+
return crypto_ops::check_signature(prefix_hash, pub, sig);
146+
}
147+
148+
/* To send money to a key:
149+
* * The sender generates an ephemeral key and includes it in transaction output.
150+
* * To spend the money, the receiver generates a key image from it.
151+
* * Then he selects a bunch of outputs, including the one he spends, and uses them to generate a ring signature.
152+
* To check the signature, it is necessary to collect all the keys that were used to generate it. To detect double spends, it is necessary to check that each key image is used at most once.
153+
*/
154+
inline void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image) {
155+
crypto_ops::generate_key_image(pub, sec, image);
156+
}
157+
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
158+
const public_key *const *pubs, std::size_t pubs_count,
159+
const secret_key &sec, std::size_t sec_index,
160+
signature *sig) {
161+
crypto_ops::generate_ring_signature(prefix_hash, image, pubs, pubs_count, sec, sec_index, sig);
162+
}
163+
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
164+
const public_key *const *pubs, std::size_t pubs_count,
165+
const signature *sig) {
166+
return crypto_ops::check_ring_signature(prefix_hash, image, pubs, pubs_count, sig);
167+
}
168+
169+
/* Variants with vector<const public_key *> parameters.
170+
*/
171+
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
172+
const std::vector<const public_key *> &pubs,
173+
const secret_key &sec, std::size_t sec_index,
174+
signature *sig) {
175+
generate_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sec, sec_index, sig);
176+
}
177+
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
178+
const std::vector<const public_key *> &pubs,
179+
const signature *sig) {
180+
return check_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sig);
181+
}
182+
}
183+
184+
CRYPTO_MAKE_COMPARABLE(public_key)
185+
CRYPTO_MAKE_HASHABLE(key_image)
186+
CRYPTO_MAKE_COMPARABLE(signature)

crypto/cryptonote_core/account.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2012-2013 The Cryptonote developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <boost/archive/binary_oarchive.hpp>
6+
#include <boost/archive/binary_iarchive.hpp>
7+
#include <fstream>
8+
9+
#include "include_base_utils.h"
10+
#include "account.h"
11+
#include "warnings.h"
12+
#include "crypto/crypto.h"
13+
#include "cryptonote_core/cryptonote_basic_impl.h"
14+
#include "cryptonote_core/cryptonote_format_utils.h"
15+
using namespace std;
16+
17+
DISABLE_VS_WARNINGS(4244 4345)
18+
19+
namespace cryptonote
20+
{
21+
//-----------------------------------------------------------------
22+
account_base::account_base()
23+
{
24+
set_null();
25+
}
26+
//-----------------------------------------------------------------
27+
void account_base::set_null()
28+
{
29+
m_keys = account_keys();
30+
}
31+
//-----------------------------------------------------------------
32+
void account_base::generate()
33+
{
34+
generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key);
35+
generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key);
36+
m_creation_timestamp = time(NULL);
37+
}
38+
//-----------------------------------------------------------------
39+
const account_keys& account_base::get_keys() const
40+
{
41+
return m_keys;
42+
}
43+
//-----------------------------------------------------------------
44+
std::string account_base::get_public_address_str()
45+
{
46+
//TODO: change this code into base 58
47+
return get_account_address_as_str(m_keys.m_account_address);
48+
}
49+
//-----------------------------------------------------------------
50+
}

crypto/cryptonote_core/account.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2012-2013 The Cryptonote developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#pragma once
6+
7+
#include "cryptonote_core/cryptonote_basic.h"
8+
#include "crypto/crypto.h"
9+
#include "serialization/keyvalue_serialization.h"
10+
11+
namespace cryptonote
12+
{
13+
14+
struct account_keys
15+
{
16+
account_public_address m_account_address;
17+
crypto::secret_key m_spend_secret_key;
18+
crypto::secret_key m_view_secret_key;
19+
20+
BEGIN_KV_SERIALIZE_MAP()
21+
KV_SERIALIZE(m_account_address)
22+
KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_spend_secret_key)
23+
KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_view_secret_key)
24+
END_KV_SERIALIZE_MAP()
25+
};
26+
27+
/************************************************************************/
28+
/* */
29+
/************************************************************************/
30+
class account_base
31+
{
32+
public:
33+
account_base();
34+
void generate();
35+
const account_keys& get_keys() const;
36+
std::string get_public_address_str();
37+
38+
uint64_t get_createtime() const { return m_creation_timestamp; }
39+
void set_createtime(uint64_t val) { m_creation_timestamp = val; }
40+
41+
bool load(const std::string& file_path);
42+
bool store(const std::string& file_path);
43+
44+
template <class t_archive>
45+
inline void serialize(t_archive &a, const unsigned int /*ver*/)
46+
{
47+
a & m_keys;
48+
a & m_creation_timestamp;
49+
}
50+
51+
BEGIN_KV_SERIALIZE_MAP()
52+
KV_SERIALIZE(m_keys)
53+
KV_SERIALIZE(m_creation_timestamp)
54+
END_KV_SERIALIZE_MAP()
55+
56+
private:
57+
void set_null();
58+
account_keys m_keys;
59+
uint64_t m_creation_timestamp;
60+
};
61+
}

0 commit comments

Comments
 (0)