
It is possible to create a version of the MIRACL library which consists of 
Microsoft Studio compatible "managed code", for .NET applications.

In managed code (aka C++/CLI), the MIRACL modules must be compiled as C++ 
code into an Intermediate Language (IL), which is not native assembly 
language. This IL is compatible with code generated by other languages like 
C# and Java, supports portability and enhanced runtime error checking and 
heap management. Using sophisticated Just-In-Time compilation techniques, 
this IL can itself be compiled "on-the fly" into native machine code, and 
hence runs surprisingly fast.

To generate managed code the compiler flags /clr and /TP must be used. 
However modules containing in-line assembly or using compiler intrinsics 
cannot be compiled to IL, and are instead compiled directly to native 
machine code. This is not necessarily a problem, as IL and native assembly 
language modules can be mixed.

Since MIRACL modules must be compiled as C++ and not as C, it is important
to define MR_CPP in mirdef.h to ensure that MIRACL C++ modules are aware
that the MIRACL library now uses C++ function calling conventions.

A 100% managed code build of miracl will NOT be as fast as one compiled
directly to native machine code. 

The .NET framework supports mixing of languages, so one may want for 
example to use MIRACL functionality with C#. The best way to proceed 
is to build a C++ DLL, and link to C# using (from the command line)

csc your_program.cs /r:miracl_based_code.dll

To communicate between C++ and C#, you need to use a supported type. 
For cryptographic keys and the like a byte array is best. So on the C# side

byte[] KEY = new byte[32];

On the C++ side, declare the function parameter like

array<Byte>^ KEY

Also on the C# side

String ID="Alice";

The C++ parameter in this case is

String^ ID 

You can use the MIRACL big_to_bytes() and bytes_to_big() functions to convert 
from the internal MIRACL structures to simple byte arrays.

To build a 100% managed code (no flash arithmetic) version of MIRACL, 
proceed as follows:

Copy this to mirdef.h

#define MR_LITTLE_ENDIAN
#define MIRACL 32
#define mr_utype int
#define MR_IBITS 32
#define MR_LBITS 32
#define mr_unsign32 unsigned int
#define mr_dltype __int64
#define mr_unsign64 unsigned __int64
#define MR_ALWAYS_BINARY
#define MAXBASE ((mr_small)1<<(MIRACL-1))
#define MR_NOASM
#define MR_BITSINCHAR 8
#define MR_CPP

Build the library using this batch file

-----------------------------------------------------------
rem Compile MIRACL modules
cl /clr /TP /c /O2 /W3 mrcore.c
cl /clr /TP /c /O2 /W3 mrarth0.c
cl /clr /TP /c /O2 /W3 mrarth1.c
cl /clr /TP /c /O2 /W3 mrarth2.c
cl /clr /TP /c /O2 /W3 mralloc.c
cl /clr /TP /c /O2 /W3 mrsmall.c
cl /clr /TP /c /O2 /W3 mrio1.c
cl /clr /TP /c /O2 /W3 mrio2.c
cl /clr /TP /c /O2 /W3 mrgcd.c
cl /clr /TP /c /O2 /W3 mrjack.c
cl /clr /TP /c /O2 /W3 mrxgcd.c
cl /clr /TP /c /O2 /W3 mrarth3.c
cl /clr /TP /c /O2 /W3 mrbits.c
cl /clr /TP /c /O2 /W3 mrrand.c
cl /clr /TP /c /O2 /W3 mrprime.c
cl /clr /TP /c /O2 /W3 mrcrt.c
cl /clr /TP /c /O2 /W3 mrscrt.c
cl /clr /TP /c /O2 /W3 mrmonty.c
cl /clr /TP /c /O2 /W3 mrpower.c
cl /clr /TP /c /O2 /W3 mrsroot.c
cl /clr /TP /c /O2 /W3 mrcurve.c
cl /clr /TP /c /O2 /W3 mrfast.c
cl /clr /TP /c /O2 /W3 mrlucas.c
cl /clr /TP /c /O2 /W3 mrzzn2.c
cl /clr /TP /c /O2 /W3 mrzzn2b.c
cl /clr /TP /c /O2 /W3 mrzzn3.c
cl /clr /TP /c /O2 /W3 mrshs.c
cl /clr /TP /c /O2 /W3 mrshs256.c
cl /clr /TP /c /O2 /W3 mrshs512.c
cl /clr /TP /c /O2 /W3 mraes.c
cl /clr /TP /c /O2 /W3 mrgcm.c
cl /clr /TP /c /O2 /W3 mrstrong.c
cl /clr /TP /c /O2 /W3 mrbrick.c
cl /clr /TP /c /O2 /W3 mrebrick.c
cl /clr /TP /c /O2 /W3 mrgf2m.c
cl /clr /TP /c /O2 /W3 mrec2m.c
cl /clr /TP /c /O2 /W3 mrecn2.c

rem
rem Create library 'miracl.lib'
del miracl.lib

