From 586d4a1f99c581b73ef03dfc4b3be0f3c4c272a6 Mon Sep 17 00:00:00 2001 From: John D Demme Date: Mon, 16 Jul 2012 15:58:32 -0400 Subject: [PATCH] Initial version. Lots of stuff missing, but seems to work. Does perfect correltion on full matrix OK. --- .gitignore | 5 ++ SConstruct | 11 ++++ svf/matrix.hpp | 39 ++++++++++++++ svf/matrix_correlate.hpp | 112 +++++++++++++++++++++++++++++++++++++++ svf/svf.hpp | 43 +++++++++++++++ svf/trace.hpp | 34 ++++++++++++ tests/basictests.cpp | 65 +++++++++++++++++++++++ tests/testrunner.cpp | 28 ++++++++++ 8 files changed, 337 insertions(+) create mode 100644 .gitignore create mode 100644 SConstruct create mode 100644 svf/matrix.hpp create mode 100644 svf/matrix_correlate.hpp create mode 100644 svf/svf.hpp create mode 100644 svf/trace.hpp create mode 100644 tests/basictests.cpp create mode 100644 tests/testrunner.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5036ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +.sconsign.dblite +# +testrunner +# diff --git a/SConstruct b/SConstruct new file mode 100644 index 0000000..7daafb4 --- /dev/null +++ b/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']) diff --git a/svf/matrix.hpp b/svf/matrix.hpp new file mode 100644 index 0000000..36bdc77 --- /dev/null +++ b/svf/matrix.hpp @@ -0,0 +1,39 @@ +#ifndef __SVF_MATRIX_HPP__ +#define __SVF_MATRIX_HPP__ + +#include + +namespace SVF { + +class Matrix { +public: + Matrix() { + } + + virtual ~Matrix() { + } + + virtual double operator()(size_t i, size_t j) = 0; + double operator[](std::pair p) { + return (*this)(p.first, p.second); + } +}; + +template +class LazyMatrix : public Matrix +{ + Trace& trace; + Dist dist; +public: + LazyMatrix(Trace& trace): + trace(trace) + { } + + double operator()(size_t i, size_t j) { + return dist(trace[i], trace[j]); + } +}; + + +}; // namespace SVF +#endif // __SVF_MATRIX_HPP__ diff --git a/svf/matrix_correlate.hpp b/svf/matrix_correlate.hpp new file mode 100644 index 0000000..9f2cacc --- /dev/null +++ b/svf/matrix_correlate.hpp @@ -0,0 +1,112 @@ +#ifndef __SVF_MATRIX_CORRELATE_HPP__ +#define __SVF_MATRIX_CORRELATE_HPP__ + +#include +#include +#include + +namespace SVF { + +class Selector { +public: + typedef std::pair Pair; + virtual boost::optional next() = 0; + virtual void reset() = 0; +}; + +class FullTriangleSelector : public Selector { + size_t traceSize; + size_t x, y; + +public: + template + void init(Trace& oTrace, Trace& 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 next() { + Pair ret(x++, y); + if (x >= y) { + if (y >= traceSize) { + // We're at the end + return boost::optional(); + } + + // Go to next row + y += 1; + x = 0; + } + return ret; + } +}; + +class RandomTraceProportional: public Selector { + size_t traceSize; + +public: + RandomTraceProportional(double perTime) { + } + + template + void init(Trace& oTrace, Trace& scTrace) { + traceSize = oTrace.size(); + assert(traceSize == scTrace.size() && "Traces must be same size"); + + assert(false && "Random matrix unimplemented"); + } + + virtual boost::optional next() { + assert(false); + } +}; + + +class Pearson { +public: + double operator()(Matrix& x, Matrix& y, Selector& sel) { + boost::optional 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__ diff --git a/svf/svf.hpp b/svf/svf.hpp new file mode 100644 index 0000000..6a8e1e4 --- /dev/null +++ b/svf/svf.hpp @@ -0,0 +1,43 @@ +#ifndef __SVF_SVF_HPP__ +#define __SVF_SVF_HPP__ + +#include +#include +#include + +#include +#include + +namespace SVF { + +template +class SVF { + Trace oracleTrace; + Trace sideChannelTrace; + +public: + SVF() { } + + void pushTimestep(OracleType oPoint, SideChannelType scPoint) { + oracleTrace.push(oPoint); + sideChannelTrace.push(scPoint); + } + + template + double computeSVF(Selector selector = Selector(), + CorrelAlgo correlAlgo = CorrelAlgo()) + { + selector.init(this->oracleTrace, this->sideChannelTrace); + + LazyMatrix oMatrix(oracleTrace); + LazyMatrix scMatrix(sideChannelTrace); + return correlAlgo(oMatrix, scMatrix, selector); + } + +}; // class SVF + +}; // namespace SVF + +#endif // __SVF_SVF_HPP__ diff --git a/svf/trace.hpp b/svf/trace.hpp new file mode 100644 index 0000000..f61f18d --- /dev/null +++ b/svf/trace.hpp @@ -0,0 +1,34 @@ +#ifndef __SVF_TRACE_HPP__ +#define __SVF_TRACE_HPP__ + +#include + +namespace SVF { + +template +class Trace { + std::vector 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__ diff --git a/tests/basictests.cpp b/tests/basictests.cpp new file mode 100644 index 0000000..c51cc55 --- /dev/null +++ b/tests/basictests.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include +#include + +#include +#include + +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 + 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 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 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); diff --git a/tests/testrunner.cpp b/tests/testrunner.cpp new file mode 100644 index 0000000..7c28960 --- /dev/null +++ b/tests/testrunner.cpp @@ -0,0 +1,28 @@ +#include + +#include +#include +#include +#include +#include + +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; +}