|
| 1 | +/* |
| 2 | + * Copyright 2014-2026 The GmSSL Project. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +#ifndef GMSSL_SECP256R1_H |
| 13 | +#define GMSSL_SECP256R1_H |
| 14 | + |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include <stdlib.h> |
| 19 | + |
| 20 | + |
| 21 | +#ifdef __cplusplus |
| 22 | +extern "C" { |
| 23 | +#endif |
| 24 | + |
| 25 | + |
| 26 | +// p = 2^256 - 2^224 + 2^192 + 2^96 - 1 |
| 27 | +// = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF |
| 28 | +// a = -3 |
| 29 | +// b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b |
| 30 | +// x = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296 |
| 31 | +// y = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 |
| 32 | +// n = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551 |
| 33 | +// h = 1 |
| 34 | + |
| 35 | + |
| 36 | +typedef uint32_t secp256r1_t[8]; |
| 37 | + |
| 38 | +#define SECP256R1_K (sizeof(secp256r1_t)/sizeof(uint32_t)) |
| 39 | + |
| 40 | +extern const secp256r1_t SECP256R1_P; |
| 41 | +extern const secp256r1_t SECP256R1_B; |
| 42 | +extern const secp256r1_t SECP256R1_N; |
| 43 | +extern const uint32_t SECP256R1_U_P[9]; |
| 44 | +extern const uint32_t SECP256R1_U_N[9]; |
| 45 | + |
| 46 | +int secp256r1_is_zero(const secp256r1_t a); |
| 47 | +int secp256r1_is_one(const secp256r1_t a); |
| 48 | +int secp256r1_cmp(const secp256r1_t a, const secp256r1_t b); |
| 49 | +void secp256r1_set_zero(secp256r1_t r); |
| 50 | +void secp256r1_set_one(secp256r1_t r); |
| 51 | +void secp256r1_copy(secp256r1_t r, const secp256r1_t a); |
| 52 | +void secp256r1_to_32bytes(const secp256r1_t a, uint8_t out[32]); |
| 53 | +void secp256r1_from_32bytes(secp256r1_t r, const uint8_t in[32]); |
| 54 | +int secp256r1_print(FILE *fp, int fmt, int ind, const char *label, const secp256r1_t a); |
| 55 | + |
| 56 | +void secp256r1_modp_add(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 57 | +void secp256r1_modp_dbl(secp256r1_t r, const secp256r1_t a); |
| 58 | +void secp256r1_modp_tri(secp256r1_t r, const secp256r1_t a); |
| 59 | +void secp256r1_modp_sub(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 60 | +void secp256r1_modp_neg(secp256r1_t r, const secp256r1_t a); |
| 61 | +void secp256r1_modp_haf(secp256r1_t r, const secp256r1_t a); |
| 62 | +void secp256r1_modp_mul(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 63 | +void secp256r1_modp_sqr(secp256r1_t r, const secp256r1_t a); |
| 64 | +void secp256r1_modp_exp(secp256r1_t r, const secp256r1_t a, const secp256r1_t e); |
| 65 | +void secp256r1_modp_inv(secp256r1_t r, const secp256r1_t a); |
| 66 | + |
| 67 | +void secp256r1_modn(secp256r1_t r, const secp256r1_t a); |
| 68 | +void secp256r1_modn_add(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 69 | +void secp256r1_modn_dbl(secp256r1_t r, const secp256r1_t a); |
| 70 | +void secp256r1_modn_tri(secp256r1_t r, const secp256r1_t a); |
| 71 | +void secp256r1_modn_sub(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 72 | +void secp256r1_modn_neg(secp256r1_t r, const secp256r1_t a); |
| 73 | +void secp256r1_modn_mul(secp256r1_t r, const secp256r1_t a, const secp256r1_t b); |
| 74 | +void secp256r1_modn_sqr(secp256r1_t r, const secp256r1_t a); |
| 75 | +void secp256r1_modn_exp(secp256r1_t r, const secp256r1_t a, const secp256r1_t e); |
| 76 | +void secp256r1_modn_inv(secp256r1_t r, const secp256r1_t a); |
| 77 | + |
| 78 | + |
| 79 | +typedef struct { |
| 80 | + secp256r1_t X; |
| 81 | + secp256r1_t Y; |
| 82 | + secp256r1_t Z; |
| 83 | +} SECP256R1_POINT; |
| 84 | + |
| 85 | +extern const SECP256R1_POINT SECP256R1_POINT_G; |
| 86 | + |
| 87 | +void secp256r1_point_set_infinity(SECP256R1_POINT *R); |
| 88 | +int secp256r1_point_is_at_infinity(const SECP256R1_POINT *P); |
| 89 | +int secp256r1_point_is_on_curve(const SECP256R1_POINT *P); |
| 90 | +int secp256r1_point_equ(const SECP256R1_POINT *P, const SECP256R1_POINT *Q); |
| 91 | +int secp256r1_point_set_xy(SECP256R1_POINT *R, const secp256r1_t x, const secp256r1_t y); |
| 92 | +int secp256r1_point_get_xy(const SECP256R1_POINT *P, secp256r1_t x, secp256r1_t y); |
| 93 | +void secp256r1_point_copy(SECP256R1_POINT *R, const SECP256R1_POINT *P); |
| 94 | +void secp256r1_point_dbl(SECP256R1_POINT *R, const SECP256R1_POINT *P); |
| 95 | +void secp256r1_point_add(SECP256R1_POINT *R, const SECP256R1_POINT *P, const SECP256R1_POINT *Q); |
| 96 | +void secp256r1_point_neg(SECP256R1_POINT *R, const SECP256R1_POINT *P); |
| 97 | +void secp256r1_point_sub(SECP256R1_POINT *R, const SECP256R1_POINT *P, const SECP256R1_POINT *Q); |
| 98 | +void secp256r1_point_mul(SECP256R1_POINT *R, const secp256r1_t k, const SECP256R1_POINT *P); |
| 99 | +void secp256r1_point_mul_generator(SECP256R1_POINT *R, const secp256r1_t k); |
| 100 | +int secp256r1_point_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_POINT *P); |
| 101 | +int secp256r1_point_to_uncompressed_octets(const SECP256R1_POINT *P, uint8_t octets[65]); |
| 102 | +int secp256r1_point_from_uncompressed_octets(SECP256R1_POINT *P, const uint8_t octets[65]); |
| 103 | + |
| 104 | + |
| 105 | +#ifdef __cplusplus |
| 106 | +} |
| 107 | +#endif |
| 108 | +#endif |
0 commit comments