SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
types_stat.hpp
Go to the documentation of this file.
1 /*
2 * SICAK - SIde-Channel Analysis toolKit
3 * Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 
29 #ifndef TYPES_STAT_HPP
30 #define TYPES_STAT_HPP
31 
32 #include "types_basic.hpp"
33 
34 
42 template <class T>
44 
45 public:
46 
49  m_p1Width(0), m_p2Width(0), m_p1Card(0), m_p2Card(0), m_mOrder(0), m_csOrder(0), m_acsOrder(0),
50  m_p1M(nullptr), m_p2M(nullptr), m_p1CS(nullptr), m_p2CS(nullptr), m_p12ACS(nullptr)
51  {}
52 
54  UnivariateContext(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder):
55  m_p1Width(0), m_p2Width(0), m_p1Card(0), m_p2Card(0), m_mOrder(0), m_csOrder(0), m_acsOrder(0),
56  m_p1M(nullptr), m_p2M(nullptr), m_p1CS(nullptr), m_p2CS(nullptr), m_p12ACS(nullptr){
57 
58  (*this).init(firstWidth, secondWidth, mOrder, csOrder, acsOrder);
59 
60  }
61 
63  UnivariateContext(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder, T val):
64  m_p1Width(0), m_p2Width(0), m_p1Card(0), m_p2Card(0), m_mOrder(0), m_csOrder(0), m_acsOrder(0),
65  m_p1M(nullptr), m_p2M(nullptr), m_p1CS(nullptr), m_p2CS(nullptr), m_p12ACS(nullptr){
66 
67  (*this).init(firstWidth, secondWidth, mOrder, csOrder, acsOrder);
68  (*this).fill(val);
69 
70  }
71 
73  UnivariateContext(UnivariateContext&& other): m_p1Width(other.m_p1Width), m_p2Width(other.m_p2Width),
74  m_p1Card(other.m_p1Card), m_p2Card(other.m_p2Card),
75  m_mOrder(other.m_mOrder), m_csOrder(other.m_csOrder), m_acsOrder(other.m_acsOrder),
76  m_p1M(std::move(other.m_p1M)), m_p2M(std::move(other.m_p2M)),
77  m_p1CS(std::move(other.m_p1CS)), m_p2CS(std::move(other.m_p2CS)),
78  m_p12ACS(std::move(other.m_p12ACS))
79  {}
80 
83  m_p1Width = other.m_p1Width;
84  m_p2Width = other.m_p2Width;
85  m_p1Card = other.m_p1Card;
86  m_p2Card = other.m_p2Card;
87  m_mOrder = other.m_mOrder;
88  m_csOrder = other.m_csOrder;
89  m_acsOrder = other.m_acsOrder;
90  m_p1M = std::move(other.m_p1M);
91  m_p2M = std::move(other.m_p2M);
92  m_p1CS = std::move(other.m_p1CS);
93  m_p2CS = std::move(other.m_p2CS);
94  m_p12ACS = std::move(other.m_p12ACS);
95  return (*this);
96  }
97 
99  virtual ~UnivariateContext() {}
100 
101  virtual void init(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder) {
102 
103  if(firstWidth == m_p1Width && secondWidth == m_p2Width && mOrder == m_mOrder && csOrder == m_csOrder && acsOrder == m_acsOrder)
104  return; // already there, nothing to do
105 
106  try{
107 
108  m_p1Width = firstWidth;
109  m_p1Card = 0;
110  m_p2Width = secondWidth;
111  m_p2Card = 0;
112  m_mOrder = mOrder;
113  m_csOrder = csOrder;
114  m_acsOrder = acsOrder;
115 
116  // M
117  m_p1M.reset(new Vector<T>[mOrder]);
118  m_p2M.reset(new Vector<T>[mOrder]);
119  for(size_t order = 0; order < mOrder; order++){
120  m_p1M[order].init(firstWidth);
121  m_p2M[order].init(secondWidth);
122  }
123 
124  // CS
125  size_t csLen = (m_csOrder > 1) ? m_csOrder - 1 : 0; // 1st order CS is a constant '0', dont allocate a Vector for that
126  m_p1CS.reset(new Vector<T>[csLen]);
127  m_p2CS.reset(new Vector<T>[csLen]);
128  for(size_t order = 0; order < csLen; order++){
129  m_p1CS[order].init(firstWidth);
130  m_p2CS[order].init(secondWidth);
131  }
132 
133  // ACS
134  m_p12ACS.reset(new Matrix<T>[acsOrder]);
135  for(size_t order = 0; order < acsOrder; order++){
136  m_p12ACS[order].init(firstWidth, secondWidth);
137  }
138 
139  } catch (std::bad_alloc & e) {
140  throw RuntimeException("Context memory allocation failed");
141  }
142  }
143 
144  virtual void init(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder, T val) {
145  (*this).init(firstWidth, secondWidth, mOrder, csOrder, acsOrder);
146  (*this).fill(val);
147  }
148 
149  virtual void fill(T val) {
150 
151  for(size_t order = 0; order < m_mOrder; order++){
152  m_p1M[order].fill(val);
153  m_p2M[order].fill(val);
154  }
155 
156  size_t csLen = (m_csOrder > 1) ? m_csOrder - 1 : 0;
157  for(size_t order = 0; order < csLen; order++){
158  m_p1CS[order].fill(val);
159  m_p2CS[order].fill(val);
160  }
161 
162  for(size_t order = 0; order < m_acsOrder; order++){
163  m_p12ACS[order].fill(val);
164  }
165 
166  }
167 
169  virtual void reset() {
170 
171  (*this).fill(0);
172  m_p1Card = 0;
173  m_p2Card = 0;
174 
175  }
176 
178  virtual size_t p1Width() const { return m_p1Width; }
180  virtual size_t p2Width() const { return m_p2Width; }
181 
183  virtual size_t mOrder() const { return m_mOrder; }
185  virtual size_t csOrder() const { return m_csOrder; }
187  virtual size_t acsOrder() const { return m_acsOrder; }
188 
190  virtual size_t & p1Card() { return m_p1Card; }
192  virtual const size_t & p1Card() const { return m_p1Card; }
193 
195  virtual size_t & p2Card() { return m_p2Card; }
197  virtual const size_t & p2Card() const { return m_p2Card; }
198 
200  virtual Vector<T> & p1M(size_t order) { return m_p1M[order-1]; }
202  virtual const Vector<T> & p1M(size_t order) const { return m_p1M[order-1]; }
203 
205  virtual Vector<T> & p2M(size_t order) { return m_p2M[order-1]; }
207  virtual const Vector<T> & p2M(size_t order) const { return m_p2M[order-1]; }
208 
210  virtual Vector<T> & p1CS(size_t order) { return m_p1CS[order-2]; }
212  virtual const Vector<T> & p1CS(size_t order) const { return m_p1CS[order-2]; }
213 
215  virtual Vector<T> & p2CS(size_t order) { return m_p2CS[order-2]; }
217  virtual const Vector<T> & p2CS(size_t order) const { return m_p2CS[order-2]; }
218 
220  virtual Matrix<T> & p12ACS(size_t order) { return m_p12ACS[order-1]; }
222  virtual const Matrix<T> & p12ACS(size_t order) const { return m_p12ACS[order-1]; }
223 
224 
225 protected:
226 
227  size_t m_p1Width;
228  size_t m_p2Width;
229 
230  size_t m_p1Card;
231  size_t m_p2Card;
232 
233  size_t m_mOrder;
234  size_t m_csOrder;
235  size_t m_acsOrder;
236 
237  std::unique_ptr<Vector<T>[]> m_p1M;
238  std::unique_ptr<Vector<T>[]> m_p2M;
239 
240  std::unique_ptr<Vector<T>[]> m_p1CS;
241  std::unique_ptr<Vector<T>[]> m_p2CS;
242 
243  std::unique_ptr<Matrix<T>[]> m_p12ACS;
244 };
245 
246 
247 #endif /* TYPES_STAT_HPP */
UnivariateContext(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder, T val)
Constructs an initialized context and fills it with val.
Definition: types_stat.hpp:63
virtual const size_t & p1Card() const
Cardinality of the first population (const)
Definition: types_stat.hpp:192
virtual const Vector< T > & p1CS(size_t order) const
Central moment sum of the first population, order 2 upto csOrder (const)
Definition: types_stat.hpp:212
A class representing a vector, stored in the machine's free space.
Definition: types_basic.hpp:217
A class representing a Two-population Univariate Moment-based statistical context.
Definition: types_stat.hpp:43
UnivariateContext()
Constructs an empty context, needs to be initialized first (init)
Definition: types_stat.hpp:48
virtual Matrix< T > & p12ACS(size_t order)
Adjusted central moment sum both populations, order 1 upto acsOrder.
Definition: types_stat.hpp:220
virtual size_t mOrder() const
Maximum order of the raw moments, 1 upto mOrder.
Definition: types_stat.hpp:183
virtual size_t csOrder() const
Maximum order of the central moment sums, 2 upto csOrder.
Definition: types_stat.hpp:185
UnivariateContext(UnivariateContext &&other)
Move constructor.
Definition: types_stat.hpp:73
virtual void reset()
Fill the whole context with zeroes.
Definition: types_stat.hpp:169
virtual ~UnivariateContext()
Empty destructor.
Definition: types_stat.hpp:99
virtual size_t p2Width() const
Width of the second population.
Definition: types_stat.hpp:180
virtual size_t & p1Card()
Cardinality of the first population.
Definition: types_stat.hpp:190
virtual const size_t & p2Card() const
Cardinality of the second population (const)
Definition: types_stat.hpp:197
virtual size_t p1Width() const
Width of the first population.
Definition: types_stat.hpp:178
virtual const Matrix< T > & p12ACS(size_t order) const
Adjusted central moment sum both populations, order 1 upto acsOrder (const)
Definition: types_stat.hpp:222
virtual size_t acsOrder() const
Maximum order of the adjusted central moment sums, 1 upto acsOrder.
Definition: types_stat.hpp:187
virtual Vector< T > & p2CS(size_t order)
Central moment sum of the second population, order 2 upto csOrder.
Definition: types_stat.hpp:215
This header file contains class templates of basic data containers.
A class representing a matrix, stored in the machine's free space.
Definition: types_basic.hpp:305
virtual const Vector< T > & p2CS(size_t order) const
Central moment sum of the second population, order 2 upto csOrder (const)
Definition: types_stat.hpp:217
virtual const Vector< T > & p2M(size_t order) const
Raw moment of the second population, order 1 upto mOrder (const)
Definition: types_stat.hpp:207
virtual const Vector< T > & p1M(size_t order) const
Raw moment of the first population, order 1 upto mOrder (const)
Definition: types_stat.hpp:202
virtual size_t & p2Card()
Cardinality of the second population.
Definition: types_stat.hpp:195
UnivariateContext & operator=(UnivariateContext &&other)
Move assignment operator.
Definition: types_stat.hpp:82
An abstract class, representing all the computational contexts.
Definition: types_basic.hpp:192
virtual Vector< T > & p1CS(size_t order)
Central moment sum of the first population, order 2 upto csOrder.
Definition: types_stat.hpp:210
virtual Vector< T > & p2M(size_t order)
Raw moment of the second population, order 1 upto mOrder.
Definition: types_stat.hpp:205
An exception which cannot be directly influenced by the user, or predicted beforehand.
Definition: exceptions.hpp:76
UnivariateContext(size_t firstWidth, size_t secondWidth, size_t mOrder, size_t csOrder, size_t acsOrder)
Constructs an initialized context.
Definition: types_stat.hpp:54
virtual void fill(T val)
Fills the context's containers (vectors, matrices,...) with the 'val'.
Definition: types_stat.hpp:149
virtual Vector< T > & p1M(size_t order)
Raw moment of the first population, order 1 upto mOrder.
Definition: types_stat.hpp:200