aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/guru/quadratic_test.c
blob: 3f78e95ae9ee683433cf626311e9950d0e361940 (plain)
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
// Test quadratic field extensions.

#include "pbc.h"
#include "pbc_fp.h"
#include "pbc_fieldquadratic.h"
#include "pbc_test.h"

int main(void) {
  field_t fp, fp2;
  mpz_t prime;
  element_t a, b, c;

  mpz_init(prime);
  // Prime is 3 mod 4 so that -1 is a quadratic nonresidue.
  // For smaller tests, try the prime 83.
  mpz_setbit(prime, 256);
  do {
    mpz_nextprime(prime, prime);
  } while (mpz_fdiv_ui(prime, 4) != 3);

  field_init_fp(fp, prime);
  field_init_fi(fp2, fp);
  element_init(a, fp2);
  element_init(b, fp2);
  element_init(c, fp2);

  element_printf("field: %Z^2\n", prime);

  element_random(a);
  element_random(b);
  element_printf("a = %B, b = %B\n", a, b);

  element_add(c, a, b);
  element_printf("a + b = %B\n", c);

  element_mul(c, a, b);
  element_printf("a * b = %B\n", c);

  for (;;) {
    element_random(a);
    element_printf("new a = %B\n", a);

    if (element_is_sqr(a)) break;
    printf(" is not a square\n");
  }
  element_sqrt(c, a);
  element_printf("sqrt(a) = %B\n", c);
  element_mul(c, c, c);
  element_printf("sqrt(a) * sqrt(a) = %B\n", c);
  element_invert(c, a);
  element_printf("1/a = %B\n", c);
  element_mul(c, c, a);
  element_printf("1/a * a = %B\n", c);

  element_clear(a);
  element_clear(b);
  element_clear(c);
  field_clear(fp);
  field_clear(fp2);
  mpz_clear(prime);
  return 0;
}