ecgroup_wrapper-testhelper.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*############################################################################
  2. # Copyright 2016-2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief EcGroup C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  21. #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
  22. #include "epid/common-testhelper/errors-testhelper.h"
  23. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  25. #include "epid/common/math/bignum.h"
  26. /// ecgroup deleter type
  27. struct EcGroupDeleter {
  28. /// ecgroup deleter
  29. void operator()(EcGroup* ptr) {
  30. if (ptr) {
  31. DeleteEcGroup(&ptr);
  32. }
  33. }
  34. };
  35. /// ecgroup deleter singlton
  36. EcGroupDeleter ecgroup_deleter;
  37. /// Internal state of the ecgroup wrapper
  38. struct EcGroupObj::State {
  39. /// The stored EcGroup
  40. std::shared_ptr<EcGroup> group_;
  41. FiniteFieldObj fintefield_;
  42. /// constructor
  43. State() : group_(nullptr, ecgroup_deleter) {}
  44. // State instances are not meant to be copied.
  45. // Explicitly delete copy constructor and assignment operator.
  46. State(const State&) = delete;
  47. State& operator=(const State&) = delete;
  48. /// destructor
  49. ~State() {}
  50. };
  51. EcGroupObj::EcGroupObj() : state_(new State()) {
  52. const BigNumStr q_str = {
  53. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
  54. 0x5E, 0xEE, 0x71, 0xA4, 0x9F, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98,
  55. 0x0A, 0x82, 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x13}}};
  56. const FqElemStr b_str = {
  57. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}}};
  60. const BigNumStr p_str = {
  61. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
  62. 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
  63. 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D}}};
  64. const BigNumStr h1 = {
  65. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}};
  68. const G1ElemStr g1_str = {
  69. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}},
  72. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}}};
  75. FiniteFieldObj fq(q_str);
  76. EcGroup* temp = nullptr;
  77. NewEcGroup(fq, FfElementObj(&fq), FfElementObj(&fq, b_str),
  78. FfElementObj(&fq, g1_str.x), FfElementObj(&fq, g1_str.y),
  79. BigNumObj(p_str), BigNumObj(h1), &temp);
  80. state_->group_.reset(temp, ecgroup_deleter);
  81. state_->fintefield_ = fq;
  82. }
  83. EcGroupObj::EcGroupObj(EcGroupObj const& other) : state_(new State) {
  84. state_->group_ = other.state_->group_;
  85. state_->fintefield_ = other.state_->fintefield_;
  86. }
  87. EcGroupObj& EcGroupObj::operator=(EcGroupObj const& other) {
  88. state_->group_ = other.state_->group_;
  89. state_->fintefield_ = other.state_->fintefield_;
  90. return *this;
  91. }
  92. EcGroupObj::EcGroupObj(FiniteFieldObj* ff, FfElement const* a,
  93. FfElement const* b, FfElement const* x,
  94. FfElement const* y, BigNum const* order,
  95. BigNum const* cofactor)
  96. : state_(new State) {
  97. EcGroup* temp = nullptr;
  98. NewEcGroup(*ff, a, b, x, y, order, cofactor, &temp);
  99. state_->group_.reset(temp, ecgroup_deleter);
  100. state_->fintefield_ = *ff;
  101. }
  102. EcGroupObj::~EcGroupObj() {}
  103. EcGroupObj::operator EcGroup*() { return state_->group_.get(); }
  104. EcGroupObj::operator const EcGroup*() const { return state_->group_.get(); }
  105. EcGroup* EcGroupObj::get() { return state_->group_.get(); }
  106. EcGroup const* EcGroupObj::getc() const { return state_->group_.get(); }
  107. size_t EcGroupObj::GetElementMaxSize() const {
  108. return 2 * state_->fintefield_.GetElementMaxSize();
  109. }