Skip to content

Commit c47917b

Browse files
committed
Add ellswift module implementing ElligatorSwift
The scheme implemented is described below, and largely follows the paper "SwiftEC: Shallue–van de Woestijne Indifferentiable Function To Elliptic Curves", by Chavez-Saab, Rodriguez-Henriquez, and Tibouchi (https://eprint.iacr.org/2022/759). A new 64-byte public key format is introduced, with the property that *every* 64-byte array is an encoding for a non-infinite curve point. Each curve point has roughly 2^256 distinct encodings. This permits disguising public keys as uniformly random bytes. The new API functions: * secp256k1_ellswift_encode: convert a normal public key to an ellswift 64-byte public key, using additional entropy to pick among the many possible encodings. * secp256k1_ellswift_decode: convert an ellswift 64-byte public key to a normal public key. * secp256k1_ellswift_create: a faster and safer equivalent to calling secp256k1_ec_pubkey_create + secp256k1_ellswift_encode. * secp256k1_ellswift_xdh: x-only ECDH directly on ellswift 64-byte public keys, where the key encodings are fed to the hash function. The scheme itself is documented in secp256k1_ellswift.h.
1 parent 79e5b2a commit c47917b

8 files changed

+756
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ if(SECP256K1_ENABLE_MODULE_EXTRAKEYS)
7171
add_compile_definitions(ENABLE_MODULE_EXTRAKEYS=1)
7272
endif()
7373

74+
option(SECP256K1_ENABLE_MODULE_ELLSWIFT "Enable ElligatorSwift module." ON)
75+
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
76+
add_compile_definitions(ENABLE_MODULE_ELLSWIFT=1)
77+
endif()
78+
7479
option(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS "Enable external default callback functions." OFF)
7580
if(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS)
7681
add_compile_definitions(USE_EXTERNAL_DEFAULT_CALLBACKS=1)
@@ -270,6 +275,7 @@ message(" ECDH ................................ ${SECP256K1_ENABLE_MODULE_ECDH}
270275
message(" ECDSA pubkey recovery ............... ${SECP256K1_ENABLE_MODULE_RECOVERY}")
271276
message(" extrakeys ........................... ${SECP256K1_ENABLE_MODULE_EXTRAKEYS}")
272277
message(" schnorrsig .......................... ${SECP256K1_ENABLE_MODULE_SCHNORRSIG}")
278+
message(" ElligatorSwift ...................... ${SECP256K1_ENABLE_MODULE_ELLSWIFT}")
273279
message("Parameters:")
274280
message(" ecmult window size .................. ${SECP256K1_ECMULT_WINDOW_SIZE}")
275281
message(" ecmult gen precision bits ........... ${SECP256K1_ECMULT_GEN_PREC_BITS}")

Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,7 @@ endif
267267
if ENABLE_MODULE_SCHNORRSIG
268268
include src/modules/schnorrsig/Makefile.am.include
269269
endif
270+
271+
if ENABLE_MODULE_ELLSWIFT
272+
include src/modules/ellswift/Makefile.am.include
273+
endif

configure.ac

+10
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ AC_ARG_ENABLE(module_schnorrsig,
190190
AS_HELP_STRING([--enable-module-schnorrsig],[enable schnorrsig module [default=yes]]), [],
191191
[SECP_SET_DEFAULT([enable_module_schnorrsig], [yes], [yes])])
192192

193+
AC_ARG_ENABLE(module_ellswift,
194+
AS_HELP_STRING([--enable-module-ellswift],[enable ElligatorSwift module [default=yes]]), [],
195+
[SECP_SET_DEFAULT([enable_module_ellswift], [yes], [yes])])
196+
193197
AC_ARG_ENABLE(external_default_callbacks,
194198
AS_HELP_STRING([--enable-external-default-callbacks],[enable external default callback functions [default=no]]), [],
195199
[SECP_SET_DEFAULT([enable_external_default_callbacks], [no], [no])])
@@ -402,6 +406,10 @@ if test x"$enable_module_schnorrsig" = x"yes"; then
402406
enable_module_extrakeys=yes
403407
fi
404408

409+
if test x"$enable_module_ellswift" = x"yes"; then
410+
AC_DEFINE(ENABLE_MODULE_ELLSWIFT, 1, [Define this symbol to enable the ElligatorSwift module])
411+
fi
412+
405413
# Test if extrakeys is set after the schnorrsig module to allow the schnorrsig
406414
# module to set enable_module_extrakeys=yes
407415
if test x"$enable_module_extrakeys" = x"yes"; then
@@ -444,6 +452,7 @@ AM_CONDITIONAL([ENABLE_MODULE_ECDH], [test x"$enable_module_ecdh" = x"yes"])
444452
AM_CONDITIONAL([ENABLE_MODULE_RECOVERY], [test x"$enable_module_recovery" = x"yes"])
445453
AM_CONDITIONAL([ENABLE_MODULE_EXTRAKEYS], [test x"$enable_module_extrakeys" = x"yes"])
446454
AM_CONDITIONAL([ENABLE_MODULE_SCHNORRSIG], [test x"$enable_module_schnorrsig" = x"yes"])
455+
AM_CONDITIONAL([ENABLE_MODULE_ELLSWIFT], [test x"$enable_module_ellswift" = x"yes"])
447456
AM_CONDITIONAL([USE_EXTERNAL_ASM], [test x"$enable_external_asm" = x"yes"])
448457
AM_CONDITIONAL([USE_ASM_ARM], [test x"$set_asm" = x"arm32"])
449458
AM_CONDITIONAL([BUILD_WINDOWS], [test "$build_windows" = "yes"])
@@ -465,6 +474,7 @@ echo " module ecdh = $enable_module_ecdh"
465474
echo " module recovery = $enable_module_recovery"
466475
echo " module extrakeys = $enable_module_extrakeys"
467476
echo " module schnorrsig = $enable_module_schnorrsig"
477+
echo " module ellswift = $enable_module_ellswift"
468478
echo
469479
echo " asm = $set_asm"
470480
echo " ecmult window size = $set_ecmult_window"

include/secp256k1_ellswift.h

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#ifndef SECP256K1_ELLSWIFT_H
2+
#define SECP256K1_ELLSWIFT_H
3+
4+
#include "secp256k1.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
/* This module provides an implementation of ElligatorSwift as well as a
11+
* version of x-only ECDH using it.
12+
*
13+
* ElligatorSwift is described in https://eprint.iacr.org/2022/759 by
14+
* Chavez-Saab, Rodriguez-Henriquez, and Tibouchi. It permits encoding
15+
* uniformly chosen public keys as 64-byte arrays which are indistinguishable
16+
* from uniformly random arrays.
17+
*
18+
* Let f be the function from pairs of field elements to point X coordinates,
19+
* defined as follows (all operations modulo p = 2^256 - 2^32 - 977)
20+
* f(u,t):
21+
* - Let C = 0xa2d2ba93507f1df233770c2a797962cc61f6d15da14ecd47d8d27ae1cd5f852,
22+
* a square root of -3.
23+
* - If u=0, set u=1 instead.
24+
* - If t=0, set t=1 instead.
25+
* - If u^3 + t^2 + 7 = 0, multiply t by 2.
26+
* - Let X = (u^3 + 7 - t^2) / (2 * t)
27+
* - Let Y = (X + t) / (C * u)
28+
* - Return the first in [u + 4 * Y^2, (-X/Y - u) / 2, (X/Y - u) / 2] that is an
29+
* X coordinate on the curve (at least one of them is, for any u and t).
30+
*
31+
* Then an ElligatorSwift encoding of x consists of the 32-byte big-endian
32+
* encodings of field elements u and t concatenated, where f(u,t) = x.
33+
* The encoding algorithm is described in the paper, and effectively picks a
34+
* uniformly random pair (u,t) among those which encode x.
35+
*
36+
* If the Y coordinate is relevant, it is given the same parity as t.
37+
*
38+
* Changes w.r.t. the the paper:
39+
* - The u=0, t=0, and u^3+t^2+7=0 conditions result in decoding to the point
40+
* at infinity in the paper. Here they are remapped to finite points.
41+
* - The paper uses an additional encoding bit for the parity of y. Here the
42+
* parity of t is used (negating t does not affect the decoded x coordinate,
43+
* so this is possible).
44+
*/
45+
46+
/** A pointer to a function used by secp256k1_ellswift_xdh to hash the shared X
47+
* coordinate along with the encoded public keys to a uniform shared secret.
48+
*
49+
* Returns: 1 if a shared secret was successfully computed.
50+
* 0 will cause secp256k1_ellswift_xdh to fail and return 0.
51+
* Other return values are not allowed, and the behaviour of
52+
* secp256k1_ellswift_xdh is undefined for other return values.
53+
* Out: output: pointer to an array to be filled by the function
54+
* In: x32: pointer to the 32-byte serialized X coordinate
55+
* of the resulting shared point (will not be NULL)
56+
* ell_a64: pointer to the 64-byte encoded public key of party A
57+
* (will not be NULL)
58+
* ell_b64: pointer to the 64-byte encoded public key of party B
59+
* (will not be NULL)
60+
* data: arbitrary data pointer that is passed through
61+
*/
62+
typedef int (*secp256k1_ellswift_xdh_hash_function)(
63+
unsigned char *output,
64+
const unsigned char *x32,
65+
const unsigned char *ell_a64,
66+
const unsigned char *ell_b64,
67+
void *data
68+
);
69+
70+
/** Construct a 64-byte ElligatorSwift encoding of a given pubkey.
71+
*
72+
* Returns: 1 always.
73+
* Args: ctx: pointer to a context object
74+
* Out: ell64: pointer to a 64-byte array to be filled
75+
* In: pubkey: a pointer to a secp256k1_pubkey containing an
76+
* initialized public key
77+
* rnd32: pointer to 32 bytes of randomness
78+
*
79+
* It is recommended that rnd32 consists of 32 uniformly random bytes, not
80+
* known to any adversary trying to detect whether public keys are being
81+
* encoded, though 16 bytes of randomness (padded to an array of 32 bytes,
82+
* e.g., with zeros) suffice to make the result indistinguishable from
83+
* uniform. The randomness in rnd32 must not be a deterministic function of
84+
* the pubkey (it can be derived from the private key, though).
85+
*
86+
* It is not guaranteed that the computed encoding is stable across versions
87+
* of the library, even if all arguments to this function (including rnd32)
88+
* are the same.
89+
*
90+
* This function runs in variable time.
91+
*/
92+
SECP256K1_API int secp256k1_ellswift_encode(
93+
const secp256k1_context *ctx,
94+
unsigned char *ell64,
95+
const secp256k1_pubkey *pubkey,
96+
const unsigned char *rnd32
97+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
98+
99+
/** Decode a 64-bytes ElligatorSwift encoded public key.
100+
*
101+
* Returns: always 1
102+
* Args: ctx: pointer to a context object
103+
* Out: pubkey: pointer to a secp256k1_pubkey that will be filled
104+
* In: ell64: pointer to a 64-byte array to decode
105+
*
106+
* This function runs in variable time.
107+
*/
108+
SECP256K1_API int secp256k1_ellswift_decode(
109+
const secp256k1_context *ctx,
110+
secp256k1_pubkey *pubkey,
111+
const unsigned char *ell64
112+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
113+
114+
/** Compute an ElligatorSwift public key for a secret key.
115+
*
116+
* Returns: 1: secret was valid, public key was stored.
117+
* 0: secret was invalid, try again.
118+
* Args: ctx: pointer to a context object
119+
* Out: ell64: pointer to a 64-byte array to receive the ElligatorSwift
120+
* public key
121+
* In: seckey32: pointer to a 32-byte secret key
122+
* auxrnd32: (optional) pointer to 32 bytes of randomness
123+
*
124+
* Constant time in seckey and auxrnd32, but not in the resulting public key.
125+
*
126+
* It is recommended that auxrnd32 contains 32 uniformly random bytes, though
127+
* it is optional (and does result in encodings that are indistinguishable from
128+
* uniform even without any auxrnd32). It differs from the (mandatory) rnd32
129+
* argument to secp256k1_ellswift_encode in this regard.
130+
*
131+
* This function can be used instead of calling secp256k1_ec_pubkey_create
132+
* followed by secp256k1_ellswift_encode. It is safer, as it uses the secret
133+
* key as entropy for the encoding (supplemented with auxrnd32, if provided).
134+
*
135+
* Like secp256k1_ellswift_encode, this function does not guarantee that the
136+
* computed encoding is stable across versions of the library, even if all
137+
* arguments (including auxrnd32) are the same.
138+
*/
139+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_create(
140+
const secp256k1_context *ctx,
141+
unsigned char *ell64,
142+
const unsigned char *seckey32,
143+
const unsigned char *auxrnd32
144+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
145+
146+
/** Given a private key, and ElligatorSwift public keys sent in both directions,
147+
* compute a shared secret using x-only Elliptic Curve Diffie-Hellman (ECDH).
148+
*
149+
* Returns: 1: shared secret was succesfully computed
150+
* 0: secret was invalid or hashfp returned 0
151+
* Args: ctx: pointer to a context object.
152+
* Out: output: pointer to an array to be filled by hashfp.
153+
* In: ell_a64: pointer to the 64-byte encoded public key of party A
154+
* (will not be NULL)
155+
* ell_b64: pointer to the 64-byte encoded public key of party B
156+
* (will not be NULL)
157+
* seckey32: a pointer to our 32-byte secret key
158+
* party: boolean indicating which party we are: zero if we are
159+
* party A, non-zero if we are party B. seckey32 must be
160+
* the private key corresponding to that party's ell_?64.
161+
* This correspondence is not checked.
162+
* hashfp: pointer to a hash function.
163+
* data: arbitrary data pointer passed through to hashfp.
164+
*
165+
* Constant time in seckey32.
166+
*
167+
* This function is more efficient than decoding the public keys, and performing
168+
* ECDH on them.
169+
*/
170+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_xdh(
171+
const secp256k1_context *ctx,
172+
unsigned char *output,
173+
const unsigned char *ell_a64,
174+
const unsigned char *ell_b64,
175+
const unsigned char *seckey32,
176+
int party,
177+
secp256k1_ellswift_xdh_hash_function hashfp,
178+
void *data
179+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7);
180+
181+
#ifdef __cplusplus
182+
}
183+
#endif
184+
185+
#endif /* SECP256K1_ELLSWIFT_H */

src/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ if(SECP256K1_INSTALL)
132132
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
133133
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_schnorrsig.h")
134134
endif()
135+
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
136+
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ellswift.h")
137+
endif()
135138
install(FILES ${${PROJECT_NAME}_headers}
136139
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
137140
)
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include_HEADERS += include/secp256k1_ellswift.h
2+
noinst_HEADERS += src/modules/ellswift/main_impl.h

0 commit comments

Comments
 (0)