Segmentation fault (core dumped)

Hello everyone! I've got a problem which I'm unable to solve, namely I keep getting a segmentation fault and I've tried a lot to get it fixed, but nothing works so far.

I've got different header and source files, so I will only post the necessary to make this post not too long.

generate_input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <gmp.h>
#include <flint.h>
#include <fmpz.h>
#include <cstdlib>

#include "generate_input.hpp"	

void rand_code(fmpz_t (&u_code)[4]) {
	//this works so I won't put the implementation here
}

void inv_code(fmpz_t u_code[], fmpz_t (&u_code_inv)[4]) {
	//this works so I won't put the implementation here
}

//generate random mask
//bits in corresponding code that is erroneous are set to 0 in the mask, all other bits to 1
void rand_mask(fmpz_t (&u_mask)[4]) {
	//this works so I won't put the implementation here
} 

void combined_masks(fmpz_t (&u_maskAB)[4], fmpz_t u_maskA[], fmpz_t u_maskB[]) {
	//this works so I won't put the implementation here
}


generate_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <gmp.h>
#include <flint.h>
#include <fmpz.h>
#include <iostream>
#include <cstdlib>

#include "generate_input.hpp"
#include "num_unencrypted.hpp"

using namespace std;

int main() {
	//random generation of codeA
	fmpz_t u_codeA[4];
	for(int i=0; i<4; i++) {
		fmpz_init(u_codeA[i]);
	}

	rand_code(u_codeA);

	//inverse of codeA
	fmpz_t u_codeA_inv[4];
	for(int i=0; i<4; i++) {
		fmpz_init(u_codeA_inv[i]);
	}

	inv_code(u_codeA, u_codeA_inv);

	//random generation of codeB
	fmpz_t u_codeB[4];
	for(int i=0; i<4; i++) {
		fmpz_init(u_codeB[i]);
	}

	rand_code(u_codeB);

	//inverse of codeB
	fmpz_t u_codeB_inv[4];
	for(int i=0; i<4; i++) {
		fmpz_init(u_codeB_inv[i]);
	}

	inv_code(u_codeB, u_codeB_inv);

	//generation of maskA
	fmpz_t u_maskA[4];

	for(int i=0; i<4; i++) {
		fmpz_init(u_maskA[i]);
	}

	rand_mask(u_maskA);

	//generation of maskB
	fmpz_t u_maskB[4];

	for(int i=0; i<4; i++) {
		fmpz_init(u_maskB[i]);
	}

	rand_mask(u_maskB);

	//maskA ^ maskB
	fmpz_t u_maskAB[4];

	for(int i=0; i<4; i++) {
		fmpz_init(u_maskAB[i]);
	}

	combined_masks(u_maskAB, u_maskA, u_maskB);
	
	//Initialize class bv
	BV_she bv;
	bv_param_t param;
	bv_param_init(param);
	bv.get_param(param);

	//Create public and private key
	bv_pk_t pk;
	bv_pk_init(pk);
	bv_sk_t sk;
	bv_sk_init(sk);
	bv.keypair(pk, sk);

	//encrypt codeA
	bv_enc_t e_codeA[4];

	for(int i=0; i<4; i++) {
		bv_enc_init(e_codeA[i]);
	}

	enc_code(u_codeA, e_codeA, pk, bv);

	//Encrypt codeB
	bv_enc_t e_codeB[4];

	for(int i=0; i<4; i++) {
		bv_enc_init(e_codeB[i]);
	}

	enc_code(u_codeB, e_codeB, pk, bv);

	//encrypt inverse of codeA
	bv_enc_t e_codeA_inv[4];

	for(int i=0; i<4; i++) {
		bv_enc_init(e_codeA[i]);
	}

	enc_code(u_codeA_inv, e_codeA_inv, pk, bv);

	//encrypt inverse of codeB
	bv_enc_t e_codeB_inv[4];

	for(int i=0; i<4; i++) {
		bv_enc_init(e_codeB_inv[i]);
	}

	enc_code(u_codeB_inv, e_codeB_inv, pk, bv);

	//compute numerator
	fmpz_t u_num;
	fmpz_init(u_num);
	compute_num(bv, sk, e_codeA, e_codeB, e_codeA_inv, e_codeB_inv, u_maskAB, u_num);

	cout << "value of numerator: " << u_num << endl;


	//free memory
	for(int i=0; i<4; i++) {
		fmpz_clear(u_codeA[i]);
		fmpz_clear(u_codeA_inv[i]);
		fmpz_clear(u_codeB[i]);
		fmpz_clear(u_codeB_inv[i]);
		fmpz_clear(u_maskA[i]);
		fmpz_clear(u_maskB[i]);
		fmpz_clear(u_maskAB[i]);
		bv_enc_clear(e_codeA[i]);
		bv_enc_clear(e_codeB[i]);
		bv_enc_clear(e_codeA_inv[i]);
		bv_enc_clear(e_codeB_inv[i]);
	}

	fmpz_clear(u_num);

	return 0;
}


