2 * pmodm127 - test pmodm127's ability to calculate q mod 2^(2^127-1)
4 * Copyright (C) 2004 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: pmodm127.cal,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/custom/RCS/pmodm127.cal,v $
24 * Under source code control: 2004/02/25 14:25:32
25 * File existed as early as: 2004
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 * This file is part of the custom sample calc files.
34 * NOTE: You must use a calc that was compiled with ALLOW_CUSTOM= -DCUSTOM
35 * and run with a -C arg.
37 if (config("compile_custom") == 0) {
38 quit "calc compiled without -DCUSTOM";
39 } else if (config("allow_custom") == 0) {
40 quit "calc was run without the -D command line option";
44 define pmodm127_test1(testcnt)
46 local q; /* test factor */
47 local m127; /* 2^127-1 */
53 if (!isint(testcnt) || testcnt <= 0) {
54 return newerror("pmodm127_test1 must have an integer count>0");
58 * perform testcnt divisor tests for primes of form 2*k*(2^127-1)+1
60 * NOTE: Since this is just a test, we do not need to be optimal.
63 q = 2*4949132165849*m127+1;
64 for (i=0; i < testcnt; ++i) {
67 * determine next prime divisor
69 q = nextcand(q, -1, 0, 1, 2*m127);
71 /* compare custom function with its pmod() equivalent */
72 if (config("resource_debug") & 8) {
75 if (pmod(2, m127, q) != custom("pmodm127", q)) {
76 print "ERROR: pmodm127 failed for ", str(q);
77 return newerror("pmodm127 failed for " + str(q));
81 /* return success count */
82 if (config("resource_debug") & 8) {
83 print "passed", testcnt, "tests";
88 define pmodm127_test2(testcnt, seed)
90 local q; /* test factor */
91 local m127; /* 2^127-1 */
97 if (!isint(testcnt) || testcnt <= 0) {
98 return newerror("pmodm127_test2 must have an integer count>0");
101 return newerror("pmodm127_test2 must an integer seed");
105 * perform testcnt divisor tests random integers over [1e51, 1e52)
107 * NOTE: Since this is just a test, we do not need to be optimal.
110 for (i=0; i < testcnt; ++i) {
113 * determine next prime divisor
115 q = rand(1e51, 1e52) | 0x1;
116 if (config("resource_debug") & 8) {
120 /* compare custom function with its pmod() equivalent */
121 if (pmod(2, m127, q) != custom("pmodm127", q)) {
122 print "ERROR: pmodm127 failed for ", str(q);
123 print "ERROR: ", pmod(2,m127,q), " != ",
124 custom("pmodm127", q);
125 return newerror("pmodm127 failed for " + str(q));
129 /* return success count */
130 if (config("resource_debug") & 8) {
131 print "passed", testcnt, "tests";
136 if ((config("resource_debug") & 3) && !(config("resource_debug") & 8)) {
137 print "DEBUG: use config('resource_debug',",
138 config("resource_debug")|8 : ") to enable more debugging";