Skip to content

Commit 298b148

Browse files
committed
refactored UnivariateContext into Moments2DContext, in order to optimize future extension (especially higher order analysis)
1 parent b7fa31a commit 298b148

File tree

15 files changed

+267
-174
lines changed

15 files changed

+267
-174
lines changed

include/cpaengine.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* SICAK - SIde-Channel Analysis toolKit
3-
* Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
3+
* Copyright (C) 2018-2019 Petr Socha, FIT, CTU in Prague
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -23,7 +23,7 @@
2323
*
2424
*
2525
* \author Petr Socha
26-
* \version 1.0
26+
* \version 1.1
2727
*/
2828

2929
#ifndef CPAENGINE_H
@@ -63,15 +63,15 @@ class CpaEngine {
6363
virtual void setConstTraces(bool constTraces = false) = 0;
6464

6565
/// Create a CPA computation context based on given power traces and power predictions
66-
virtual UnivariateContext<double> createContext(const PowerTraces<int16_t> & powerTraces, const PowerPredictions<uint8_t> & powerPredictions) = 0;
66+
virtual Moments2DContext<double> createContext(const PowerTraces<int16_t> & powerTraces, const PowerPredictions<uint8_t> & powerPredictions) = 0;
6767
/// Merge the two CPA contexts, stores the result in the first of the contexts
68-
virtual void mergeContexts(UnivariateContext<double> & firstAndOut, const UnivariateContext<double> & second) = 0;
68+
virtual void mergeContexts(Moments2DContext<double> & firstAndOut, const Moments2DContext<double> & second) = 0;
6969
/// Compute correlation matrix based on given context
70-
virtual Matrix<double> finalizeContext(const UnivariateContext<double> & context) = 0;
70+
virtual Matrix<double> finalizeContext(const Moments2DContext<double> & context) = 0;
7171

7272
};
7373

74-
#define CpaEngine_iid "cz.cvut.fit.Sicak.CpaEngineInterface/1.0"
74+
#define CpaEngine_iid "cz.cvut.fit.Sicak.CpaEngineInterface/1.1"
7575

7676
Q_DECLARE_INTERFACE(CpaEngine, CpaEngine_iid)
7777

include/filehandling.hpp

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* SICAK - SIde-Channel Analysis toolKit
3-
* Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
3+
* Copyright (C) 2018-2019 Petr Socha, FIT, CTU in Prague
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -23,7 +23,7 @@
2323
*
2424
*
2525
* \author Petr Socha
26-
* \version 1.0
26+
* \version 1.1
2727
*/
2828

2929

@@ -32,6 +32,7 @@
3232

