Ask Your Question
3

Including a sage library in c code

asked 2011-02-05 13:34:11 +0100

Podias_k gravatar image

I have a .c file implementing some calculations on lattice basis. I use NTL, GMP and MPFR for my data types and functios. I'd like to use as input for my calculations some basis matrices generated by sage.crypto.lattice.gen_lattice.

  1. Is it possible for me to include "something" so I can call this function in my code and get the matrices?

  2. Is there any other way to export this matrices and import them to my c code? (I need to do these calculations for at least 100 different basis of 200,250,500 size so the process must be as "automated" as possible)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-02-07 09:45:25 +0100

Although this probably won't work (see last paragraph of this response) I think your best chance is to use Cython. Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to easily write code that can be used by Python but is optimized to use fast C data types instead of slower Python data structures.

Create a .pyx file containing your code and execute

$ sage -cython -a mycythonfile.pyx

This will output a corresponding .c file which can be compiled into a shared object library and imported in Python along with an .html file that neatly shows the C equivalent of each line of code.

Using Cython, you could make a copy of $SAGE_ROOT/devel/sage/crypto/lattice.py, called lattice.pyx, and then run the above command on it. However, I doubt that this will work since lattice.py probably depends on other Sage modules and objects. One would need to convert those to C as well and link them together and that sounds like quite a task just to send some matrices to a C program.

Now, if your goal is to just send these matrices generated by sage.crypto.lattice to your C program, I'd recommend using Python's file-handling routines to save the string representations of these matrices (using __repr__()) to a text file (or binary file if you have a lot of them) and then read them in using your C program. You can find out more about writing strings to files in the Python "Input and Output" Documentation.

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

Stats

Asked: 2011-02-05 13:34:11 +0100

Seen: 479 times

Last updated: Feb 07 '11