162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/* mpihelp-sub.c  -  MPI helper functions
362306a36Sopenharmony_ci *	Copyright (C) 1994, 1996 Free Software Foundation, Inc.
462306a36Sopenharmony_ci *	Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This file is part of GnuPG.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Note: This code is heavily based on the GNU MP Library.
962306a36Sopenharmony_ci *	 Actually it's the same code with only minor changes in the
1062306a36Sopenharmony_ci *	 way the data is stored; this is to support the abstraction
1162306a36Sopenharmony_ci *	 of an optional secure memory allocation which may be used
1262306a36Sopenharmony_ci *	 to avoid revealing of sensitive data due to paging etc.
1362306a36Sopenharmony_ci *	 The GNU MP Library itself is published under the LGPL;
1462306a36Sopenharmony_ci *	 however I decided to publish this code under the plain GPL.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "mpi-internal.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/****************
2062306a36Sopenharmony_ci * Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
2162306a36Sopenharmony_ci * There are no restrictions on the relative sizes of
2262306a36Sopenharmony_ci * the two arguments.
2362306a36Sopenharmony_ci * Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ciint mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	mpi_size_t i;
2862306a36Sopenharmony_ci	mpi_limb_t op1_word, op2_word;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	for (i = size - 1; i >= 0; i--) {
3162306a36Sopenharmony_ci		op1_word = op1_ptr[i];
3262306a36Sopenharmony_ci		op2_word = op2_ptr[i];
3362306a36Sopenharmony_ci		if (op1_word != op2_word)
3462306a36Sopenharmony_ci			goto diff;
3562306a36Sopenharmony_ci	}
3662306a36Sopenharmony_ci	return 0;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cidiff:
3962306a36Sopenharmony_ci	/* This can *not* be simplified to
4062306a36Sopenharmony_ci	 *   op2_word - op2_word
4162306a36Sopenharmony_ci	 * since that expression might give signed overflow.  */
4262306a36Sopenharmony_ci	return (op1_word > op2_word) ? 1 : -1;
4362306a36Sopenharmony_ci}
44