xref: /OK3568_Linux_fs/kernel/lib/mpi/mpih-cmp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* mpihelp-sub.c  -  MPI helper functions
3*4882a593Smuzhiyun  *	Copyright (C) 1994, 1996 Free Software Foundation, Inc.
4*4882a593Smuzhiyun  *	Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This file is part of GnuPG.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Note: This code is heavily based on the GNU MP Library.
9*4882a593Smuzhiyun  *	 Actually it's the same code with only minor changes in the
10*4882a593Smuzhiyun  *	 way the data is stored; this is to support the abstraction
11*4882a593Smuzhiyun  *	 of an optional secure memory allocation which may be used
12*4882a593Smuzhiyun  *	 to avoid revealing of sensitive data due to paging etc.
13*4882a593Smuzhiyun  *	 The GNU MP Library itself is published under the LGPL;
14*4882a593Smuzhiyun  *	 however I decided to publish this code under the plain GPL.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "mpi-internal.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /****************
20*4882a593Smuzhiyun  * Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
21*4882a593Smuzhiyun  * There are no restrictions on the relative sizes of
22*4882a593Smuzhiyun  * the two arguments.
23*4882a593Smuzhiyun  * Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.
24*4882a593Smuzhiyun  */
mpihelp_cmp(mpi_ptr_t op1_ptr,mpi_ptr_t op2_ptr,mpi_size_t size)25*4882a593Smuzhiyun int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	mpi_size_t i;
28*4882a593Smuzhiyun 	mpi_limb_t op1_word, op2_word;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	for (i = size - 1; i >= 0; i--) {
31*4882a593Smuzhiyun 		op1_word = op1_ptr[i];
32*4882a593Smuzhiyun 		op2_word = op2_ptr[i];
33*4882a593Smuzhiyun 		if (op1_word != op2_word)
34*4882a593Smuzhiyun 			goto diff;
35*4882a593Smuzhiyun 	}
36*4882a593Smuzhiyun 	return 0;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun diff:
39*4882a593Smuzhiyun 	/* This can *not* be simplified to
40*4882a593Smuzhiyun 	 *   op2_word - op2_word
41*4882a593Smuzhiyun 	 * since that expression might give signed overflow.  */
42*4882a593Smuzhiyun 	return (op1_word > op2_word) ? 1 : -1;
43*4882a593Smuzhiyun }
44