aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/byzantine-lab/mcl/ffi/cs/bn256.cs
blob: 0e1ed032ce762f879a1f6b76677f568ec6765523 (plain) (blame)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace mcl {
    public class BN256 {
        [DllImport("mclBn256.dll")]
        public static extern int mclBn_init(int curve, int maxUnitSize);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_clear(ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_setInt(ref Fr y, int x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_setStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_isValid(ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_isEqual(ref Fr x, ref Fr y);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_isZero(ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_isOne(ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_setByCSPRNG(ref Fr x);

        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_setHashOf(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnFr_getStr([Out]StringBuilder buf, long maxBufSize, ref Fr x, int ioMode);

        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_neg(ref Fr y, ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_inv(ref Fr y, ref Fr x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_add(ref Fr z, ref Fr x, ref Fr y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_sub(ref Fr z, ref Fr x, ref Fr y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_mul(ref Fr z, ref Fr x, ref Fr y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnFr_div(ref Fr z, ref Fr x, ref Fr y);

        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_clear(ref G1 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG1_setStr(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG1_isValid(ref G1 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG1_isEqual(ref G1 x, ref G1 y);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG1_isZero(ref G1 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG1_hashAndMapTo(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
        [DllImport("mclBn256.dll")]
        public static extern long mclBnG1_getStr([Out]StringBuilder buf, long maxBufSize, ref G1 x, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_neg(ref G1 y, ref G1 x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_dbl(ref G1 y, ref G1 x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_add(ref G1 z, ref G1 x, ref G1 y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_sub(ref G1 z, ref G1 x, ref G1 y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG1_mul(ref G1 z, ref G1 x, ref Fr y);

        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_clear(ref G2 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG2_setStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG2_isValid(ref G2 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG2_isEqual(ref G2 x, ref G2 y);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG2_isZero(ref G2 x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnG2_hashAndMapTo(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
        [DllImport("mclBn256.dll")]
        public static extern long mclBnG2_getStr([Out]StringBuilder buf, long maxBufSize, ref G2 x, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_neg(ref G2 y, ref G2 x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_dbl(ref G2 y, ref G2 x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_add(ref G2 z, ref G2 x, ref G2 y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_sub(ref G2 z, ref G2 x, ref G2 y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnG2_mul(ref G2 z, ref G2 x, ref Fr y);

        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_clear(ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnGT_setStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnGT_isEqual(ref GT x, ref GT y);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnGT_isZero(ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern int mclBnGT_isOne(ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern long mclBnGT_getStr([Out]StringBuilder buf, long maxBufSize, ref GT x, int ioMode);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_neg(ref GT y, ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_inv(ref GT y, ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_add(ref GT z, ref GT x, ref GT y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_sub(ref GT z, ref GT x, ref GT y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_mul(ref GT z, ref GT x, ref GT y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_div(ref GT z, ref GT x, ref GT y);

        [DllImport("mclBn256.dll")]
        public static extern void mclBnGT_pow(ref GT z, ref GT x, ref Fr y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBn_pairing(ref GT z, ref G1 x, ref G2 y);
        [DllImport("mclBn256.dll")]
        public static extern void mclBn_finalExp(ref GT y, ref GT x);
        [DllImport("mclBn256.dll")]
        public static extern void mclBn_millerLoop(ref GT z, ref G1 x, ref G2 y);

        public static void init()
        {
            const int curveFp254BNb = 0;
            const int maxUnitSize = 4;
            if (mclBn_init(curveFp254BNb, maxUnitSize) != 0) {
                throw new InvalidOperationException("mclBn_init");
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct Fr {
            private ulong v0, v1, v2, v3;
            public void Clear()
            {
                mclBnFr_clear(ref this);
            }
            public void SetInt(int x)
            {
                mclBnFr_setInt(ref this, x);
            }
            public void SetStr(string s, int ioMode)
            {
                if (mclBnFr_setStr(ref this, s, s.Length, ioMode) != 0) {
                    throw new ArgumentException("mclBnFr_setStr" + s);
                }
            }
            public bool IsValid()
            {
                return mclBnFr_isValid(ref this) == 1;
            }
            public bool Equals(Fr rhs)
            {
                return mclBnFr_isEqual(ref this, ref rhs) == 1;
            }
            public bool IsZero()
            {
                return mclBnFr_isZero(ref this) == 1;
            }
            public bool IsOne()
            {
                return mclBnFr_isOne(ref this) == 1;
            }
            public void SetByCSPRNG()
            {
                mclBnFr_setByCSPRNG(ref this);
            }
            public void SetHashOf(String s)
            {
                if (mclBnFr_setHashOf(ref this, s, s.Length) != 0) {
                    throw new InvalidOperationException("mclBnFr_setHashOf:" + s);
                }
            }
            public string GetStr(int ioMode)
            {
                StringBuilder sb = new StringBuilder(1024);
                long size = mclBnFr_getStr(sb, sb.Capacity, ref this, ioMode);
                if (size == 0) {
                    throw new InvalidOperationException("mclBnFr_getStr:");
                }
                return sb.ToString();
            }
            public void Neg(Fr x)
            {
                mclBnFr_neg(ref this, ref x);
            }
            public void Inv(Fr x)
            {
                mclBnFr_inv(ref this, ref x);
            }
            public void Add(Fr x, Fr y)
            {
                mclBnFr_add(ref this, ref x, ref y);
            }
            public void Sub(Fr x, Fr y)
            {
                mclBnFr_sub(ref this, ref x, ref y);
            }
            public void Mul(Fr x, Fr y)
            {
                mclBnFr_mul(ref this, ref x, ref y);
            }
            public void Div(Fr x, Fr y)
            {
                mclBnFr_div(ref this, ref x, ref y);
            }
            public static Fr operator -(Fr x)
            {
                Fr y = new Fr();
                y.Neg(x);
                return y;
            }
            public static Fr operator +(Fr x, Fr y)
            {
                Fr z = new Fr();
                z.Add(x, y);
                return z;
            }
            public static Fr operator -(Fr x, Fr y)
            {
                Fr z = new Fr();
                z.Sub(x, y);
                return z;
            }
            public static Fr operator *(Fr x, Fr y)
            {
                Fr z = new Fr();
                z.Mul(x, y);
                return z;
            }
            public static Fr operator /(Fr x, Fr y)
            {
                Fr z = new Fr();
                z.Div(x, y);
                return z;
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct G1 {
            private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
            public void Clear()
            {
                mclBnG1_clear(ref this);
            }
            public void setStr(String s, int ioMode)
            {
                if (mclBnG1_setStr(ref this, s, s.Length, ioMode) != 0) {
                    throw new ArgumentException("mclBnG1_setStr:" + s);
                }
            }
            public bool IsValid()
            {
                return mclBnG1_isValid(ref this) == 1;
            }
            public bool Equals(G1 rhs)
            {
                return mclBnG1_isEqual(ref this, ref rhs) == 1;
            }
            public bool IsZero()
            {
                return mclBnG1_isZero(ref this) == 1;
            }
            public void HashAndMapTo(String s)
            {
                if (mclBnG1_hashAndMapTo(ref this, s, s.Length) != 0) {
                    throw new ArgumentException("mclBnG1_hashAndMapTo:" + s);
                }
            }
            public string GetStr(int ioMode)
            {
                StringBuilder sb = new StringBuilder(1024);
                long size = mclBnG1_getStr(sb, sb.Capacity, ref this, ioMode);
                if (size == 0) {
                    throw new InvalidOperationException("mclBnG1_getStr:");
                }
                return sb.ToString();
            }
            public void Neg(G1 x)
            {
                mclBnG1_neg(ref this, ref x);
            }
            public void Dbl(G1 x)
            {
                mclBnG1_dbl(ref this, ref x);
            }
            public void Add(G1 x, G1 y)
            {
                mclBnG1_add(ref this, ref x, ref y);
            }
            public void Sub(G1 x, G1 y)
            {
                mclBnG1_sub(ref this, ref x, ref y);
            }
            public void Mul(G1 x, Fr y)
            {
                mclBnG1_mul(ref this, ref x, ref y);
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct G2 {
            private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
            private ulong v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23;
            public void Clear()
            {
                mclBnG2_clear(ref this);
            }
            public void setStr(String s, int ioMode)
            {
                if (mclBnG2_setStr(ref this, s, s.Length, ioMode) != 0) {
                    throw new ArgumentException("mclBnG2_setStr:" + s);
                }
            }
            public bool IsValid()
            {
                return mclBnG2_isValid(ref this) == 1;
            }
            public bool Equals(G2 rhs)
            {
                return mclBnG2_isEqual(ref this, ref rhs) == 1;
            }
            public bool IsZero()
            {
                return mclBnG2_isZero(ref this) == 1;
            }
            public void HashAndMapTo(String s)
            {
                if (mclBnG2_hashAndMapTo(ref this, s, s.Length) != 0) {
                    throw new ArgumentException("mclBnG2_hashAndMapTo:" + s);
                }
            }
            public string GetStr(int ioMode)
            {
                StringBuilder sb = new StringBuilder(1024);
                long size = mclBnG2_getStr(sb, sb.Capacity, ref this, ioMode);
                if (size == 0) {
                    throw new InvalidOperationException("mclBnG2_getStr:");
                }
                return sb.ToString();
            }
            public void Neg(G2 x)
            {
                mclBnG2_neg(ref this, ref x);
            }
            public void Dbl(G2 x)
            {
                mclBnG2_dbl(ref this, ref x);
            }
            public void Add(G2 x, G2 y)
            {
                mclBnG2_add(ref this, ref x, ref y);
            }
            public void Sub(G2 x, G2 y)
            {
                mclBnG2_sub(ref this, ref x, ref y);
            }
            public void Mul(G2 x, Fr y)
            {
                mclBnG2_mul(ref this, ref x, ref y);
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct GT {
            private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
            private ulong v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23;
            private ulong v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35;
            private ulong v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47;
            public void Clear()
            {
                mclBnGT_clear(ref this);
            }
            public void setStr(String s, int ioMode)
            {
                if (mclBnGT_setStr(ref this, s, s.Length, ioMode) != 0) {
                    throw new ArgumentException("mclBnGT_setStr:" + s);
                }
            }
            public bool Equals(GT rhs)
            {
                return mclBnGT_isEqual(ref this, ref rhs) == 1;
            }
            public bool IsZero()
            {
                return mclBnGT_isZero(ref this) == 1;
            }
            public bool IsOne()
            {
                return mclBnGT_isOne(ref this) == 1;
            }
            public string GetStr(int ioMode)
            {
                StringBuilder sb = new StringBuilder(1024);
                long size = mclBnGT_getStr(sb, sb.Capacity, ref this, ioMode);
                if (size == 0) {
                    throw new InvalidOperationException("mclBnGT_getStr:");
                }
                return sb.ToString();
            }
            public void Neg(GT x)
            {
                mclBnGT_neg(ref this, ref x);
            }
            public void Inv(GT x)
            {
                mclBnGT_inv(ref this, ref x);
            }
            public void Add(GT x, GT y)
            {
                mclBnGT_add(ref this, ref x, ref y);
            }
            public void Sub(GT x, GT y)
            {
                mclBnGT_sub(ref this, ref x, ref y);
            }
            public void Mul(GT x, GT y)
            {
                mclBnGT_mul(ref this, ref x, ref y);
            }
            public void Div(GT x, GT y)
            {
                mclBnGT_div(ref this, ref x, ref y);
            }
            public static GT operator -(GT x)
            {
                GT y = new GT();
                y.Neg(x);
                return y;
            }
            public static GT operator +(GT x, GT y)
            {
                GT z = new GT();
                z.Add(x, y);
                return z;
            }
            public static GT operator -(GT x, GT y)
            {
                GT z = new GT();
                z.Sub(x, y);
                return z;
            }
            public static GT operator *(GT x, GT y)
            {
                GT z = new GT();
                z.Mul(x, y);
                return z;
            }
            public static GT operator /(GT x, GT y)
            {
                GT z = new GT();
                z.Div(x, y);
                return z;
            }
            public void Pow(GT x, Fr y)
            {
                mclBnGT_pow(ref this, ref x, ref y);
            }
            public void Pairing(G1 x, G2 y)
            {
                mclBn_pairing(ref this, ref x, ref y);
            }
            public void FinalExp(GT x)
            {
                mclBn_finalExp(ref this, ref x);
            }
            public void MillerLoop(G1 x, G2 y)
            {
                mclBn_millerLoop(ref this, ref x, ref y);
            }
        }
    }
}