-5

I have a code in C++ called source.cpp. And I have a library in C. I use Cygwin and run: g++ source.cpp -L/cygdrive/c/cygwin/home/Win7/libpaillier -l:libpaillier.a -lgmpxx -lgmp to create an .exe file and then run it. At the end I get " segmentation fault (core dumped)". I need to know why I'm getting this error.

Source.cpp:

#include<iostream>
#include<gmp.h>
#include <gmpxx.h>

using namespace std;

#include <string>
extern "C"{
#include<paillier.h>
}
#include<math.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
    paillier_pubkey_t* pu;//The public key
    paillier_prvkey_t* pr;//The private key 
    paillier_get_rand_t get_rand;
    paillier_keygen(1024, &pu,&pr, get_rand );
    return 0;
}

Paillier library: http://acsc.cs.utexas.edu/libpaillier/

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
user13676
  • 121
  • 1
  • 5

2 Answers2

1

You need to pass a valid random generator as the last argument to paillier_keygen. The library provides two which you can use:

paillier_keygen(1024, &pu,&pr, paillier_get_rand_devrandom);
paillier_keygen(1024, &pu,&pr, paillier_get_rand_devurandom);
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

You have declared a pointer to a random function (paillier_get_rand_t get_rand;), but haven't initialized it to point at a function. Now, when you pass this pointer to paillier_keygen which tries to call the function it (should) point to, you get a segfault.

The library in question provides, as @TartanLlama mentioned, two random number generation functions that you could initialize your pointer with (or pass them directly to the function): paillier_get_rand_devrandom and paillier_get_rand_devurandom.

You could also use another RNG function. Just make sure it has a header compatible with the function pointer type in question: typedef void (*paillier_get_rand_t) ( void* buf, int len ); (relevant SO question). Also, you'd want it to conform to the library's requirements, i.e. generating a random byte sequence of len bytes and storing it in buf.

EDIT: I originally misread your code, that's why I talked about several pointers

Community
  • 1
  • 1