forked from miracl/MIRACL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
managed.txt
206 lines (170 loc) · 6.93 KB
/
managed.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
------------------------------------------------------------------