]>
git.saurik.com Git - apple/security.git/blob - OSX/include/security_cryptkit/CurveParamDocs/factor.c
1 /**************************************************************
5 * General purpose factoring program
8 * 18 May 97 REC - invoked new, fast divide functions
9 * 26 Apr 97 RDW - fixed tabs and unix EOL
10 * 20 Apr 97 RDW - conversion to TC4.5
12 * c. 1997 Perfectly Scientific, Inc.
13 * All Rights Reserved.
16 *************************************************************/
37 #define NUM_PRIMES 6542 /* PrimePi[2^16]. */
40 /* compiler options */
43 #pragma warning( disable : 4127 4706 ) /* disable conditional is constant warning */
47 /* global variables */
49 extern giant scratch2
;
51 giant xr
= NULL
, xs
= NULL
, zs
= NULL
, zr
= NULL
, xorg
= NULL
,
52 zorg
= NULL
, t1
= NULL
, t2
= NULL
, t3
= NULL
, N
= NULL
,
53 gg
= NULL
, An
= NULL
, Ad
= NULL
;
54 giant xb
[D
+2], zb
[D
+2], xzb
[D
+2];
55 int modmode
= 0, Q
, modcount
= 0;
58 /* function prototypes */
60 void ell_even(giant x1
, giant z1
, giant x2
, giant z2
, giant An
,
62 void ell_odd(giant x1
, giant z1
, giant x2
, giant z2
, giant
xor,
64 void ell_mul(giant xx
, giant zz
, int n
, giant An
, giant Ad
, giant N
);
70 /**************************************************************
74 **************************************************************/
88 low
= (unsigned short)rand();
89 hi
= (unsigned short)rand();
90 result
= ((hi
<< 16) | low
) ^ ((int)tp
);
92 return (result
& 0x7fffffff);
101 /* Start the random number generator at a new position. */
105 srand((int)tp
+ (int)getpid());
176 /* Perform a divide (by the discovered factor) and switch back
177 to non-Fermat-non-Mersenne (i.e. normal) mod mode. */
271 unsigned int c
= (unsigned int)0x80000000;
277 ell_even(xx
, zz
, xx
, zz
, An
, Ad
, N
);
282 ell_even(xx
, zz
, xs
, zs
, An
, Ad
, N
);
294 ell_odd(xs
, zs
, xx
, zz
, xorg
, zorg
, N
);
295 ell_even(xs
, zs
, xs
, zs
, An
, Ad
, N
);
299 ell_odd(xx
, zz
, xs
, zs
, xorg
, zorg
, N
);
300 ell_even(xx
, zz
, xx
, zz
, An
, Ad
, N
);
308 /* From R. P. Brent, priv. comm. 1996:
309 Let s > 5 be a pseudo-random seed (called $\sigma$ in the Tech. Report),
313 Then starting point is (x_1, y_1) where
317 a = (v-u)^3(3u+v)/(4u^3 v) - 2
357 s_modg(N
, t2
); /* (v-u)^3. */
364 s_modg(N
, t2
); /* (v-u)^3 (3u+v). */
376 gtog(t2
, An
); /* An/Ad is now A + 2. */
387 N
= newgiant(INFINITY
);
393 nsh
= q
/4; /* Allowing (easily) enough space per giant,
394 since N is about 2^q, which is q bits, or
395 q/16 shorts. But squaring, etc. is allowed,
396 so we need at least q/8, and we choose q/4
397 to be conservative. */
407 xorg
= newgiant(nsh
);
409 zorg
= newgiant(nsh
);
424 xb
[j
] = newgiant(nsh
);
425 zb
[j
] = newgiant(nsh
);
426 xzb
[j
] = newgiant(nsh
);
439 powermodg(t1
, t2
, x
);
455 /**************************************************************
459 **************************************************************/
466 int j
, k
, C
, nshorts
, cnt
, count
,
467 limitbits
= 0, pass
, npr
, rem
;
471 if (!strcmp(argv
[argc
-1], "-r"))
475 /* This segment only takes effect in random mode. */
476 limitbits
= atoi(argv
[argc
-2]);
486 modmode
= atoi(argv
[1]);
500 for (k
=0, npr
=1;; k
++)
505 if (npr
>= NUM_PRIMES
)
512 printf("Sieving...\n");
514 for (j
=0; j
< NUM_PRIMES
; j
++)
517 rem
= idivg(pr
[j
], t1
);
520 printf("%d ", pr
[j
]);
543 printf("Commencing Pollard rho...\n");
546 itog(3, t1
); itog(3, t2
);
548 for (j
=0; j
< 15000; j
++)
568 t3
->sign
= abs(t3
->sign
);
574 if ((gcompg(N
,gg
) != 0) && (!isone(gg
)))
576 fprintf(stdout
,"\n");
597 printf("Commencing Pollard (p-1)...\n");
601 for (j
=0; j
< NUM_PRIMES
; j
++)
603 cnt
= (int)(8*log(2.0)/log(1.0*pr
[j
]));
606 for (k
=0; k
< cnt
; k
++)
608 powermod(t1
, pr
[j
], N
);
624 if ((gcompg(N
,gg
) != 0) && (!isone(gg
)))
626 fprintf(stdout
,"\n");
645 } /* This is the end of (randmode == 0) */
648 printf("Commencing ECM...\n");
666 else if (pass
<= 100)
680 /* Next, choose curve with order divisible by 16 and choose
681 * a point (xr/zr) on said curve.
684 /* Order-div-12 case.
685 * cnt = 8020345; Brent's parameter for stage one discovery
686 * of 27-digit factor of F_13.
689 cnt
= psi_rand(); /* cnt = 8020345; */
690 choose12(xr
, zr
, cnt
, An
, Ad
, N
);
691 printf("Choosing curve %d, with s = %d, B = %d, C = %d:\n", pass
,cnt
, B
, C
); fflush(stdout
);
695 for (j
=0;j
<nshorts
;j
++)
697 ell_mul(xr
, zr
, 1<<16, An
, Ad
, N
);
698 ell_mul(xr
, zr
, 3*3*3*3*3*3*3*3*3*3*3, An
, Ad
, N
);
699 ell_mul(xr
, zr
, 5*5*5*5*5*5*5, An
, Ad
, N
);
700 ell_mul(xr
, zr
, 7*7*7*7*7*7, An
, Ad
, N
);
701 ell_mul(xr
, zr
, 11*11*11*11, An
, Ad
, N
);
702 ell_mul(xr
, zr
, 13*13*13*13, An
, Ad
, N
);
703 ell_mul(xr
, zr
, 17*17*17, An
, Ad
, N
);
710 ell_mul(xr
, zr
, k
, An
, Ad
, N
);
712 ell_mul(xr
, zr
, k
, An
, Ad
, N
);
722 if ((!isone(gg
))&&(bitlen(gg
)>limitbits
))
724 fprintf(stdout
,"\n");
725 gwriteln(gg
, stdout
);
751 /* Continue; Invoke, to test Stage 1 only. */
755 ell_mul(xb
[0], zb
[0], k
*D
+1 , An
, Ad
, N
);
758 ell_mul(xb
[D
+1], zb
[D
+1], (k
+2)*D
+1 , An
, Ad
, N
);
760 for (j
=1; j
<= D
; j
++)
764 ell_mul(xb
[j
], zb
[j
], 2*j
, An
, Ad
, N
);
770 printf("\nCommencing second stage, curve %d...\n",pass
); fflush(stdout
);
780 s_modg(N
,gg
); /* Accumulate. */
781 for (j
= 1; j
< D
; j
++)
783 if (!isprime(k
*D
+1+ 2*j
))
786 /* Next, accumulate (xa - xb)(za + zb) - xa za + xb zb. */
799 if((++count
)%1000
==0)
808 ell_odd(xb
[D
], zb
[D
], xb
[D
+1], zb
[D
+1], xb
[0], zb
[0], N
);
814 if((!isone(gg
))&&(bitlen(gg
)>limitbits
))
816 fprintf(stdout
,"\n");
817 gwriteln(gg
, stdout
);