diff --git a/secure-join/AggTree/PerfectShuffle.h b/secure-join/AggTree/PerfectShuffle.h index 6a9fc1eaf..4c43f3cc2 100644 --- a/secure-join/AggTree/PerfectShuffle.h +++ b/secure-join/AggTree/PerfectShuffle.h @@ -1,7 +1,9 @@ #pragma once #include "secure-join/Defines.h" #include "cryptoTools/Common/Defines.h" +#include #include +#include namespace secJoin { @@ -74,12 +76,13 @@ namespace secJoin u64 n32 = output.size() / sizeof(u32); - auto in0 = (u16*)input0.data(); - auto in1 = (u16*)input1.data(); - auto out = (u32*)output.data(); for (u64 i = 0; i < n32; ++i) { - out[i] = cPerfectShuffle(in0[i], in1[i]); + u16 a = 0, b = 0; + std::memcpy(&a, input0.data() + i * sizeof(u16), sizeof(a)); + std::memcpy(&b, input1.data() + i * sizeof(u16), sizeof(b)); + u32 packed = cPerfectShuffle(a, b); + std::memcpy(output.data() + i * sizeof(u32), &packed, sizeof(packed)); } auto n8 = n32 * sizeof(u32); @@ -102,17 +105,16 @@ namespace secJoin if(output0.size() != (input.size() + 1) / 2) throw RTE_LOC; u64 n32 = input.size() / sizeof(u32); - auto out0 = (u16*)output0.data(); - auto out1 = (u16*)output1.data(); - auto in = (u32*)input.data(); for (u64 i = 0; i < n32; ++i) { - auto t = cPerfectUnshuffle(in[i]); - assert((u8*)&(out0[i]) < output0.data() + output0.size()); - assert((u8*)&(out1[i]) < output1.data() + output1.size()); - - out0[i] = ((u16*)&t)[0]; - out1[i] = ((u16*)&t)[1]; + u32 word = 0; + std::memcpy(&word, input.data() + i * sizeof(u32), sizeof(word)); + auto t = cPerfectUnshuffle(word); + assert(output0.data() + (i + 1) * sizeof(u16) <= output0.data() + output0.size()); + assert(output1.data() + (i + 1) * sizeof(u16) <= output1.data() + output1.size()); + + std::memcpy(output0.data() + i * sizeof(u16), &t[0], sizeof(u16)); + std::memcpy(output1.data() + i * sizeof(u16), &t[1], sizeof(u16)); } auto n8 = n32 * sizeof(u32); @@ -257,18 +259,18 @@ namespace secJoin } // perfect shuffle 4 blocks on x0,x1 into 8 blocks of y. - inline void ssePerfectShuffle(const oc::block* x0, const oc::block* x1, oc::block* y) + OC_FORCEINLINE void ssePerfectShuffle(const oc::block* x0, const oc::block* x1, oc::block* y) { // perfect shuffle the bytes. const oc::block b = _mm_set_epi8(15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0); - y[0] = _mm_set_epi64x(((u64*)x1)[0], ((u64*)x0)[0]); - y[1] = _mm_set_epi64x(((u64*)x1)[1], ((u64*)x0)[1]); - y[2] = _mm_set_epi64x(((u64*)x1)[2], ((u64*)x0)[2]); - y[3] = _mm_set_epi64x(((u64*)x1)[3], ((u64*)x0)[3]); - y[4] = _mm_set_epi64x(((u64*)x1)[4], ((u64*)x0)[4]); - y[5] = _mm_set_epi64x(((u64*)x1)[5], ((u64*)x0)[5]); - y[6] = _mm_set_epi64x(((u64*)x1)[6], ((u64*)x0)[6]); - y[7] = _mm_set_epi64x(((u64*)x1)[7], ((u64*)x0)[7]); + y[0] = _mm_unpacklo_epi64(x0[0], x1[0]); + y[1] = _mm_unpackhi_epi64(x0[0], x1[0]); + y[2] = _mm_unpacklo_epi64(x0[1], x1[1]); + y[3] = _mm_unpackhi_epi64(x0[1], x1[1]); + y[4] = _mm_unpacklo_epi64(x0[2], x1[2]); + y[5] = _mm_unpackhi_epi64(x0[2], x1[2]); + y[6] = _mm_unpacklo_epi64(x0[3], x1[3]); + y[7] = _mm_unpackhi_epi64(x0[3], x1[3]); y[0] = _mm_shuffle_epi8(y[0], b); y[1] = _mm_shuffle_epi8(y[1], b); y[2] = _mm_shuffle_epi8(y[2], b); @@ -285,11 +287,10 @@ namespace secJoin } // perfect unshuffle 8 blocks of y into 4 blocks on x0,x1 into. - inline void ssePerfectUnshuffle(const oc::block* yy, oc::block* x0, oc::block* x1) + OC_FORCEINLINE void ssePerfectUnshuffle(const oc::block* yy, oc::block* x0, oc::block* x1) { std::array y; - std::copy((u8*)yy, (u8*)(yy + y.size()), (u8*)y.data()); - // m emcpy(y.data(), yy, sizeof(y)); + std::memcpy(y.data(), yy, sizeof(y)); // perfect shuffle the bits. ssePerfectShuffle_round<1>(y.data()); @@ -308,26 +309,14 @@ namespace secJoin y[7] = _mm_shuffle_epi8(y[7], b); - u64* yyy = (u64*)y.data(); - u64* xx1 = (u64*)x1; - u64* xx0 = (u64*)x0; - xx0[0] = yyy[0]; - xx1[0] = yyy[1]; - xx0[1] = yyy[2]; - xx1[1] = yyy[3]; - xx0[2] = yyy[4]; - xx1[2] = yyy[5]; - xx0[3] = yyy[6]; - xx1[3] = yyy[7]; - - xx0[4] = yyy[8]; - xx1[4] = yyy[9]; - xx0[5] = yyy[10]; - xx1[5] = yyy[11]; - xx0[6] = yyy[12]; - xx1[6] = yyy[13]; - xx0[7] = yyy[14]; - xx1[7] = yyy[15]; + x0[0] = _mm_unpacklo_epi64(y[0], y[1]); + x1[0] = _mm_unpackhi_epi64(y[0], y[1]); + x0[1] = _mm_unpacklo_epi64(y[2], y[3]); + x1[1] = _mm_unpackhi_epi64(y[2], y[3]); + x0[2] = _mm_unpacklo_epi64(y[4], y[5]); + x1[2] = _mm_unpackhi_epi64(y[4], y[5]); + x0[3] = _mm_unpacklo_epi64(y[6], y[7]); + x1[3] = _mm_unpackhi_epi64(y[6], y[7]); } inline void ssePerfectShuffle(span input0, span input1, span output) @@ -336,15 +325,14 @@ namespace secJoin assert(input0.size() == (output.size() + 1) / 2); u64 n1024 = output.size() / sizeof(std::array); - auto in0 = (oc::block*)input0.data(); - auto in1 = (oc::block*)input1.data(); - auto out = (oc::block*)output.data(); for (u64 i = 0; i < n1024; ++i) { - ssePerfectShuffle(in0, in1, out); - in0 += 4; - in1 += 4; - out += 8; + std::array x0, x1; + std::array y; + std::memcpy(x0.data(), input0.data() + i * sizeof(x0), sizeof(x0)); + std::memcpy(x1.data(), input1.data() + i * sizeof(x1), sizeof(x1)); + ssePerfectShuffle(x0.data(), x1.data(), y.data()); + std::memcpy(output.data() + i * sizeof(y), y.data(), sizeof(y)); } auto n64 = n1024 * 16; @@ -377,19 +365,14 @@ namespace secJoin u64 n1024 = input.size() / sizeof(std::array); - auto out0 = (oc::block*)output0.data(); - auto out1 = (oc::block*)output1.data(); - auto in = (oc::block*)input.data(); for (u64 i = 0; i < n1024; ++i) { - assert((u8*)(in + 8) <= input.data() + input.size()); - assert((u8*)(out0 + 4) <= output0.data() + output0.size()); - assert((u8*)(out1 + 4) <= output1.data() + output1.size()); - ssePerfectUnshuffle(in, out0, out1); - - in += 8; - out0 += 4; - out1 += 4; + std::array y; + std::array x0, x1; + std::memcpy(y.data(), input.data() + i * sizeof(y), sizeof(y)); + ssePerfectUnshuffle(y.data(), x0.data(), x1.data()); + std::memcpy(output0.data() + i * sizeof(x0), x0.data(), sizeof(x0)); + std::memcpy(output1.data() + i * sizeof(x1), x1.data(), sizeof(x1)); } @@ -434,4 +417,4 @@ namespace secJoin cPerfectUnshuffle(input, output0, output1); #endif } -} \ No newline at end of file +} diff --git a/secure-join/CorGenerator/BinOleBatch.cpp b/secure-join/CorGenerator/BinOleBatch.cpp index d27a9b003..8379835cf 100644 --- a/secure-join/CorGenerator/BinOleBatch.cpp +++ b/secure-join/CorGenerator/BinOleBatch.cpp @@ -2,6 +2,7 @@ #include "CorGenerator.h" #include "secure-join/Util/match.h" #include "secure-join/Util/Simd.h" +#include namespace secJoin { @@ -264,7 +265,7 @@ namespace secJoin span add, span mult) { - auto aIter16 = (u16*)add.data(); + auto aIter = reinterpret_cast(add.data()); auto bIter8 = (u8*)mult.data(); @@ -379,7 +380,8 @@ namespace secJoin u16 ap = movemask_epi8(a00); - *aIter16++ = ap; + std::memcpy(aIter, &ap, sizeof(ap)); + aIter += sizeof(ap); m += 16; } @@ -393,8 +395,8 @@ namespace secJoin span mult) { - auto bIter16 = (u16*)add.data(); - auto aIter16 = (u16*)mult.data(); + auto bIter = reinterpret_cast(add.data()); + auto aIter = reinterpret_cast(mult.data()); if (add.size() * 128 != B.size()) throw RTE_LOC; @@ -533,11 +535,14 @@ namespace secJoin u16 ap = movemask_epi8(a00); u16 bp = movemask_epi8(b00); - assert(aIter16 < (u16*)(mult.data() + mult.size())); - assert(bIter16 < (u16*)(add.data() + add.size())); + assert(aIter + sizeof(u16) <= reinterpret_cast(mult.data() + mult.size())); + assert(bIter + sizeof(u16) <= reinterpret_cast(add.data() + add.size())); - *aIter16++ = ap ^ bp; - *bIter16++ = ap; + u16 mixed = ap ^ bp; + std::memcpy(aIter, &mixed, sizeof(mixed)); + aIter += sizeof(mixed); + std::memcpy(bIter, &ap, sizeof(ap)); + bIter += sizeof(ap); } } diff --git a/secure-join/CorGenerator/F4BitOtBatch.cpp b/secure-join/CorGenerator/F4BitOtBatch.cpp index c79be6e71..87000b2dc 100644 --- a/secure-join/CorGenerator/F4BitOtBatch.cpp +++ b/secure-join/CorGenerator/F4BitOtBatch.cpp @@ -2,6 +2,7 @@ #include "CorGenerator.h" #include "secure-join/Util/match.h" #include "secure-join/Util/Simd.h" +#include namespace secJoin { @@ -449,7 +450,7 @@ namespace secJoin mOts.resize(A.size()); mChoiceLsb.resize(A.size()); mChoiceMsb.resize(A.size()); - auto aIter16 = (u16*)mOts.data(); + auto aIter = reinterpret_cast(mOts.data()); auto lsbIter8 = (u8*)mChoiceLsb.data(); auto msbIter8 = (u8*)mChoiceMsb.data(); @@ -575,7 +576,8 @@ namespace secJoin u16 ap = movemask_epi8(a00); - *aIter16++ = ap; + std::memcpy(aIter, &ap, sizeof(ap)); + aIter += sizeof(ap); m += 16; } @@ -588,12 +590,12 @@ namespace secJoin for (u64 i = 0; i < 4; ++i) mOts[i].resize(B.size()); - std::array iters + std::array iters { - (u16*)mOts[0].data(), - (u16*)mOts[1].data(), - (u16*)mOts[2].data(), - (u16*)mOts[3].data() + reinterpret_cast(mOts[0].data()), + reinterpret_cast(mOts[1].data()), + reinterpret_cast(mOts[2].data()), + reinterpret_cast(mOts[3].data()) }; //auto bIter16 = (u16*)add.data(); //auto aIter16 = (u16*)mult.data(); @@ -716,8 +718,9 @@ namespace secJoin u16 ap = movemask_epi8(a00); - assert(iters[b] < (u16*)(mOts[b].data() + mOts[b].size())); - *iters[b]++ = ap; + assert(iters[b] + sizeof(ap) <= reinterpret_cast(mOts[b].data() + mOts[b].size())); + std::memcpy(iters[b], &ap, sizeof(ap)); + iters[b] += sizeof(ap); } } } diff --git a/secure-join/Prf/mod3.cpp b/secure-join/Prf/mod3.cpp index b50b2895b..8215e1676 100644 --- a/secure-join/Prf/mod3.cpp +++ b/secure-join/Prf/mod3.cpp @@ -4,6 +4,10 @@ #include "cryptoTools/Common/BitVector.h" #include "cryptoTools/Common/Aligned.h" #include "secure-join/Util/Simd.h" +#include +#include +#include +#include namespace secJoin { @@ -321,15 +325,6 @@ namespace secJoin : mVal(v) {} - SampleBlock256(SampleBlock256* v) - { - load(v); - } - void load(SampleBlock256* v) - { - mVal = _mm256_load_si256(&v->mVal); - } - static auto set32(u32 v) { return _mm256_set1_epi32(v); @@ -396,16 +391,6 @@ namespace secJoin SampleBlock256() = default; - SampleBlock256(SampleBlock256* v) - { - load(v); - } - void load(SampleBlock256* v) - { - mVal[0] = block(((block*)v)[0]); - mVal[1] = block(((block*)v)[1]); - } - static auto set32(u32 v) { SampleBlock256 r; @@ -443,39 +428,36 @@ namespace secJoin template SampleBlock256 srli_epi32() const { - - SampleBlock256 r; - auto rr = (i32*)&r; - auto vv = (const i32*)&mVal; + auto src = std::bit_cast>(*this); + std::array dst{}; for (u64 i = 0; i < 8; ++i) - rr[i] = vv[i] >> s; - return r; - //return _mm256_srli_epi32(mVal, s); + dst[i] = src[i] >> s; + return std::bit_cast(dst); } SampleBlock256 sllv_epi32(const SampleBlock256& s) const { - SampleBlock256 r; - auto rr = (i32*)&r; - auto ss = (const i32*)&s; - auto vv = (const i32*)&mVal; + auto src = std::bit_cast>(*this); + auto sh = std::bit_cast>(s); + std::array dst{}; for (u64 i = 0; i < 8; ++i) - rr[i] = vv[i] << ss[i]; - return r; - //return _mm256_sllv_epi32(mVal, s.mVal); + dst[i] = src[i] << sh[i]; + return std::bit_cast(dst); } template static SampleBlock256 i32gather_epi32(const int* ptr, const SampleBlock256& index) { - SampleBlock256 r; - auto rr = (i32*)&r; - auto idx = (const i32*)&index; + auto idx = std::bit_cast>(index); + std::array dst{}; for (u64 i = 0; i < 8; ++i) - rr[i] = *(const int*)((char*)ptr + idx[i] * v); - return r; - //return _mm256_i32gather_epi32(ptr, index.mVal, v); + { + i32 lane; + std::memcpy(&lane, reinterpret_cast(ptr) + static_cast(idx[i]) * v, sizeof(lane)); + dst[i] = lane; + } + return std::bit_cast(dst); } int testz_si256(const SampleBlock256& o) const @@ -490,19 +472,21 @@ namespace secJoin SampleBlock256 add_epi32(const SampleBlock256& s) const { - - SampleBlock256 r; - auto rr = (i32*)&r; - auto ss = (const i32*)&s; - auto vv = (const i32*)&mVal; + auto src = std::bit_cast>(*this); + auto rhs = std::bit_cast>(s); + std::array dst{}; for (u64 i = 0; i < 8; ++i) - rr[i] = vv[i] + ss[i]; - return r; + dst[i] = src[i] + rhs[i]; + return std::bit_cast(dst); } }; #endif + static_assert(std::is_trivially_copyable_v); + static_assert(sizeof(SampleBlock256) == sizeof(std::array)); + static_assert(sizeof(SampleBlock256) == 2 * sizeof(block)); + void sampleMod3Lookup(PRNG& prng, span msbVec, span lsbVec) { //if ((u64)msbVec.data() % 32) @@ -519,9 +503,9 @@ namespace secJoin oc::AlignedArray rands; oc::AlignedArray rands2; - auto e = (SampleBlock256*)(rands.data() + rands.size()); + const block* const randsEnd = rands.data() + rands.size(); auto e2 = (u8*)(rands2.data() + rands2.size()); - auto randsPtr = e; + const block* randsPtr = randsEnd; u8* rands2Ptr = e2; oc::AlignedArray lsbSum; @@ -539,35 +523,25 @@ namespace secJoin for (u64 k = 0; k < 8; ++k) { - if (randsPtr >= e) + if (randsPtr == randsEnd) { prng.mAes.ecbEncCounterMode(prng.mBlockIdx, rands.size(), rands.data()); prng.mBlockIdx += rands.size(); - randsPtr = (SampleBlock256*)rands.data(); + randsPtr = rands.data(); } - SampleBlock256 indexBase(randsPtr); + SampleBlock256 indexBase; + std::memcpy(&indexBase, randsPtr, sizeof(indexBase)); + randsPtr += 2; for (u64 j = 0; j < lsbSum.size(); ++j) { auto indexes = indexBase & SampleBlock256::set32(255); indexBase = indexBase.srli_epi32<8>(); - //__m256i indexes = _mm256_set_epi32( - // randsPtr->m256i_u8[j * 8 + 7], - // randsPtr->m256i_u8[j * 8 + 6], - // randsPtr->m256i_u8[j * 8 + 5], - // randsPtr->m256i_u8[j * 8 + 4], - // randsPtr->m256i_u8[j * 8 + 3], - // randsPtr->m256i_u8[j * 8 + 2], - // randsPtr->m256i_u8[j * 8 + 1], - // randsPtr->m256i_u8[j * 8 + 0] - //); - v_[j] = SampleBlock256::i32gather_epi32<4>((const i32*)mod3TableV.data(), indexes); lsb_[j] = SampleBlock256::i32gather_epi32<4>((const i32*)mod3TableLsb.data(), indexes); msb_[j] = SampleBlock256::i32gather_epi32<4>((const i32*)mod3TableMsb.data(), indexes); } - randsPtr++; // shift the sum if we have a valid sample lsbSum[0] = lsbSum[0].sllv_epi32(v_[0]); @@ -615,16 +589,15 @@ namespace secJoin for (u64 j = 0; j < lsbSum.size(); ++j) { + auto lsbLanes = std::bit_cast>(lsbSum[j]); + auto msbLanes = std::bit_cast>(msbSum[j]); + auto sizeLanes = std::bit_cast>(size[j]); for (u64 k = 0; k < 8; ++k) { - u32* lsbSum32 = (u32*)&lsbSum[j]; - u32* msbSum32 = (u32*)&msbSum[j]; - u32* size32 = (u32*)&size[j]; - - auto& lsbk = lsbSum32[k]; - auto& msbk = msbSum32[k]; - auto& sizek = size32[k]; + auto& lsbk = lsbLanes[k]; + auto& msbk = msbLanes[k]; + auto& sizek = sizeLanes[k]; while (sizek < 32) { @@ -647,6 +620,9 @@ namespace secJoin sizek += flag; } } + + lsbSum[j] = std::bit_cast(lsbLanes); + msbSum[j] = std::bit_cast(msbLanes); } auto s = std::min(msbVec.size() - i, lsbSum.size() * 2); diff --git a/secure-join/Sort/BitInjection.cpp b/secure-join/Sort/BitInjection.cpp index 92f77ad49..6a115c66a 100644 --- a/secure-join/Sort/BitInjection.cpp +++ b/secure-join/Sort/BitInjection.cpp @@ -1,24 +1,36 @@ #include "BitInjection.h" +#include namespace secJoin { - inline void unpack(span in, u64 bitCount, span out) + template + static void unpackWords(span in, span out) { + for (u64 i = 0; i < out.size(); ++i) + { + u32 word = 0; + std::memcpy(&word, in.data() + i * Bytes, Bytes); + out[i] = word; + } + } + inline void unpack(span in, u64 bitCount, span out) + { + auto bytes = oc::divCeil(bitCount, 8); + if (out.size() * bytes != in.size()) + throw RTE_LOC; - if (bitCount == 32) - osuCrypto::copyBytes(out, in); - else + switch (bytes) { - auto n = oc::divCeil(bitCount, 8); - if (out.size() * n != in.size()) - throw RTE_LOC; - for (u64 j = 0; j < out.size(); ++j) - out[j] = *(u32*)&in[j * n]; + case 0: std::fill(out.begin(), out.end(), 0); break; + case 1: unpackWords<1>(in, out); break; + case 2: unpackWords<2>(in, out); break; + case 3: unpackWords<3>(in, out); break; + case 4: osuCrypto::copyBytes(out, in); break; + default: throw RTE_LOC; } - } inline void pack(span in, u64 bitCount, span out) { @@ -79,6 +91,9 @@ namespace secJoin auto updates = oc::AlignedUnVector{}; auto mask = u32{}; + if (outBitCount > 32) + throw std::runtime_error("outBitCount exceeds 32 bits. " LOCATION); + if (mInBitCount > in.cols() * 8) throw std::runtime_error("mInBitCount longer than the row size. " LOCATION); @@ -93,7 +108,7 @@ namespace secJoin out.resize(in.rows(), mInBitCount); - mask = outBitCount == 32 ? -1 : ((1 << outBitCount) - 1); + mask = outBitCount == 32 ? ~u32{0} : ((u32{1} << outBitCount) - 1); if (mRole) { diff --git a/secure-join/Sort/RadixSort.cpp b/secure-join/Sort/RadixSort.cpp index ef2c89f0a..db121e2be 100644 --- a/secure-join/Sort/RadixSort.cpp +++ b/secure-join/Sort/RadixSort.cpp @@ -1,5 +1,6 @@ #include "RadixSort.h" #include "secure-join/Sort/BitInjection.h" +#include namespace secJoin { @@ -865,7 +866,8 @@ namespace secJoin for (u64 i = 0; i < n; ++i) { - u16 x = *(u16*)s0; + u16 x = 0; + std::memcpy(&x, s0, sizeof(x)); sk(i) = (x >> shift) & mask; s0 += step; } diff --git a/secure-join/Util/Simd.h b/secure-join/Util/Simd.h index dd26a6e4b..d11f4c90f 100644 --- a/secure-join/Util/Simd.h +++ b/secure-join/Util/Simd.h @@ -1,5 +1,6 @@ #pragma once #include "secure-join/Defines.h" +#include #ifdef ENABLE_SSE #include #endif @@ -105,7 +106,8 @@ namespace secJoin #ifdef ENABLE_SSE _mm_storeu_si32(mem, a); #else - * (i32*)mem = a.get(0); + i32 lane = a.get(0); + std::memcpy(mem, &lane, sizeof(lane)); #endif } } diff --git a/tests/AggTree_Tests.cpp b/tests/AggTree_Tests.cpp index b842d0339..e04e09c3f 100644 --- a/tests/AggTree_Tests.cpp +++ b/tests/AggTree_Tests.cpp @@ -173,6 +173,25 @@ void perfectShuffle_span_Test() if (XX[1] != X[1]) throw RTE_LOC; } + + // Unaligned byte spans spanning a full SIMD batch and an odd tail. + alignas(block) std::array input, actual; + alignas(block) std::array evens, odds; + auto packed = oc::span(input).subspan(1); + auto roundTrip = oc::span(actual).subspan(1); + auto even = oc::span(evens).subspan(1, 65); + auto odd = oc::span(odds).subspan(2, 65); + prng.get(packed.data(), packed.size()); + + std::array refEven, refOdd; + cPerfectUnshuffle(packed, refEven, refOdd); + perfectUnshuffle(packed, even, odd); + if (!std::equal(even.begin(), even.end(), refEven.begin()) || + !std::equal(odd.begin(), odd.end(), refOdd.begin())) + throw RTE_LOC; + perfectShuffle(even, odd, roundTrip); + if (!std::equal(roundTrip.begin(), roundTrip.end(), packed.begin())) + throw RTE_LOC; } void perfectShuffle_128_Test() diff --git a/tests/AltModWPrf_Test.cpp b/tests/AltModWPrf_Test.cpp index 273ee800a..b7e4d7c4c 100644 --- a/tests/AltModWPrf_Test.cpp +++ b/tests/AltModWPrf_Test.cpp @@ -163,7 +163,17 @@ void AltModWPrf_mod3BitDecompostion_test() void AltModWPrf_sampleMod3_test(const oc::CLP& cmd) { - + { + // The 65th output block crosses the 128-block random buffer refill. + PRNG prng(oc::ZeroBlock); + std::array msb, lsb; + sampleMod3Lookup(prng, msb, lsb); + if (msb.front() != block(0x4891092ad0281014ull, 0xb804758096382277ull) || + lsb.front() != block(0x1648668104450b62ull, 0x439b084468069488ull) || + msb.back() != block(0x850452644220150bull, 0x089706388b014672ull) || + lsb.back() != block(0x0a9a08102c530234ull, 0x360850075462380dull)) + throw RTE_LOC; + } std::array, 256>const mod3TableFull{ { { 0, 0, 0, 0, 0},{ 1, 0, 0, 0, 0},{ 2, 0, 0, 0, 0},{ 0, 1, 0, 0, 0},{ 1, 1, 0, 0, 0},{ 2, 1, 0, 0, 0},{ 0, 2, 0, 0, 0},{ 1, 2, 0, 0, 0}, { 2, 2, 0, 0, 0},{ 0, 0, 1, 0, 0},{ 1, 0, 1, 0, 0},{ 2, 0, 1, 0, 0},{ 0, 1, 1, 0, 0},{ 1, 1, 1, 0, 0},{ 2, 1, 1, 0, 0},{ 0, 2, 1, 0, 0}, diff --git a/tests/GMW_Test.cpp b/tests/GMW_Test.cpp index 802e639e9..c21513c42 100644 --- a/tests/GMW_Test.cpp +++ b/tests/GMW_Test.cpp @@ -10,6 +10,7 @@ #include "coproto/Socket/LocalAsyncSock.h" #include "secure-join/Util/Util.h" +#include using coproto::LocalAsyncSocket; using namespace secJoin; @@ -213,10 +214,11 @@ namespace secJoin_Tests for (u64 i = 0; i < n; ++i) { - auto& i0s0 = *(i64*)s0[i].data(); - auto& i0s1 = *(i64*)s1[i].data(); - + i64 i0s0 = 0; + i64 i0s1 = 0; + std::memcpy(&i0s0, s0[i].data(), sizeof(i0s0)); i0s1 = v[i] ^ i0s0; + std::memcpy(s1[i].data(), &i0s1, sizeof(i0s1)); } return { s0, s1 }; diff --git a/tests/RadixSort_Test.cpp b/tests/RadixSort_Test.cpp index f0f2ce269..39672b860 100644 --- a/tests/RadixSort_Test.cpp +++ b/tests/RadixSort_Test.cpp @@ -192,7 +192,7 @@ void RadixSort_oneHot_test(const oc::CLP& cmd) } -void RadixSort_bitInjection_test(const oc::CLP& cmd) +static void checkBitInjection(const oc::CLP& cmd, u64 outBits) { auto comm = coproto::LocalAsyncSocket::makePair(); @@ -230,11 +230,15 @@ void RadixSort_bitInjection_test(const oc::CLP& cmd) bi0.preprocess(); bi1.preprocess(); - macoro::sync_wait(macoro::when_all_ready( - bi0.bitInjection(k0, 32, f0, comm[0]), - bi1.bitInjection(k1, 32, f1, comm[1]), + auto tasks = macoro::sync_wait(macoro::when_all_ready( + bi0.bitInjection(k0, outBits, f0, comm[0]), + bi1.bitInjection(k1, outBits, f1, comm[1]), g0.start(), g1.start() )); + std::get<0>(tasks).result(); + std::get<1>(tasks).result(); + std::get<2>(tasks).result(); + std::get<3>(tasks).result(); auto ff = reveal(f0, f1); if ((u64)ff.rows() != n) @@ -242,12 +246,13 @@ void RadixSort_bitInjection_test(const oc::CLP& cmd) if ((u64)ff.cols() != L) throw RTE_LOC; + auto mask = ~u32{0} >> (32 - outBits); for (u64 i = 0; i < k.rows(); ++i) { auto iter = oc::BitIterator(k[i].data()); for (u64 j = 0; j < L; ++j) { - auto v = ff(i, j); + auto v = ff(i, j) & mask; auto b = *iter++; if (v != b) { @@ -257,6 +262,12 @@ void RadixSort_bitInjection_test(const oc::CLP& cmd) } } +void RadixSort_bitInjection_test(const oc::CLP& cmd) +{ + for (u64 outBits : {7, 13, 21, 31, 32}) + checkBitInjection(cmd, outBits); +} + void RadixSort_genValMasks2_test(const oc::CLP& cmd) { diff --git a/tests/UnitTests.cpp b/tests/UnitTests.cpp index d4880d822..e8c6cd39b 100644 --- a/tests/UnitTests.cpp +++ b/tests/UnitTests.cpp @@ -92,6 +92,7 @@ namespace secJoin_Tests t.add("RadixSort_mock_test ", RadixSort_mock_test); + t.add("perfectShuffle_span_Test ", perfectShuffle_span_Test); t.add("AggTree_plain_Test ", AggTree_plain_Test); t.add("AggTree_levelReveal_Test ", AggTree_levelReveal_Test); t.add("AggTree_dup_pre_levelReveal_Test ", AggTree_dup_pre_levelReveal_Test); diff --git a/thirdparty/getLibOTe.cmake b/thirdparty/getLibOTe.cmake index b7e2ab5c2..ba9689edb 100644 --- a/thirdparty/getLibOTe.cmake +++ b/thirdparty/getLibOTe.cmake @@ -3,6 +3,9 @@ set(USER_NAME ) set(TOKEN ) set(GIT_REPOSITORY "https://github.com/osu-crypto/libOTe.git") set(GIT_TAG "657f6da90bff5774a2d01c824e997572d5e8ba00") +# Keep libOTe's PPRF API while picking up the GCC 15 fixes in cryptoTools, +# coproto, macoro and libdivide. +set(CRYPTOTOOLS_TAG "994bbfa789ff2e7f37b7d022e7667996f1c551b9") set(DEP_NAME libOTe) set(CLONE_DIR "${SECUREJOIN_THIRDPARTY_CLONE_DIR}/${DEP_NAME}") @@ -23,9 +26,8 @@ endif() string (REPLACE ";" "%" CMAKE_PREFIX_PATH_STR "${CMAKE_PREFIX_PATH}") find_program(GIT git REQUIRED) -set(DOWNLOAD_CMD ${GIT} clone --recursive ${GIT_REPOSITORY}) +set(DOWNLOAD_CMD ${GIT} clone ${GIT_REPOSITORY}) set(CHECKOUT_CMD ${GIT} checkout ${GIT_TAG}) -set(SUBMODULE_CMD ${GIT} submodule update --recursive) message("============= Building ${DEP_NAME} =============") @@ -35,7 +37,16 @@ endif() run(NAME "libOTe Checkout ${GIT_TAG} " CMD ${CHECKOUT_CMD} WD ${CLONE_DIR}) -run(NAME "libOTe submodule" CMD ${SUBMODULE_CMD} WD ${CLONE_DIR}) +set(CRYPTOTOOLS_DIR "${CLONE_DIR}/cryptoTools") +if(NOT EXISTS "${CRYPTOTOOLS_DIR}/.git") + run(NAME "Initialize cryptoTools" CMD ${GIT} submodule update --init cryptoTools WD ${CLONE_DIR}) +endif() +execute_process(COMMAND ${GIT} cat-file -e ${CRYPTOTOOLS_TAG}^{commit} + WORKING_DIRECTORY "${CRYPTOTOOLS_DIR}" RESULT_VARIABLE missing_cryptoTools ERROR_QUIET) +if(missing_cryptoTools) + run(NAME "Fetch cryptoTools" CMD ${GIT} fetch origin ${CRYPTOTOOLS_TAG} WD ${CRYPTOTOOLS_DIR}) +endif() +run(NAME "Checkout cryptoTools ${CRYPTOTOOLS_TAG}" CMD ${GIT} checkout ${CRYPTOTOOLS_TAG} WD ${CRYPTOTOOLS_DIR}) option(ENABLE_CIRCUITS " " true) if(ENABLE_MOCK_OT)