From 2a82e2531ab7a8912f6f589a18689eba5ce9aef0 Mon Sep 17 00:00:00 2001 From: Valdas Rakutis Date: Sat, 20 Mar 2021 13:35:21 +0200 Subject: [PATCH 1/4] [cs] add some arithmetic operator overloads for g2, static factory, fix serialization issue --- ffi/cs/mcl/mcl.cs | 50 ++++++++++++++++++++++++++++++++++++++++++--- ffi/cs/test/test.cs | 4 ++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 3897bd9..99687a3 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -606,11 +606,20 @@ namespace mcl { [StructLayout(LayoutKind.Sequential)] public struct G1 { public Fp x, y, z; + public static G1 Zero() + { + var g1 = new G1(); + g1.x.SetInt(1); + g1.y.SetInt(1); + g1.z.SetInt(0); + + return g1; + } public void Clear() { mclBnG1_clear(ref this); } - public void SetStr(String s, int ioMode) + public void SetStr(string s, int ioMode) { if (mclBnG1_setStr(ref this, s, s.Length, ioMode) != 0) { throw new ArgumentException("mclBnG1_setStr:" + s); @@ -711,11 +720,22 @@ namespace mcl { [StructLayout(LayoutKind.Sequential)] public struct G2 { public Fp2 x, y, z; + public static G2 Zero() + { + var g2 = new G2(); + g2.x.a.SetInt(1); + g2.x.b.SetInt(0); + g2.y.a.SetInt(1); + g2.y.b.SetInt(0); + g2.x.a.SetInt(0); + g2.x.a.SetInt(0); + return g2; + } public void Clear() { mclBnG2_clear(ref this); } - public void SetStr(String s, int ioMode) + public void SetStr(string s, int ioMode) { if (mclBnG2_setStr(ref this, s, s.Length, ioMode) != 0) { throw new ArgumentException("mclBnG2_setStr:" + s); @@ -788,6 +808,30 @@ namespace mcl { { MCL.Mul(ref this, x, y); } + public static G2 operator -(in G2 x) + { + var result = new G2(); + result.Neg(x); + return result; + } + public static G2 operator +(in G2 left, in G2 right) + { + var result = new G2(); + result.Add(left, right); + return result; + } + public static G2 operator -(in G2 left, in G2 right) + { + var result = new G2(); + result.Sub(left, right); + return result; + } + public static G2 operator *(in G2 left, in Fr right) + { + var result = new G2(); + result.Mul(left, right); + return result; + } } [StructLayout(LayoutKind.Sequential)] public struct GT { @@ -816,7 +860,7 @@ namespace mcl { } public string GetStr(int ioMode) { - StringBuilder sb = new StringBuilder(1024); + StringBuilder sb = new StringBuilder(2048); long size = mclBnGT_getStr(sb, sb.Capacity, this, ioMode); if (size == 0) { throw new InvalidOperationException("mclBnGT_getStr:"); diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index 19b0e2f..eb5318d 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -178,6 +178,8 @@ namespace mcl { MulVec(ref Q, xVec, yVec); assert("mulVecG1", P.Equals(Q)); } + G1 W = G1.Zero(); + assert("W.IsZero", W.IsZero()); } static void TestG2() { @@ -224,6 +226,8 @@ namespace mcl { MulVec(ref Q, xVec, yVec); assert("mulVecG2", P.Equals(Q)); } + G2 W = G2.Zero(); + assert("W.IsZero", W.IsZero()); } static void TestPairing() { From aa3e88e54e3e98c24a020c95dac3aae4e6fb164a Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 24 Mar 2021 10:36:14 +0900 Subject: [PATCH 2/4] [cs] check that default value is zero --- ffi/cs/test/test.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index eb5318d..d3c023b 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -47,6 +47,7 @@ namespace mcl { { Console.WriteLine("TestFr"); Fr x = new Fr(); + assert("x.isZero", x.IsZero()); x.Clear(); assert("0", x.GetStr(10) == "0"); assert("0.IzZero", x.IsZero()); @@ -92,6 +93,7 @@ namespace mcl { { Console.WriteLine("TestFp"); Fp x = new Fp(); + assert("x.isZero", x.IsZero()); x.Clear(); assert("0", x.GetStr(10) == "0"); assert("0.IzZero", x.IsZero()); @@ -137,6 +139,7 @@ namespace mcl { { Console.WriteLine("TestG1"); G1 P = new G1(); + assert("P.isZero", P.IsZero()); P.Clear(); assert("P.IsValid", P.IsValid()); assert("P.IsZero", P.IsZero()); @@ -185,6 +188,7 @@ namespace mcl { { Console.WriteLine("TestG2"); G2 P = new G2(); + assert("P.isZero", P.IsZero()); P.Clear(); assert("P is valid", P.IsValid()); assert("P is zero", P.IsZero()); From 19aad60e2af9f277e18ed7661235bcdde089b976 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 24 Mar 2021 10:37:41 +0900 Subject: [PATCH 3/4] [cs] remove unnecessary set --- ffi/cs/mcl/mcl.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 99687a3..e508b8b 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -609,10 +609,6 @@ namespace mcl { public static G1 Zero() { var g1 = new G1(); - g1.x.SetInt(1); - g1.y.SetInt(1); - g1.z.SetInt(0); - return g1; } public void Clear() @@ -723,12 +719,6 @@ namespace mcl { public static G2 Zero() { var g2 = new G2(); - g2.x.a.SetInt(1); - g2.x.b.SetInt(0); - g2.y.a.SetInt(1); - g2.y.b.SetInt(0); - g2.x.a.SetInt(0); - g2.x.a.SetInt(0); return g2; } public void Clear() From 1456a13f4c4572438dfd7a186309318c860db8d6 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 24 Mar 2021 10:38:01 +0900 Subject: [PATCH 4/4] [cs] rename variables --- ffi/cs/mcl/mcl.cs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index e508b8b..0202a66 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -312,10 +312,9 @@ namespace mcl { private U128 v0, v1; public static Fr One() { - var fr = new Fr(); - fr.SetInt(1); - - return fr; + var x = new Fr(); + x.SetInt(1); + return x; } public static Fr Zero() => new Fr(); public void Clear() @@ -462,9 +461,9 @@ namespace mcl { private U128 v0, v1, v2; public static Fp One() { - var fp = new Fp(); - fp.SetInt(1); - return fp; + var x = new Fp(); + x.SetInt(1); + return x; } public static Fp Zero() => new Fp(); public void Clear() @@ -804,23 +803,23 @@ namespace mcl { result.Neg(x); return result; } - public static G2 operator +(in G2 left, in G2 right) + public static G2 operator +(in G2 x, in G2 y) { - var result = new G2(); - result.Add(left, right); - return result; + var z = new G2(); + z.Add(x, y); + return z; } - public static G2 operator -(in G2 left, in G2 right) + public static G2 operator -(in G2 x, in G2 y) { - var result = new G2(); - result.Sub(left, right); - return result; + var z = new G2(); + z.Sub(x, y); + return z; } - public static G2 operator *(in G2 left, in Fr right) + public static G2 operator *(in G2 x, in Fr y) { - var result = new G2(); - result.Mul(left, right); - return result; + var z = new G2(); + z.Mul(x, y); + return z; } } [StructLayout(LayoutKind.Sequential)]