2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2014-03-03 22:07:58 +00:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "crypto-tests.h"
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "../Io.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2015-07-30 15:22:07 +00:00
|
|
|
typedef Crypto::Hash chash;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
bool operator !=(const Crypto::EllipticCurveScalar &a, const Crypto::EllipticCurveScalar &b) {
|
|
|
|
return 0 != memcmp(&a, &b, sizeof(Crypto::EllipticCurveScalar));
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
bool operator !=(const Crypto::EllipticCurvePoint &a, const Crypto::EllipticCurvePoint &b) {
|
|
|
|
return 0 != memcmp(&a, &b, sizeof(Crypto::EllipticCurvePoint));
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
bool operator !=(const Crypto::KeyDerivation &a, const Crypto::KeyDerivation &b) {
|
|
|
|
return 0 != memcmp(&a, &b, sizeof(Crypto::KeyDerivation));
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
fstream input;
|
|
|
|
string cmd;
|
|
|
|
size_t test = 0;
|
|
|
|
bool error = false;
|
|
|
|
setup_random();
|
|
|
|
if (argc != 2) {
|
|
|
|
cerr << "invalid arguments" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
input.open(argv[1], ios_base::in);
|
|
|
|
for (;;) {
|
|
|
|
++test;
|
|
|
|
input.exceptions(ios_base::badbit);
|
|
|
|
if (!(input >> cmd)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
|
|
|
|
if (cmd == "check_scalar") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::EllipticCurveScalar scalar;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected, actual;
|
|
|
|
get(input, scalar, expected);
|
|
|
|
actual = check_scalar(scalar);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "random_scalar") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::EllipticCurveScalar expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, expected);
|
|
|
|
random_scalar(actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "hash_to_scalar") {
|
|
|
|
vector<char> data;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::EllipticCurveScalar expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, data, expected);
|
|
|
|
hash_to_scalar(data.data(), data.size(), actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "generate_keys") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey expected1, actual1;
|
|
|
|
Crypto::SecretKey expected2, actual2;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, expected1, expected2);
|
|
|
|
generate_keys(actual1, actual2);
|
|
|
|
if (expected1 != actual1 || expected2 != actual2) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "check_key") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey key;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected, actual;
|
|
|
|
get(input, key, expected);
|
|
|
|
actual = check_key(key);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "secret_key_to_public_key") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::SecretKey sec;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected1, actual1;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey expected2, actual2;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, sec, expected1);
|
|
|
|
if (expected1) {
|
|
|
|
get(input, expected2);
|
|
|
|
}
|
|
|
|
actual1 = secret_key_to_public_key(sec, actual2);
|
|
|
|
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "generate_key_derivation") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey key1;
|
|
|
|
Crypto::SecretKey key2;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected1, actual1;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyDerivation expected2, actual2;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, key1, key2, expected1);
|
|
|
|
if (expected1) {
|
|
|
|
get(input, expected2);
|
|
|
|
}
|
|
|
|
actual1 = generate_key_derivation(key1, key2, actual2);
|
|
|
|
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "derive_public_key") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyDerivation derivation;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t output_index;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey base;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected1, actual1;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey expected2, actual2;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, derivation, output_index, base, expected1);
|
|
|
|
if (expected1) {
|
|
|
|
get(input, expected2);
|
|
|
|
}
|
|
|
|
actual1 = derive_public_key(derivation, output_index, base, actual2);
|
|
|
|
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "derive_secret_key") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyDerivation derivation;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t output_index;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::SecretKey base;
|
|
|
|
Crypto::SecretKey expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, derivation, output_index, base, expected);
|
|
|
|
derive_secret_key(derivation, output_index, base, actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
} else if (cmd == "underive_public_key") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyDerivation derivation;
|
2015-04-06 16:13:07 +00:00
|
|
|
size_t output_index;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey derived_key;
|
2015-04-06 16:13:07 +00:00
|
|
|
bool expected1, actual1;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey expected2, actual2;
|
2015-04-06 16:13:07 +00:00
|
|
|
get(input, derivation, output_index, derived_key, expected1);
|
|
|
|
if (expected1) {
|
|
|
|
get(input, expected2);
|
|
|
|
}
|
|
|
|
actual1 = underive_public_key(derivation, output_index, derived_key, actual2);
|
|
|
|
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
} else if (cmd == "generate_signature") {
|
|
|
|
chash prefix_hash;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey pub;
|
|
|
|
Crypto::SecretKey sec;
|
|
|
|
Crypto::Signature expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, prefix_hash, pub, sec, expected);
|
|
|
|
generate_signature(prefix_hash, pub, sec, actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "check_signature") {
|
|
|
|
chash prefix_hash;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey pub;
|
|
|
|
Crypto::Signature sig;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected, actual;
|
|
|
|
get(input, prefix_hash, pub, sig, expected);
|
|
|
|
actual = check_signature(prefix_hash, pub, sig);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "hash_to_point") {
|
|
|
|
chash h;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::EllipticCurvePoint expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, h, expected);
|
|
|
|
hash_to_point(h, actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "hash_to_ec") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey key;
|
|
|
|
Crypto::EllipticCurvePoint expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, key, expected);
|
|
|
|
hash_to_ec(key, actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "generate_key_image") {
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::PublicKey pub;
|
|
|
|
Crypto::SecretKey sec;
|
|
|
|
Crypto::KeyImage expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, pub, sec, expected);
|
|
|
|
generate_key_image(pub, sec, actual);
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "generate_ring_signature") {
|
|
|
|
chash prefix_hash;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyImage image;
|
|
|
|
vector<Crypto::PublicKey> vpubs;
|
|
|
|
vector<const Crypto::PublicKey *> pubs;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t pubs_count;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::SecretKey sec;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t sec_index;
|
2015-07-30 15:22:07 +00:00
|
|
|
vector<Crypto::Signature> expected, actual;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t i;
|
|
|
|
get(input, prefix_hash, image, pubs_count);
|
|
|
|
vpubs.resize(pubs_count);
|
|
|
|
pubs.resize(pubs_count);
|
|
|
|
for (i = 0; i < pubs_count; i++) {
|
|
|
|
get(input, vpubs[i]);
|
|
|
|
pubs[i] = &vpubs[i];
|
|
|
|
}
|
|
|
|
get(input, sec, sec_index);
|
|
|
|
expected.resize(pubs_count);
|
2015-07-30 15:22:07 +00:00
|
|
|
getvar(input, pubs_count * sizeof(Crypto::Signature), expected.data());
|
2014-03-03 22:07:58 +00:00
|
|
|
actual.resize(pubs_count);
|
|
|
|
generate_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sec, sec_index, actual.data());
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else if (cmd == "check_ring_signature") {
|
|
|
|
chash prefix_hash;
|
2015-07-30 15:22:07 +00:00
|
|
|
Crypto::KeyImage image;
|
|
|
|
vector<Crypto::PublicKey> vpubs;
|
|
|
|
vector<const Crypto::PublicKey *> pubs;
|
2014-03-03 22:07:58 +00:00
|
|
|
size_t pubs_count;
|
2015-07-30 15:22:07 +00:00
|
|
|
vector<Crypto::Signature> sigs;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool expected, actual;
|
|
|
|
size_t i;
|
|
|
|
get(input, prefix_hash, image, pubs_count);
|
|
|
|
vpubs.resize(pubs_count);
|
|
|
|
pubs.resize(pubs_count);
|
|
|
|
for (i = 0; i < pubs_count; i++) {
|
|
|
|
get(input, vpubs[i]);
|
|
|
|
pubs[i] = &vpubs[i];
|
|
|
|
}
|
|
|
|
sigs.resize(pubs_count);
|
2015-07-30 15:22:07 +00:00
|
|
|
getvar(input, pubs_count * sizeof(Crypto::Signature), sigs.data());
|
2014-03-03 22:07:58 +00:00
|
|
|
get(input, expected);
|
|
|
|
actual = check_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sigs.data());
|
|
|
|
if (expected != actual) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw ios_base::failure("Unknown function: " + cmd);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
error:
|
|
|
|
cerr << "Wrong result on test " << test << endl;
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
return error ? 1 : 0;
|
|
|
|
}
|