num_unencrypted.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <gmp.h>
#include <flint.h>
#include <fmpz.h>

#include "bv_she.hpp"
#include "bv_types.hpp"
#include "num_unencrypted.hpp"

//function that encrypts a code, given as array
void enc_code(fmpz_t u_code[], bv_enc_t (&e_code)[4], bv_pk_t &pk, BV_she &bv) {
	//encrypt u_code per bit
	for(int i=0; i<4; i++) {
		bv.encrypt(e_code[i], u_code[i], pk);
	}	
}

int denominator(fmpz_t u_maskAB[]) {
	//works
}

void compute_num(BV_she bv, bv_sk_t sk, bv_enc_t e_codeA[], bv_enc_t e_codeB[], bv_enc_t e_codeAinv[], bv_enc_t e_codeBinv[], fmpz_t u_maskAB[], fmpz_t &u_num) {
	int addMaskAB = denominator(u_maskAB);
	bv_enc_t e_addMultAB;
	bv_enc_init(e_addMultAB);
	bv_enc_t e_addMultABinv;
	bv_enc_init(e_addMultABinv);
	bv_enc_t temp;
	bv_enc_init(temp);


	for(int i=0; i<4; i++) {
		if(fmpz_is_one(u_maskAB[i])) {
			//Multiply codeA[i] and codeB[i] and put the result in temp
			bv.mult(temp, e_codeA[i], e_codeB[i]);
			//Add temp to addMultAB
			bv.add(e_addMultAB, e_addMultAB, temp);

			//Perform the same operations for the inversed codeA and codeB
			bv.mult(temp, e_codeAinv[i], e_codeBinv[i]);
			bv.add(e_addMultABinv, e_addMultABinv, temp);
		}
	}


	//first decrypt addMultAB and addMultABinv, then compute numerator
	fmpz_t u_addMultAB;
	fmpz_init(u_addMultAB);
	fmpz_t u_addMultABinv;
	fmpz_init(u_addMultABinv);

	bv.decrypt(u_addMultAB, e_addMultAB, sk);
	bv.decrypt(u_addMultABinv, e_addMultABinv, sk);

	//compute numerator (num)
	fmpz_set_ui(u_num, addMaskAB);
	fmpz_sub(u_num, u_num, u_addMultAB);
	fmpz_sub(u_num, u_num, u_addMultABinv); 
}


I don't know if bv_she.cpp and bv_types.cpp are worth mentioning, but those files are not mine, I just use them and I'm sure they work. They're basically source files with functions that enable you to encrypt data and perform computations like additions and multiplication on ciphertexts. But if anyone thinks they're relevant, I will post them here.

I think the problem is something with pointers and references. I've been struggling with them for a while and I think I understand them better, but probably I'll have done something wrong again.

I would be extremely greatful if someone wants to take a look at it! I'm sorry for the long codes, I hope it's still readable.
Last edited on
Hi,

Consider using a debugger, it will save you days of staring at code.

There should be a GUI one in your IDE somewhere.

Good Luck !!
As Tim says, use a debugger. That you've already tried a lot to get it fixed it unfortunate; you could have saved yourself a lot of time.

Build your code with debugging symbols. Run it under a debugger. The debugger shows you the line of code in which the segFault happens. You examine all the values of the variables and you deduce what the problem is, and then you fix it.

Under *nix, the debugger most typically available is gdb. Here is an article I wrote here a long time ago titled "Debugging using gdb: Find a segFault"

http://www.cplusplus.com/articles/iwTbqMoL/
> I've got different header and source files, so I will only post the necessary to make this post not too long.
post the necessary to reproduce your problem, any less is useless.


use github or similar to upload "big" projects.
Last edited on
it will be easy to use debugger there because your problem is about accessing address that you dont own
Thanks a lot everyone! I used gdb to debug (thanks for your article Moschops) and I get the following line:
1
2
Program received signal SIGSEGV, Segmentation fault.
0xb7d7764a in __gmpz_set() from /usr/lib/i386-linux-gnu/libgmp.so.10


I've already looked in the manual of GMP, but that didn't really help me. Then I found out that I don't even use GMP, so I removed the #include <gmp.h> lines from my files, but I still get the same error.

Edit: I'm now looking at the results of backtrace.
Last edited on
Thanks a lot guys!! GDB was my best friend today, I solved the error (turned out that somewhere in my code I initialized the wrong array) and some other errors as well and now it works. :)
Hi,
Note that some IDEs come with a GUI debugger that uses gdb.
Thanks, I'll keep that in mind. :) But for now I use the command line to compile and run code anyway, so that's no problem for me.
Topic archived. No new replies allowed.