raw
ch1_mpi                 1 /* mpi-cmp.c  -  MPI functions
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
ch1_mpi 3 *
ch1_mpi 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 5 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 7 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 8 *
ch1_mpi 9 * This program is free software: you can redistribute it and/or modify
ch1_mpi 10 * it under the terms of the GNU General Public License as published by
ch1_mpi 11 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 12 * (at your option) any later version.
ch1_mpi 13 *
ch1_mpi 14 * This program is distributed in the hope that it will be useful,
ch1_mpi 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 17 * GNU General Public License for more details.
ch1_mpi 18 *
ch1_mpi 19 * You should have received a copy of the GNU General Public License
ch1_mpi 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ch1_mpi 21 */
ch1_mpi 22
ch1_mpi 23 #include <stdio.h>
ch1_mpi 24 #include <stdlib.h>
ch1_mpi 25
ch1_mpi 26 #include "knobs.h"
ch1_mpi 27 #include "mpi-internal.h"
ch1_mpi 28
ch1_mpi 29 int
ch1_mpi 30 mpi_cmp_ui( MPI u, unsigned long v )
ch1_mpi 31 {
ch1_mpi 32 mpi_limb_t limb = v;
ch1_mpi 33
ch1_mpi 34 mpi_normalize( u );
ch1_mpi 35 if( !u->nlimbs && !limb )
ch1_mpi 36 return 0;
ch1_mpi 37 if( u->sign )
ch1_mpi 38 return -1;
ch1_mpi 39 if( u->nlimbs > 1 )
ch1_mpi 40 return 1;
ch1_mpi 41
ch1_mpi 42 if( u->d[0] == limb )
ch1_mpi 43 return 0;
ch1_mpi 44 else if( u->d[0] > limb )
ch1_mpi 45 return 1;
ch1_mpi 46 else
ch1_mpi 47 return -1;
ch1_mpi 48 }
ch1_mpi 49
ch1_mpi 50 int
ch1_mpi 51 mpi_cmp( MPI u, MPI v )
ch1_mpi 52 {
ch1_mpi 53 mpi_size_t usize, vsize;
ch1_mpi 54 int cmp;
ch1_mpi 55
ch1_mpi 56 mpi_normalize( u );
ch1_mpi 57 mpi_normalize( v );
ch1_mpi 58 usize = u->nlimbs;
ch1_mpi 59 vsize = v->nlimbs;
ch1_mpi 60 if( !u->sign && v->sign )
ch1_mpi 61 return 1;
ch1_mpi 62 if( u->sign && !v->sign )
ch1_mpi 63 return -1;
ch1_mpi 64 if( usize != vsize && !u->sign && !v->sign )
ch1_mpi 65 return usize - vsize;
ch1_mpi 66 if( usize != vsize && u->sign && v->sign )
ch1_mpi 67 return vsize + usize;
ch1_mpi 68 if( !usize )
ch1_mpi 69 return 0;
ch1_mpi 70 if( !(cmp=mpihelp_cmp( u->d, v->d, usize )) )
ch1_mpi 71 return 0;
ch1_mpi 72 if( (cmp < 0?1:0) == (u->sign?1:0))
ch1_mpi 73 return 1;
ch1_mpi 74 return -1;
ch1_mpi 75 }
ch1_mpi 76
ch1_mpi 77