/*############################################################################
  # Copyright 2016 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/*!
 * \file
 * \brief Pairing unit tests.
 */

#include <cstring>

#include "gtest/gtest.h"

#include "epid/common-testhelper/errors-testhelper.h"
#include "epid/common-testhelper/epid_params-testhelper.h"
#include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
#include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
#include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
#include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"

extern "C" {
#include "epid/common/math/pairing.h"
#include "epid/common/math/src/ecgroup-internal.h"
#include "epid/common/math/src/finitefield-internal.h"
#include "epid/common/math/src/pairing-internal.h"
}

/// compares Fq12ElemStr values
bool operator==(GtElemStr const& lhs, GtElemStr const& rhs) {
  return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
}

namespace {

class PairingTest : public Epid20Params, public ::testing::Test {
 public:
  static const BigNumStr t_str;

  virtual void SetUp() { params = new Epid20Params(); }
  virtual void TearDown() { delete params; }

  Epid20Params* params;
};

const BigNumStr PairingTest::t_str = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x68, 0x82, 0xF5, 0xC0, 0x30, 0xB0, 0xA8, 0x01};

///////////////////////////////////////////////////////////////////////
// NewPairingState / DeletePairingState

// test that delete works in a "normal" valid case.
TEST_F(PairingTest, DeleteWorksGivenNewlyCreatedPairingState) {
  PairingState* ps = nullptr;
  EpidStatus sts = kEpidNoErr;
  EXPECT_EQ(kEpidNoErr,
            NewPairingState(this->params->G1, this->params->G2,
                            this->params->GT, &this->t_str, true, &ps));
  EXPECT_EQ(kEpidNoErr, sts);
  EXPECT_NO_THROW(DeletePairingState(&ps));
}

// test that delete works if there is nothing to do
TEST_F(PairingTest, DeleteWorksGivenNullPointer) {
  EXPECT_NO_THROW(DeletePairingState(nullptr));
  PairingState* ps = nullptr;
  EXPECT_NO_THROW(DeletePairingState(&ps));
}

// test that new succeeds with valid parameters
TEST_F(PairingTest, NewSucceedsGivenValidParameters) {
  PairingState* ps = nullptr;
  EXPECT_EQ(kEpidNoErr,
            NewPairingState(this->params->G1, this->params->G2,
                            this->params->GT, &this->t_str, true, &ps));
  DeletePairingState(&ps);
}

// test that new fails if any options are NULL
TEST_F(PairingTest, NewFailsGivenNullParameters) {
  PairingState* ps = nullptr;
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(nullptr, this->params->G2, this->params->GT,
                            &this->t_str, true, &ps));
  DeletePairingState(&ps);
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(this->params->G1, nullptr, this->params->GT,
                            &this->t_str, true, &ps));
  DeletePairingState(&ps);
  EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
                                            nullptr, &this->t_str, true, &ps));
  DeletePairingState(&ps);
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(this->params->G1, this->params->G2,
                            this->params->GT, nullptr, true, &ps));
  DeletePairingState(&ps);
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(this->params->G1, this->params->G2,
                            this->params->GT, &this->t_str, true, nullptr));
}

// test that new checks that G1 is valid
TEST_F(PairingTest, NewFailsGivenInvalidG1) {
  PairingState* ps = nullptr;
  EcGroup ga;
  ga.ipp_ec = nullptr;
  ga.scratch_buffer = nullptr;
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(&ga, this->params->G2, this->params->GT,
                            &this->t_str, true, &ps));
  DeletePairingState(&ps);
}

// test that new checks that G2 is valid
TEST_F(PairingTest, NewFailsGivenInvalidG2) {
  PairingState* ps = nullptr;
  EcGroup gb;
  gb.ipp_ec = nullptr;
  gb.scratch_buffer = nullptr;
  EXPECT_EQ(kEpidBadArgErr,
            NewPairingState(this->params->G1, &gb, this->params->GT,
                            &this->t_str, true, &ps));
  DeletePairingState(&ps);
}

