3
$\begingroup$

I'm programming a project where I use multivariate symbolic polynomials. At some point I need to perform a PolynomialMod over a prime modulus, but the library GiNaC which I use works only over the Reals so has no finite field arithmetic implemented.

As I always program on Mathematica before going on C/C++ I know that the desired result can be achieved using Mathematica. Then I'm thinking on linking C++ with Mathematica.

I only need calling an own-coded Mathematica function, send arguments and receive an output that defines the desired polynomial over $F_p$.

In the end I see two options: find a library compatible with C++ that makes multivariate symbolic polynomial arithmetic over $F_p$ (which is not existant IMO) or just use Mathematica as it works flawlessly.

In the case of using Mathematica, which is the way to go? I've seen https://reference.wolfram.com/language/CCodeGenerator/tutorial/CodeGeneration.html#139183296 as the CodeGeneration tool that translates Wolfram Language to C++. Others says to use pure C embedding, loading the core of Mathematica, then calling your own custom function, but I need concrete examples or documentation that helps me to pass through this.


TL;DR

What I want: calling a custom Mathematica function that accepts $5$ arguments and returns a polynomial modulo an irreducible polynomial over $F_p$.

What I have: The own custom function works well inside a notebook.

What I don't have: I don't know how to link that own custom function with my C++ code. Must rely on Mathematica as there is no library compatible with C++ that satisfy my needs.

$\endgroup$
5
  • 1
    $\begingroup$ You can use the Mathematica kernel to do the computation through MathLink (recently renamed WSTP). This will make your program dependent on Mathematica. It will require a working Mathematica installation to run. $\endgroup$
    – Szabolcs
    Commented Dec 15, 2019 at 22:16
  • $\begingroup$ Right now I compiled a function that reads string data from .txt that is converted to symbolic expression, so PolynomialMod is applied. That compiled function is converted to a shared library (.so extension) by LibraryGenerate that is loaded into my program by dlopen. I have Intel-MKL libs so everything is fine, Wolfram Runtime runs and C includes located. Seems like I'll be able to do this. Anyways, I'm interested on passing Symbolic expressions into functions, any approach is welcomed! $\endgroup$
    – kub0x
    Commented Dec 15, 2019 at 22:41
  • 1
    $\begingroup$ The only way to transfer symbols is MathLink. I have very little experience with the Wolfram Runtime, but I do not believe that it provides any symbolic functionality. You will need a full Mathematica installation. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2019 at 9:47
  • $\begingroup$ @Szabolcs I've found gitlab.com/neel.basu/mathematicapp to be helpful. Just declare the functions you need, in my case $12$ functions of Mathematica are being used. The only problem I've found is the ReplaceAll function not working as intended, so I've written a wrapper to pass from Mathematica expression to a string, then replace via character replacement in C++ and convert back to expression. My subject is to construct multivariate polynomial equation systems that allow the owner to recover the original input (crypto purposes). Will post it here when code is published. $\endgroup$
    – kub0x
    Commented Dec 21, 2019 at 18:55
  • $\begingroup$ Adding to the previous comment: mathematicapp is a library built on top of MathLink so symbolic expressions are returned originally in Mathematica Language this is Plus[x1,x2] is $x_1+x_2$. So it works amazingly without writting my own wrapper to pass expressions via MathLink. $\endgroup$
    – kub0x
    Commented Dec 21, 2019 at 18:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.