Skip to content

Commit

Permalink
minor and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed Dec 15, 2023
1 parent e99bf99 commit 1db91e4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: CI
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master, ci ]
branches: [ master, ci, mx ]
pull_request: {}

# Allows you to run this workflow manually from the Actions tab
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
run: python3 build.py --setup --relic --par=4

- name: build cryptoTools
run: python3 build.py --par=4 -D ENABLE_RELIC=ON -D ENABLE_SSE=OFF
run: python3 build.py --par=4 -D ENABLE_RELIC=ON -D ENABLE_SSE=OFF -D CRYPTO_TOOLS_STD_VER=17

- name: unit tests
run: ./out/build/osx/frontend_cryptoTools/frontend_cryptoTools -u
Expand Down
2 changes: 0 additions & 2 deletions cryptoTools/Circuit/MxCircuitLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,6 @@ namespace osuCrypto
multiplex(sign, t, a1, ret);
}



//
void lessThan(
span<const Bit> a1,
Expand Down
33 changes: 33 additions & 0 deletions cryptoTools/Circuit/MxCircuitLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ namespace osuCrypto
IntType it,
AdderType at);

inline void add(span<const Bit> a1_, span<const Bit> a2_, span<Bit> sum, IntType it, AdderType at, Optimized op)
{
if (op == Optimized::Size)
rippleAdder(a1_, a2_, sum, it, at);
else
parallelPrefix(a1_, a2_, sum, it, at);
}

// compute the summation x[0] + x[1] + ...
void parallelSummation(
Expand All @@ -93,6 +100,32 @@ namespace osuCrypto
IntType it
);

void negate(
span<const Bit> a1,
span<Bit> ret,
Optimized op);


void removeSign(
span<const Bit> a1,
span<Bit> ret,
Optimized op);


void addSign(
Bit sign,
span<const Bit> a1,
span<Bit> ret,
Optimized op);


void lessThan(
span<const Bit> a1,
span<const Bit> a2,
Bit& ret,
IntType it,
Optimized op);

// computes dst = a1 * a2;
void multiply(
span<const Bit> a1,
Expand Down
73 changes: 68 additions & 5 deletions cryptoTools/Circuit/MxTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ namespace osuCrypto
r[i] = self()[i] & b[i];
return r;
}

C& operator&=(const C&b) {
*this = *this & b;
return *this;
}


C operator|(const C& b)const
{
if (self().size() != b.size())
Expand All @@ -46,6 +53,12 @@ namespace osuCrypto
r[i] = self()[i] | b[i];
return r;
}

C& operator|=(const C& b) {
*this = *this | b;
return *this;
}

C operator^(const C& b)const
{
if (self().size() != b.size())
Expand All @@ -56,6 +69,11 @@ namespace osuCrypto
return r;
}

C& operator^=(const C& b) {
*this = *this ^ b;
return *this;
}

C operator~()const
{
auto r = C::makeFromSize(self().size());
Expand Down Expand Up @@ -213,22 +231,67 @@ namespace osuCrypto
C operator+(const C& b)const
{
auto r = C::makeFromSize(self().size());
parallelPrefix(self().asBits(), b.asBits(), r.asBits(), Signed, AdderType::Addition);
add(self().asBits(), b.asBits(), r.asBits(), Signed, AdderType::Addition, Optimized::Depth);
return r;
}

C& operator+=(const C&b) {
*this = *this + b;
return *this;
}

C operator-(const C& b)const
{
auto r = C::makeFromSize(self().size());
parallelPrefix(self().asBits(), b.asBits(), r.asBits(), Signed, AdderType::Subtraction);
add(self().asBits(), b.asBits(), r.asBits(), Signed, AdderType::Subtraction, Optimized::Depth);
return r;
}

C& operator-=(const C&b) {
*this = *this - b;
return *this;
}

C operator-() const
{
auto r = C::makeFromSize(self().size());
negate(self().asBits(), r.asBits(), Optimized::Depth);
return r;
}

C operator*(const C& b)const
{
auto r = C::makeFromSize(self().size());
multiply(self().asBits(), b.asBits(), r.asBits(), Optimized::Depth, Signed);
return r;
}

C& operator*=(const C&b) {
*this = *this * b;
return *this;
}


C operator/(const C& b)const
{
auto r = C::makeFromSize(self().size());
divideRemainder(self().asBits(), b.asBits(), r.asBits(), {}, Optimized::Depth, Signed);
return r;
}

C operator%(const C& b) const
{
auto d = C::makeFromSize(self().size());
auto r = C::makeFromSize(self().size());
divideRemainder(self().asBits(), b.asBits(), d.asBits(), r.asBits(), Optimized::Depth, Signed);
return r;
}

Bit operator<(const C& b)const
{
BVector r(self().size() + 1);
parallelPrefix(self().asBits(), b.asBits(), r.asBits(), Signed, AdderType::Subtraction);
return r.back();
Bit r;
lessThan(self().asBits(), b.asBits(), r, Signed, Optimized::Depth);
return r;
}

Bit operator>(const C& b)const
Expand Down
34 changes: 28 additions & 6 deletions tests_cryptoTools/MxCircuit_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void MxCircuit_Bit_Ops_Test(const oc::CLP& cmd)
throw UnitTestSkipped("ENABLE_CIRCUITS=false");
#endif
}


