From 9325c347831e8ddcd4b0ad5b0accd1ec9ea36459 Mon Sep 17 00:00:00 2001 From: Valdas Rakutis Date: Sun, 14 Mar 2021 17:32:20 +0200 Subject: [PATCH 1/4] [cs] add missing functions, some arithmetic operator overloads, static factory --- ffi/cs/mcl/mcl.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 8a7b6fe..133186d 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -19,6 +19,7 @@ namespace mcl { [DllImport(dllName)] public static extern int mclBn_init(int curve, int compiledTimeVar); [DllImport(dllName)] public static extern void mclBn_setETHserialization(int enable); [DllImport(dllName)] public static extern int mclBn_setMapToMode(int mode); + [DllImport(dllName)] public static extern int mclBn_FrEvaluatePolynomial(ref Fr z, [In] Fr[] poly, long bufSize, in Fr y); [DllImport(dllName)] public static extern void mclBnFr_clear(ref Fr x); [DllImport(dllName)] public static extern void mclBnFr_setInt(ref Fr y, int x); [DllImport(dllName)] public static extern int mclBnFr_setStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode); @@ -72,6 +73,7 @@ namespace mcl { [DllImport(dllName)] public static extern void mclBnG1_add(ref G1 z, in G1 x, in G1 y); [DllImport(dllName)] public static extern void mclBnG1_sub(ref G1 z, in G1 x, in G1 y); [DllImport(dllName)] public static extern void mclBnG1_mul(ref G1 z, in G1 x, in Fr y); + [DllImport(dllName)] public static extern void mclBnG1_mulVec(ref G1 x, [In]G1[] vec1, [In]Fr[] vec2, long bufSize); [DllImport(dllName)] public static extern void mclBnG2_clear(ref G2 x); [DllImport(dllName)] public static extern int mclBnG2_setStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode); @@ -291,6 +293,14 @@ namespace mcl { [StructLayout(LayoutKind.Sequential)] public struct Fr { private U128 v0, v1; + public static Fr One() + { + var fr = new Fr(); + fr.SetInt(1); + + return fr; + } + public static Fr Zero() => new Fr(); public void Clear() { mclBnFr_clear(ref this); @@ -433,6 +443,13 @@ namespace mcl { [StructLayout(LayoutKind.Sequential)] public struct Fp { private U128 v0, v1, v2; + public static Fp One() + { + var fp = new Fp(); + fp.SetInt(1); + return fp; + } + public static Fp Zero() => new Fp(); public void Clear() { mclBnFp_clear(ref this); @@ -649,6 +666,30 @@ namespace mcl { { MCL.Mul(ref this, x, y); } + public static G1 operator -(in G1 x) + { + var result = new G1(); + result.Neg(x); + return result; + } + public static G1 operator +(in G1 left, in G1 right) + { + var result = new G1(); + result.Add(left, right); + return result; + } + public static G1 operator -(in G1 left, in G1 right) + { + var result = new G1(); + result.Sub(left, right); + return result; + } + public static G1 operator *(in G1 left, in Fr right) + { + var result = new G1(); + result.Mul(left, right); + return result; + } } [StructLayout(LayoutKind.Sequential)] public struct G2 { From c2d843e6e0d62d5a00590cadadd3e2d17b260832 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 15 Mar 2021 20:49:05 +0900 Subject: [PATCH 2/4] [cs] fix correct value of test for mapToG1 --- ffi/cs/test/test.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index 832d969..c9393e3 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -228,9 +228,8 @@ namespace mcl { var tbl = new[] { new { msg = "asdf", - dst = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_", - x = "bc73d15443009a8ff2ddce864136d892274dd8365c60d0d2d44cc543387348e366a8f1e1401427e37743c29ed2c939a", - y = "101e26428a1b78c05458cb1cc37d2d87876ad3437096d2827f376702d4451667fe1fa82e82795495d33d466133ed1862", + x = "a72df17570d0eb81260042edbea415ad49bdb94a1bc1ce9d1bf147d0d48268170764bb513a3b994d662e1faba137106", + y = "122b77eca1ed58795b7cd456576362f4f7bd7a572a29334b4817898a42414d31e9c0267f2dc481a4daf8bcf4a460322", }, }; G1 P = new G1(); From 9ee82069bf0c12083ff5bbb6dbb3f9d2de59f0ab Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 15 Mar 2021 21:13:04 +0900 Subject: [PATCH 3/4] [cs] add MulVec for G1 --- ffi/cs/mcl/mcl.cs | 12 ++++++++++-- ffi/cs/test/test.cs | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 133186d..24fc55d 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text; using System.Runtime.InteropServices; @@ -73,7 +73,7 @@ namespace mcl { [DllImport(dllName)] public static extern void mclBnG1_add(ref G1 z, in G1 x, in G1 y); [DllImport(dllName)] public static extern void mclBnG1_sub(ref G1 z, in G1 x, in G1 y); [DllImport(dllName)] public static extern void mclBnG1_mul(ref G1 z, in G1 x, in Fr y); - [DllImport(dllName)] public static extern void mclBnG1_mulVec(ref G1 x, [In]G1[] vec1, [In]Fr[] vec2, long bufSize); + [DllImport(dllName)] public static extern void mclBnG1_mulVec(ref G1 z, [In]G1[] x, [In]Fr[] y, long n); [DllImport(dllName)] public static extern void mclBnG2_clear(ref G2 x); [DllImport(dllName)] public static extern int mclBnG2_setStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode); @@ -217,6 +217,14 @@ namespace mcl { { mclBnG1_normalize(ref y, x); } + public static void MulVec(ref G1 z, in G1[] x, in Fr[] y) + { + int n = x.Length; + if (n <= 0 || n != y.Length) { + throw new ArgumentException("bad length"); + } + mclBnG1_mulVec(ref z, x, y, (long)n); + } public static void Add(ref G2 z, in G2 x, in G2 y) { mclBnG2_add(ref z, x, y); diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index c9393e3..2972cc8 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -164,6 +164,20 @@ namespace mcl { Q.Deserialize(buf); assert("P == Q", P.Equals(Q)); } + { + const int n = 5; + G1[] xVec = new G1[n]; + Fr[] yVec = new Fr[n]; + P.Clear(); + for (int i = 0; i < n; i++) { + xVec[i].HashAndMapTo(i.ToString()); + yVec[i].SetByCSPRNG(); + Q.Mul(xVec[i], yVec[i]); + P.Add(P, Q); + } + MulVec(ref Q, xVec, yVec); + assert("mulVecG1", P.Equals(Q)); + } } static void TestG2() { From 67cea40560814a411ac1284308748b28066f58d5 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 15 Mar 2021 21:13:19 +0900 Subject: [PATCH 4/4] [cs] add MulVec for G2 --- ffi/cs/mcl/mcl.cs | 9 +++++++++ ffi/cs/test/test.cs | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 24fc55d..3897bd9 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -88,6 +88,7 @@ namespace mcl { [DllImport(dllName)] public static extern void mclBnG2_add(ref G2 z, in G2 x, in G2 y); [DllImport(dllName)] public static extern void mclBnG2_sub(ref G2 z, in G2 x, in G2 y); [DllImport(dllName)] public static extern void mclBnG2_mul(ref G2 z, in G2 x, in Fr y); + [DllImport(dllName)] public static extern void mclBnG2_mulVec(ref G2 z, [In] G2[] x, [In] Fr[] y, long n); [DllImport(dllName)] public static extern void mclBnGT_clear(ref GT x); [DllImport(dllName)] public static extern int mclBnGT_setStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode); @@ -249,6 +250,14 @@ namespace mcl { { mclBnG2_normalize(ref y, x); } + public static void MulVec(ref G2 z, in G2[] x, in Fr[] y) + { + int n = x.Length; + if (n <= 0 || n != y.Length) { + throw new ArgumentException("bad length"); + } + mclBnG2_mulVec(ref z, x, y, (long)n); + } public static void Add(ref GT z, in GT x, in GT y) { mclBnGT_add(ref z, x, y); diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index 2972cc8..19b0e2f 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -210,6 +210,20 @@ namespace mcl { Q.Deserialize(buf); assert("P == Q", P.Equals(Q)); } + { + const int n = 5; + G2[] xVec = new G2[n]; + Fr[] yVec = new Fr[n]; + P.Clear(); + for (int i = 0; i < n; i++) { + xVec[i].HashAndMapTo(i.ToString()); + yVec[i].SetByCSPRNG(); + Q.Mul(xVec[i], yVec[i]); + P.Add(P, Q); + } + MulVec(ref Q, xVec, yVec); + assert("mulVecG2", P.Equals(Q)); + } } static void TestPairing() {