Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

worksheet to console ?

Hi I have written a code in sageworksheet format, it runs nice and smooth on sagecloud, but I have one problem : I want to make it more interactive so the user can enter the parameters by himself, so I'm willing to convert this into a sage script or a pyhton script, I have installed sage on my ubuntu machine, again the code runs on the notebook but not on the console it gives me some syntax error on " P<x> " All I want is to make it run as a Python script or Sage script so I can use input function to ask users to enter the parameters ! Here is the code :

P.<x> = PolynomialRing(ZZ); 

def bezout(f,g):    
    P.<x> = PolynomialRing(QQ)
    f = f+x-x
    g = g+x-x
    e=xgcd(f,g)
    gcd=e[0]
    u=e[1]
    v=e[2]
    return (u,v,gcd)

def polymod(f,q):  
    P.<x> = PolynomialRing(QQ)
    f = f
    c = f.coefficients(sparse=False)
    N = len(c)
    for i in range(N):
        c[i] = Rational(c[i]).mod_ui(q);
    p = sum(c[i]*(x^(i)) for i in range(N));
    return p

def center(f,q): 
    u = q/2
    v = -u
    c = f.coefficients(sparse=False)
    N = len(c)
    for i in range(N):
        if c[i]<v:
            c[i] = c[i] + q;
        elif c[i]>u:
            c[i] = c[i] - q;
        else:
            c[i] = c[i];
    p = sum(c[i]*(x^(i)) for i in range(N));
    return p


class Ntru:           
    N = None
    p = None
    q = None
    d = None
    f = None
    g = None
    h = None
    fp = None
    fq = None
    Phi = None

    def __init__(self,N,p,q,d):  
        self.N = N
        self.p = p
        self.q = q
        self.d = d
        v = self.N
        self.Phi = x^v -1


    def test(self): 
        if not is_prime(self.N):
            print "N n est pas premier, pensez a changer ce parametre"
            return False
        if gcd(self.N,self.p) != 1:
            print "N et p ne sont pas premiers entre eux, pensez a changer ces parametres"
            return False
        if gcd(self.N,self.q) != 1:
            print "N et q ne sont pas premiers entre eux, pensez a changer ces parameres"
            return False
        if self.q <= (6*self.d+1)*self.p :
            print "q doit etre superieur a (6d+1)*p "
            return False
        return True


    def genPublicKey(self,f_new,g_new):   

        self.f = f_new
        self.g = g_new
        (b_f,b_phi,bz) = bezout(self.f,self.Phi)
        self.fp = polymod(b_f,self.p)
        self.fq = polymod(b_f,self.q)
        self.h = polymod((self.fq*self.g).quo_rem(self.Phi)[1],self.q)
        if not self.test():
            print "le cryptage ne peut s effectuer avec ces parametres !"
            quit()

    def encrypt(self,message,rand): 
        if self.h!=None:
            temp=(self.p*rand*self.h + message).quo_rem(self.Phi)[1]
            e=polymod(temp,self.q)
            return e
        else:
            print "Impossible de faire le cryptage : la cle n a pas encore ete generee"
            print "Veuillez en generer une avec la fonction genPublicKey"

    def decrypt(self,encryptedMsg): 
        a = (self.f*encryptedMsg).quo_rem(self.Phi)[1]
        a = polymod(a,self.q)
        a = center(a,self.q)
        tmp = (self.fp*a).quo_rem(self.Phi)[1]
        m=polymod(tmp,self.p)
        return m



NTRU=Ntru(167,3,128,3) 
f = 1+x-x^2-x^3-x^4+x^5+x^6 
g = -1+x^2+x^3+x^4-x^5-x^6

NTRU.f = f 
NTRU.g = g
NTRU.genPublicKey(f,g)
print "La cle publique est : ",NTRU.h 

msg = 1+x+x^4+x^5+x^6 
rand = -1 -x + x^2 + x^3 - x^4 + x^6 

enc = NTRU.encrypt(msg,rand)
print "Le Message crypte est : ",enc   
dec = NTRU.decrypt(enc) 
print "Le Message dechiffre est : ",dec 
print "Le Message en clair etait : ",msg

Thank you !