1 // Copyright (c) 2013 Pieter Wuille
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef _SECP256K1_NUM_REPR_IMPL_H_
6 #define _SECP256K1_NUM_REPR_IMPL_H_
11 #include <openssl/bn.h>
12 #include <openssl/crypto.h>
17 void static secp256k1_num_init(secp256k1_num_t *r) {
21 void static secp256k1_num_free(secp256k1_num_t *r) {
25 void static secp256k1_num_clear(secp256k1_num_t *r) {
29 void static secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a) {
30 BN_copy(&r->bn, &a->bn);
33 void static secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num_t *a) {
34 unsigned int size = BN_num_bytes(&a->bn);
35 VERIFY_CHECK(size <= rlen);
37 BN_bn2bin(&a->bn, r + rlen - size);
40 void static secp256k1_num_set_bin(secp256k1_num_t *r, const unsigned char *a, unsigned int alen) {
41 BN_bin2bn(a, alen, &r->bn);
44 void static secp256k1_num_set_int(secp256k1_num_t *r, int a) {
45 BN_set_word(&r->bn, a < 0 ? -a : a);
46 BN_set_negative(&r->bn, a < 0);
49 void static secp256k1_num_mod_inverse(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *m) {
50 BN_CTX *ctx = BN_CTX_new();
51 BN_mod_inverse(&r->bn, &a->bn, &m->bn, ctx);
55 void static secp256k1_num_mod_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b, const secp256k1_num_t *m) {
56 BN_CTX *ctx = BN_CTX_new();
57 BN_mod_mul(&r->bn, &a->bn, &b->bn, &m->bn, ctx);
61 int static secp256k1_num_cmp(const secp256k1_num_t *a, const secp256k1_num_t *b) {
62 return BN_ucmp(&a->bn, &b->bn);
65 int static secp256k1_num_eq(const secp256k1_num_t *a, const secp256k1_num_t *b) {
66 return BN_cmp(&a->bn, &b->bn) == 0;
69 void static secp256k1_num_add(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
70 BN_add(&r->bn, &a->bn, &b->bn);
73 void static secp256k1_num_sub(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
74 BN_sub(&r->bn, &a->bn, &b->bn);
77 void static secp256k1_num_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
78 BN_CTX *ctx = BN_CTX_new();
79 BN_mul(&r->bn, &a->bn, &b->bn, ctx);
83 void static secp256k1_num_div(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
84 BN_CTX *ctx = BN_CTX_new();
85 BN_div(&r->bn, NULL, &a->bn, &b->bn, ctx);
89 void static secp256k1_num_mod(secp256k1_num_t *r, const secp256k1_num_t *m) {
90 BN_CTX *ctx = BN_CTX_new();
91 BN_nnmod(&r->bn, &r->bn, &m->bn, ctx);
95 int static secp256k1_num_bits(const secp256k1_num_t *a) {
96 return BN_num_bits(&a->bn);
99 int static secp256k1_num_shift(secp256k1_num_t *r, int bits) {
100 int ret = BN_is_zero(&r->bn) ? 0 : r->bn.d[0] & ((1 << bits) - 1);
101 BN_rshift(&r->bn, &r->bn, bits);
105 int static secp256k1_num_is_zero(const secp256k1_num_t *a) {
106 return BN_is_zero(&a->bn);
109 int static secp256k1_num_is_odd(const secp256k1_num_t *a) {
110 return BN_is_odd(&a->bn);
113 int static secp256k1_num_is_neg(const secp256k1_num_t *a) {
114 return BN_is_negative(&a->bn);
117 int static secp256k1_num_get_bit(const secp256k1_num_t *a, int pos) {
118 return BN_is_bit_set(&a->bn, pos);
121 void static secp256k1_num_inc(secp256k1_num_t *r) {
122 BN_add_word(&r->bn, 1);
125 void static secp256k1_num_set_hex(secp256k1_num_t *r, const char *a, int alen) {
126 char *str = (char*)malloc(alen+1);
127 memcpy(str, a, alen);
129 BIGNUM *pbn = &r->bn;
130 BN_hex2bn(&pbn, str);
134 void static secp256k1_num_get_hex(char *r, int rlen, const secp256k1_num_t *a) {
135 char *str = BN_bn2hex(&a->bn);
136 int len = strlen(str);
137 VERIFY_CHECK(rlen >= len);
138 for (int i=0; i<rlen-len; i++)
140 memcpy(r+rlen-len, str, len);
144 void static secp256k1_num_split(secp256k1_num_t *rl, secp256k1_num_t *rh, const secp256k1_num_t *a, int bits) {
145 BN_copy(&rl->bn, &a->bn);
146 BN_rshift(&rh->bn, &a->bn, bits);
147 BN_mask_bits(&rl->bn, bits);
150 void static secp256k1_num_negate(secp256k1_num_t *r) {
151 BN_set_negative(&r->bn, !BN_is_negative(&r->bn));