/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss or reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <nanu-nanu.h>

/** @addtogroup nanu */
/** @{ */

/**
*	Modular subtraction.
*
*	Subtract the second elemen from the third and calculate the
*	remainder of dividing the result with the topmost element.
*	Note that if the subtraction result would be negative, then the
*	modulus is added to it until it becomes positive. Therefore, while
*	it is an error to subtract a number from a smaller one (since the
*	result is not a natural number), it is allowed for modular subtraction.
*	the modular subtraction of 'b' from 'a' modulo 'm' returns a number,
*	'y', which is smaller than the modulus, so that the following equation
*	is true:
*
*	<tt>a mod m = ( b + y ) mod m</tt>
*
*	@param	nanu			The natural number machine state descriptor
*
*	@retval	BC_NANU_NANU	The operation succeeded
*	@retval	BC_NANU_SUND	There were less that 3 elements on the stack
*	@retval	BC_NANU_NANN	The modulus was 0
*	@retval	BC_NANU_OMEM	There's not enough memory in the pool to store
*							the result.
*
*	@par Stack image
*		m b a ...  ==> (a-b)\%m ...
*/

int		BC_NanuMsub( BC_NANU nanu )
{
int		ec;

	if ( ( ec = BC_NanuRoll( nanu, -2 ) ) >= 0 ) {		// b a m

		if ( BC_NanuComp( nanu, BC_NANU_LE ) ) {

			// 'b' is less than or equal to 'a', we can subtract

			BC_NanuRsub( nanu );						// a-b m
			BC_NanuSwap( nanu );						// m a-b
			BC_NanuRmod( nanu );						// (a-b)%m
		}
		else {

			// The subtraction would result in a negative number.
			// So, instead of calculating ( b - a ) mod m we calculate
			// m - ( a - b ) mod m

			BC_NanuSwap( nanu );						// a b m
			BC_NanuRsub( nanu );						// b-a m

			if ( ( ec = BC_NanuOver( nanu ) ) >= 0 &&	// m b-a m
				 ( ec = BC_NanuRmod( nanu ) ) >= 0 ) {	// (b-a)%m m

				// If the interim result is 0, we have to
				// return that and do not subtract it from
				// the modulus

				if ( BC_NanuSize( nanu ) == 0 ) {

					BC_NanuSwap( nanu );
					BC_NanuDrop( nanu );
				}
				else {

					BC_NanuRsub( nanu );
				}
			}
		}
	}

	return( ec );
}

/** @} */

