|
| 1 | +/*********************************************************************** |
| 2 | + * Distributed under the MIT software license, see the accompanying * |
| 3 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php.* |
| 4 | + ***********************************************************************/ |
| 5 | + |
| 6 | +#ifndef SECP256K1_MODULE_HAZMAT_MAIN_H |
| 7 | +#define SECP256K1_MODULE_HAZMAT_MAIN_H |
| 8 | + |
| 9 | +#include "../../../include/secp256k1.h" |
| 10 | +#include "../../../include/secp256k1_hazmat.h" |
| 11 | +#include "../../scalar.h" |
| 12 | +#include "../../group.h" |
| 13 | +#include "../../eckey.h" |
| 14 | +#include "../../ecmult_const.h" |
| 15 | + |
| 16 | +typedef struct { |
| 17 | + secp256k1_gej gej; |
| 18 | + int z_is_one; /* set if z == 1, i.e. gej can be converted to ge trivially by assigning x/y */ |
| 19 | +} secp256k1_hazmat_point_struct; |
| 20 | + |
| 21 | +/* Verify that the opaque data types are large enough to hold the underlying structures |
| 22 | + (note that this function is never called at run-time and only exists since the STATIC_ASSERT |
| 23 | + macro can only be used inside of functions) */ |
| 24 | +static void secp256k1_hazmat_assertions(void) { |
| 25 | + STATIC_ASSERT(sizeof(secp256k1_hazmat_scalar) >= sizeof(secp256k1_scalar)); |
| 26 | + STATIC_ASSERT(sizeof(secp256k1_hazmat_point) >= sizeof(secp256k1_hazmat_point_struct)); |
| 27 | +} |
| 28 | + |
| 29 | +int secp256k1_hazmat_scalar_parse(secp256k1_hazmat_scalar *s, const unsigned char *bin32) { |
| 30 | + int overflow; |
| 31 | + secp256k1_scalar_set_b32((secp256k1_scalar*)s, bin32, &overflow); |
| 32 | + return !overflow; |
| 33 | +} |
| 34 | + |
| 35 | +void secp256k1_hazmat_scalar_serialize(unsigned char *bin32, const secp256k1_hazmat_scalar *s) { |
| 36 | + secp256k1_scalar_get_b32(bin32, (secp256k1_scalar*)s); |
| 37 | +} |
| 38 | + |
| 39 | +void secp256k1_hazmat_scalar_set_zero(secp256k1_hazmat_scalar *s) { |
| 40 | + *((secp256k1_scalar*)s) = secp256k1_scalar_zero; |
| 41 | +} |
| 42 | + |
| 43 | +int secp256k1_hazmat_scalar_is_zero(const secp256k1_hazmat_scalar *s) { |
| 44 | + return secp256k1_scalar_is_zero((secp256k1_scalar*)s); |
| 45 | +} |
| 46 | + |
| 47 | +void secp256k1_hazmat_scalar_add(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2) { |
| 48 | + secp256k1_scalar_add((secp256k1_scalar*)sres, (secp256k1_scalar*)s1, (secp256k1_scalar*)s2); |
| 49 | +} |
| 50 | + |
| 51 | +void secp256k1_hazmat_scalar_mul(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2) { |
| 52 | + secp256k1_scalar_mul((secp256k1_scalar*)sres, (secp256k1_scalar*)s1, (secp256k1_scalar*)s2); |
| 53 | +} |
| 54 | + |
| 55 | +void secp256k1_hazmat_scalar_negate(secp256k1_hazmat_scalar *s) { |
| 56 | + secp256k1_scalar_negate((secp256k1_scalar*)s, (secp256k1_scalar*)s); |
| 57 | +} |
| 58 | + |
| 59 | +static void secp256k1_hazmat_point_to_ge(secp256k1_ge *ge, secp256k1_hazmat_point_struct *p) { |
| 60 | + if (p->z_is_one) { |
| 61 | + secp256k1_ge_set_xy(ge, &p->gej.x, &p->gej.y); |
| 62 | + } else { |
| 63 | + secp256k1_ge_set_gej(ge, &p->gej); |
| 64 | + p->z_is_one = 1; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +int secp256k1_hazmat_point_parse(secp256k1_hazmat_point *p, const unsigned char *pubkey33) { |
| 69 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 70 | + secp256k1_ge ge; |
| 71 | + |
| 72 | + if (!secp256k1_eckey_pubkey_parse(&ge, pubkey33, 33)) { |
| 73 | + return 0; |
| 74 | + } |
| 75 | + secp256k1_gej_set_ge(&ps->gej, &ge); |
| 76 | + ps->z_is_one = 1; |
| 77 | + return 1; |
| 78 | +} |
| 79 | + |
| 80 | +void secp256k1_hazmat_point_serialize(unsigned char *pubkey33, secp256k1_hazmat_point *p) { |
| 81 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 82 | + secp256k1_ge ge; |
| 83 | + size_t size; |
| 84 | + int ret; |
| 85 | + |
| 86 | + secp256k1_hazmat_point_to_ge(&ge, ps); |
| 87 | + ret = secp256k1_eckey_pubkey_serialize(&ge, pubkey33, &size, 1); |
| 88 | + VERIFY_CHECK(ret == 1 && size == 33); |
| 89 | + (void)ret; |
| 90 | +} |
| 91 | + |
| 92 | +void secp256k1_hazmat_point_set_infinity(secp256k1_hazmat_point *p) { |
| 93 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 94 | + |
| 95 | + secp256k1_gej_set_infinity(&ps->gej); |
| 96 | + ps->z_is_one = 0; |
| 97 | +} |
| 98 | + |
| 99 | +int secp256k1_hazmat_point_is_infinity(const secp256k1_hazmat_point *p) { |
| 100 | + const secp256k1_hazmat_point_struct *ps = (const secp256k1_hazmat_point_struct*)p; |
| 101 | + |
| 102 | + return secp256k1_gej_is_infinity(&ps->gej); |
| 103 | +} |
| 104 | + |
| 105 | +void secp256k1_hazmat_point_add(secp256k1_hazmat_point *pres, secp256k1_hazmat_point *p1, secp256k1_hazmat_point *p2) { |
| 106 | + secp256k1_hazmat_point_struct *press = (secp256k1_hazmat_point_struct*)pres; |
| 107 | + secp256k1_hazmat_point_struct *p1s = (secp256k1_hazmat_point_struct*)p1; |
| 108 | + secp256k1_hazmat_point_struct *p2s = (secp256k1_hazmat_point_struct*)p2; |
| 109 | + secp256k1_ge ge; |
| 110 | + |
| 111 | + secp256k1_hazmat_point_to_ge(&ge, p2s); |
| 112 | + secp256k1_gej_add_ge(&press->gej, &p1s->gej, &ge); |
| 113 | + press->z_is_one = 0; |
| 114 | +} |
| 115 | + |
| 116 | +void secp256k1_hazmat_point_negate(secp256k1_hazmat_point *p) { |
| 117 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 118 | + |
| 119 | + secp256k1_gej_neg(&ps->gej, &ps->gej); |
| 120 | + /* negation only changes y; z is untouched, so no update of z_is_one is needed */ |
| 121 | +} |
| 122 | + |
| 123 | +int secp256k1_hazmat_point_equal(const secp256k1_hazmat_point *p1, const secp256k1_hazmat_point *p2) { |
| 124 | + const secp256k1_hazmat_point_struct *p1s = (secp256k1_hazmat_point_struct*)p1; |
| 125 | + const secp256k1_hazmat_point_struct *p2s = (secp256k1_hazmat_point_struct*)p2; |
| 126 | + |
| 127 | + return secp256k1_gej_eq_var(&p1s->gej, &p2s->gej); |
| 128 | +} |
| 129 | + |
| 130 | +void secp256k1_hazmat_multiply_with_generator(const secp256k1_context *ctx, secp256k1_hazmat_point *p, const secp256k1_hazmat_scalar *s) { |
| 131 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 132 | + |
| 133 | + secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &ps->gej, (secp256k1_scalar*)s); |
| 134 | + ps->z_is_one = 0; |
| 135 | +} |
| 136 | + |
| 137 | +void secp256k1_hazmat_multiply_with_point(secp256k1_hazmat_point *pres, const secp256k1_hazmat_scalar *s, secp256k1_hazmat_point *p) { |
| 138 | + secp256k1_hazmat_point_struct *press = (secp256k1_hazmat_point_struct*)pres; |
| 139 | + secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p; |
| 140 | + secp256k1_ge ge; |
| 141 | + |
| 142 | + secp256k1_hazmat_point_to_ge(&ge, ps); |
| 143 | + secp256k1_ecmult_const(&press->gej, &ge, (secp256k1_scalar*)s); |
| 144 | + press->z_is_one = 0; |
| 145 | +} |
| 146 | + |
| 147 | +#endif |
0 commit comments