]> Git Repo - secp256k1.git/blame - src/field.h
Merge pull request #140
[secp256k1.git] / src / field.h
CommitLineData
71712b27
GM
1/**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
0a433ea2 6
b394396b
PW
7#ifndef _SECP256K1_FIELD_
8#define _SECP256K1_FIELD_
9
910d0de4
PW
10/** Field element module.
11 *
12 * Field elements can be represented in several ways, but code accessing
13 * it (and implementations) need to take certain properaties into account:
14 * - Each field element can be normalized or not.
15 * - Each field element has a magnitude, which represents how far away
16 * its representation is away from normalization. Normalized elements
17 * always have a magnitude of 1, but a magnitude of 1 doesn't imply
18 * normality.
19 */
20
78cd96b1
CF
21#if defined HAVE_CONFIG_H
22#include "libsecp256k1-config.h"
23#endif
24
f0c89aad 25#if defined(USE_FIELD_GMP)
ea165f47 26#include "field_gmp.h"
f0c89aad 27#elif defined(USE_FIELD_10X26)
3231676b 28#include "field_10x26.h"
f0c89aad 29#elif defined(USE_FIELD_5X52)
e6d142a8 30#include "field_5x52.h"
f0c89aad
PW
31#else
32#error "Please select field implementation"
ea165f47 33#endif
b394396b 34
910d0de4 35typedef struct {
597128d3 36#ifndef USE_NUM_NONE
910d0de4 37 secp256k1_num_t p;
597128d3 38#endif
f24041d6 39 secp256k1_fe_t order;
910d0de4
PW
40} secp256k1_fe_consts_t;
41
42static const secp256k1_fe_consts_t *secp256k1_fe_consts = NULL;
43
44/** Initialize field element precomputation data. */
a4a43d75 45static void secp256k1_fe_start(void);
910d0de4
PW
46
47/** Unload field element precomputation data. */
a4a43d75 48static void secp256k1_fe_stop(void);
910d0de4
PW
49
50/** Normalize a field element. */
a4a43d75 51static void secp256k1_fe_normalize(secp256k1_fe_t *r);
910d0de4
PW
52
53/** Set a field element equal to a small integer. Resulting field element is normalized. */
a4a43d75 54static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a);
910d0de4
PW
55
56/** Verify whether a field element is zero. Requires the input to be normalized. */
a4a43d75 57static int secp256k1_fe_is_zero(const secp256k1_fe_t *a);
910d0de4
PW
58
59/** Check the "oddness" of a field element. Requires the input to be normalized. */
a4a43d75 60static int secp256k1_fe_is_odd(const secp256k1_fe_t *a);
910d0de4
PW
61
62/** Compare two field elements. Requires both inputs to be normalized */
a4a43d75 63static int secp256k1_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
910d0de4 64
f24041d6
PW
65/** Compare two field elements. Requires both inputs to be normalized */
66static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
67
d907ebc0
PW
68/** Set a field element equal to 32-byte big endian value. If succesful, the resulting field element is normalized. */
69static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a);
910d0de4
PW
70
71/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
a4a43d75 72static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a);
910d0de4
PW
73
74/** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input
75 * as an argument. The magnitude of the output is one higher. */
a4a43d75 76static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m);
910d0de4
PW
77
78/** Multiplies the passed field element with a small integer constant. Multiplies the magnitude by that
79 * small integer. */
a4a43d75 80static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a);
910d0de4
PW
81
82/** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */
a4a43d75 83static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4
PW
84
85/** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.
86 * The output magnitude is 1 (but not guaranteed to be normalized). */
be82e92f 87static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b);
910d0de4
PW
88
89/** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.
90 * The output magnitude is 1 (but not guaranteed to be normalized). */
a4a43d75 91static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4 92
09ca4f32
PD
93/** Sets a field element to be the (modular) square root (if any exist) of another. Requires the
94 * input's magnitude to be at most 8. The output magnitude is 1 (but not guaranteed to be
95 * normalized). Return value indicates whether a square root was found. */
a4a43d75 96static int secp256k1_fe_sqrt(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4
PW
97
98/** Sets a field element to be the (modular) inverse of another. Requires the input's magnitude to be
99 * at most 8. The output magnitude is 1 (but not guaranteed to be normalized). */
a4a43d75 100static void secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a);
b394396b 101
910d0de4 102/** Potentially faster version of secp256k1_fe_inv, without constant-time guarantee. */
a4a43d75 103static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
cb4d29c8 104
f16be77f
PD
105/** Calculate the (modular) inverses of a batch of field elements. Requires the inputs' magnitudes to be
106 * at most 8. The output magnitudes are 1 (but not guaranteed to be normalized). The inputs and
107 * outputs must not overlap in memory. */
a4a43d75 108static void secp256k1_fe_inv_all(size_t len, secp256k1_fe_t r[len], const secp256k1_fe_t a[len]);
f16be77f
PD
109
110/** Potentially faster version of secp256k1_fe_inv_all, without constant-time guarantee. */
a4a43d75 111static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const secp256k1_fe_t a[len]);
f16be77f 112
910d0de4 113/** Convert a field element to a hexadecimal string. */
a4a43d75 114static void secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a);
e8c2a8ec 115
910d0de4 116/** Convert a hexadecimal string to a field element. */
d907ebc0 117static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen);
b394396b 118
efb7d4b2
PW
119/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
120static void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag);
121
b394396b 122#endif
This page took 0.042989 seconds and 4 git commands to generate.