/src/relic/src/fpx/relic_fp4_sqr.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 squaring in a quartic extension of a prime field. |
28 | | * |
29 | | * @ingroup fpx |
30 | | */ |
31 | | |
32 | | #include "relic_core.h" |
33 | | #include "relic_fp_low.h" |
34 | | #include "relic_fpx_low.h" |
35 | | |
36 | | /*============================================================================*/ |
37 | | /* Public definitions */ |
38 | | /*============================================================================*/ |
39 | | |
40 | | #if FPX_RDC == BASIC || !defined(STRIP) |
41 | | |
42 | 0 | void fp4_sqr_basic(fp4_t c, const fp4_t a) { |
43 | 0 | fp2_t t0, t1; |
44 | |
|
45 | 0 | fp2_null(t0); |
46 | 0 | fp2_null(t1); |
47 | |
|
48 | 0 | RLC_TRY { |
49 | 0 | fp2_new(t0); |
50 | 0 | fp2_new(t1); |
51 | |
|
52 | 0 | fp2_add(t0, a[0], a[1]); |
53 | 0 | fp2_mul_nor(t1, a[1]); |
54 | 0 | fp2_add(t1, a[0], t1); |
55 | 0 | fp2_mul(t0, t0, t1); |
56 | 0 | fp2_mul(c[1], a[0], a[1]); |
57 | 0 | fp2_sub(c[0], t0, c[1]); |
58 | 0 | fp2_mul_nor(t1, c[1]); |
59 | 0 | fp2_sub(c[0], c[0], t1); |
60 | 0 | fp2_dbl(c[1], c[1]); |
61 | 0 | } RLC_CATCH_ANY { |
62 | 0 | RLC_THROW(ERR_CAUGHT); |
63 | 0 | } RLC_FINALLY { |
64 | 0 | fp2_free(t0); |
65 | 0 | fp2_free(t1); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | #endif |
70 | | |
71 | | #if PP_EXT == LAZYR || !defined(STRIP) |
72 | | |
73 | 4.53M | void fp4_sqr_unr(dv4_t c, const fp4_t a) { |
74 | 4.53M | fp2_t t; |
75 | 4.53M | dv2_t u0, u1; |
76 | | |
77 | 4.53M | fp2_null(t); |
78 | 4.53M | dv2_null(u0); |
79 | 4.53M | dv2_null(u1); |
80 | | |
81 | 4.53M | RLC_TRY { |
82 | 4.53M | fp2_new(t); |
83 | 4.53M | dv2_new(u0); |
84 | 4.53M | dv2_new(u1); |
85 | | |
86 | | /* t0 = a^2. */ |
87 | 4.53M | fp2_sqrn_low(u0, a[0]); |
88 | | /* t1 = b^2. */ |
89 | 4.53M | fp2_sqrn_low(u1, a[1]); |
90 | | |
91 | 4.53M | fp2_addm_low(t, a[0], a[1]); |
92 | | |
93 | | /* c = a^2 + b^2 * E. */ |
94 | 4.53M | fp2_nord_low(c[0], u1); |
95 | 4.53M | fp2_addc_low(c[0], c[0], u0); |
96 | | |
97 | | /* d = (a + b)^2 - a^2 - b^2 = 2 * a * b. */ |
98 | 4.53M | fp2_addc_low(u1, u1, u0); |
99 | 4.53M | fp2_sqrn_low(c[1], t); |
100 | 4.53M | fp2_subc_low(c[1], c[1], u1); |
101 | 9.07M | } RLC_CATCH_ANY { |
102 | 0 | RLC_THROW(ERR_CAUGHT); |
103 | 9.07M | } RLC_FINALLY { |
104 | 4.53M | fp2_free(t); |
105 | 4.53M | dv2_free(u0); |
106 | 4.53M | dv2_free(u1); |
107 | 4.53M | } |
108 | 4.53M | } |
109 | | |
110 | 1.13M | void fp4_sqr_lazyr(fp4_t c, const fp4_t a) { |
111 | 1.13M | dv4_t t; |
112 | | |
113 | 1.13M | dv4_null(t); |
114 | | |
115 | 1.13M | RLC_TRY { |
116 | 1.13M | dv4_new(t); |
117 | 1.13M | fp4_sqr_unr(t, a); |
118 | 1.13M | fp2_rdcn_low(c[0], t[0]); |
119 | 1.13M | fp2_rdcn_low(c[1], t[1]); |
120 | 2.26M | } RLC_CATCH_ANY { |
121 | 0 | RLC_THROW(ERR_CAUGHT); |
122 | 2.26M | } RLC_FINALLY { |
123 | 1.13M | dv4_free(t); |
124 | 1.13M | } |
125 | 1.13M | } |
126 | | |
127 | | #endif |