Ask Your Question
0

worksheet to console ?

asked 2015-06-08 21:44:29 +0100

hashirama gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-06-08 23:48:12 +0100

tmonteil gravatar image

Note that your script is not pure Python since it relies on the Sage preparser (e.g. you use ^ for power while in Python it should be **).

You can put this into a file with a .sage extension, for example myscript.sage so that Sage will understand that the script has to be preparsed, then you can call the script either from the terminal with

./sage myscript.sage

or from the Sage console with:

 %runfile myscript.sage
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-06-08 21:44:29 +0100

Seen: 876 times

Last updated: Jun 08 '15