-1

I met this problem when verifying whether points in a finite field GF(13) is on elliptic curve y^2 = x^3 + x + 1: first I set the loop boundary as i<2,the results came out right.

#include <stdio.h>
#include <math.h>

void main ()
{
    int a[13], b[13];
    int j, i, m, k = 0;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 13; j++)
            if ((i * i * i + i + 1) % 13 == (j * j) % 13) {
                a[k] = i;
                b[k] = j;
                k++;
            }
    printf ("\n There are %d points on the curve\nThe points are: ", k);
    for (m = 0; m < k; m++)
        printf ("\nx=%d,y=%d \n", a[m], b[m]);
}

The result is link 1

after I change the i<2 to i<13,

for(i=0;i<13;i++)

the first 4 points changed: link 2

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    It's doing exactly what it is supposed to. The only nit-picking issue is `main()` is type `int` (e.g. `int main(void)`) and main returns a value, `return 0;` at least. – David C. Rankin Oct 16 '15 at 03:58
  • @DavidC.Rankin true with the return type, but it's had implicit return value of `0` since (I think) C99. – RamblingMad Oct 16 '15 at 04:16
  • 1
    Agreed, but depending on the compiler, you will receive a warning for failing to provide the return. And nobody here runs code that generates any warnings... `:)` – David C. Rankin Oct 16 '15 at 04:27

1 Answers1

2

You are entering undefined behavior. If you set a break inside the loop you will notice that k >= 13 which is outside the bounds of your arrays.

You could add some bounds checking and consider the size of your arrays.

Matthew Sanders
  • 4,875
  • 26
  • 45