Re: Proving primality of an integer
From: Cristiano (cristiano.pi@nsquipo.it)
Date: 02/09/03
- Next message: Martin Vesterlund: "Re: EVEN MORE NASA STUFF"
- Previous message: Tom St Denis: "Re: Why was Rijndael picked over tfish as aes?"
- In reply to: Aldo: "Proving primality of an integer"
- Next in thread: e_minus: "Re: Proving primality of an integer"
- Reply: e_minus: "Re: Proving primality of an integer"
- Reply: Phil Carmody: "Re: Proving primality of an integer"
- Reply: Aldo: "Re: Proving primality of an integer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: "Cristiano" <cristiano.pi@nsquipo.it> Date: Sun, 09 Feb 2003 11:24:39 GMT
Aldo wrote:
>
> The reader can download Ubasic interpreter from the Internet, or
> translate the program into C.
Very good! Your implementation is very fast.
Just a question: if your program say "N is prime", N is really prime
without any doubt?
This is my translation in c++ for miracl (N.get_bit() is not included in
the miracl library):
// -------------------------
// (S(x)*S(x)) % (x^r-1) % n
// -------------------------
void S_x_S(Big *S,Big *T,const int R,Big &N)
{
for(int i=0;i<=R;i++) { S[i]=T[i]; T[i]=0; }
for(int i=0;i<R;i++)
for(int j=0;j<R;j++) {
int k=(i+j)%R; T[k]=(T[k]+S[i]*S[j])%N;
}
}
// -------------------------
// (S(x)*A(x)) % (x^r-1) % n
// -------------------------
void S_x_A(Big *S,Big *T,const int R,int *A,Big &N)
{
for(int i=0;i<=R;i++) { S[i]=T[i]; T[i]=0; }
for(int i=0;i<R;i++)
for(int j=0;j<=1;j++) {
int k=(i+j)%R; T[k]=(T[k]+S[i]*A[j])%N;
}
}
int AKS(Big &N)
{
int R=2,K=N%R;
while(K*K%R==1) { R++; K=N%R; }
if(K==0) return 0; // N is divisible by R
Big *S=new Big[R+1],*T=new Big[R+1];
int A[2]={-1,1}; // A = x-1
T[0]=-1; T[1]=1;
// Mod exp: (x-1)^n (mod x^r-1, n)
// -------------------------------
int I=bits(N)-2;
while (I>=0) {
S_x_S(S,T,R,N); // (S*S)%(x^r-1)%n
if(N.get_bit(I)) S_x_A(S,T,R,A,N); // (S*A)@(x^r-1)@n
I--;
}
// Normalization as in UBASIC
for(int i=0;i<=R;i++) if(T[i]<0) T[i]+=N;
// -----------
// Test result
// -----------
for(int i=0;i<=R;i++) S[i]=0;
S[K]=1; S[0]=N-1;
I=0; while (I<R && S[I]==T[I]) I++;
delete[] S; delete[] T;
if(I==R) return 1; // N is prime
return 0; // N is composite
}
Cristiano
- Next message: Martin Vesterlund: "Re: EVEN MORE NASA STUFF"
- Previous message: Tom St Denis: "Re: Why was Rijndael picked over tfish as aes?"
- In reply to: Aldo: "Proving primality of an integer"
- Next in thread: e_minus: "Re: Proving primality of an integer"
- Reply: e_minus: "Re: Proving primality of an integer"
- Reply: Phil Carmody: "Re: Proving primality of an integer"
- Reply: Aldo: "Re: Proving primality of an integer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|