aboutsummaryrefslogtreecommitdiff
path: root/src/gen/equation.c
blob: 7928128d4f1220427e4103fc1631752d7c828329 (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
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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017-2018 J08nY
 */
#include "equation.h"
#include "field.h"
#include "io/input.h"

GENERATOR(a_gen_random) {
	curve->a = genrand(curve->field);
	return 1;
}

GENERATOR(a_gen_input) {
	pari_sp ltop = avma;
	GEN inp = input_int("a:", cfg->bits);
	if (gequalm1(inp)) {
		avma = ltop;
		return 0;
	} else if (equalii(inp, gen_m2)) {
		avma = ltop;
		return INT_MIN;
	}

	GEN elem = field_ielement(curve->field, inp);
	if (!elem) {
		avma = ltop;
		return 0;
	}
	curve->a = elem;
	return 1;
}

static GEN a = NULL;
static curve_t *curve_a = NULL;

GENERATOR(a_gen_once) {
	if (a && curve_a == curve) {
		curve->a = gcopy(a);
		return 1;
	}

	int inp = a_gen_input(curve, args, state);
	if (inp > 0) {
		a = gclone(curve->a);
		curve_a = curve;
		return 1;
	} else {
		return 0;
	}
}

GENERATOR(a_gen_zero) {
	curve->a = gen_0;
	return 1;
}

GENERATOR(a_gen_one) {
	curve->a = gen_1;
	return 1;
}

GENERATOR(b_gen_random) {
	curve->b = genrand(curve->field);
	return 1;
}

GENERATOR(b_gen_input) {
	pari_sp ltop = avma;
	GEN inp = input_int("b:", cfg->bits);
	if (gequalm1(inp)) {
		avma = ltop;
		return 0;
	} else if (equalii(inp, gen_m2)) {
		avma = ltop;
		return INT_MIN;
	}

	GEN elem = field_ielement(curve->field, inp);
	if (!elem) {
		avma = ltop;
		return 0;
	}
	curve->b = elem;
	return 1;
}

static GEN b = NULL;
static curve_t *curve_b = NULL;

GENERATOR(b_gen_once) {
	if (b && curve_b == curve) {
		curve->b = gcopy(b);
		return 1;
	}

	int inp = b_gen_input(curve, args, state);
	if (inp > 0) {
		b = gclone(curve->b);
		curve_b = curve;
		return 1;
	} else {
		return 0;
	}
}

GENERATOR(b_gen_zero) {
	curve->b = gen_0;
	return 1;
}

GENERATOR(b_gen_one) {
	curve->b = gen_1;
	return 1;
}

void equation_quit(void) {
	if (a && isclone(a)) {
		gunclone(a);
	}
	if (b && isclone(b)) {
		gunclone(b);
	}
}