Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
ctidh_api.c
Go to the documentation of this file.
1#include <assert.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <immintrin.h>
6#include "../common/fips202.h"
7
8#define HASH_BYTES NUMBER_OF_WORDS*8
9#define HASH(data,len,out) shake256(out, HASH_BYTES, data, len);
10
11#if defined AVX2
12 #include "../common/fp/avx2/fp-avx2.h"
13#elif defined GMP
15#elif defined KARATSUBA
17#else
18 #include "../common/fp/mulx/fp.h"
19#endif
20#include "../common/namespace.h"
21#include "../common/primes.h"
22#include "ctidh.h"
23// #include "cpucycles.h"
24
25const size_t NSAPI(pk_size) = sizeof(public_key) * 1;
26const size_t NSAPI(sk_size) = sizeof(private_key);
27const size_t NSAPI(ss_size) = sizeof(fp) * 1;
28
29#define secsidh_keygen NSAPI(keygen)
30#define secsidh_derive NSAPI(derive)
31
32#define cprintf(...) printf(__VA_ARGS__)
33// #define N primes_num
34
35#include "ctidh_api.h"
36#include "ctidh.h"
37
38#ifdef ENABLE_CT_TESTING
39#include <valgrind/memcheck.h>
40#endif
41
42
43// void action(public_key *out, public_key const *in, private_key const *priv)
44
45// Public (Montgomery curve affine coefficient) and private (integer vector) keys generation
47{
48 ctidh_private((private_key*) sk); // random private integer vector
49
50 action(pk,&base,(private_key*)sk);
51
52 // we need to compute the seed of the full order point
53 fp u;
54 fulltorsion_points(u, pk->A);
55
56 pk->seed = u[0];
57 printf("seed: %ld\n", pk->seed);
58}
59
60
61
62// Secret sharing derivation (Montgomery curve affine coefficient)
63bool internal_derive(fp* ss, public_key* const pk, private_key* const sk)
64{
65
66#ifdef ENABLE_CT_TESTING
67 VALGRIND_MAKE_MEM_DEFINED(pk, sizeof(public_key));
68#endif
69
70 if (!validate((public_key*)pk)) return 0; // validating the input Montgomery curve affine coefficiente (it must be supersingular!)
71
72 public_key shared;
73 action((public_key*)&shared, pk, sk); // Secrect sharing Montgomery curve affine coefficient: [sk] * pk
74
75 //HASH((uint8_t*)shared.A, sizeof(fp), (uint8_t*) ss);
76 fp_copy(*ss, shared.A);
77 return 1;
78}
79
80void skgen(int8_t* sk) // secret key generation
81{
83}
84
85void pkgen(public_key* pk, int8_t* const sk) // public key generation
86{
87 action(pk,&base,(private_key*)sk);
88}
89
90// ----------------------------------- helpers for the interface between static and public API
91
92#define SECSIDH_SUCCESS 0
93#define SECSIDH_FAILURE -1
94
95
96/* Safely clear a buffer b of size s */
97static inline
98void secsidh_clear(void *b, size_t s)
99{
100 /*
101 * TODO: tricks might be needed to ensure the compiler does not
102 * strip this call away.
103 *
104 * Potentially this could be an externally provided function at some
105 * point.
106 */
107 memset(b, 0, s);
108}
109
110/*
111 * Converts the internal representation of a secret key into an octet
112 * buffer.
113 *
114 * TODO: at some point this should take care of endianess issues.
115 */
116static inline
117void secsidh_sk2oct(uint8_t *buf, const private_key *sk)
118{
119 memcpy(buf, sk,sizeof(private_key));
120}
121
122/*
123 * Converts the internal representation of a public key into an octet
124 * buffer.
125 *
126 * TODO: at some point this should take care of endianess issues.
127 */
128static inline
129void secsidh_pk2oct(uint8_t *buf, const public_key *pk)
130{
131 // uint8_t test[2 * sizeof(fp)];
132 //memcpy(test, pk, 2*sizeof(fp));
133
134 // fp_2oct(buf + sizeof(fp), &pk[1]);
135
136 memcpy(&buf[sizeof(fp)], &pk->seed, sizeof(uint64_t));
137 fp_2oct(buf, &pk->A);
138
139
140 //memcpy(buf, pk, sizeof(public_key));
141
142 // assert(memcmp(test, buf, sizeof(test))==0);
143}
144
145/*
146 * Converts the internal representation of a shared secret into an octet
147 * buffer.
148 *
149 * TODO: at some point this should take care of endianess issues.
150 */
151static inline
152void secsidh_ss2oct(uint8_t *buf, const fp ss[1])
153{
154 fp_2oct(buf, ss);
155}
156
157/*
158 * Converts the octet buffer representation of a public key into our
159 * internal representation.
160 *
161 * TODO: at some point this should take care of endianess issues.
162 */
163static inline
164void secsidh_oct2pk(public_key *pk, const uint8_t *buf)
165{
166 memcpy(&pk->seed, &buf[sizeof(fp)], sizeof(uint64_t));
167 oct2_fp(&pk->A, buf);
168
169
170 //memcpy(pk, buf, sizeof(public_key));
171}
172
173/*
174 * Converts the octet buffer representation of a secret key into our
175 * internal representation.
176 *
177 * TODO: at some point this should take care of endianess issues.
178 */
179static inline
180void secsidh_oct2sk(private_key *sk, const uint8_t *buf)
181{
182 memcpy(sk, buf, sizeof(private_key));
183}
184
185// ----------------------------------- Interface between static and public API
186
187int secsidh_keygen(uint8_t *pk, uint8_t *sk)
188{
189 public_key ipk[2] = {0};
190 private_key isk;
191 internal_keygen(ipk, &isk);
192
193
194 secsidh_pk2oct(pk, (const public_key*)ipk);
195
196 secsidh_sk2oct(sk, &isk);
197 secsidh_clear(&isk, sizeof(isk));
198
199 return SECSIDH_SUCCESS;
200}
201
202int secsidh_derive(uint8_t *ss, const uint8_t *peer_pk, const uint8_t *sk)
203{
204 int ret;
205 public_key ipeer_pk = {0};
206 fp iss;
207 private_key isk;
208
209 secsidh_oct2pk(&ipeer_pk, peer_pk);
210 memset(&iss, 0, sizeof(fp));
211
212 secsidh_oct2sk(&isk, sk);
213
214 ret = internal_derive(&iss, &ipeer_pk, &isk) == 1 ? SECSIDH_SUCCESS : SECSIDH_FAILURE;
215
216
217 secsidh_clear(&isk, sizeof(isk));
218 secsidh_ss2oct(ss, (const fp*)iss);
219 secsidh_clear(iss, sizeof(iss));
220
221 return ret;
222}
#define sk_size
Definition checkct.c:29
#define pk_size
Definition checkct.c:28
#define ss_size
Definition checkct.c:30
#define validate
Definition ctidh.h:53
#define ctidh_private
Definition ctidh.h:47
#define base
Definition ctidh.h:44
#define fulltorsion_points
Definition ctidh.h:57
#define action
Definition ctidh.h:54
#define SECSIDH_FAILURE
Definition ctidh_api.c:93
#define secsidh_keygen
Definition ctidh_api.c:29
#define SECSIDH_SUCCESS
Definition ctidh_api.c:92
#define secsidh_derive
Definition ctidh_api.c:30
#define internal_keygen
Definition ctidh_api.h:21
#define internal_derive
Definition ctidh_api.h:23
#define skgen
Definition ctidh_api.h:27
#define pkgen
Definition ctidh_api.h:29
uint64_t fp[NUMBER_OF_WORDS]
Definition fp-gmp.h:22
#define fp_copy
Definition fp-gmp.h:79
#define NSAPI(fname)
Definition namespace.h:25
uint64_t seed
Definition ctidh.h:41