SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
ompttest.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 
19 /*
20 * Implemented algorithms are derived from equations published in:
21 * Schneider, T., Moradi, A., & Güneysu, T. (2016, April). Robust and one-pass parallel
22 * computation of correlation-based attacks at arbitrary order. In International
23 * Workshop on Constructive Side-Channel Analysis and Secure Design (pp. 199-217). Springer, Cham.
24 */
25 
36 #ifndef OMPCPA_H
37 #define OMPCPA_H
38 
39 #include <cmath>
40 #include <omp.h>
41 #include "exceptions.hpp"
42 #include "types_power.hpp"
43 #include "types_stat.hpp"
44 
50 template <class T, class U>
51 void UniFoTTestAddTraces(UnivariateContext<T>& c, const PowerTraces<U>& randTraces, const PowerTraces<U>& constTraces) {
52 
53  if(c.mOrder() != 1 || c.csOrder() != 2 || c.acsOrder() != 0 || c.p1Width() != c.p2Width())
54  throw RuntimeException("Not a valid first-order univariate t-test context!");
55 
56  if (c.p1Width() != randTraces.samplesPerTrace() || c.p1Width() != constTraces.samplesPerTrace())
57  throw RuntimeException("Numbers of samples don't match.");
58 
59  const long long samplesPerTrace = randTraces.samplesPerTrace();
60  const long long noOfRandTraces = randTraces.noOfTraces();
61  const long long noOfConstTraces = constTraces.noOfTraces();
62 
63  for (long long trace = 0; trace < noOfRandTraces; trace++) {
64 
65  for (long long sample = 0; sample < samplesPerTrace; sample++) {
66  T temp = 0;
67  temp = (static_cast<T>(randTraces(sample, trace)) - c.p1M(1)(sample));
68  c.p1M(1)(sample) += (temp / static_cast<T>((c.p1Card() + 1)));
69  c.p1CS(2)(sample) += temp * (static_cast<T>(randTraces(sample, trace)) - c.p1M(1)(sample));
70  }
71 
72  c.p1Card() = c.p1Card() + 1;
73 
74  }
75 
76  for (long long trace = 0; trace < noOfConstTraces; trace++) {
77 
78  for (long long sample = 0; sample < samplesPerTrace; sample++) {
79  T temp = 0;
80  temp = (static_cast<T>(constTraces(sample, trace)) - c.p2M(1)(sample));
81  c.p2M(1)(sample) += (temp / static_cast<T>((c.p2Card() + 1)));
82  c.p2CS(2)(sample) += temp * (static_cast<T>(constTraces(sample, trace)) - c.p2M(1)(sample));
83  }
84 
85  c.p2Card() = c.p2Card() + 1;
86 
87  }
88 
89 }
90 
96 template <class T>
98 
99  if(firstAndOut.mOrder() != 1 || firstAndOut.csOrder() != 2 || firstAndOut.acsOrder() != 0 || second.mOrder() != 1 || second.csOrder() != 2 || second.acsOrder() != 0 ||
100  firstAndOut.p1Width() != firstAndOut.p2Width() || second.p1Width() != second.p2Width())
101  throw RuntimeException("Not valid first-order univariate t-test contexts!");
102 
103  if(firstAndOut.p1Width() != second.p1Width())
104  throw RuntimeException("Only contexts with same number of samples per trace can be merged");
105 
106  const size_t samplesPerTrace = firstAndOut.p1Width();
107 
108 
109  // random
110  size_t firstSize = firstAndOut.p1Card();
111  size_t secondSize = second.p1Card();
112  // merge the MSums
113  for(size_t sample = 0; sample < samplesPerTrace; sample++) {
114  firstAndOut.p1CS(2)(sample) += second.p1CS(2)(sample);
115  firstAndOut.p1CS(2)(sample) += (firstSize * secondSize) *
116  ( (second.p1M(1)(sample) - firstAndOut.p1M(1)(sample)) / (firstSize + secondSize) ) *
117  ( (second.p1M(1)(sample) - firstAndOut.p1M(1)(sample)) / (firstSize + secondSize) ) *
118  (firstSize + secondSize);
119  }
120  // then merge the means
121  for(size_t sample = 0; sample < samplesPerTrace; sample++) {
122  firstAndOut.p1M(1)(sample) = ( (firstAndOut.p1M(1)(sample) * firstSize) + (second.p1M(1)(sample) * secondSize) ) / (firstSize + secondSize);
123  }
124 
125 
126  // const
127  firstSize = firstAndOut.p2Card();
128  secondSize = second.p2Card();
129 
130  for(size_t sample = 0; sample < samplesPerTrace; sample++) {
131  firstAndOut.p2CS(2)(sample) += second.p2CS(2)(sample);
132  firstAndOut.p2CS(2)(sample) += (firstSize * secondSize) *
133  ( (second.p2M(1)(sample) - firstAndOut.p2M(1)(sample)) / (firstSize + secondSize) ) *
134  ( (second.p2M(1)(sample) - firstAndOut.p2M(1)(sample)) / (firstSize + secondSize) ) *
135  (firstSize + secondSize);
136  }
137 
138 
139 
140  for(size_t sample = 0; sample < samplesPerTrace; sample++) {
141  firstAndOut.p2M(1)(sample) = ( (firstAndOut.p2M(1)(sample) * firstSize) + (second.p2M(1)(sample) * secondSize) ) / (firstSize + secondSize);
142  }
143 
144  // finally update the cardinality of the context
145  firstAndOut.p1Card() += second.p1Card();
146  firstAndOut.p2Card() += second.p2Card();
147 
148 }
149 
155 template <class T>
157 
158  if(c.mOrder() != 1 || c.csOrder() != 2 || c.acsOrder() != 0 || c.p1Width() != c.p2Width())
159  throw RuntimeException("Not a valid first-order univariate t-test context!");
160 
161  size_t samplesPerTrace = c.p1Width();
162 
163  tValsDegs.init(samplesPerTrace, 2);
164 
165  for(size_t sample = 0; sample < samplesPerTrace; sample++){
166 
167  // t-values
168  tValsDegs(sample, 0) = (c.p2M(1)(sample) - c.p1M(1)(sample))
169  / sqrt(((c.p2CS(2)(sample) / (double)(c.p2Card() - 1)) / (double)c.p2Card())
170  + ((c.p1CS(2)(sample) / (double)(c.p1Card() - 1)) / (double)c.p1Card()));
171 
172  // degrees of freedom
173  tValsDegs(sample, 1) = ( ( ( (c.p2CS(2)(sample) / (double)(c.p2Card() - 1) ) / (double)c.p2Card() )
174  + ( (c.p1CS(2)(sample) / (double)(c.p1Card() - 1) ) / (double)c.p1Card() ) )
175  * ( ( (c.p2CS(2)(sample) / (double)(c.p2Card() - 1) ) / (double)c.p2Card() )
176  + ( (c.p1CS(2)(sample) / (double)(c.p1Card() - 1) ) / (double)c.p1Card() ) ) )
177  /
178  ( ( ( ( (c.p2CS(2)(sample) / (double)(c.p2Card() - 1) ) / (double)c.p2Card() )
179  * ( (c.p2CS(2)(sample) / (double)(c.p2Card() - 1) ) / (double)c.p2Card() ) )
180  / (double)(c.p2Card() - 1) )
181  + ( ( ( (c.p1CS(2)(sample) / (double)(c.p1Card() - 1) ) / (double)c.p1Card())
182  * ( (c.p1CS(2)(sample) / (double)(c.p1Card() - 1) ) / (double)c.p1Card()) )
183  / (double)(c.p1Card() - 1) ) );
184 
185  }
186 
187 }
188 
189 #endif /* OMPCPA_H */
190 
191 
virtual size_t noOfTraces() const
Returns number of power traces.
Definition: types_power.hpp:67
An abstract class, representing all the matrix-like data types.
Definition: types_basic.hpp:132
A class representing a Two-population Univariate Moment-based statistical context.
Definition: types_stat.hpp:43
This header file contains exceptions.
void UniFoTTestMergeContexts(UnivariateContext< T > &firstAndOut, const UnivariateContext< T > &second)
Merges two UnivariateContext and leaves the result in first context given.
Definition: ompttest.hpp:97
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
void UniFoTTestComputeTValsDegs(const UnivariateContext< T > &c, MatrixType< T > &tValsDegs)
Computes final t-values and degrees of freedom based on a UnivariateContext given,...
Definition: ompttest.hpp:156
virtual void init(size_t cols, size_t rows)=0
Initializes the matrix with a specified number of cols and rows.
void UniFoTTestAddTraces(UnivariateContext< T > &c, const PowerTraces< U > &randTraces, const PowerTraces< U > &constTraces)
Adds given random and constant power traces to the given statistical context. Use zeroed or meaningfu...
Definition: ompttest.hpp:51
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 size_t p1Width() const
Width of the first population.
Definition: types_stat.hpp:178
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 power traces and power consumption containers.
virtual size_t samplesPerTrace() const
Returns number of samples per trace.
Definition: types_power.hpp:65
virtual size_t & p2Card()
Cardinality of the second population.
Definition: types_stat.hpp:195
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
This header file contains class templates of statistical computational contexts.
A class representing a Matrix with 'noOfTraces' power traces, with 'samplesPerTrace' samples per powe...
Definition: types_power.hpp:44
virtual Vector< T > & p1M(size_t order)
Raw moment of the first population, order 1 upto mOrder.
Definition: types_stat.hpp:200