algebra/include/nil/crypto3/algebra/algorithms/strxor.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_STRXOR_HPP
27 #define CRYPTO3_STRXOR_HPP
28 
29 #include <boost/concept/assert.hpp>
30 #include <boost/assert.hpp>
31 
32 #include <iterator>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace algebra {
37  template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
38  constexpr inline
39  typename std::enable_if<std::is_same<typename std::iterator_traits<InputIterator1>::value_type,
40  typename std::iterator_traits<InputIterator2>::value_type>::value,
41  OutputIterator>::type
42  strxor(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
43  OutputIterator out) {
44  BOOST_ASSERT(std::distance(first1, last1) == std::distance(first2, last2));
45 
46  while (first1 != last1 && first2 != last2) {
47  *out++ = *first1++ ^ *first2++;
48  }
49  }
50 
51  template<typename InputType, typename OutputType>
52  constexpr inline void strxor(const InputType &in1, const InputType &in2, OutputType &out) {
53  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<InputType>));
54  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<OutputType>));
55  BOOST_CONCEPT_ASSERT((boost::WriteableRangeConcept<OutputType>));
56 
57  BOOST_ASSERT(std::distance(in1.begin(), in1.end()) == std::distance(in2.begin(), in2.end()) &&
58  std::distance(in1.begin(), in1.end()) == std::distance(out.begin(), out.end()));
59 
60  auto in1_i = in1.begin();
61  auto in2_i = in2.begin();
62  auto out_i = out.begin();
63  while (in1_i != in1.end() && in2_i != in2.end()) {
64  *out_i++ = *in1_i++ ^ *in2_i++;
65  }
66  }
67  } // namespace algebra
68  } // namespace crypto3
69 } // namespace nil
70 
71 #endif // CRYPTO3_STRXOR_HPP
constexpr std::enable_if< std::is_same< typename std::iterator_traits< InputIterator1 >::value_type, typename std::iterator_traits< InputIterator2 >::value_type >::value, OutputIterator >::type strxor(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator out)
Definition: algebra/include/nil/crypto3/algebra/algorithms/strxor.hpp:42
Definition: pair.hpp:31