1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* crypto/bn/bn_add.c */
2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * All rights reserved.
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This package is an SSL implementation written
6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * by Eric Young (eay@cryptsoft.com).
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The implementation was written so as to conform with Netscapes SSL.
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This library is free for commercial and non-commercial use as long as
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the following conditions are aheared to.  The following conditions
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * apply to all code found in this distribution, be it the RC4, RSA,
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * included with this distribution is covered by the same copyright terms
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright remains Eric Young's, and as such any Copyright notices in
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the code are not to be removed.
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * If this package is used in a product, Eric Young should be given attribution
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * as the author of the parts of the library used.
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This can be in the form of a textual message at program startup or
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in documentation (online or textual) provided with the package.
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the copyright
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    documentation and/or other materials provided with the distribution.
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this software
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    must display the following acknowledgement:
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes cryptographic software written by
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *     Eric Young (eay@cryptsoft.com)"
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    The word 'cryptographic' can be left out if the rouines from the library
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    being used are not cryptographic related :-).
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. If you include any Windows specific code (or a derivative thereof) from
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the apps directory (application code) you must include an acknowledgement:
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SUCH DAMAGE.
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The licence and distribution terms for any publically available version or
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * derivative of this code cannot be changed.  i.e. this code cannot simply be
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * copied and put under another distribution licence
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * [including the GNU Public Licence.]
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h>
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h"
61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "bn_lcl.h"
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* r can == a or b */
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const BIGNUM *tmp;
67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int a_neg = a->neg, ret;
68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(a);
70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(b);
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/*  a +  b	a+b
73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 *  a + -b	a-b
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * -a +  b	b-a
75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * -a + -b	-(a+b)
76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (a_neg ^ b->neg)
78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* only one is negative */
80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (a_neg)
81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{ tmp=a; a=b; b=tmp; }
82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* we are now a - b */
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (BN_ucmp(a,b) < 0)
86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!BN_usub(r,b,a)) return(0);
88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			r->neg=1;
89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		else
91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!BN_usub(r,a,b)) return(0);
93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			r->neg=0;
94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return(1);
96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ret = BN_uadd(r,a,b);
99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	r->neg = a_neg;
100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(r);
101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return ret;
102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* unsigned add of b to a */
105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int max,min,dif;
108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	BN_ULONG *ap,*bp,*rp,carry,t1,t2;
109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const BIGNUM *tmp;
110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(a);
112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(b);
113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (a->top < b->top)
115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{ tmp=a; a=b; b=tmp; }
116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	max = a->top;
117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	min = b->top;
118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	dif = max - min;
119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (bn_wexpand(r,max+1) == NULL)
121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 0;
122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	r->top=max;
124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ap=a->d;
127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bp=b->d;
128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	rp=r->d;
129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	carry=bn_add_words(rp,ap,bp,min);
131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	rp+=min;
132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ap+=min;
133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bp+=min;
134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (carry)
136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		while (dif)
138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			dif--;
140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t1 = *(ap++);
141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t2 = (t1+1) & BN_MASK2;
142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*(rp++) = t2;
143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (t2)
144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				{
145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				carry=0;
146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				break;
147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				}
148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (carry)
150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* carry != 0 => dif == 0 */
152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*rp = 1;
153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			r->top++;
154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (dif && rp != ap)
157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		while (dif--)
158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* copy remaining words if ap != rp */
159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*(rp++) = *(ap++);
160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	r->neg = 0;
161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(r);
162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 1;
163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* unsigned subtraction of b from a, a must be larger than b. */
166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int max,min,dif;
169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	register BN_ULONG t1,t2,*ap,*bp,*rp;
170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int i,carry;
171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(IRIX_CC_BUG) && !defined(LINT)
172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int dummy;
173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(a);
176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(b);
177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	max = a->top;
179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	min = b->top;
180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	dif = max - min;
181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (dif < 0)	/* hmm... should not be happening */
183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return(0);
186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (bn_wexpand(r,max) == NULL) return(0);
189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ap=a->d;
191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bp=b->d;
192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	rp=r->d;
193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 1
195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	carry=0;
196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	for (i = min; i != 0; i--)
197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		t1= *(ap++);
199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		t2= *(bp++);
200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (carry)
201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			carry=(t1 <= t2);
203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t1=(t1-t2-1)&BN_MASK2;
204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		else
206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			carry=(t1 < t2);
208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t1=(t1-t2)&BN_MASK2;
209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(IRIX_CC_BUG) && !defined(LINT)
211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		dummy=t1;
212656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
213656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*(rp++)=t1&BN_MASK2;
214656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
215656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else
216656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	carry=bn_sub_words(rp,ap,bp,min);
217656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ap+=min;
218656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bp+=min;
219656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	rp+=min;
220656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (carry) /* subtracted */
222656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!dif)
224656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			/* error: a < b */
225656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return 0;
226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		while (dif)
227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
228656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			dif--;
229656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t1 = *(ap++);
230656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			t2 = (t1-1)&BN_MASK2;
231656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*(rp++) = t2;
232656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (t1)
233656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				break;
234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
235656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
236656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 0
237656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	memcpy(rp,ap,sizeof(*rp)*(max-i));
238656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else
239656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (rp != ap)
240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
241656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		for (;;)
242656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{
243656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!dif--) break;
244656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rp[0]=ap[0];
245656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!dif--) break;
246656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rp[1]=ap[1];
247656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!dif--) break;
248656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rp[2]=ap[2];
249656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			if (!dif--) break;
250656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rp[3]=ap[3];
251656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			rp+=4;
252656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			ap+=4;
253656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
254656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
255656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif
256656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
257656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	r->top=max;
258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	r->neg=0;
259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_correct_top(r);
260656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return(1);
261656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
262656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
263656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int max;
266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int add=0,neg=0;
267656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const BIGNUM *tmp;
268656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
269656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(a);
270656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(b);
271656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/*  a -  b	a-b
273656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 *  a - -b	a+b
274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * -a -  b	-(a+b)
275656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 * -a - -b	b-a
276656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	 */
277656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (a->neg)
278656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (b->neg)
280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{ tmp=a; a=b; b=tmp; }
281656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		else
282656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			{ add=1; neg=1; }
283656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
284656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	else
285656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
286656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (b->neg) { add=1; neg=0; }
287656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
288656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
289656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (add)
290656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
291656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!BN_uadd(r,a,b)) return(0);
292656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		r->neg=neg;
293656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return(1);
294656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
295656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
296656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* We are actually doing a - b :-) */
297656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
298656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	max=(a->top > b->top)?a->top:b->top;
299656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (bn_wexpand(r,max) == NULL) return(0);
300656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (BN_ucmp(a,b) < 0)
301656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
302656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!BN_usub(r,b,a)) return(0);
303656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		r->neg=1;
304656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
305656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	else
306656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
307656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!BN_usub(r,a,b)) return(0);
308656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		r->neg=0;
309656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
310656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	bn_check_top(r);
311656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return(1);
312656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
313656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
314