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 **********************************************************************/
7 #ifndef _SECP256K1_NUM_REPR_IMPL_H_
8 #define _SECP256K1_NUM_REPR_IMPL_H_
18 static void secp256k1_num_sanity(const secp256k1_num_t *a) {
19 VERIFY_CHECK(a->limbs == 1 || (a->limbs > 1 && a->data[a->limbs-1] != 0));
22 #define secp256k1_num_sanity(a) do { } while(0)
25 static void secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a) {
29 static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num_t *a) {
30 unsigned char tmp[65];
33 if (a->limbs>1 || a->data[0] != 0) {
34 len = mpn_get_str(tmp, 256, (mp_limb_t*)a->data, a->limbs);
36 while (shift < len && tmp[shift] == 0) shift++;
37 VERIFY_CHECK(len-shift <= (int)rlen);
38 memset(r, 0, rlen - len + shift);
40 memcpy(r + rlen - len + shift, tmp + shift, len - shift);
42 memset(tmp, 0, sizeof(tmp));
45 static void secp256k1_num_set_bin(secp256k1_num_t *r, const unsigned char *a, unsigned int alen) {
47 VERIFY_CHECK(alen > 0);
48 VERIFY_CHECK(alen <= 64);
49 len = mpn_set_str(r->data, a, alen, 256);
54 VERIFY_CHECK(len <= NUM_LIMBS*2);
57 while (r->limbs > 1 && r->data[r->limbs-1]==0) {
62 static void secp256k1_num_add_abs(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
63 mp_limb_t c = mpn_add(r->data, a->data, a->limbs, b->data, b->limbs);
66 VERIFY_CHECK(r->limbs < 2*NUM_LIMBS);
67 r->data[r->limbs++] = c;
71 static void secp256k1_num_sub_abs(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
72 mp_limb_t c = mpn_sub(r->data, a->data, a->limbs, b->data, b->limbs);
75 while (r->limbs > 1 && r->data[r->limbs-1]==0) {
80 static void secp256k1_num_mod(secp256k1_num_t *r, const secp256k1_num_t *m) {
81 secp256k1_num_sanity(r);
82 secp256k1_num_sanity(m);
84 if (r->limbs >= m->limbs) {
85 mp_limb_t t[2*NUM_LIMBS];
86 mpn_tdiv_qr(t, r->data, 0, r->data, r->limbs, m->data, m->limbs);
87 memset(t, 0, sizeof(t));
89 while (r->limbs > 1 && r->data[r->limbs-1]==0) {
94 if (r->neg && (r->limbs > 1 || r->data[0] != 0)) {
95 secp256k1_num_sub_abs(r, m, r);
100 static void secp256k1_num_mod_inverse(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *m) {
102 mp_limb_t g[NUM_LIMBS+1];
103 mp_limb_t u[NUM_LIMBS+1];
104 mp_limb_t v[NUM_LIMBS+1];
107 secp256k1_num_sanity(a);
108 secp256k1_num_sanity(m);
110 /** mpn_gcdext computes: (G,S) = gcdext(U,V), where
113 * * U has equal or more limbs than V, and V has no padding
114 * If we set U to be (a padded version of) a, and V = m:
120 VERIFY_CHECK(m->limbs <= NUM_LIMBS);
121 VERIFY_CHECK(m->data[m->limbs-1] != 0);
122 for (i = 0; i < m->limbs; i++) {
123 u[i] = (i < a->limbs) ? a->data[i] : 0;
127 gn = mpn_gcdext(g, r->data, &sn, u, m->limbs, v, m->limbs);
128 VERIFY_CHECK(gn == 1);
129 VERIFY_CHECK(g[0] == 1);
130 r->neg = a->neg ^ m->neg;
132 mpn_sub(r->data, m->data, m->limbs, r->data, -sn);
134 while (r->limbs > 1 && r->data[r->limbs-1]==0) {
140 memset(g, 0, sizeof(g));
141 memset(u, 0, sizeof(u));
142 memset(v, 0, sizeof(v));
145 static int secp256k1_num_is_zero(const secp256k1_num_t *a) {
146 return (a->limbs == 1 && a->data[0] == 0);
149 static int secp256k1_num_is_neg(const secp256k1_num_t *a) {
150 return (a->limbs > 1 || a->data[0] != 0) && a->neg;
153 static int secp256k1_num_cmp(const secp256k1_num_t *a, const secp256k1_num_t *b) {
154 if (a->limbs > b->limbs) {
157 if (a->limbs < b->limbs) {
160 return mpn_cmp(a->data, b->data, a->limbs);
163 static int secp256k1_num_eq(const secp256k1_num_t *a, const secp256k1_num_t *b) {
164 if (a->limbs > b->limbs) {
167 if (a->limbs < b->limbs) {
170 if ((a->neg && !secp256k1_num_is_zero(a)) != (b->neg && !secp256k1_num_is_zero(b))) {
173 return mpn_cmp(a->data, b->data, a->limbs) == 0;
176 static void secp256k1_num_subadd(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b, int bneg) {
177 if (!(b->neg ^ bneg ^ a->neg)) { /* a and b have the same sign */
179 if (a->limbs >= b->limbs) {
180 secp256k1_num_add_abs(r, a, b);
182 secp256k1_num_add_abs(r, b, a);
185 if (secp256k1_num_cmp(a, b) > 0) {
187 secp256k1_num_sub_abs(r, a, b);
189 r->neg = b->neg ^ bneg;
190 secp256k1_num_sub_abs(r, b, a);
195 static void secp256k1_num_add(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
196 secp256k1_num_sanity(a);
197 secp256k1_num_sanity(b);
198 secp256k1_num_subadd(r, a, b, 0);
201 static void secp256k1_num_sub(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
202 secp256k1_num_sanity(a);
203 secp256k1_num_sanity(b);
204 secp256k1_num_subadd(r, a, b, 1);
207 static void secp256k1_num_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
208 mp_limb_t tmp[2*NUM_LIMBS+1];
209 secp256k1_num_sanity(a);
210 secp256k1_num_sanity(b);
212 VERIFY_CHECK(a->limbs + b->limbs <= 2*NUM_LIMBS+1);
213 if ((a->limbs==1 && a->data[0]==0) || (b->limbs==1 && b->data[0]==0)) {
219 if (a->limbs >= b->limbs) {
220 mpn_mul(tmp, a->data, a->limbs, b->data, b->limbs);
222 mpn_mul(tmp, b->data, b->limbs, a->data, a->limbs);
224 r->limbs = a->limbs + b->limbs;
225 if (r->limbs > 1 && tmp[r->limbs - 1]==0) {
228 VERIFY_CHECK(r->limbs <= 2*NUM_LIMBS);
229 mpn_copyi(r->data, tmp, r->limbs);
230 r->neg = a->neg ^ b->neg;
231 memset(tmp, 0, sizeof(tmp));
234 static void secp256k1_num_shift(secp256k1_num_t *r, int bits) {
236 if (bits % GMP_NUMB_BITS) {
237 /* Shift within limbs. */
238 mpn_rshift(r->data, r->data, r->limbs, bits % GMP_NUMB_BITS);
240 if (bits >= GMP_NUMB_BITS) {
241 /* Shift full limbs. */
242 for (i = 0; i < r->limbs; i++) {
243 int index = i + (bits / GMP_NUMB_BITS);
244 if (index < r->limbs && index < 2*NUM_LIMBS) {
245 r->data[i] = r->data[index];
251 while (r->limbs>1 && r->data[r->limbs-1]==0) {
256 static void secp256k1_num_negate(secp256k1_num_t *r) {