/src/relic/src/ed/relic_ed_norm.c
Line | Count | Source |
1 | | /* |
2 | | * RELIC is an Efficient LIbrary for Cryptography |
3 | | * Copyright (c) 2019 RELIC Authors |
4 | | * |
5 | | * This file is part of RELIC. RELIC is legal property of its developers, |
6 | | * whose names are not listed here. Please refer to the COPYRIGHT file |
7 | | * for contact information. |
8 | | * |
9 | | * RELIC is free software; you can redistribute it and/or modify it under the |
10 | | * terms of the version 2.1 (or later) of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; or version 2.0 of the Apache |
12 | | * License as published by the Apache Software Foundation. See the LICENSE files |
13 | | * for more details. |
14 | | * |
15 | | * RELIC is distributed in the hope that it will be useful, but WITHOUT ANY |
16 | | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
17 | | * A PARTICULAR PURPOSE. See the LICENSE files for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public or the |
20 | | * Apache License along with RELIC. If not, see <https://www.gnu.org/licenses/> |
21 | | * or <https://www.apache.org/licenses/>. |
22 | | */ |
23 | | |
24 | | /** |
25 | | * @file |
26 | | * |
27 | | * Implementation of point normalization on Edwards elliptic curves. |
28 | | * |
29 | | * @ingroup ep |
30 | | */ |
31 | | |
32 | | #include "relic_core.h" |
33 | | |
34 | | /*============================================================================*/ |
35 | | /* Private definitions */ |
36 | | /*============================================================================*/ |
37 | | |
38 | | #if ED_ADD == PROJC || ED_ADD == EXTND || !defined(STRIP) |
39 | | |
40 | | /** |
41 | | * Normalizes a point represented in projective coordinates. |
42 | | * |
43 | | * @param r - the result. |
44 | | * @param p - the point to normalize. |
45 | | */ |
46 | 0 | static void ed_norm_imp(ed_t r, const ed_t p, int inverted) { |
47 | 0 | if (p->coord != BASIC) { |
48 | 0 | if (inverted) { |
49 | 0 | fp_copy(r->z, p->z); |
50 | 0 | } else { |
51 | 0 | fp_inv(r->z, p->z); |
52 | 0 | } |
53 | 0 | fp_mul(r->x, p->x, r->z); |
54 | 0 | fp_mul(r->y, p->y, r->z); |
55 | | #if ED_ADD == EXTND |
56 | | fp_mul(r->t, p->t, r->z); |
57 | | #endif |
58 | 0 | fp_set_dig(r->z, 1); |
59 | |
|
60 | 0 | r->coord = BASIC; |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | #endif /* ED_ADD == PROJC */ |
65 | | |
66 | | /*============================================================================*/ |
67 | | /* Public definitions */ |
68 | | /*============================================================================*/ |
69 | | |
70 | 0 | void ed_norm(ed_t r, const ed_t p) { |
71 | 0 | if (ed_is_infty(p)) { |
72 | 0 | ed_set_infty(r); |
73 | 0 | return; |
74 | 0 | } |
75 | | |
76 | 0 | if (p->coord == BASIC) { |
77 | | /* If the point is represented in affine coordinates, just copy it. */ |
78 | 0 | ed_copy(r, p); |
79 | 0 | return; |
80 | 0 | } |
81 | 0 | #if ED_ADD == PROJC || ED_ADD == EXTND || !defined(STRIP) |
82 | 0 | ed_norm_imp(r, p, 0); |
83 | 0 | #endif /* ED_ADD != BASIC*/ |
84 | 0 | } |
85 | | |
86 | 0 | void ed_norm_sim(ed_t *r, const ed_t *t, int n) { |
87 | 0 | fp_t* a = RLC_ALLOCA(fp_t, n); |
88 | |
|
89 | 0 | RLC_TRY { |
90 | 0 | if (a == NULL) { |
91 | 0 | RLC_THROW(ERR_NO_MEMORY); |
92 | 0 | } |
93 | 0 | for (int i = 0; i < n; i++) { |
94 | 0 | fp_null(a[i]); |
95 | 0 | fp_new(a[i]); |
96 | 0 | fp_copy(a[i], t[i]->z); |
97 | 0 | } |
98 | |
|
99 | 0 | fp_inv_sim(a, (const fp_t *)a, n); |
100 | |
|
101 | 0 | for (int i = 0; i < n; i++) { |
102 | 0 | fp_copy(r[i]->x, t[i]->x); |
103 | 0 | fp_copy(r[i]->y, t[i]->y); |
104 | | #if ED_ADD == EXTND |
105 | | fp_copy(r[i]->t, t[i]->t); |
106 | | #endif |
107 | 0 | if (!ed_is_infty(t[i])) { |
108 | 0 | fp_copy(r[i]->z, a[i]); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | 0 | #if ED_ADD == PROJC || ED_ADD == EXTND || !defined(STRIP) |
113 | 0 | for (int i = 0; i < n; i++) { |
114 | 0 | ed_norm_imp(r[i], r[i], 1); |
115 | 0 | } |
116 | 0 | #endif /* ED_ADD != BASIC*/ |
117 | 0 | } |
118 | 0 | RLC_CATCH_ANY { |
119 | 0 | RLC_THROW(ERR_CAUGHT); |
120 | 0 | } |
121 | 0 | RLC_FINALLY { |
122 | 0 | for (int i = 0; i < n; i++) { |
123 | 0 | fp_free(a[i]); |
124 | 0 | } |
125 | | RLC_FREE(a); |
126 | 0 | } |
127 | 0 | } |