]> Git Repo - secp256k1.git/blame - src/field.h
Implement endomorphism optimization for secp256k1_ecmult_const
[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
7277fd76 25#if defined(USE_FIELD_10X26)
3231676b 26#include "field_10x26.h"
f0c89aad 27#elif defined(USE_FIELD_5X52)
e6d142a8 28#include "field_5x52.h"
f0c89aad
PW
29#else
30#error "Please select field implementation"
ea165f47 31#endif
b394396b 32
910d0de4 33/** Normalize a field element. */
a4a43d75 34static void secp256k1_fe_normalize(secp256k1_fe_t *r);
910d0de4 35
0295f0a3
PW
36/** Weakly normalize a field element: reduce it magnitude to 1, but don't fully normalize. */
37static void secp256k1_fe_normalize_weak(secp256k1_fe_t *r);
38
39bd94d8
PW
39/** Normalize a field element, without constant-time guarantee. */
40static void secp256k1_fe_normalize_var(secp256k1_fe_t *r);
41
eed599dd
PD
42/** Verify whether a field element represents zero i.e. would normalize to a zero value. The field
43 * implementation may optionally normalize the input, but this should not be relied upon. */
44static int secp256k1_fe_normalizes_to_zero(secp256k1_fe_t *r);
45
49ee0dbe
PD
46/** Verify whether a field element represents zero i.e. would normalize to a zero value. The field
47 * implementation may optionally normalize the input, but this should not be relied upon. */
48static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe_t *r);
49
910d0de4 50/** Set a field element equal to a small integer. Resulting field element is normalized. */
a4a43d75 51static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a);
910d0de4
PW
52
53/** Verify whether a field element is zero. Requires the input to be normalized. */
a4a43d75 54static int secp256k1_fe_is_zero(const secp256k1_fe_t *a);
910d0de4
PW
55
56/** Check the "oddness" of a field element. Requires the input to be normalized. */
a4a43d75 57static int secp256k1_fe_is_odd(const secp256k1_fe_t *a);
910d0de4 58
d7174edf
PW
59/** Compare two field elements. Requires magnitude-1 inputs. */
60static int secp256k1_fe_equal_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
910d0de4 61
f24041d6
PW
62/** Compare two field elements. Requires both inputs to be normalized */
63static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
64
d907ebc0
PW
65/** Set a field element equal to 32-byte big endian value. If succesful, the resulting field element is normalized. */
66static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a);
910d0de4
PW
67
68/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
a4a43d75 69static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a);
910d0de4
PW
70
71/** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input
72 * as an argument. The magnitude of the output is one higher. */
a4a43d75 73static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m);
910d0de4
PW
74
75/** Multiplies the passed field element with a small integer constant. Multiplies the magnitude by that
76 * small integer. */
a4a43d75 77static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a);
910d0de4
PW
78
79/** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */
a4a43d75 80static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4
PW
81
82/** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.
83 * The output magnitude is 1 (but not guaranteed to be normalized). */
be82e92f 84static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b);
910d0de4
PW
85
86/** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.
87 * The output magnitude is 1 (but not guaranteed to be normalized). */
a4a43d75 88static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4 89
09ca4f32
PD
90/** Sets a field element to be the (modular) square root (if any exist) of another. Requires the
91 * input's magnitude to be at most 8. The output magnitude is 1 (but not guaranteed to be
92 * normalized). Return value indicates whether a square root was found. */
39bd94d8 93static int secp256k1_fe_sqrt_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
910d0de4
PW
94
95/** Sets a field element to be the (modular) inverse of another. Requires the input's magnitude to be
96 * at most 8. The output magnitude is 1 (but not guaranteed to be normalized). */
a4a43d75 97static void secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a);
b394396b 98
910d0de4 99/** Potentially faster version of secp256k1_fe_inv, without constant-time guarantee. */
a4a43d75 100static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
cb4d29c8 101
f16be77f
PD
102/** Calculate the (modular) inverses of a batch of field elements. Requires the inputs' magnitudes to be
103 * at most 8. The output magnitudes are 1 (but not guaranteed to be normalized). The inputs and
104 * outputs must not overlap in memory. */
3627437d 105static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t *r, const secp256k1_fe_t *a);
f16be77f 106
ff889f7d
PW
107/** Convert a field element to the storage type. */
108static void secp256k1_fe_to_storage(secp256k1_fe_storage_t *r, const secp256k1_fe_t*);
109
110/** Convert a field element back from the storage type. */
111static void secp256k1_fe_from_storage(secp256k1_fe_t *r, const secp256k1_fe_storage_t*);
112
efb7d4b2 113/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
ff889f7d 114static void secp256k1_fe_storage_cmov(secp256k1_fe_storage_t *r, const secp256k1_fe_storage_t *a, int flag);
efb7d4b2 115
bb0ea50d
GM
116/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
117static void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag);
118
b394396b 119#endif
This page took 0.050817 seconds and 4 git commands to generate.