homenc / HElib

HElib is an open-source software library that implements homomorphic encryption. It supports the BGV scheme with bootstrapping and the Approximate Number CKKS scheme. HElib also includes optimizations for efficient homomorphic evaluation, focusing on effective use of ciphertext packing techniques and on the Gentry-Halevi-Smart optimizations.
https://homenc.github.io/HElib
Other
3.11k stars 761 forks source link

binaryArithmetic: Segmentation Fault #354

Open patrick-schwarz opened 4 years ago

patrick-schwarz commented 4 years ago

Contact: patrick.schwarz@student.tugraz.at Environment:

Problem: After some additions i get a segmentation fault. MultTwoNumbers and AddManyNumbers fail as well.

Code:

#include <helib/binaryArith.h>
#include <cmath>
#include <vector>
#include <NTL/BasicThreadPool.h>

int main(int argc, char *argv[]) {
    long bitSize = 6;
    long L = long(log(3)/log(2)*bitSize * 30); // for addition
    long p = 2; // Plaintext base [default=2], should be a prime number
    long m = 4095;
    long r = 1;
    long threadCount = 16;

    helib::Context context(m, p, r);
    buildModChain(context, L);

    helib::SecKey secKey(context);
    secKey.GenSecKey();
    addSome1DMatrices(secKey);
    addFrbMatrices(secKey);

    helib::PubKey &pubKey = secKey;
    long n_slots =  context.ea->size();

    std::vector<long> r_num(n_slots);
    for (long s = 0; s < n_slots; s++) {
        r_num[s] = NTL::RandomBits_long(bitSize);
    }

    NTL::Vec<helib::Ctxt> enca(NTL::INIT_SIZE,bitSize,helib::Ctxt(pubKey));
    for (long i = 0; i < bitSize; i++) {
        std::vector<long> ibits(n_slots);
        for (long s = 0; s < n_slots; s++)
            ibits[s] = (r_num[s] >> i) & 1;

        context.ea->encrypt(enca[i], pubKey, ibits);
    }

    NTL::SetNumThreads(threadCount);

    for (size_t i = 0; i < 1000; i++) {
        std::cout << std::to_string(NTL::AvailableThreads()) + "-" + std::to_string(i) + "\n" << std::flush;
        NTL::Vec<helib::Ctxt> sum, a, b;
        vecCopy(a, enca);
        vecCopy(b, enca);
        helib::CtPtrs_VecCt wsum(sum), wa(a), wb(b);

        //multTwoNumbers(wsum, wa, wb);
        addTwoNumbers(wsum, wa, wb);
    }

}

Error:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Backtrace:

#0  0x000055bf35c0a0cc in helib::Ctxt::operator= (this=0x7, other=...) at /test/helib/include/helib/Ctxt.h:343
#1  0x00007f69ca9417e5 in helib::AddDAG::getCtxt (this=this@entry=0x7ffdfdf84920, node=node@entry=0x55bf37bf53f0, a=..., b=...) at /test/lib/HElib-master/src/binaryArith.cpp:418
#2  0x00007f69ca942bdd in helib::AddDAG::addCtxtFromNode (b=..., a=..., node=<optimized out>, c=..., this=0x7ffdfdf84920) at /test/lib/HElib-master/src/binaryArith.cpp:146
#3  helib::AddDAG::<lambda(long int, long int)>::operator() (last=2, first=<optimized out>, __closure=0x7ffdfdf84840) at /test/lib/HElib-master/src/binaryArith.cpp:346
#4  NTL::BasicThreadPool::ConcurrentTaskFct1<helib::AddDAG::apply(helib::CtPtrs&, const CtPtrs&, const CtPtrs&, long int)::<lambda(long int, long int)> >::run(long) (this=<optimized out>, index=<optimized out>) at /test/ntl/usr/local/include/NTL/BasicThreadPool.h:313
#5  0x000055bf35c0af5a in NTL::BasicThreadPool::runOneTask (task=warning: RTTI symbol not found for class 'NTL::BasicThreadPool::ConcurrentTaskFct1<helib::AddDAG::apply(helib::PtrVector<helib::Ctxt>&, helib::PtrVector<helib::Ctxt> const&, helib::PtrVector<helib::Ctxt> const&, long)::{lambda(long, long)#1}>'
0x7ffdfdf84860, index=1) at /test/ntl/usr/local/include/NTL/BasicThreadPool.h:392
#6  0x000055bf35c0b099 in NTL::BasicThreadPool::worker (localSignal=0x55bf386042b0) at /test/ntl/usr/local/include/NTL/BasicThreadPool.h:409
#7  0x000055bf35c0ef5d in std::__invoke_impl<void, void (*)(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*), NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*> (__f=@0x55bf386a9a10: 0x55bf35c0b042 <NTL::BasicThreadPool::worker(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*)>) at /usr/include/c++/7/bits/invoke.h:60
#8  0x000055bf35c0bed2 in std::__invoke<void (*)(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*), NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*> (__fn=@0x55bf386a9a10: 0x55bf35c0b042 <NTL::BasicThreadPool::worker(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*)>) at /usr/include/c++/7/bits/invoke.h:95
#9  0x000055bf35c1c82f in std::thread::_Invoker<std::tuple<void (*)(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*), NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*> >::_M_invoke<0ul, 1ul> (this=0x55bf386a9a08) at /usr/include/c++/7/thread:234
#10 0x000055bf35c1c632 in std::thread::_Invoker<std::tuple<void (*)(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*), NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*> >::operator() (this=0x55bf386a9a08) at /usr/include/c++/7/thread:243
#11 0x000055bf35c1bf8a in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*), NTL::BasicThreadPool::CompositeSignal<NTL::BasicThreadPool::ConcurrentTask*, long>*> > >::_M_run (this=0x55bf386a9a00) at /usr/include/c++/7/thread:186
#12 0x00007f69c9f2e6df in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007f69ca2016db in start_thread (arg=0x7f69c9248700) at pthread_create.c:463
#14 0x00007f69c95eb88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
z0xi commented 2 years ago

I also meet this problem after using some "addManyNumbers". Can anyone explain this?