ffelement_wrapper-testhelper.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*############################################################################
  2. # Copyright 2016 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 FfElement C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/errors-testhelper.h"
  21. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  22. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  23. #include "epid/common/math/bignum.h"
  24. /// ffelement deleter type
  25. struct FfElementDeleter {
  26. /// ffelement deleter
  27. void operator()(FfElement* ffe) {
  28. if (ffe) {
  29. DeleteFfElement(&ffe);
  30. }
  31. }
  32. };
  33. /// ffelement deleter singlton
  34. FfElementDeleter ff_element_deleter;
  35. /// Internal state of the ffelement wrapper
  36. struct FfElementObj::State {
  37. /// The containing field
  38. FiniteFieldObj ff_;
  39. /// size of the element data
  40. size_t size;
  41. /// The stored FfElement
  42. std::shared_ptr<FfElement> ffe_;
  43. State() : ff_(), size(0), ffe_() {}
  44. /// write a new value
  45. void write(FiniteFieldObj* ff, unsigned char const* buf, size_t buflen) {
  46. ff_ = *ff;
  47. bool orig_has_data = (buf != nullptr) && (buflen > 0);
  48. std::shared_ptr<FfElement> ffe;
  49. FfElement* ffe_ptr;
  50. THROW_ON_EPIDERR(NewFfElement(ff_, &ffe_ptr));
  51. ffe.reset(ffe_ptr, ff_element_deleter);
  52. size = buflen;
  53. if (orig_has_data) {
  54. THROW_ON_EPIDERR(ReadFfElement(ff_, buf, buflen, ffe.get()));
  55. }
  56. ffe_ = ffe;
  57. }
  58. };
  59. FfElementObj::FfElementObj() : state_(new State) {}
  60. FfElementObj::FfElementObj(FfElementObj const& other) : state_(new State) {
  61. std::vector<unsigned char> buf = other.data();
  62. state_->write(&other.state_->ff_, &buf[0], buf.size());
  63. }
  64. FfElementObj& FfElementObj::operator=(FfElementObj const& other) {
  65. std::vector<unsigned char> buf = other.data();
  66. state_->write(&other.state_->ff_, &buf[0], buf.size());
  67. return *this;
  68. }
  69. FfElementObj::FfElementObj(FiniteFieldObj* ff) : state_(new State) {
  70. state_->write(ff, nullptr, 0);
  71. }
  72. FfElementObj::FfElementObj(FiniteFieldObj* ff, FpElemStr const& bytes)
  73. : state_(new State) {
  74. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  75. }
  76. FfElementObj::FfElementObj(FiniteFieldObj* ff, FqElemStr const& bytes)
  77. : state_(new State) {
  78. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  79. }
  80. FfElementObj::FfElementObj(FiniteFieldObj* ff, Fq2ElemStr const& bytes)
  81. : state_(new State) {
  82. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  83. }
  84. FfElementObj::FfElementObj(FiniteFieldObj* ff, Fq3ElemStr const& bytes)
  85. : state_(new State) {
  86. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  87. }
  88. FfElementObj::FfElementObj(FiniteFieldObj* ff, Fq6ElemStr const& bytes)
  89. : state_(new State) {
  90. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  91. }
  92. FfElementObj::FfElementObj(FiniteFieldObj* ff, Fq12ElemStr const& bytes)
  93. : state_(new State) {
  94. init(ff, (unsigned char*)&bytes, sizeof(bytes));
  95. }
  96. FfElementObj::FfElementObj(FiniteFieldObj* ff,
  97. std::vector<unsigned char> const& bytes)
  98. : state_(new State) {
  99. init(ff, &bytes[0], bytes.size());
  100. }
  101. FfElementObj::FfElementObj(FiniteFieldObj* ff, void const* bytes, size_t size)
  102. : state_(new State) {
  103. init(ff, (unsigned char const*)bytes, size);
  104. }
  105. void FfElementObj::init(FiniteFieldObj* ff, unsigned char const* bytes,
  106. size_t size) {
  107. state_->write(ff, bytes, size);
  108. }
  109. FfElementObj::~FfElementObj() {}
  110. FfElementObj::operator FfElement*() { return state_->ffe_.get(); }
  111. FfElementObj::operator const FfElement*() const { return state_->ffe_.get(); }
  112. FfElement* FfElementObj::get() { return state_->ffe_.get(); }
  113. FfElement const* FfElementObj::getc() const { return state_->ffe_.get(); }
  114. std::vector<unsigned char> FfElementObj::data() const {
  115. std::vector<unsigned char> buf;
  116. if (state_->ffe_.get() != nullptr) {
  117. buf.resize(state_->ff_.GetElementMaxSize());
  118. THROW_ON_EPIDERR(
  119. WriteFfElement(state_->ff_, state_->ffe_.get(), &buf[0], buf.size()));
  120. }
  121. return buf;
  122. }