Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version. Lots of stuff missing, but seems to work. Does
perfect correltion on full matrix OK.
- Loading branch information
Showing
8 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
*.o | |||
.sconsign.dblite | |||
#<AutoIgnore> | |||
testrunner | |||
#<AutoIgnore/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
env = Environment(CPPPATH = ['.'], | |||
CXXFLAGS = ['-O3', '-mfpmath=sse', '-msse4', '-march=native', | |||
'-Wall', '-g', '-std=c++0x', | |||
'-fopenmp'], | |||
LIBS=[], | |||
LIBPATH=[], | |||
LINKFLAGS=[]) | |||
|
|||
|
|||
testrunner = env.Program('testrunner', Glob('tests/*.cpp'), | |||
LIBS=['cppunit']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
#ifndef __SVF_MATRIX_HPP__ | |||
#define __SVF_MATRIX_HPP__ | |||
|
|||
#include <svf/trace.hpp> | |||
|
|||
namespace SVF { | |||
|
|||
class Matrix { | |||
public: | |||
Matrix() { | |||
} | |||
|
|||
virtual ~Matrix() { | |||
} | |||
|
|||
virtual double operator()(size_t i, size_t j) = 0; | |||
double operator[](std::pair<size_t, size_t> p) { | |||
return (*this)(p.first, p.second); | |||
} | |||
}; | |||
|
|||
template <typename Type, typename Dist> | |||
class LazyMatrix : public Matrix | |||
{ | |||
Trace<Type>& trace; | |||
Dist dist; | |||
public: | |||
LazyMatrix(Trace<Type>& trace): | |||
trace(trace) | |||
{ } | |||
|
|||
double operator()(size_t i, size_t j) { | |||
return dist(trace[i], trace[j]); | |||
} | |||
}; | |||
|
|||
|
|||
}; // namespace SVF | |||
#endif // __SVF_MATRIX_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,112 @@ | |||
#ifndef __SVF_MATRIX_CORRELATE_HPP__ | |||
#define __SVF_MATRIX_CORRELATE_HPP__ | |||
|
|||
#include <cassert> | |||
#include <cmath> | |||
#include <boost/optional.hpp> | |||
|
|||
namespace SVF { | |||
|
|||
class Selector { | |||
public: | |||
typedef std::pair<size_t, size_t> Pair; | |||
virtual boost::optional<Pair> next() = 0; | |||
virtual void reset() = 0; | |||
}; | |||
|
|||
class FullTriangleSelector : public Selector { | |||
size_t traceSize; | |||
size_t x, y; | |||
|
|||
public: | |||
template<typename OType, typename SCType> | |||
void init(Trace<OType>& oTrace, Trace<SCType>& scTrace) { | |||
traceSize = oTrace.size(); | |||
reset(); | |||
|
|||
assert(traceSize == scTrace.size() && "Traces must be same size"); | |||
assert(traceSize > 1 && "Traces must have at least two elements each"); | |||
} | |||
|
|||
virtual void reset() { | |||
x = 0; | |||
y = 0; | |||
} | |||
|
|||
virtual boost::optional<Pair> next() { | |||
Pair ret(x++, y); | |||
if (x >= y) { | |||
if (y >= traceSize) { | |||
// We're at the end | |||
return boost::optional<Pair>(); | |||
} | |||
|
|||
// Go to next row | |||
y += 1; | |||
x = 0; | |||
} | |||
return ret; | |||
} | |||
}; | |||
|
|||
class RandomTraceProportional: public Selector { | |||
size_t traceSize; | |||
|
|||
public: | |||
RandomTraceProportional(double perTime) { | |||
} | |||
|
|||
template<typename OType, typename SCType> | |||
void init(Trace<OType>& oTrace, Trace<SCType>& scTrace) { | |||
traceSize = oTrace.size(); | |||
assert(traceSize == scTrace.size() && "Traces must be same size"); | |||
|
|||
assert(false && "Random matrix unimplemented"); | |||
} | |||
|
|||
virtual boost::optional<Pair> next() { | |||
assert(false); | |||
} | |||
}; | |||
|
|||
|
|||
class Pearson { | |||
public: | |||
double operator()(Matrix& x, Matrix& y, Selector& sel) { | |||
boost::optional<Selector::Pair> p; | |||
|
|||
size_t size = 0; | |||
double xbar = 0.0, ybar = 0.0; | |||
|
|||
sel.reset(); | |||
while ( (p = sel.next()) ) { | |||
size += 1; | |||
xbar += x[*p]; | |||
ybar += y[*p]; | |||
} | |||
|
|||
// Compute averages | |||
xbar /= size; | |||
ybar /= size; | |||
|
|||
sel.reset(); | |||
size_t secondCounter = 0; | |||
double sum = 0.0, sx = 0.0, sy = 0.0; | |||
while (p = sel.next()) { | |||
secondCounter += 1; | |||
|
|||
double xd = x[*p] - xbar; | |||
double yd = x[*p] - ybar; | |||
|
|||
sum += xd * yd; | |||
sx += xd * xd; | |||
sy += yd * yd; | |||
} | |||
assert(secondCounter == size); | |||
|
|||
return sum / (sqrt(sx) * sqrt(sy)); | |||
} | |||
}; | |||
|
|||
}; // namespace SVF | |||
#endif // __SVF_MATRIX_CORRELATE_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
#ifndef __SVF_SVF_HPP__ | |||
#define __SVF_SVF_HPP__ | |||
|
|||
#include <svf/trace.hpp> | |||
#include <svf/matrix.hpp> | |||
#include <svf/matrix_correlate.hpp> | |||
|
|||
#include <stdint.h> | |||
#include <cassert> | |||
|
|||
namespace SVF { | |||
|
|||
template <typename OracleType, typename OracleDist, | |||
typename SideChannelType, typename SideChannelDist> | |||
class SVF { | |||
Trace<OracleType> oracleTrace; | |||
Trace<SideChannelType> sideChannelTrace; | |||
|
|||
public: | |||
SVF() { } | |||
|
|||
void pushTimestep(OracleType oPoint, SideChannelType scPoint) { | |||
oracleTrace.push(oPoint); | |||
sideChannelTrace.push(scPoint); | |||
} | |||
|
|||
template<typename Selector = FullTriangleSelector, | |||
typename CorrelAlgo = Pearson> | |||
double computeSVF(Selector selector = Selector(), | |||
CorrelAlgo correlAlgo = CorrelAlgo()) | |||
{ | |||
selector.init(this->oracleTrace, this->sideChannelTrace); | |||
|
|||
LazyMatrix<OracleType, OracleDist> oMatrix(oracleTrace); | |||
LazyMatrix<SideChannelType, SideChannelDist> scMatrix(sideChannelTrace); | |||
return correlAlgo(oMatrix, scMatrix, selector); | |||
} | |||
|
|||
}; // class SVF | |||
|
|||
}; // namespace SVF | |||
|
|||
#endif // __SVF_SVF_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
#ifndef __SVF_TRACE_HPP__ | |||
#define __SVF_TRACE_HPP__ | |||
|
|||
#include <vector> | |||
|
|||
namespace SVF { | |||
|
|||
template <typename Type> | |||
class Trace { | |||
std::vector<Type> trace; | |||
|
|||
public: | |||
Trace() { | |||
} | |||
|
|||
~Trace() { | |||
} | |||
|
|||
void push(Type point) { | |||
trace.push_back(point); | |||
} | |||
|
|||
Type operator[](size_t i) { | |||
return trace[i]; | |||
} | |||
|
|||
size_t size() { | |||
return trace.size(); | |||
} | |||
}; // class Trace | |||
|
|||
}; // namespace SVF | |||
|
|||
#endif // __SVF_TRACE_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
#include <svf/svf.hpp> | |||
|
|||
#include <cppunit/TestResult.h> | |||
#include <cppunit/TestResultCollector.h> | |||
#include <cppunit/extensions/HelperMacros.h> | |||
|
|||
#include <iostream> | |||
#include <cmath> | |||
|
|||
class ExactMatch : public CPPUNIT_NS::TestCase | |||
{ | |||
CPPUNIT_TEST_SUITE(ExactMatch); | |||
CPPUNIT_TEST(testExactInt); | |||
CPPUNIT_TEST(testExactIntSampled); | |||
CPPUNIT_TEST_SUITE_END(); | |||
|
|||
public: | |||
ExactMatch() { | |||
} | |||
|
|||
~ExactMatch() { | |||
} | |||
|
|||
void setUp(void) { | |||
} | |||
|
|||
void tearDown(void) { | |||
} | |||
|
|||
protected: | |||
|
|||
struct ScalarDist { | |||
template <typename T> | |||
double operator()(T a, T b) { | |||
double ad = a; | |||
double ab = b; | |||
return sqrt(pow(ad, 2) + pow(ab, 2)); | |||
} | |||
}; | |||
|
|||
void testExactInt(void) { | |||
SVF::SVF<int, ScalarDist, int, ScalarDist> svf; | |||
|
|||
for (size_t i=0; i<1000; i++) { | |||
svf.pushTimestep(i, i); | |||
} | |||
|
|||
double svfVal = svf.computeSVF(); | |||
CPPUNIT_ASSERT_EQUAL(svfVal, 1.0); | |||
} | |||
|
|||
void testExactIntSampled(void) { | |||
SVF::SVF<int, ScalarDist, int, ScalarDist> svf; | |||
|
|||
for (size_t i=0; i<1000; i++) { | |||
svf.pushTimestep(i, i); | |||
} | |||
|
|||
//double svfVal = svf.computeSVF(SVF::RandomTraceProportional(25)); | |||
//CPPUNIT_ASSERT_EQUAL(svfVal, 1.0); | |||
CPPUNIT_ASSERT(false); | |||
} | |||
}; | |||
|
|||
CPPUNIT_TEST_SUITE_REGISTRATION(ExactMatch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
#include <iostream> | |||
|
|||
#include <cppunit/TestRunner.h> | |||
#include <cppunit/TestResult.h> | |||
#include <cppunit/TestResultCollector.h> | |||
#include <cppunit/extensions/HelperMacros.h> | |||
#include <cppunit/BriefTestProgressListener.h> | |||
|
|||
int main( int ac, char **av ) | |||
{ | |||
//--- Create the event manager and test controller | |||
CPPUNIT_NS::TestResult controller; | |||
|
|||
//--- Add a listener that colllects test result | |||
CPPUNIT_NS::TestResultCollector result; | |||
controller.addListener( &result ); | |||
|
|||
//--- Add a listener that print dots as test run. | |||
CPPUNIT_NS::BriefTestProgressListener progress; | |||
controller.addListener( &progress ); | |||
|
|||
//--- Add the top suite to the test runner | |||
CPPUNIT_NS::TestRunner runner; | |||
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); | |||
runner.run( controller ); | |||
|
|||
return result.wasSuccessful() ? 0 : 1; | |||
} |