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
|
/*
*
* ec.c
*
* Elliptic Curve Arithmetic Functions
*
* Copyright (C) Lenka Fibikova 2000
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ec.h"
EC *EC_new()
{
EC *ret;
ret=(EC *)malloc(sizeof(EC));
if (ret == NULL) return NULL;
ret->A = BN_new();
ret->B = BN_new();
ret->p = BN_new();
ret->h = BN_new();
ret->is_in_mont = 0;
if (ret->A == NULL || ret->B == NULL || ret->p == NULL || ret->h == NULL)
{
if (ret->A != NULL) BN_free(ret->A);
if (ret->B != NULL) BN_free(ret->B);
if (ret->p != NULL) BN_free(ret->p);
if (ret->h != NULL) BN_free(ret->h);
free(ret);
return(NULL);
}
return(ret);
}
void EC_clear_free(EC *E)
{
if (E == NULL) return;
if (E->A != NULL) BN_clear_free(E->A);
if (E->B != NULL) BN_clear_free(E->B);
if (E->p != NULL) BN_clear_free(E->p);
if (E->h != NULL) BN_clear_free(E->h);
E->is_in_mont = 0;
free(E);
}
#ifdef MONTGOMERY
int EC_to_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
{
assert(E != NULL);
assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
assert(mont != NULL);
assert(mont->p != NULL);
assert(ctx != NULL);
if (E->is_in_mont) return 1;
if (!BN_lshift(E->A, E->A, mont->R_num_bits)) return 0;
if (!BN_mod(E->A, E->A, mont->p, ctx)) return 0;
if (!BN_lshift(E->B, E->B, mont->R_num_bits)) return 0;
if (!BN_mod(E->B, E->B, mont->p, ctx)) return 0;
if (!BN_lshift(E->h, E->h, mont->R_num_bits)) return 0;
if (!BN_mod(E->h, E->h, mont->p, ctx)) return 0;
E->is_in_mont = 1;
return 1;
}
int EC_from_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
{
assert(E != NULL);
assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
assert(mont != NULL);
assert(mont->p != NULL);
assert(ctx != NULL);
if (!E->is_in_mont) return 1;
if (!BN_mont_red(E->A, mont)) return 0;
if (!BN_mont_red(E->B, mont)) return 0;
if (!BN_mont_red(E->h, mont)) return 0;
E->is_in_mont = 0;
return 1;
}
#endif /* MONTGOMERY */
int EC_set_half(EC *E)
/* h <- 1/2 mod p = (p + 1)/2 */
{
assert(E != NULL);
assert(E->p != NULL);
assert(E->h != NULL);
assert(!E->is_in_mont);
if (BN_copy(E->h, E->p) == NULL) return 0;
if (!BN_add_word(E->h, 1)) return 0;
if (!BN_rshift1(E->h, E->h)) return 0;
return 1;
}
|