-2
#define n 500
#define N 1024
#define N2 (N/2+1)
#define K2 6
#define BS_base 23
#define BS_exp 2
typedef double complex_double[2];
typedef complex_double Ring_FFT[N2];
typedef Ring_FFT ct_FFT[K2][2];  // Ciphertext in FFT form ct_FFT[K2][2] => Ring_FFT[6][2] => complex_double[6][2][513] => double[6][2][513][2]
typedef ct_FFT BootstrappingKey[n][BS_base][BS_exp]; 
//BootstrappingKey[500][23][2] => ct_FFT[500][23][2] => ct_FFT[500][23][2][6][2] => complex_double[500][23][2][6][2][513] => double[500][23][2][6][2][513][2]
typedef struct {
BootstrappingKey *BSkey;
SwitchingKey *KSkey;
} EvalKey;


EvalKey EK;
EK.BSkey = (BootstrappingKey*) malloc(sizeof(BootstrappingKey));
printf("sizeof BootstrappingKey: %lu\n",sizeof(BootstrappingKey));
printf("EK.BSkey: %p\n",EK.BSkey);
if(EK.BSkey == NULL) {
    fprintf(stderr, "BAD BAD BAD!\n");
    return -1;
}

EK.KSkey = (SwitchingKey*) malloc(sizeof(SwitchingKey));

if(EK.KSkey == NULL) {
    fprintf(stderr, "EVEN WORSE!\n");
    return -1;
}
printf("\nPointer value of EK.BSkey = %p\n",(EK.BSkey));
printf("\nPointer value of EK.KSkey = %p\n",(EK.KSkey));
printf("Starting another huge loop\n");
for(int i =0; i< n; ++i)
  for(int j=0;j < BS_base;++j)
    for(int k=0;k < BS_exp; ++k)
      for(int l=0; l < K2; ++l)
        for(int a =0; a <2; ++a)
          for(int b =0; b < N2; ++b)
            for(int c =0; c < 2; ++c){
                printf("Arrrrgh: i=%d, j=%d, k=%d, l=%d, a=%d, b=%d, c=%d\n",i,j,k,l,a,b,c);
                *(EK.BSkey)[i][j][k][l][a][b][c] = 1.0;
              }

This is a really large array for a Homomorphic encryption project. However I kept getting segmentation faults. I wrote this forloop just for debugging purposes. As soon as the loop reaches:

Arrrrgh: i=0, j=22, k=1, l=5, a=1, b=512, c=1<br>
Arrrrgh: i=1, j=0, k=0, l=0, a=0, b=0, c=0

I got a segmentation fault (output of Valgrind):

==8127== Invalid write of size 8 ==8127== at 0x401CFB: FHEWKeyGen (FHEW.c:100) ==8127== by 0x400D94: main (gen.c:46)

I would really appreciate some help.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Raoul
  • 11
  • 5
  • 1
    What platform? How much memory (including virtual) do you have? – unwind Jun 02 '15 at 09:04
  • 1
    Is it just me or are you referencing way more dimensions that you define in your typedef for `BootstrappingKey`. You define it with 3 dimensions of size 500*23*2 but then reference it with `*(EK.BSkey)[i][j][k][l][a][b][c]`. That just can't be good and can definitely be the source of your problem. – David Hoelzer Jun 02 '15 at 09:07
  • 1
    "*I wrote this forloop just for debugging purposes*" How about a real debugger? – Eregrith Jun 02 '15 at 09:11
  • @DavidHoelzer It wouldn't compile if it didn't work out, would it? You can't just randomly index things, it has to match the declaration. It's a chain of `typedef`s, `BootstrappingKey` is an array of arrays of arrays, sorta. – unwind Jun 02 '15 at 09:12
  • 2
    I'm pretty sure you can simplify this program quite a bit... – Lundin Jun 02 '15 at 09:13
  • 1
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Jun 02 '15 at 09:14
  • In case you are unaware, C has `complex double` as a built-in type that you can do arithmetic on, and extract Real and Imaginary parts of. You could use that instead of your `complex_double`. [See here](http://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c) for examples – M.M Jun 02 '15 at 09:22
  • That's the hugest pile of `for`s I've ever seen. – Quentin Jun 02 '15 at 09:55

1 Answers1

1

*(EK.BSkey)[i][j][k][l][a][b][c] should be (*EK.BSkey)[i][j][k][l][a][b][c].

You need to dereference the pointer EK.BSkey to get a designator for the array it points to, and then all the indices are applied.

The way you have it originally is equivalent to EK.BSkey[i][j][k][l][a][b][c][0]; the highest precedence operation is EK.BSkey[i] which reads out of bounds if i > 0 because EK.BSkey points to a single instance of a BootstrappingKey.

I think the loop indices are in the right order!

M.M
  • 138,810
  • 21
  • 208
  • 365