Skip to content

Commit

Permalink
Initial version. Lots of stuff missing, but seems to work. Does
Browse files Browse the repository at this point in the history
perfect correltion on full matrix OK.
  • Loading branch information
teqdruid committed Jul 16, 2012
1 parent 54ff86e commit 586d4a1
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.o
.sconsign.dblite
#<AutoIgnore>
testrunner
#<AutoIgnore/>
11 changes: 11 additions & 0 deletions SConstruct
@@ -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'])
39 changes: 39 additions & 0 deletions svf/matrix.hpp
@@ -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__
112 changes: 112 additions & 0 deletions svf/matrix_correlate.hpp
@@ -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__
43 changes: 43 additions & 0 deletions svf/svf.hpp
@@ -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__
34 changes: 34 additions & 0 deletions svf/trace.hpp
@@ -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__
65 changes: 65 additions & 0 deletions tests/basictests.cpp
@@ -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);
28 changes: 28 additions & 0 deletions tests/testrunner.cpp
@@ -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;
}

0 comments on commit 586d4a1

Please sign in to comment.