From bae46a0de6a8a49f83ac94e70c4d06773c09b36b Mon Sep 17 00:00:00 2001 From: Martin Kromm Date: Wed, 25 Sep 2019 10:48:12 +0200 Subject: [PATCH] LowMC Reimplemented the graycode for the lowmc example such that the lowmc example can be compiled again. --- src/examples/CMakeLists.txt | 4 +-- src/examples/lowmc/common/lowmccircuit.cpp | 36 +++++++++++++++++++--- src/examples/lowmc/common/lowmccircuit.h | 5 +-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt index b2d3aa35..18e17c3e 100644 --- a/src/examples/CMakeLists.txt +++ b/src/examples/CMakeLists.txt @@ -3,9 +3,7 @@ add_subdirectory(bench_operations) add_subdirectory(euclidean_distance) add_subdirectory(float) add_subdirectory(innerproduct) -#currently not supported due to dependency to gpl code -#TODO make it work again -#add_subdirectory(lowmc) +add_subdirectory(lowmc) add_subdirectory(millionaire_prob) add_subdirectory(min-euclidean-dist) add_subdirectory(psi_phasing) diff --git a/src/examples/lowmc/common/lowmccircuit.cpp b/src/examples/lowmc/common/lowmccircuit.cpp index eed2a5f8..2eb006a4 100644 --- a/src/examples/lowmc/common/lowmccircuit.cpp +++ b/src/examples/lowmc/common/lowmccircuit.cpp @@ -20,7 +20,8 @@ #include static uint32_t m_nRndCtr; -static code* m_tGrayCode; +static uint32_t* m_tGrayCode; +static uint32_t* m_tGrayCodeIncrement; static uint32_t m_nZeroGate; //sboxes (m), key-length (k), statesize (n), data (d), rounds (r) @@ -91,8 +92,8 @@ share* BuildLowMCCircuit(share* val, share* key, BooleanCircuit* circ, LowMCPara m_nZeroGate = zerogate; //Build the GrayCode for the optimal window-size - uint32_t wsize = floor_log2(statesize) - 2; - m_tGrayCode = build_code(wsize); + m_tGrayCode = BuildGrayCode(statesize); + m_tGrayCodeIncrement = BuildGrayCodeIncrement(statesize); //copy the input to the current state for (i = 0; i < statesize; i++) @@ -117,7 +118,8 @@ share* BuildLowMCCircuit(share* val, share* key, BooleanCircuit* circ, LowMCPara } - destroy_code(m_tGrayCode); + free(m_tGrayCode); + free(m_tGrayCodeIncrement); #if PRINT_PERFORMANCE_STATS std::cout << "Total Number of Boolean Gates: " << circ->GetNumGates() << std::endl; @@ -220,7 +222,7 @@ void FourRussiansMatrixMult(std::vector& state, uint32_t lowmcstatesiz for (i = 0, bitctr = 0; i < ceil_divide(lowmcstatesize, wsize); i++) { //for each column-window for (j = 1; j < (1 << wsize); j++) { - lut[m_tGrayCode->ord[j]] = circ->PutXORGate(lut[m_tGrayCode->ord[j - 1]], state_pad[i * wsize + m_tGrayCode->inc[j - 1]]); + lut[m_tGrayCode[j]] = circ->PutXORGate(lut[m_tGrayCode[j - 1]], state_pad[i * wsize + m_tGrayCodeIncrement[j - 1]]); } for (j = 0; j < lowmcstatesize; j++, bitctr += wsize) { @@ -273,3 +275,27 @@ void CallbackMultiplyAndDestroy4RMatrix(GATE* gate, void* matrix) { //TODO } +uint32_t* BuildGrayCode(uint32_t length) { + uint32_t* gray_code = (uint32_t*) malloc(sizeof(uint32_t) * length); + for(uint32_t i = 0; i < length; ++i) { + gray_code[i] = i ^ (i >> 1); + } + return gray_code; +} + +uint32_t* BuildGrayCodeIncrement(uint32_t length) { + uint32_t* gray_code_increment = (uint32_t*) malloc(sizeof(uint32_t) * length); + for(uint32_t i = 0; i < length; ++i) { + gray_code_increment[i] = 0; + } + uint32_t length_inc = 2; + while(length_inc < length) { + uint32_t length_count = length_inc - 1; + while(length_count <= length) { + (gray_code_increment[length_count])++; + length_count += length_inc; + } + length_inc <<= 1; + } + return gray_code_increment; +} diff --git a/src/examples/lowmc/common/lowmccircuit.h b/src/examples/lowmc/common/lowmccircuit.h index b68ca271..ad4eaf76 100644 --- a/src/examples/lowmc/common/lowmccircuit.h +++ b/src/examples/lowmc/common/lowmccircuit.h @@ -22,8 +22,6 @@ #include "../../../abycore/aby/abyparty.h" #include #include -//TODO make the graycode functionallity work again, the current graycode implementation depends on gpl code -//#include #include static const BYTE mpccseed[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; @@ -74,4 +72,7 @@ void CallbackMultiplyAndDestroy4RMatrix(GATE* gate, void* matrix); void FourRussiansMatrixMult(std::vector& state, uint32_t lowmcstatesize, BooleanCircuit* circ); +uint32_t* BuildGrayCode(uint32_t length); +uint32_t* BuildGrayCodeIncrement(uint32_t length); + #endif /* __LOWMCCIRCUIT_H_ */