lib /OUT:miracl.lib mrio2.obj mrio1.obj mrrand.obj mrprime.obj mrcrt.obj mrscrt.obj mrfast.obj 
lib /OUT:miracl.lib miracl.lib mrjack.obj mrxgcd.obj mrgcd.obj  mrarth3.obj mrarth2.obj mrpower.obj mrsroot.obj
lib /OUT:miracl.lib miracl.lib mrmonty.obj mralloc.obj mrarth1.obj mrarth0.obj mrsmall.obj mrcore.obj mrgcm.obj
lib /OUT:miracl.lib miracl.lib mrcurve.obj mrshs.obj mraes.obj mrlucas.obj mrstrong.obj mrbrick.obj mrbits.obj 
lib /OUT:miracl.lib miracl.lib mrshs256.obj mrshs512.obj mrebrick.obj mrgf2m.obj mrec2m.obj mrzzn2.obj mrzzn3.obj
lib /OUT:miracl.lib miracl.lib mrecn2.obj mrzzn2b.obj

del mr*.obj

rem compile one example program

cl /O2 /clr pk-demo.cpp big.cpp ecn.cpp miracl.lib
--------------------------------------------------------------

For a 64-bit build use this as mirdef.h

#define MR_LITTLE_ENDIAN
#define MIRACL 64
#define mr_utype __int64
#define mr_unsign64 unsigned __int64
#define MR_IBITS 32
#define MR_LBITS 32
#define mr_unsign32 unsigned int
#define MAXBASE ((mr_small)1<<(MIRACL-1))
#define MR_BITSINCHAR 8
#define MR_ALWAYS_BINARY
#define MR_CPP
#define MR_NO_INTRINSICS

and build using this batch file

---------------------------------------------------------------

cl /clr /TP /c /O2 /W3 mrcore.c
cl /clr /TP /c /O2 /W3 mrarth0.c
cl /clr /TP /c /O2 /W3 mrarth1.c
cl /clr /TP /c /O2 /W3 mrarth2.c
cl /clr /TP /c /O2 /W3 mralloc.c
cl /clr /TP /c /O2 /W3 mrsmall.c
cl /clr /TP /c /O2 /W3 mrio1.c
cl /clr /TP /c /O2 /W3 mrio2.c
cl /clr /TP /c /O2 /W3 mrgcd.c
cl /clr /TP /c /O2 /W3 mrjack.c
cl /clr /TP /c /O2 /W3 mrxgcd.c
cl /clr /TP /c /O2 /W3 mrarth3.c
cl /clr /TP /c /O2 /W3 mrbits.c
cl /clr /TP /c /O2 /W3 mrrand.c
cl /clr /TP /c /O2 /W3 mrprime.c
cl /clr /TP /c /O2 /W3 mrcrt.c
cl /clr /TP /c /O2 /W3 mrscrt.c
cl /clr /TP /c /O2 /W3 mrmonty.c
cl /clr /TP /c /O2 /W3 mrpower.c
cl /clr /TP /c /O2 /W3 mrsroot.c
cl /clr /TP /c /O2 /W3 mrcurve.c
cl /clr /TP /c /O2 /W3 mrfast.c
cl /clr /TP /c /O2 /W3 mrlucas.c
cl /clr /TP /c /O2 /W3 mrzzn2.c
cl /clr /TP /c /O2 /W3 mrzzn2b.c
cl /clr /TP /c /O2 /W3 mrzzn3.c
cl /clr /TP /c /O2 /W3 mrshs.c
cl /clr /TP /c /O2 /W3 mrshs256.c
cl /clr /TP /c /O2 /W3 mrshs512.c
cl /clr /TP /c /O2 /W3 mraes.c
cl /clr /TP /c /O2 /W3 mrgcm.c
cl /clr /TP /c /O2 /W3 mrstrong.c
cl /clr /TP /c /O2 /W3 mrbrick.c
cl /clr /TP /c /O2 /W3 mrebrick.c
cl /clr /TP /c /O2 /W3 mrgf2m.c
cl /clr /TP /c /O2 /W3 mrec2m.c
cl /clr /TP /c /O2 /W3 mrecn2.c
copy mrmuldv.w64 mrmuldv.c
cl /clr /TP /c /O2 /W3 mrmuldv.c

rem
rem Create library 'miracl.lib'
del miracl.lib

lib /OUT:miracl.lib mrio2.obj mrio1.obj mrrand.obj mrprime.obj mrcrt.obj mrscrt.obj mrfast.obj mrmuldv.obj
lib /OUT:miracl.lib miracl.lib mrjack.obj mrxgcd.obj mrgcd.obj  mrarth3.obj mrarth2.obj mrpower.obj mrsroot.obj
lib /OUT:miracl.lib miracl.lib mrmonty.obj mralloc.obj mrarth1.obj mrarth0.obj mrsmall.obj mrcore.obj
lib /OUT:miracl.lib miracl.lib mrcurve.obj mrshs.obj mraes.obj mrlucas.obj mrstrong.obj mrbrick.obj mrbits.obj 
lib /OUT:miracl.lib miracl.lib mrshs256.obj mrshs512.obj mrebrick.obj mrgf2m.obj mrec2m.obj mrzzn2.obj mrzzn3.obj
lib /OUT:miracl.lib miracl.lib mrecn2.obj mrzzn2b.obj mrgcm.obj

del mr*.obj
rem

cl /O2 /clr pk-demo.cpp big.cpp ecn.cpp miracl.lib

------------------------------------------------------------------
