xref: /freebsd/crypto/libecc/include/libecc/curves/aff_pt.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2017 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #ifndef __AFF_PT_H__
17 #define __AFF_PT_H__
18 
19 #include <libecc/fp/fp.h>
20 #include <libecc/fp/fp_sqrt.h>
21 #include <libecc/curves/ec_shortw.h>
22 #include <libecc/curves/ec_montgomery.h>
23 #include <libecc/curves/ec_edwards.h>
24 
25 typedef struct {
26 	fp x;
27 	fp y;
28 	ec_shortw_crv_src_t crv;
29 	word_t magic;
30 } aff_pt;
31 
32 typedef aff_pt *aff_pt_t;
33 typedef const aff_pt_t aff_pt_src_t;
34 
35 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_check_initialized(aff_pt_src_t in);
36 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_init(aff_pt_t in, ec_shortw_crv_src_t curve);
37 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_init_from_coords(aff_pt_t in,
38 			    ec_shortw_crv_src_t curve,
39 			    fp_src_t xcoord, fp_src_t ycoord);
40 void aff_pt_uninit(aff_pt_t in);
41 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_y_from_x(fp_t y1, fp_t y2, fp_src_t x, ec_shortw_crv_src_t curve);
42 ATTRIBUTE_WARN_UNUSED_RET int is_on_shortw_curve(fp_src_t x, fp_src_t y, ec_shortw_crv_src_t curve, int *on_curve);
43 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_is_on_curve(aff_pt_src_t pt, int *on_curve);
44 ATTRIBUTE_WARN_UNUSED_RET int ec_shortw_aff_copy(aff_pt_t out, aff_pt_src_t in);
45 ATTRIBUTE_WARN_UNUSED_RET int ec_shortw_aff_cmp(aff_pt_src_t in1, aff_pt_src_t in2, int *cmp);
46 ATTRIBUTE_WARN_UNUSED_RET int ec_shortw_aff_eq_or_opp(aff_pt_src_t in1, aff_pt_src_t in2,
47 			    int *eq_or_opp);
48 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_import_from_buf(aff_pt_t pt,
49 			   const u8 *pt_buf,
50 			   u16 pt_buf_len, ec_shortw_crv_src_t crv);
51 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_export_to_buf(aff_pt_src_t pt, u8 *pt_buf, u32 pt_buf_len);
52 
53 /*** Edwards curves related ***/
54 typedef struct {
55 	fp x;
56 	fp y;
57 	ec_edwards_crv_src_t crv;
58 	word_t magic;
59 } aff_pt_edwards;
60 
61 typedef aff_pt_edwards *aff_pt_edwards_t;
62 typedef const aff_pt_edwards_t aff_pt_edwards_src_t;
63 
64 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_check_initialized(aff_pt_edwards_src_t in);
65 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_init(aff_pt_edwards_t in, ec_edwards_crv_src_t curve);
66 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_init_from_coords(aff_pt_edwards_t in,
67 			     ec_edwards_crv_src_t curve,
68 			     fp_src_t ucoord, fp_src_t vcoord);
69 void aff_pt_edwards_uninit(aff_pt_edwards_t in);
70 ATTRIBUTE_WARN_UNUSED_RET int is_on_edwards_curve(fp_src_t u, fp_src_t v, ec_edwards_crv_src_t curve, int *on_curve);
71 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_is_on_curve(aff_pt_edwards_src_t pt, int *on_curve);
72 ATTRIBUTE_WARN_UNUSED_RET int ec_edwards_aff_copy(aff_pt_edwards_t out, aff_pt_edwards_src_t in);
73 ATTRIBUTE_WARN_UNUSED_RET int ec_edwards_aff_cmp(aff_pt_edwards_src_t in1, aff_pt_edwards_src_t in2, int *cmp);
74 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_import_from_buf(aff_pt_edwards_t pt,
75 			   const u8 *pt_buf,
76 			   u16 pt_buf_len, ec_edwards_crv_src_t crv);
77 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_export_to_buf(aff_pt_edwards_src_t pt, u8 *pt_buf, u32 pt_buf_len);
78 
79 ATTRIBUTE_WARN_UNUSED_RET int curve_edwards_to_montgomery(ec_edwards_crv_src_t edwards_crv, ec_montgomery_crv_t montgomery_crv, fp_src_t alpha_edwards);
80 ATTRIBUTE_WARN_UNUSED_RET int curve_edwards_montgomery_check(ec_edwards_crv_src_t edwards_crv, ec_montgomery_crv_src_t montgomery_crv, fp_src_t alpha_edwards);
81 
82 ATTRIBUTE_WARN_UNUSED_RET int curve_montgomery_to_edwards(ec_montgomery_crv_src_t montgomery_crv, ec_edwards_crv_t edwards_crv, fp_src_t alpha_edwards);
83 
84 ATTRIBUTE_WARN_UNUSED_RET int curve_edwards_to_shortw(ec_edwards_crv_src_t edwards_crv, ec_shortw_crv_t shortw_crv, fp_src_t alpha_edwards);
85 ATTRIBUTE_WARN_UNUSED_RET int curve_edwards_shortw_check(ec_edwards_crv_src_t edwards_crv, ec_shortw_crv_src_t shortw_crv, fp_src_t alpha_edwards);
86 ATTRIBUTE_WARN_UNUSED_RET int curve_shortw_to_edwards(ec_shortw_crv_src_t shortw_crv, ec_edwards_crv_t edwards_crv, fp_src_t alpha_montgomery, fp_src_t gamma_montgomery, fp_src_t alpha_edwards);
87 
88 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_to_shortw(aff_pt_edwards_src_t in_edwards, ec_shortw_crv_src_t shortw_crv, aff_pt_t out_shortw, fp_src_t alpha_edwards);
89 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_shortw_to_edwards(aff_pt_src_t in_shortw, ec_edwards_crv_src_t edwards_crv, aff_pt_edwards_t out_edwards, fp_src_t alpha_edwards);
90 
91 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_y_from_x(fp_t y1, fp_t y2, fp_src_t x, ec_edwards_crv_src_t crv);
92 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_x_from_y(fp_t x1, fp_t x2, fp_src_t y, ec_edwards_crv_src_t crv);
93 
94 /*** Montgomery curves related ***/
95 typedef struct {
96 	fp u;
97 	fp v;
98 	ec_montgomery_crv_src_t crv;
99 	word_t magic;
100 } aff_pt_montgomery;
101 
102 typedef aff_pt_montgomery *aff_pt_montgomery_t;
103 typedef const aff_pt_montgomery_t aff_pt_montgomery_src_t;
104 
105 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_check_initialized(aff_pt_montgomery_src_t in);
106 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_init(aff_pt_montgomery_t in, ec_montgomery_crv_src_t curve);
107 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_init_from_coords(aff_pt_montgomery_t in,
108                              ec_montgomery_crv_src_t curve,
109                              fp_src_t ucoord, fp_src_t vcoord);
110 void aff_pt_montgomery_uninit(aff_pt_montgomery_t in);
111 ATTRIBUTE_WARN_UNUSED_RET int is_on_montgomery_curve(fp_src_t u, fp_src_t v, ec_montgomery_crv_src_t curve, int *on_curve);
112 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_is_on_curve(aff_pt_montgomery_src_t pt, int *on_curve);
113 ATTRIBUTE_WARN_UNUSED_RET int ec_montgomery_aff_copy(aff_pt_montgomery_t out, aff_pt_montgomery_src_t in);
114 ATTRIBUTE_WARN_UNUSED_RET int ec_montgomery_aff_cmp(aff_pt_montgomery_src_t in1, aff_pt_montgomery_src_t in2, int *cmp);
115 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_import_from_buf(aff_pt_montgomery_t pt,
116                            const u8 *pt_buf,
117                            u16 pt_buf_len, ec_montgomery_crv_src_t crv);
118 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_export_to_buf(aff_pt_montgomery_src_t pt, u8 *pt_buf, u32 pt_buf_len);
119 
120 ATTRIBUTE_WARN_UNUSED_RET int curve_montgomery_to_shortw(ec_montgomery_crv_src_t montgomery_crv, ec_shortw_crv_t shortw_crv);
121 
122 ATTRIBUTE_WARN_UNUSED_RET int curve_montgomery_shortw_check(ec_montgomery_crv_src_t montgomery_crv, ec_shortw_crv_src_t shortw_crv);
123 ATTRIBUTE_WARN_UNUSED_RET int curve_shortw_to_montgomery(ec_shortw_crv_src_t shortw_crv, ec_montgomery_crv_t montgomery_crv, fp_src_t alpha_montgomery, fp_src_t gamma_montgomery);
124 
125 
126 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_to_shortw(aff_pt_montgomery_src_t in_montgomery, ec_shortw_crv_src_t shortw_crv, aff_pt_t out_shortw);
127 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_shortw_to_montgomery(aff_pt_src_t in_shortw, ec_montgomery_crv_src_t montgomery_crv, aff_pt_montgomery_t out_montgomery);
128 
129 
130 /*****/
131 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_edwards_to_montgomery(aff_pt_edwards_src_t in_edwards, ec_montgomery_crv_src_t montgomery_crv, aff_pt_montgomery_t out_montgomery, fp_src_t alpha);
132 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_to_edwards(aff_pt_montgomery_src_t in_montgomery, ec_edwards_crv_src_t edwards_crv, aff_pt_edwards_t out_edwards, fp_src_t alpha);
133 
134 ATTRIBUTE_WARN_UNUSED_RET int aff_pt_montgomery_v_from_u(fp_t v1, fp_t v2, fp_src_t u, ec_montgomery_crv_src_t crv);
135 
136  #endif /* __AFF_PT_H__ */
137