|
| 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 */ |
0 commit comments