finite_field_wrapper-testhelper.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 FiniteField C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  21. #include "epid/common-testhelper/errors-testhelper.h"
  22. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  23. #include "epid/common/math/bignum.h"
  24. /// finite field deleter type
  25. struct FiniteFieldDeleter {
  26. /// finite field deleter
  27. void operator()(FiniteField* ff) {
  28. if (ff) {
  29. DeleteFiniteField(&ff);
  30. }
  31. }
  32. };
  33. /// finite field deleter singlton
  34. FiniteFieldDeleter finite_field_deleter;
  35. /// Internal state of the finite field wrapper
  36. struct FiniteFieldObj::State {
  37. /// Inner state of complex fields
  38. struct InnerState {
  39. /// The ground field
  40. FiniteFieldObj gf_;
  41. };
  42. /// Inner state
  43. /*!
  44. We store a pointer to InnerState so simple fields
  45. that are not composed from other fields do not result
  46. in an infinite series of fields.
  47. Instead simple fields have a NULL inner_state and
  48. complex fields have it set.
  49. */
  50. InnerState* inner_state;
  51. /// The stored FiniteField
  52. std::shared_ptr<FiniteField> ff_;
  53. /// Maximum size of field element
  54. size_t size_;
  55. /// constructor
  56. State() : ff_(nullptr, finite_field_deleter), size_(0) {
  57. inner_state = nullptr;
  58. }
  59. // State instances are not meant to be copied.
  60. // Explicitly delete copy constructor and assignment operator.
  61. State(const State&) = delete;
  62. State& operator=(const State&) = delete;
  63. /// destructor
  64. ~State() {
  65. if (inner_state) {
  66. delete inner_state;
  67. inner_state = nullptr;
  68. }
  69. }
  70. /// setter for inner_state
  71. void SetInnerState(FiniteFieldObj const& gf) {
  72. if (!inner_state) {
  73. inner_state = new InnerState;
  74. inner_state->gf_ = gf;
  75. }
  76. }
  77. /// setter for inner_state
  78. void SetInnerState(InnerState* state) {
  79. if (state) {
  80. if (!inner_state) {
  81. inner_state = new InnerState;
  82. }
  83. if (!inner_state) {
  84. inner_state->gf_ = state->gf_;
  85. }
  86. } else {
  87. if (inner_state) {
  88. delete inner_state;
  89. inner_state = nullptr;
  90. }
  91. }
  92. }
  93. };
  94. FiniteFieldObj::FiniteFieldObj() : state_(new State()) {
  95. /*
  96. to avoid a bug in ipp this is one less than the
  97. actual max value we could take.
  98. */
  99. const BigNumStr max_prime = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  100. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  101. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  102. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFe};
  103. FiniteField* temp = nullptr;
  104. NewFiniteField(&max_prime, &temp);
  105. state_->ff_.reset(temp, finite_field_deleter);
  106. state_->size_ = sizeof(max_prime);
  107. }
  108. FiniteFieldObj::FiniteFieldObj(FiniteFieldObj const& other)
  109. : state_(new State) {
  110. state_->ff_ = other.state_->ff_;
  111. state_->size_ = other.state_->size_;
  112. state_->SetInnerState(other.state_->inner_state);
  113. }
  114. FiniteFieldObj& FiniteFieldObj::operator=(FiniteFieldObj const& other) {
  115. state_->ff_ = other.state_->ff_;
  116. state_->size_ = other.state_->size_;
  117. state_->SetInnerState(other.state_->inner_state);
  118. return *this;
  119. }
  120. FiniteFieldObj::FiniteFieldObj(BigNumStr const& prime) : state_(new State) {
  121. FiniteField* temp = nullptr;
  122. NewFiniteField(&prime, &temp);
  123. state_->ff_.reset(temp, finite_field_deleter);
  124. state_->size_ = sizeof(prime);
  125. }
  126. FiniteFieldObj::FiniteFieldObj(FiniteFieldObj const& ground_field,
  127. FfElementObj const& ground_element, int degree)
  128. : state_(new State) {
  129. FiniteField* temp = nullptr;
  130. state_->SetInnerState(ground_field);
  131. NewFiniteFieldViaBinomalExtension(ground_field, ground_element, degree,
  132. &temp);
  133. state_->ff_.reset(temp, finite_field_deleter);
  134. state_->size_ = ground_field.GetElementMaxSize() * degree;
  135. }
  136. FiniteFieldObj::FiniteFieldObj(FiniteFieldObj const& ground_field,
  137. BigNumStr const* irr_polynomial, int degree)
  138. : state_(new State) {
  139. FiniteField* temp = nullptr;
  140. state_->SetInnerState(ground_field);
  141. NewFiniteFieldViaPolynomialExtension(ground_field, irr_polynomial, degree,
  142. &temp);
  143. state_->ff_.reset(temp, finite_field_deleter);
  144. state_->size_ = ground_field.GetElementMaxSize() * degree;
  145. }
  146. FiniteFieldObj::~FiniteFieldObj() {}
  147. FiniteFieldObj::operator FiniteField*() { return state_->ff_.get(); }
  148. FiniteFieldObj::operator const FiniteField*() const {
  149. return state_->ff_.get();
  150. }
  151. FiniteField* FiniteFieldObj::get() { return state_->ff_.get(); }
  152. FiniteField const* FiniteFieldObj::getc() const { return state_->ff_.get(); }
  153. size_t FiniteFieldObj::GetElementMaxSize() const { return state_->size_; }