diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 8a7b6fe..3897bd9 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; @@ -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 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); @@ -86,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); @@ -215,6 +218,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); @@ -239,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); @@ -291,6 +310,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 +460,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 +683,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 { diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index 832d969..19b0e2f 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() { @@ -196,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() { @@ -228,9 +256,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();