secp256k1/src/group.h

123 lines
5.1 KiB
C
Raw Normal View History

2013-05-09 13:24:32 +00:00
// Copyright (c) 2013 Pieter Wuille
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-03-08 00:20:41 +00:00
#ifndef _SECP256K1_GROUP_
#define _SECP256K1_GROUP_
2013-03-16 14:51:55 +00:00
#include "num.h"
2013-03-08 00:20:41 +00:00
#include "field.h"
2013-04-02 22:58:51 +00:00
/** A group element of the secp256k1 curve, in affine coordinates. */
2013-03-31 04:34:15 +00:00
typedef struct {
secp256k1_fe_t x;
secp256k1_fe_t y;
2013-04-02 22:58:51 +00:00
int infinity; // whether this represents the point at infinity
2013-03-31 04:34:15 +00:00
} secp256k1_ge_t;
2013-04-02 22:58:51 +00:00
/** A group element of the secp256k1 curve, in jacobian coordinates. */
2013-03-31 04:34:15 +00:00
typedef struct {
2013-04-02 22:58:51 +00:00
secp256k1_fe_t x; // actual X: x/z^2
secp256k1_fe_t y; // actual Y: y/z^3
2013-03-31 04:34:15 +00:00
secp256k1_fe_t z;
2013-04-02 22:58:51 +00:00
int infinity; // whether this represents the point at infinity
2013-03-31 04:34:15 +00:00
} secp256k1_gej_t;
2013-04-02 22:58:51 +00:00
/** Global constants related to the group */
2013-03-31 04:34:15 +00:00
typedef struct {
2013-04-02 22:58:51 +00:00
secp256k1_num_t order; // the order of the curve (= order of its generator)
secp256k1_num_t half_order; // half the order of the curve (= order of its generator)
2013-04-02 22:58:51 +00:00
secp256k1_ge_t g; // the generator point
#ifdef USE_ENDOMORPHISM
2013-04-02 22:58:51 +00:00
// constants related to secp256k1's efficiently computable endomorphism
2013-03-31 04:34:15 +00:00
secp256k1_fe_t beta;
secp256k1_num_t lambda, a1b2, b1, a2;
#endif
2013-03-31 04:34:15 +00:00
} secp256k1_ge_consts_t;
2013-04-30 13:57:40 +00:00
static const secp256k1_ge_consts_t *secp256k1_ge_consts = NULL;
2013-03-31 04:34:15 +00:00
2013-04-02 22:58:51 +00:00
/** Initialize the group module. */
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_start(void);
2013-04-02 22:58:51 +00:00
/** De-initialize the group module. */
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_stop(void);
2013-04-02 22:58:51 +00:00
/** Set a group element equal to the point at infinity */
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_set_infinity(secp256k1_ge_t *r);
2013-04-02 22:58:51 +00:00
/** Set a group element equal to the point with given X and Y coordinates */
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_set_xy(secp256k1_ge_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y);
2013-04-02 22:58:51 +00:00
/** Set a group element (affine) equal to the point with the given X coordinate, and given oddness
* for Y. Return value indicates whether the result is valid. */
int static secp256k1_ge_set_xo(secp256k1_ge_t *r, const secp256k1_fe_t *x, int odd);
2013-04-02 22:58:51 +00:00
/** Check whether a group element is the point at infinity. */
2013-03-31 04:34:15 +00:00
int static secp256k1_ge_is_infinity(const secp256k1_ge_t *a);
2013-04-02 22:58:51 +00:00
/** Check whether a group element is valid (i.e., on the curve). */
int static secp256k1_ge_is_valid(const secp256k1_ge_t *a);
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a);
2013-04-02 22:58:51 +00:00
/** Get a hex representation of a point. *rlen will be overwritten with the real length. */
2013-03-31 04:34:15 +00:00
void static secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a);
2013-04-02 22:58:51 +00:00
/** Set a group element equal to another which is given in jacobian coordinates */
2013-03-31 15:02:52 +00:00
void static secp256k1_ge_set_gej(secp256k1_ge_t *r, secp256k1_gej_t *a);
2013-03-31 04:34:15 +00:00
/** Set a batch of group elements equal to the inputs given in jacobian coordinates */
void static secp256k1_ge_set_all_gej(size_t len, secp256k1_ge_t r[len], const secp256k1_gej_t a[len]);
2013-04-02 22:58:51 +00:00
/** Set a group element (jacobian) equal to the point at infinity. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_set_infinity(secp256k1_gej_t *r);
2013-04-02 22:58:51 +00:00
/** Set a group element (jacobian) equal to the point with given X and Y coordinates. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_set_xy(secp256k1_gej_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y);
2013-04-02 22:58:51 +00:00
/** Set a group element (jacobian) equal to another which is given in affine coordinates. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_set_ge(secp256k1_gej_t *r, const secp256k1_ge_t *a);
2013-04-02 22:58:51 +00:00
/** Get the X coordinate of a group element (jacobian). */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_get_x(secp256k1_fe_t *r, const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_neg(secp256k1_gej_t *r, const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
/** Check whether a group element is the point at infinity. */
2013-03-31 04:34:15 +00:00
int static secp256k1_gej_is_infinity(const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
/** Set r equal to the double of a. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_double(secp256k1_gej_t *r, const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
/** Set r equal to the sum of a and b. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_add(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_gej_t *b);
2013-04-02 22:58:51 +00:00
/** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient
2013-04-02 22:58:51 +00:00
than secp256k1_gej_add. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_ge_t *b);
2013-04-02 22:58:51 +00:00
/** Get a hex representation of a point. *rlen will be overwritten with the real length. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
#ifdef USE_ENDOMORPHISM
2013-04-02 22:58:51 +00:00
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_mul_lambda(secp256k1_gej_t *r, const secp256k1_gej_t *a);
2013-04-02 22:58:51 +00:00
/** Find r1 and r2 such that r1+r2*lambda = a, and r1 and r2 are maximum 128 bits long (given that a is
not more than 256 bits). */
2013-03-31 04:34:15 +00:00
void static secp256k1_gej_split_exp(secp256k1_num_t *r1, secp256k1_num_t *r2, const secp256k1_num_t *a);
#endif
2013-03-31 04:34:15 +00:00
/** Clear a secp256k1_gej_t to prevent leaking sensitive information. */
void static secp256k1_gej_clear(secp256k1_gej_t *r);
/** Clear a secp256k1_ge_t to prevent leaking sensitive information. */
void static secp256k1_ge_clear(secp256k1_ge_t *r);
2013-03-08 00:20:41 +00:00
#endif