// test that new checks that GT is valid
TEST_F(PairingTest, NewFailsGivenInvalidGT) {
  PairingState* ps = nullptr;
  FiniteField ff;
  ff.ipp_ff = nullptr;
  EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
                                            &ff, &this->t_str, true, &ps));
  DeletePairingState(&ps);
}
///////////////////////////////////////////////////////////////////////
// Pairing
TEST_F(PairingTest, PairingWorksFromG1AndG2ToGt) {
  const bool neg = true;

  GtElemStr r_expected_str = {
      0xba, 0x10, 0x1f, 0xf6, 0x46, 0x8b, 0xe9, 0x32, 0x4f, 0xc0, 0xa5, 0x01,
      0xad, 0x5e, 0xe2, 0x31, 0x16, 0x29, 0x96, 0xed, 0xa7, 0xde, 0x4c, 0xe1,
      0xd2, 0x8d, 0x33, 0xca, 0x50, 0xab, 0x7b, 0xc6, 0x15, 0xeb, 0x79, 0xf4,
      0xeb, 0xde, 0x30, 0xb6, 0xc4, 0x07, 0x7c, 0x42, 0xcb, 0x04, 0x54, 0xf2,
      0x1f, 0x4d, 0x1f, 0xc0, 0xdf, 0xa2, 0x2b, 0x9e, 0x34, 0xc4, 0x4c, 0x84,
      0x14, 0xd3, 0x62, 0x07, 0xf1, 0x8b, 0x84, 0xd1, 0x46, 0x57, 0xb6, 0xe7,
      0x80, 0xe1, 0x46, 0x49, 0x1c, 0x0d, 0xef, 0x81, 0x31, 0xb0, 0xbe, 0x8c,
      0xb9, 0x08, 0xd0, 0xd3, 0xc4, 0x56, 0xca, 0xad, 0xf9, 0x1d, 0x75, 0x19,
      0x3f, 0xee, 0x7c, 0x43, 0xc1, 0xfa, 0x4e, 0x50, 0xb7, 0x19, 0x01, 0x00,
      0x6f, 0xd5, 0x16, 0xb6, 0xf4, 0x85, 0xe0, 0xeb, 0x2e, 0x5f, 0x0a, 0x7e,
      0xf8, 0xac, 0xbc, 0x05, 0xec, 0x73, 0xb5, 0x57, 0xe3, 0xb3, 0x18, 0x29,
      0xbb, 0xef, 0x86, 0x50, 0x87, 0xcf, 0x70, 0xba, 0x13, 0x8b, 0xb1, 0xb6,
      0x2d, 0x6f, 0x65, 0x3d, 0xa1, 0x0b, 0xe3, 0x92, 0xc5, 0x72, 0x86, 0x6a,
      0xb3, 0xeb, 0xe0, 0xe5, 0xda, 0x0e, 0x57, 0x87, 0xd5, 0xa9, 0x61, 0xa5,
      0x1e, 0xcb, 0x04, 0x86, 0xcd, 0xc3, 0x18, 0x2a, 0x36, 0xa0, 0x81, 0x73,
      0xe7, 0x13, 0x87, 0x80, 0x8d, 0x1a, 0xfe, 0x6e, 0x4b, 0xa3, 0x13, 0x03,
      0x66, 0x9e, 0x80, 0x4d, 0x8a, 0xaa, 0x00, 0x95, 0x72, 0xce, 0xbb, 0x51,
      0xe8, 0x01, 0x09, 0x41, 0xd3, 0x63, 0x28, 0x05, 0xa4, 0xbe, 0xd6, 0x41,
      0xa6, 0x2f, 0x5f, 0xbf, 0x0b, 0x13, 0xb4, 0x54, 0x5b, 0x50, 0x65, 0xdc,
      0x6f, 0x29, 0xd6, 0xda, 0xbf, 0xc2, 0x06, 0xea, 0x3b, 0xb2, 0xf1, 0xd4,
      0x26, 0x5c, 0x92, 0x6b, 0x95, 0x6d, 0x88, 0xab, 0x8f, 0xc6, 0x9d, 0x31,
      0xe4, 0x9b, 0x71, 0x49, 0xe0, 0xce, 0x97, 0x8f, 0xc9, 0x9f, 0xbc, 0xa8,
      0x4a, 0xc6, 0xaa, 0x4a, 0xc8, 0x0d, 0x2a, 0x60, 0x1a, 0x43, 0x40, 0x03,
      0xb3, 0x53, 0x30, 0x98, 0x1f, 0x3f, 0xdf, 0x5c, 0x0f, 0xf0, 0x84, 0x8e,
      0x5a, 0x5d, 0x41, 0xd2, 0x47, 0x78, 0x6d, 0x9f, 0x89, 0xce, 0xf5, 0x8e,
      0xb6, 0x54, 0xa2, 0x26, 0xe5, 0x40, 0x39, 0x5c, 0x59, 0x08, 0xb3, 0xda,
      0xf5, 0xf8, 0xa0, 0x18, 0x33, 0x57, 0xd1, 0x72, 0xbb, 0xba, 0x6c, 0xed,
      0xe8, 0xa0, 0x5e, 0xc8, 0x81, 0xc5, 0xac, 0x15, 0x1b, 0xd0, 0xe6, 0xc8,
      0x92, 0xf9, 0x43, 0x03, 0x5a, 0x00, 0x42, 0xe3, 0x49, 0xa5, 0xf7, 0x19,
      0x78, 0x8a, 0x39, 0x89, 0x32, 0xae, 0xbf, 0x4d, 0x4b, 0xb3, 0x33, 0x76,
      0x16, 0xfd, 0x0b, 0xfe, 0x42, 0x1e, 0x17, 0x37, 0x2a, 0x04, 0xea, 0x26,
      0xba, 0x6e, 0x2c, 0x36, 0xaf, 0x35, 0x1b, 0x75, 0x6d, 0x17, 0xdc, 0x8e,
  };
  G1ElemStr ga_elem_str = {
      0xd7, 0xe2, 0xf9, 0x37, 0x21, 0x0f, 0x09, 0x97, 0x0f, 0xca, 0xa6,
      0x03, 0x7d, 0x91, 0xc3, 0x75, 0x8a, 0xc9, 0x44, 0x11, 0xfc, 0xaa,
      0x55, 0x67, 0xba, 0xce, 0xaf, 0x8d, 0xf6, 0x7c, 0x84, 0x83, 0x04,
      0xb7, 0xa6, 0xff, 0x9f, 0x0d, 0x26, 0x73, 0xaf, 0x6c, 0xd0, 0x0a,
      0xf6, 0x13, 0xc9, 0x44, 0x3f, 0xf0, 0x82, 0x58, 0x48, 0x59, 0x03,
      0x3f, 0x88, 0xe2, 0x46, 0xd6, 0x0f, 0x93, 0x42, 0x4b,
  };
  G2ElemStr gb_elem_str = {
      0x3f, 0x4c, 0xb5, 0x2d, 0xbc, 0x72, 0xb0, 0x9c, 0x6f, 0xb2, 0xb5, 0xc1,
      0xdc, 0xfb, 0xda, 0x35, 0x91, 0xa6, 0x8d, 0x51, 0x37, 0x70, 0xe2, 0x17,
      0xad, 0x53, 0x23, 0xdc, 0xa3, 0xc3, 0xfd, 0x4c, 0x90, 0xfa, 0x4f, 0xa2,
      0xcb, 0x35, 0xf3, 0x50, 0x5e, 0x8e, 0xf4, 0xce, 0x7f, 0xb0, 0x8a, 0x69,
      0x49, 0xdf, 0xf5, 0x4f, 0xb0, 0xc1, 0xd7, 0xf9, 0xb8, 0xfb, 0x89, 0xd1,
      0xb6, 0xf8, 0x74, 0x04, 0xef, 0xc6, 0x60, 0x05, 0x62, 0xf3, 0x17, 0x5a,
      0x80, 0xf4, 0x4b, 0x97, 0x08, 0x3e, 0x43, 0xa1, 0x44, 0x4c, 0x54, 0x86,
      0x16, 0x20, 0xb9, 0xcc, 0xfb, 0xbd, 0x00, 0x5f, 0xc8, 0x01, 0xfb, 0x5b,
      0xc1, 0x6e, 0x2b, 0x46, 0xe2, 0x04, 0x70, 0xeb, 0xa2, 0xaa, 0x86, 0x5a,
      0x35, 0x14, 0x0e, 0xc9, 0xdf, 0xba, 0x9b, 0x6f, 0x3a, 0xca, 0x94, 0x9c,
      0x44, 0x89, 0x94, 0xa3, 0xeb, 0x61, 0x8b, 0x01,
  };

  GtElemStr r_str = {0};

  FfElementObj r(&this->params->GT);
  EcPointObj ga_elem(&this->params->G1, ga_elem_str);
  EcPointObj gb_elem(&this->params->G2, gb_elem_str);

  PairingState* ps = nullptr;
  THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
                                   this->params->GT, &this->t_str, neg, &ps));
  EXPECT_EQ(kEpidNoErr, Pairing(ps, r, ga_elem, gb_elem));
  DeletePairingState(&ps);

  THROW_ON_EPIDERR(WriteFfElement(this->params->GT, r, &r_str, sizeof(r_str)));
  EXPECT_EQ(r_expected_str, r_str);
}

}  // namespace
