⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t-cong_2exp.c

📁 a very popular packet of cryptography tools,it encloses the most common used algorithm and protocols
💻 C
字号:
/* test mpz_congruent_2exp_p *//*Copyright 2001 Free Software Foundation, Inc.This file is part of the GNU MP Library.The GNU MP Library is free software; you can redistribute it and/or modifyit under the terms of the GNU Lesser General Public License as published bythe Free Software Foundation; either version 2.1 of the License, or (at youroption) any later version.The GNU MP Library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITYor FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General PublicLicense for more details.You should have received a copy of the GNU Lesser General Public Licensealong with the GNU MP Library; see the file COPYING.LIB.  If not, write tothe Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,MA 02111-1307, USA.*/#include <stdio.h>#include <stdlib.h>#include "gmp.h"#include "gmp-impl.h"#include "tests.h"voidcheck_one (mpz_srcptr a, mpz_srcptr c, unsigned long d, int want){  mpz_t  diff, d2exp;  int    got;  int    swap;  for (swap = 0; swap <= 1; swap++)    {      got = (mpz_congruent_2exp_p (a, c, d) != 0);      if (want != got)        {          mpz_init (diff);          mpz_init (d2exp);          mpz_sub (diff, a, c);          mpz_set_ui (d2exp, 1L);          mpz_mul_2exp (d2exp, d2exp, d);          printf ("mpz_congruent_2exp_p wrong\n");          printf ("   expected %d got %d\n", want, got);          mpz_trace ("   a", a);          mpz_trace ("   c", c);          mpz_trace (" a-c", diff);          mpz_trace (" 2^d", d2exp);          printf    ("   d=%lu\n", d);          mp_trace_base = -16;          mpz_trace ("   a", a);          mpz_trace ("   c", c);          mpz_trace (" a-c", diff);          mpz_trace (" 2^d", d2exp);          printf    ("   d=0x%lX\n", d);          abort ();        }      MPZ_SRCPTR_SWAP (a, c);    }}voidcheck_data (void){  static const struct {    const char     *a;    const char     *c;    unsigned long  d;    int            want;  } data[] = {    /* anything is congruent mod 1 */    { "0", "0", 0, 1 },    { "1", "0", 0, 1 },    { "0", "1", 0, 1 },    { "123", "456", 0, 1 },    { "0x123456789123456789", "0x987654321987654321", 0, 1 },  };  mpz_t   a, c;  int     i;  mpz_init (a);  mpz_init (c);  for (i = 0; i < numberof (data); i++)    {      mpz_set_str_or_abort (a, data[i].a, 0);      mpz_set_str_or_abort (c, data[i].c, 0);      check_one (a, c, data[i].d, data[i].want);    }  mpz_clear (a);  mpz_clear (c);}voidcheck_random (int argc, char *argv[]){  gmp_randstate_ptr rands = RANDS;  unsigned long  d;  mpz_t  a, c, ra, rc;  int    i;  int    want;  int    reps = 5000;  if (argc >= 2)    reps = atoi (argv[1]);  mpz_init (a);  mpz_init (c);  mpz_init (ra);  mpz_init (rc);  for (i = 0; i < reps; i++)    {      mpz_errandomb (a, rands, 8*BITS_PER_MP_LIMB);      mpz_errandomb (c, rands, 8*BITS_PER_MP_LIMB);      d = urandom() % (8*BITS_PER_MP_LIMB);      mpz_mul_2exp (a, a, urandom() % (2*BITS_PER_MP_LIMB));      mpz_mul_2exp (c, c, urandom() % (2*BITS_PER_MP_LIMB));      mpz_negrandom (a, rands);      mpz_negrandom (c, rands);      mpz_fdiv_r_2exp (ra, a, d);      mpz_fdiv_r_2exp (rc, c, d);      want = (mpz_cmp (ra, rc) == 0);      check_one (a, c, d, want);      mpz_sub (ra, ra, rc);      mpz_sub (a, a, ra);      check_one (a, c, d, 1);    }  mpz_clear (a);  mpz_clear (c);  mpz_clear (ra);  mpz_clear (rc);}intmain (int argc, char *argv[]){  tests_start ();  check_data ();  check_random (argc, argv);  tests_end ();  exit (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -