// Copyright (c) 2012-2013 The Cryptonote developers
// 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 "warnings.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
#include "crypto-tests.h"
#include "../io.h"
#include "warnings.h"

PUSH_GCC_WARNINGS
DISABLE_GCC_WARNING(strict-aliasing)


using namespace std;
using namespace crypto;
typedef crypto::hash chash;

bool operator !=(const ec_scalar &a, const ec_scalar &b) {
  return 0 != memcmp(&a, &b, sizeof(ec_scalar));
}

bool operator !=(const ec_point &a, const ec_point &b) {
  return 0 != memcmp(&a, &b, sizeof(ec_point));
}

static string tohex(const void *data, size_t len) {
  string res;
  res.reserve(len * 2);
  const unsigned char *p = reinterpret_cast<const unsigned char *>(data);
  for (size_t i = 0; i < len; i++) {
    char buf[3];
    snprintf(buf, sizeof(buf), "%02x", p[i]);
    res += buf;
  }
  return res;
}

template<typename T>
static string tohex(const T &v) { return tohex(&v, sizeof(T)); }

int main(int argc, char *argv[]) {
  fstream input;
  string cmd;
  size_t test = 0;
  bool error = false;
  bool generate = false;
  setup_random();

  if (argc == 3 && string(argv[1]) == "--generate") {
    generate = true;
    input.open(argv[2], ios_base::in);
  } else if (argc == 2) {
    input.open(argv[1], ios_base::in);
  } else {
    cerr << "usage: crypto-tests [--generate] <tests.txt>" << endl;
    return 1;
  }

  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") {
      ec_scalar scalar;
      bool expected = false, actual = false;
      get(input, scalar, expected);
      actual = check_scalar(scalar);
      if (generate) {
        cout << "check_scalar " << tohex(scalar) << " " << (actual ? "true" : "false") << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "random_scalar") {
      ec_scalar expected, actual;
      get(input, expected);
      random_scalar(actual);
      if (generate) {
        cout << "random_scalar " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "hash_to_scalar") {
      vector<char> data;
      ec_scalar expected, actual;
      get(input, data, expected);
      hash_to_scalar(data.data(), data.size(), actual);
      if (generate) {
        cout << "hash_to_scalar " << (data.empty() ? "x" : tohex(data.data(), data.size())) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "generate_keys") {
      public_key expected1, actual1;
      secret_key expected2, actual2;
      get(input, expected1, expected2);
      generate_keys(actual1, actual2);
      if (generate) {
        cout << "generate_keys " << tohex(actual1) << " " << tohex(actual2) << endl;
      } else if (expected1 != actual1 || expected2 != actual2) {
        goto error;
      }
    } else if (cmd == "check_key") {
      public_key key;
      bool expected, actual;
      get(input, key, expected);
      actual = check_key(key);
      if (generate) {
        cout << "check_key " << tohex(key) << " " << (actual ? "true" : "false") << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "secret_key_to_public_key") {
      secret_key sec;
      bool expected1 = false, actual1 = false;
      public_key expected2, actual2;
      get(input, sec, expected1);
      if (expected1) {
        get(input, expected2);
      }
      actual1 = secret_key_to_public_key(sec, actual2);
      if (generate) {
        cout << "secret_key_to_public_key " << tohex(sec) << " " << (actual1 ? "true" : "false");
        if (actual1) cout << " " << tohex(actual2);
        cout << endl;
      } else if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
        goto error;
      }
    } else if (cmd == "generate_key_derivation") {
      public_key key1;
      secret_key key2;
      bool expected1 = false, actual1 = false;
      key_derivation expected2, actual2;
      get(input, key1, key2, expected1);
      if (expected1) {
        get(input, expected2);
      }
      actual1 = generate_key_derivation(key1, key2, actual2);
      if (generate) {
        cout << "generate_key_derivation " << tohex(key1) << " " << tohex(key2) << " " << (actual1 ? "true" : "false");
        if (actual1) cout << " " << tohex(actual2);
        cout << endl;
      } else if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
        goto error;
      }
    } else if (cmd == "derive_public_key") {
      key_derivation derivation;
      size_t output_index;
      public_key base;
      bool expected1 = false, actual1 = false;
      public_key expected2, actual2;
      get(input, derivation, output_index, base, expected1);
      if (expected1) {
        get(input, expected2);
      }
      actual1 = derive_public_key(derivation, output_index, base, actual2);
      if (generate) {
        cout << "derive_public_key " << tohex(derivation) << " " << output_index << " " << tohex(base) << " " << (actual1 ? "true" : "false");
        if (actual1) cout << " " << tohex(actual2);
        cout << endl;
      } else if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
        goto error;
      }
    } else if (cmd == "derive_secret_key") {
      key_derivation derivation;
      size_t output_index;
      secret_key base;
      secret_key expected, actual;
      get(input, derivation, output_index, base, expected);
      derive_secret_key(derivation, output_index, base, actual);
      if (generate) {
        cout << "derive_secret_key " << tohex(derivation) << " " << output_index << " " << tohex(base) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "generate_signature") {
      chash prefix_hash;
      public_key pub;
      secret_key sec;
      signature expected, actual;
      get(input, prefix_hash, pub, sec, expected);
      generate_signature(prefix_hash, pub, sec, actual);
      if (generate) {
        cout << "generate_signature " << tohex(prefix_hash) << " " << tohex(pub) << " " << tohex(sec) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "check_signature") {
      chash prefix_hash;
      public_key pub;
      signature sig;
      bool expected = false, actual = false;
      get(input, prefix_hash, pub, sig, expected);
      actual = check_signature(prefix_hash, pub, sig);
      if (generate) {
        cout << "check_signature " << tohex(prefix_hash) << " " << tohex(pub) << " " << tohex(sig) << " " << (actual ? "true" : "false") << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "hash_to_point") {
      chash h;
      ec_point expected, actual;
      get(input, h, expected);
      hash_to_point(h, actual);
      if (generate) {
        cout << "hash_to_point " << tohex(h) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "hash_to_ec") {
      public_key key;
      ec_point expected, actual;
      get(input, key, expected);
      hash_to_ec(key, actual);
      if (generate) {
        cout << "hash_to_ec " << tohex(key) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "generate_key_image") {
      public_key pub;
      secret_key sec;
      key_image expected, actual;
      get(input, pub, sec, expected);
      generate_key_image(pub, sec, actual);
      if (generate) {
        cout << "generate_key_image " << tohex(pub) << " " << tohex(sec) << " " << tohex(actual) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "generate_ring_signature") {
      chash prefix_hash;
      key_image image;
      vector<public_key> vpubs;
      vector<const public_key *> pubs;
      size_t pubs_count;
      secret_key sec;
      size_t sec_index;
      vector<signature> 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];
      }
      get(input, sec, sec_index);
      expected.resize(pubs_count);
      getvar(input, pubs_count * sizeof(signature), expected.data());
      actual.resize(pubs_count);
      generate_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sec, sec_index, actual.data());
      if (generate) {
        cout << "generate_ring_signature " << tohex(prefix_hash) << " " << tohex(image) << " " << pubs_count;
        for (i = 0; i < pubs_count; i++) cout << " " << tohex(vpubs[i]);
        cout << " " << tohex(sec) << " " << sec_index << " " << tohex(actual.data(), pubs_count * sizeof(signature)) << endl;
      } else if (expected != actual) {
        goto error;
      }
    } else if (cmd == "check_ring_signature") {
      chash prefix_hash;
      key_image image;
      vector<public_key> vpubs;
      vector<const public_key *> pubs;
      size_t pubs_count;
      vector<signature> sigs;
      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);
      getvar(input, pubs_count * sizeof(signature), sigs.data());
      get(input, expected);
      actual = check_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sigs.data());
      if (generate) {
        cout << "check_ring_signature " << tohex(prefix_hash) << " " << tohex(image) << " " << pubs_count;
        for (i = 0; i < pubs_count; i++) cout << " " << tohex(vpubs[i]);
        cout << " " << tohex(sigs.data(), pubs_count * sizeof(signature)) << " " << (actual ? "true" : "false") << endl;
      } else 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;
}
POP_GCC_WARNINGS