template<typename T, typename V, typename ...Args>
void MxCircuit_int_Ops_Test(const oc::CLP& cmd, Args... args)
{
Expand All @@ -110,6 +112,10 @@ void MxCircuit_int_Ops_Test(const oc::CLP& cmd, Args... args)

auto vPlus = a + b;
auto vSub = a - b;
auto vNeg = -a;
auto vMult = a * b;
auto vDiv = a / b;
auto vRem = a % b;

auto vEqq = a == a;
auto vEq = a == b;
Expand Down Expand Up @@ -162,6 +168,10 @@ void MxCircuit_int_Ops_Test(const oc::CLP& cmd, Args... args)

cir.output(vPlus);
cir.output(vSub);
cir.output(vNeg);
cir.output(vMult);
cir.output(vDiv);
cir.output(vRem);

cir.output(vEqq);
cir.output(vEq);
Expand Down Expand Up @@ -214,6 +224,10 @@ void MxCircuit_int_Ops_Test(const oc::CLP& cmd, Args... args)

auto vPlus = out[k++].getSpan<V>()[0];
auto vSub = out[k++].getSpan<V>()[0];
auto vNeg = out[k++].getSpan<V>()[0];
auto vMult = out[k++].getSpan<V>()[0];
auto vDiv = out[k++].getSpan<V>()[0];
auto vRem = out[k++].getSpan<V>()[0];


bool vEqq = out[k++][0];
Expand Down Expand Up @@ -255,6 +269,14 @@ void MxCircuit_int_Ops_Test(const oc::CLP& cmd, Args... args)
throw RTE_LOC;
if (vSub != (a - b))
throw RTE_LOC;
if (vNeg != -a)
throw RTE_LOC;
if (vMult != (a * b))
throw RTE_LOC;
if (b && vDiv != (a / b))
throw RTE_LOC;
if (b && vRem != (a % b))
throw RTE_LOC;

if (!vEqq)
throw RTE_LOC;
Expand Down Expand Up @@ -469,7 +491,7 @@ T signEx(T v, u64 s)
if (s == sizeof(T) * 8)
return v;

i64 sign = *BitIterator((u8*)&v, s-1);
i64 sign = *BitIterator((u8*)&v, s - 1);

if (sign && std::is_signed_v<T>)
{
Expand Down Expand Up @@ -528,8 +550,8 @@ void MxCircuit_parallelPrefix_impl(u64 trials, Mx::AdderType at, PRNG& prng)

if (c != cAct)
{
std::cout << " exp " << c << "\t" << BitVector((u8*)&c, s2) << "\n";
std::cout << " act " << cAct << "\t"<< BitVector((u8*)&cAct, s2) << "\n";
std::cout << " exp " << c << "\t" << BitVector((u8*)&c, s2) << "\n";
std::cout << " act " << cAct << "\t" << BitVector((u8*)&cAct, s2) << "\n";
throw RTE_LOC;
}
}
Expand Down Expand Up @@ -625,7 +647,7 @@ void MxCircuit_parallelSummation_impl(u64 trials, Mx::Optimized op, PRNG& prng)
for (u64 i = 0; i < trials; ++i)
{
u64 numTerms = (prng.get<u32>() % 16 + 1);
auto s0 = (prng.get<u32>() % 16) + 1;
auto s0 = (prng.get<u32>() % 16) + 1;

Mx::Circuit cir;
std::vector<Mx::BVector> X(numTerms);
Expand Down Expand Up @@ -681,7 +703,7 @@ void MxCircuit_parallelSummation_Test(const oc::CLP& cmd)

PRNG prng(ZeroBlock);
auto trials = cmd.getOr<u64>("trials", 10);

MxCircuit_parallelSummation_impl<u64>(trials, Mx::Optimized::Depth, prng);
MxCircuit_parallelSummation_impl<u64>(trials, Mx::Optimized::Size, prng);
MxCircuit_parallelSummation_impl<i64>(trials, Mx::Optimized::Depth, prng);
Expand Down Expand Up @@ -780,7 +802,7 @@ void MxCircuit_divideRemainder_impl(u64 trials, Mx::Optimized op, PRNG& prng)
{
T a = signEx(prng.get<T>(), s0);
T b = 0;
while(b == 0)
while (b == 0)
b = signEx(prng.get<T>(), s1);
T q = a / b;
T r = a % b;
Expand Down

0 comments on commit 1db91e4

Please sign in to comment.