3333
#include <fstream>
3434
#include <string>
35+
#include <cstring>
3536
#include "types_basic.hpp"
3637
#include "types_power.hpp"
3738
#include "types_stat.hpp"
@@ -213,30 +214,42 @@ void writeArrayToFile(std::fstream & fs, const ArrayType<T> & arr){
213214
*
214215
*/
215216
template<class T>
216-
UnivariateContext<T> readContextFromFile(std::fstream & fs){
217+
Moments2DContext<T> readContextFromFile(std::fstream & fs){
217218

218-
uint64_t ctxAttrs[7];
219+
// Read and compare ID signature
220+
Vector<uint8_t> ctxIdAttr(256);
219221

220-
fs.read(reinterpret_cast<char *>(ctxAttrs), sizeof(uint64_t) * 7);
222+
fillArrayFromFile(fs, ctxIdAttr);
221223

222-
if(fs.fail())
223-
throw RuntimeException("Failed to read context from file");
224+
if(strcmp(reinterpret_cast<char *>(ctxIdAttr.data()), "cz.cvut.fit.Sicak.Moments2DContext/1.1")) throw RuntimeException("Error reading a context from a file: invalid ID signature. Maybe incompatible version?");
225+
226+
// Read size parameters
227+
Vector<uint64_t> ctxSizeAttrs(9);
224228

225-
UnivariateContext<T> ret(ctxAttrs[0], ctxAttrs[1], ctxAttrs[4], ctxAttrs[5], ctxAttrs[6]);
226-
ret.p1Card() = ctxAttrs[2];
227-
ret.p2Card() = ctxAttrs[3];
229+
fillArrayFromFile(fs, ctxSizeAttrs);
230+
231+
Moments2DContext<T> ret(ctxSizeAttrs(0), ctxSizeAttrs(1), ctxSizeAttrs(2), ctxSizeAttrs(3), ctxSizeAttrs(4), ctxSizeAttrs(5), ctxSizeAttrs(6));
232+
ret.p1Card() = ctxSizeAttrs(7);
233+
ret.p2Card() = ctxSizeAttrs(8);
228234

229-
for(size_t order = 1; order <= ret.mOrder(); order++){
235+
// Read the data
236+
for(size_t order = 1; order <= ret.p1MOrder(); order++){
230237
fillArrayFromFile(fs, ret.p1M(order));
238+
}
239+
240+
for(size_t order = 1; order <= ret.p2MOrder(); order++){
231241
fillArrayFromFile(fs, ret.p2M(order));
232242
}
233243

234-
for(size_t order = 2; order <= ret.csOrder(); order++){
244+
for(size_t order = 2; order <= ret.p1CSOrder(); order++){
235245
fillArrayFromFile(fs, ret.p1CS(order));
246+
}
247+
248+
for(size_t order = 2; order <= ret.p2CSOrder(); order++){
236249
fillArrayFromFile(fs, ret.p2CS(order));
237250
}
238251

239-
for(size_t order = 1; order <= ret.acsOrder(); order++){
252+
for(size_t order = 1; order <= ret.p12ACSOrder(); order++){
240253
fillArrayFromFile(fs, ret.p12ACS(order));
241254
}
242255

@@ -251,33 +264,51 @@ UnivariateContext<T> readContextFromFile(std::fstream & fs){
251264
*
252265
*/
253266
template<class T>
254-
void writeContextToFile(std::fstream & fs, const UnivariateContext<T> & ctx){
267+
void writeContextToFile(std::fstream & fs, const Moments2DContext<T> & ctx){
255268

256-
uint64_t ctxAttrs[7];
257-
ctxAttrs[0] = ctx.p1Width();
258-
ctxAttrs[1] = ctx.p2Width();
259-
ctxAttrs[2] = ctx.p1Card();
260-
ctxAttrs[3] = ctx.p2Card();
261-
ctxAttrs[4] = ctx.mOrder();
262-
ctxAttrs[5] = ctx.csOrder();
263-
ctxAttrs[6] = ctx.acsOrder();
269+
// Write ID signature
270+
const char * ctxId = ctx.getId();
271+
if(strlen(ctxId) > 255) throw RuntimeException("Context ID overflow.");
264272

265-
fs.write(reinterpret_cast<char *>(ctxAttrs), sizeof(uint64_t) * 7);
273+
Vector<uint8_t> ctxIdAttr(256, 0);
274+
for(size_t i = 0; i < strlen(ctxId); i++){
275+
ctxIdAttr(i) = ctxId[i];
276+
}
266277

267-
if(fs.fail())
268-
throw RuntimeException("Failed to write context to file");
278+
writeArrayToFile(fs, ctxIdAttr);
279+
280+
// Write size attributes
281+
Vector<uint64_t> ctxSizeAttrs(9);
282+
ctxSizeAttrs(0) = ctx.p1Width();
283+
ctxSizeAttrs(1) = ctx.p2Width();
284+
ctxSizeAttrs(2) = ctx.p1MOrder();
285+
ctxSizeAttrs(3) = ctx.p2MOrder();
286+
ctxSizeAttrs(4) = ctx.p1CSOrder();
287+
ctxSizeAttrs(5) = ctx.p2CSOrder();
288+
ctxSizeAttrs(6) = ctx.p12ACSOrder();
289+
ctxSizeAttrs(7) = ctx.p1Card();
290+
ctxSizeAttrs(8) = ctx.p2Card();
269291

270-
for(size_t order = 1; order <= ctx.mOrder(); order++){
292+
writeArrayToFile(fs, ctxSizeAttrs);
293+
294+
// Write the data
295+
for(size_t order = 1; order <= ctx.p1MOrder(); order++){
271296
writeArrayToFile(fs, ctx.p1M(order));
297+
}
298+
299+
for(size_t order = 1; order <= ctx.p2MOrder(); order++){
272300
writeArrayToFile(fs, ctx.p2M(order));
273301
}
274302

275-
for(size_t order = 2; order <= ctx.csOrder(); order++){
303+
for(size_t order = 2; order <= ctx.p1CSOrder(); order++){
276304
writeArrayToFile(fs, ctx.p1CS(order));
305+
}
306+
307+
for(size_t order = 2; order <= ctx.p2CSOrder(); order++){
277308
writeArrayToFile(fs, ctx.p2CS(order));
278309
}
279310

280-
for(size_t order = 1; order <= ctx.acsOrder(); order++){
311+
for(size_t order = 1; order <= ctx.p12ACSOrder(); order++){
281312
writeArrayToFile(fs, ctx.p12ACS(order));
282313
}
283314

include/ttestengine.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* SICAK - SIde-Channel Analysis toolKit
3-
* Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
3+
* Copyright (C) 2018-2019 Petr Socha, FIT, CTU in Prague
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -23,7 +23,7 @@
2323
*
2424
*
2525
* \author Petr Socha
26-
* \version 1.0
26+
* \version 1.1
2727
*/
2828

2929
#ifndef TTESTENGINE_H
@@ -60,14 +60,14 @@ class TTestEngine {
6060
virtual QString queryDevices() = 0;
6161

6262
/// Create a t-test computation context based on given random and constant power traces
63-
virtual UnivariateContext<double> createContext(const PowerTraces<int16_t> & randTraces, const PowerTraces<int16_t> & constTraces) = 0;
63+
virtual Moments2DContext<double> createContext(const PowerTraces<int16_t> & randTraces, const PowerTraces<int16_t> & constTraces) = 0;
6464
/// Merge the two t-test contexts, stores the result in the first of the contexts
65-
virtual void mergeContexts(UnivariateContext<double> & firstAndOut, const UnivariateContext<double> & second) = 0;
65+
virtual void mergeContexts(Moments2DContext<double> & firstAndOut, const Moments2DContext<double> & second) = 0;
6666
/// Compute t-values (stored in first row) and degrees of freedom (second row) based on the given context
67-
virtual Matrix<double> finalizeContext(const UnivariateContext<double> & context) = 0;
67+
virtual Matrix<double> finalizeContext(const Moments2DContext<double> & context) = 0;
6868
};
6969

70-
#define TTestEngine_iid "cz.cvut.fit.Sicak.TTestInterface/1.0"
70+
#define TTestEngine_iid "cz.cvut.fit.Sicak.TTestInterface/1.1"
7171

7272
Q_DECLARE_INTERFACE(TTestEngine, TTestEngine_iid)
7373

include/types_basic.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ class ComputationalContext : public StructuredType {
205205
virtual ~ComputationalContext() {}
206206
/// Fills the context's containers (vectors, matrices,...) with the 'val'
207207
virtual void fill(T val) = 0;
208+
/// Fill the whole context with zeroes
209+
virtual void reset() = 0;
210+
/// Return id of the context
211+
virtual const char * getId() const = 0;
208212

209213
};
210214

0 commit comments

Comments
 (0)