aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-03-06 04:52:07 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-03-06 04:52:07 +0800
commit61b26942f2d59a10750da26c04db7c0eb8ab0f7c (patch)
tree7b6b2513dbab902fc5d40dcdedb5331af30ff567
parentca9d69fe32427bed60a48acd2eebac60a29e1a60 (diff)
downloaddexon-mcl-61b26942f2d59a10750da26c04db7c0eb8ab0f7c.tar.gz
dexon-mcl-61b26942f2d59a10750da26c04db7c0eb8ab0f7c.tar.zst
dexon-mcl-61b26942f2d59a10750da26c04db7c0eb8ab0f7c.zip
split program.cs to bn256.cs and bn256_test.cs
-rw-r--r--ffi/cs/Program.cs561
-rw-r--r--ffi/cs/Properties/AssemblyInfo.cs8
-rw-r--r--ffi/cs/bn256.cs479
-rw-r--r--ffi/cs/bn256.csproj (renamed from ffi/cs/pairing.csproj)11
-rw-r--r--ffi/cs/bn256.sln (renamed from ffi/cs/pairing.sln)2
-rw-r--r--ffi/cs/bn256_test.cs146
6 files changed, 636 insertions, 571 deletions
diff --git a/ffi/cs/Program.cs b/ffi/cs/Program.cs
deleted file mode 100644
index 853f8ca..0000000
--- a/ffi/cs/Program.cs
+++ /dev/null
@@ -1,561 +0,0 @@
-using System;
-using System.Text;
-using System.Runtime.InteropServices;
-
-namespace mcl {
- class BN256 {
- [DllImport("bn256_if.dll")] public static extern int BN256_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name);
- [DllImport("bn256_if.dll")] public static extern int BN256_init();
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_clear([Out] Fr x);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_setInt([Out] Fr y, int x);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_setStr([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_isValid([In] Fr x);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_isSame([In] Fr x, [In] Fr y);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_isZero([In] Fr x);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_isOne([In] Fr x);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_setRand([Out] Fr x);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_setMsg([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_Fr_getStr([Out]StringBuilder buf, long maxBufSize, [In] Fr x);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_neg([Out] Fr y, [In] Fr x);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_inv([Out] Fr y, [In] Fr x);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_add([Out] Fr z, [In] Fr x, [In] Fr y);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_sub([Out] Fr z, [In] Fr x, [In] Fr y);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_mul([Out] Fr z, [In] Fr x, [In] Fr y);
- [DllImport("bn256_if.dll")] public static extern void BN256_Fr_div([Out] Fr z, [In] Fr x, [In] Fr y);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_clear([Out] G1 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_setStr([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_isValid([In] G1 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_isSame([In] G1 x, [In] G1 y);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_isZero([In] G1 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_hashAndMapTo([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_G1_getStr([Out]StringBuilder buf, long maxBufSize, [In] G1 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_neg([Out] G1 y, [In] G1 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_dbl([Out] G1 y, [In] G1 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_add([Out] G1 z, [In] G1 x, [In] G1 y);
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_sub([Out] G1 z, [In] G1 x, [In] G1 y);
- [DllImport("bn256_if.dll")] public static extern void BN256_G1_mul([Out] G1 z, [In] G1 x, [In] Fr y);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_clear([Out] G2 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_setStr([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_isValid([In] G2 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_isSame([In] G2 x, [In] G2 y);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_isZero([In] G2 x);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_hashAndMapTo([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_G2_getStr([Out]StringBuilder buf, long maxBufSize, [In] G2 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_neg([Out] G2 y, [In] G2 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_dbl([Out] G2 y, [In] G2 x);
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_add([Out] G2 z, [In] G2 x, [In] G2 y);
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_sub([Out] G2 z, [In] G2 x, [In] G2 y);
- [DllImport("bn256_if.dll")] public static extern void BN256_G2_mul([Out] G2 z, [In] G2 x, [In] Fr y);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_clear([Out] GT x);
- [DllImport("bn256_if.dll")] public static extern int BN256_GT_setStr([Out] GT x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256_if.dll")] public static extern int BN256_GT_isSame([In] GT x, [In] GT y);
- [DllImport("bn256_if.dll")] public static extern int BN256_GT_isZero([In] GT x);
- [DllImport("bn256_if.dll")] public static extern int BN256_GT_isOne([In] GT x);
- [DllImport("bn256_if.dll")] public static extern int BN256_GT_getStr([Out]StringBuilder buf, long maxBufSize, [In] GT x);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_neg([Out] GT y, [In] GT x);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_inv([Out] GT y, [In] GT x);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_add([Out] GT z, [In] GT x, [In] GT y);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_sub([Out] GT z, [In] GT x, [In] GT y);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_mul([Out] GT z, [In] GT x, [In] GT y);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_div([Out] GT z, [In] GT x, [In] GT y);
-
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_finalExp([Out] GT y, [In] GT x);
- [DllImport("bn256_if.dll")] public static extern void BN256_GT_pow([Out] GT z, [In] GT x, [In] Fr y);
- [DllImport("bn256_if.dll")] public static extern void BN256_pairing([Out] GT z, [In] G1 x, [In] G2 y);
- [DllImport("bn256_if.dll")] public static extern void BN256_millerLoop([Out] GT z, [In] G1 x, [In] G2 y);
-
- [StructLayout(LayoutKind.Sequential)]
- public class Fr {
- private ulong v0, v1, v2, v3;
- public void Clear()
- {
- BN256_Fr_clear(this);
- }
- public void SetInt(int x)
- {
- BN256_Fr_setInt(this, x);
- }
- public Fr Clone()
- {
- return (Fr)MemberwiseClone();
- }
- public void SetStr(string s)
- {
- if (BN256_Fr_setStr(this, s) != 0) {
- throw new ArgumentException("BN256_Fr_setStr", s);
- }
- }
- public bool IsValid()
- {
- return BN256_Fr_isValid(this) == 1;
- }
- public bool Equals(Fr rhs)
- {
- return BN256_Fr_isSame(this, rhs) == 1;
- }
- public bool IsZero()
- {
- return BN256_Fr_isZero(this) == 1;
- }
- public bool IsOne()
- {
- return BN256_Fr_isOne(this) == 1;
- }
- public void SetRand()
- {
- BN256_Fr_setRand(this);
- }
- public void SetMsg(String s)
- {
- BN256_Fr_setMsg(this, s);
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1024);
- if (BN256_Fr_getStr(sb, sb.Capacity + 1, this) != 0) {
- throw new ArgumentException("BN256_Fr_getStr");
- }
- return sb.ToString();
- }
- public static void Neg(Fr y, Fr x)
- {
- BN256_Fr_neg(y, x);
- }
- public static void Inv(Fr y, Fr x)
- {
- BN256_Fr_inv(y, x);
- }
- public static void Add(Fr z, Fr y, Fr x)
- {
- BN256_Fr_add(z, y, x);
- }
- public static void Sub(Fr z, Fr y, Fr x)
- {
- BN256_Fr_sub(z, y, x);
- }
- public static void Mul(Fr z, Fr y, Fr x)
- {
- BN256_Fr_mul(z, y, x);
- }
- public static void Div(Fr z, Fr y, Fr x)
- {
- BN256_Fr_div(z, y, x);
- }
- public static Fr operator-(Fr x)
- {
- Fr y = new Fr();
- Neg(y, x);
- return y;
- }
- public static Fr operator+(Fr x, Fr y)
- {
- Fr z = new Fr();
- Add(z, x, y);
- return z;
- }
- public static Fr operator-(Fr x, Fr y)
- {
- Fr z = new Fr();
- Sub(z, x, y);
- return z;
- }
- public static Fr operator*(Fr x, Fr y)
- {
- Fr z = new Fr();
- Mul(z, x, y);
- return z;
- }
- public static Fr operator/(Fr x, Fr y)
- {
- Fr z = new Fr();
- Div(z, x, y);
- return z;
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class G1 {
- private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
- public void Clear()
- {
- BN256_G1_clear(this);
- }
- public void setStr(String s)
- {
- if (BN256_G1_setStr(this, s) != 0) {
- throw new ArgumentException("BN256_G1_setStr", s);
- }
- }
- public G1 Clone()
- {
- return (G1)MemberwiseClone();
- }
- public bool IsValid()
- {
- return BN256_G1_isValid(this) == 1;
- }
- public bool Equals(G1 rhs)
- {
- return BN256_G1_isSame(this, rhs) == 1;
- }
- public bool IsZero()
- {
- return BN256_G1_isZero(this) == 1;
- }
- public void HashAndMapTo(String s)
- {
- if (BN256_G1_hashAndMapTo(this, s) != 0) {
- throw new ArgumentException("BN256_G1_hashAndMapTo", s);
- }
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1024);
- if (BN256_G1_getStr(sb, sb.Capacity + 1, this) != 0) {
- throw new ArgumentException("BN256_G1_getStr");
- }
- return sb.ToString();
- }
- public static void Neg(G1 y, G1 x)
- {
- BN256_G1_neg(y, x);
- }
- public static void Dbl(G1 y, G1 x)
- {
- BN256_G1_dbl(y, x);
- }
- public static void Add(G1 z, G1 y, G1 x)
- {
- BN256_G1_add(z, y, x);
- }
- public static void Sub(G1 z, G1 y, G1 x)
- {
- BN256_G1_sub(z, y, x);
- }
- public static void Mul(G1 z, G1 y, Fr x)
- {
- BN256_G1_mul(z, y, x);
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class 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()
- {
- BN256_G2_clear(this);
- }
- public void setStr(String s)
- {
- if (BN256_G2_setStr(this, s) != 0) {
- throw new ArgumentException("BN256_G2_setStr", s);
- }
- }
- public G2 Clone()
- {
- return (G2)MemberwiseClone();
- }
- public bool IsValid()
- {
- return BN256_G2_isValid(this) == 1;
- }
- public bool Equals(G2 rhs)
- {
- return BN256_G2_isSame(this, rhs) == 1;
- }
- public bool IsZero()
- {
- return BN256_G2_isZero(this) == 1;
- }
- public void HashAndMapTo(String s)
- {
- if (BN256_G2_hashAndMapTo(this, s) != 0) {
- throw new ArgumentException("BN256_G2_hashAndMapTo", s);
- }
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1024);
- if (BN256_G2_getStr(sb, sb.Capacity + 1, this) != 0) {
- throw new ArgumentException("BN256_G2_getStr");
- }
- return sb.ToString();
- }
- public static void Neg(G2 y, G2 x)
- {
- BN256_G2_neg(y, x);
- }
- public static void Dbl(G2 y, G2 x)
- {
- BN256_G2_dbl(y, x);
- }
- public static void Add(G2 z, G2 y, G2 x)
- {
- BN256_G2_add(z, y, x);
- }
- public static void Sub(G2 z, G2 y, G2 x)
- {
- BN256_G2_sub(z, y, x);
- }
- public static void Mul(G2 z, G2 y, Fr x)
- {
- BN256_G2_mul(z, y, x);
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class 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()
- {
- BN256_GT_clear(this);
- }
- public void setStr(String s)
- {
- if (BN256_GT_setStr(this, s) != 0) {
- throw new ArgumentException("BN256_GT_setStr", s);
- }
- }
- public GT Clone()
- {
- return (GT)MemberwiseClone();
- }
- public bool Equals(GT rhs)
- {
- return BN256_GT_isSame(this, rhs) == 1;
- }
- public bool IsZero()
- {
- return BN256_GT_isZero(this) == 1;
- }
- public bool IsOne()
- {
- return BN256_GT_isOne(this) == 1;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1024);
- if (BN256_GT_getStr(sb, sb.Capacity + 1, this) != 0) {
- throw new ArgumentException("BN256_GT_getStr");
- }
- return sb.ToString();
- }
- public static void Neg(GT y, GT x)
- {
- BN256_GT_neg(y, x);
- }
- public static void Inv(GT y, GT x)
- {
- BN256_GT_inv(y, x);
- }
- public static void Add(GT z, GT y, GT x)
- {
- BN256_GT_add(z, y, x);
- }
- public static void Sub(GT z, GT y, GT x)
- {
- BN256_GT_sub(z, y, x);
- }
- public static void Mul(GT z, GT y, GT x)
- {
- BN256_GT_mul(z, y, x);
- }
- public static void Div(GT z, GT y, GT x)
- {
- BN256_GT_div(z, y, x);
- }
- public static GT operator-(GT x)
- {
- GT y = new GT();
- Neg(y, x);
- return y;
- }
- public static GT operator+(GT x, GT y)
- {
- GT z = new GT();
- Add(z, x, y);
- return z;
- }
- public static GT operator-(GT x, GT y)
- {
- GT z = new GT();
- Sub(z, x, y);
- return z;
- }
- public static GT operator*(GT x, GT y)
- {
- GT z = new GT();
- Mul(z, x, y);
- return z;
- }
- public static GT operator/(GT x, GT y)
- {
- GT z = new GT();
- Div(z, x, y);
- return z;
- }
- public static void FinalExp(GT y, GT x)
- {
- BN256_GT_finalExp(y, x);
- }
- public static void Pow(GT z, GT x, Fr y)
- {
- BN256_GT_pow(z, x, y);
- }
- }
- public static void Pairing(GT z, G1 x, G2 y)
- {
- BN256_pairing(z, x, y);
- }
- public static void MillerLoop(GT z, G1 x, G2 y)
- {
- BN256_millerLoop(z, x, y);
- }
-
- static void Main(string[] args)
- {
- try {
- Console.WriteLine("64bit = {0}", System.Environment.Is64BitProcess);
- int ret;
- ret = BN256_init();
- Console.WriteLine("ret= {0}", ret);
- ret = BN256_setErrFile("c:/tmp/abc.txt");
- Console.WriteLine("ret= {0}", ret);
- TestFr();
- TestG1();
- TestG2();
- TestPairing();
- } catch (Exception e) {
- Console.WriteLine("ERR={0}", e);
- }
- }
- static void TestFr()
- {
- Console.WriteLine("TestFr");
- Fr x = new Fr();
- x.Clear();
- Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
- x.SetInt(1);
- Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
- x.SetInt(3);
- Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
- x.SetInt(-5);
- Console.WriteLine("x = {0}", x);
- x = -x;
- Console.WriteLine("x = {0}", x);
- x.SetInt(4);
- x = x * x;
- Console.WriteLine("x = {0}", x);
- Fr y;// = new Fr();
-// y = x;
- y = x.Clone();
- Console.WriteLine("y = {0}", y);
- Console.WriteLine("x == y {0}", x.Equals(y));
- x.SetInt(123);
- Console.WriteLine("y = {0}", y);
- Console.WriteLine("x == y {0}", x.Equals(y));
- try {
- x.SetStr("1234567891234x");
- Console.WriteLine("x = {0}", x);
- } catch (Exception e) {
- Console.WriteLine("exception test {0}", e);
- }
- x.SetStr("1234567891234");
- Console.WriteLine("x = {0}", x);
- }
- static void TestG1()
- {
- Console.WriteLine("TestG1");
- G1 P = new G1();
- P.Clear();
- Console.WriteLine("P = {0}", P);
- Console.WriteLine("P is valid {0}", P.IsValid());
- Console.WriteLine("P is zero {0}", P.IsZero());
- P.HashAndMapTo("abc");
- Console.WriteLine("P = {0}", P);
- Console.WriteLine("P is valid {0}", P.IsValid());
- Console.WriteLine("P is zero {0}", P.IsZero());
- G1 Q = new G1();
- G1.Neg(Q, P);
- Console.WriteLine("Q = {0}", Q);
- G1.Add(Q, Q, P);
- Console.WriteLine("Q = {0}", Q);
- G1.Dbl(Q, P);
- G1 R = new G1();
- G1.Add(R, P, P);
- Console.WriteLine("P == R {0}", P.Equals(P));
- Console.WriteLine("P == R {0}", P.Equals(R));
- Console.WriteLine("Q == R {0}", Q.Equals(R));
- Console.WriteLine("Q = {0}", Q);
- Console.WriteLine("R = {0}", R);
- Fr x = new Fr();
- x.SetInt(3);
- G1.Add(R, R, P);
- G1.Mul(Q, P, x);
- Console.WriteLine("Q == R {0}", Q.Equals(R));
- }
- static void TestG2()
- {
- Console.WriteLine("TestG2");
- G2 P = new G2();
- P.Clear();
- Console.WriteLine("P = {0}", P);
- Console.WriteLine("P is valid {0}", P.IsValid());
- Console.WriteLine("P is zero {0}", P.IsZero());
- P.HashAndMapTo("abc");
- Console.WriteLine("P = {0}", P);
- Console.WriteLine("P is valid {0}", P.IsValid());
- Console.WriteLine("P is zero {0}", P.IsZero());
- G2 Q = new G2();
- G2.Neg(Q, P);
- Console.WriteLine("Q = {0}", Q);
- G2.Add(Q, Q, P);
- Console.WriteLine("Q = {0}", Q);
- G2.Dbl(Q, P);
- G2 R = new G2();
- G2.Add(R, P, P);
- Console.WriteLine("P == R {0}", P.Equals(P));
- Console.WriteLine("P == R {0}", P.Equals(R));
- Console.WriteLine("Q == R {0}", Q.Equals(R));
- Console.WriteLine("Q = {0}", Q);
- Console.WriteLine("R = {0}", R);
- Fr x = new Fr();
- x.SetInt(3);
- G2.Add(R, R, P);
- G2.Mul(Q, P, x);
- Console.WriteLine("Q == R {0}", Q.Equals(R));
- }
- static void TestPairing()
- {
- Console.WriteLine("TestG2");
- G1 P = new G1();
- P.setStr("1 -1 1");
- G2 Q = new G2();
- Q.HashAndMapTo("1");
- Fr a = new Fr();
- Fr b = new Fr();
- a.SetStr("12345678912345673453");
- b.SetStr("230498230982394243424");
- G1 aP = new G1();
- G2 bQ = new G2();
- G1.Mul(aP, P, a);
- G2.Mul(bQ, Q, b);
- GT e1 = new GT();
- GT e2 = new GT();
- GT e3 = new GT();
- Pairing(e1, P, Q);
- Pairing(e2, aP, Q);
- GT.Pow(e3, e1, a);
- Console.WriteLine("e2.Equals(e3) {0}", e2.Equals(e3));
- Pairing(e2, P, bQ);
- GT.Pow(e3, e1, b);
- Console.WriteLine("e2.Equals(e3) {0}", e2.Equals(e3));
- }
- }
-}
diff --git a/ffi/cs/Properties/AssemblyInfo.cs b/ffi/cs/Properties/AssemblyInfo.cs
index 0b87e98..8db58d7 100644
--- a/ffi/cs/Properties/AssemblyInfo.cs
+++ b/ffi/cs/Properties/AssemblyInfo.cs
@@ -5,16 +5,16 @@ using System.Runtime.InteropServices;
// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
// アセンブリに関連付けられている情報を変更するには、
// これらの属性値を変更してください。
-[assembly: AssemblyTitle("pairing")]
+[assembly: AssemblyTitle("bn256")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("pairing")]
+[assembly: AssemblyProduct("bn256")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
-// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから
+// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから
// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、
// その型の ComVisible 属性を true に設定してください。
[assembly: ComVisible(false)]
@@ -29,7 +29,7 @@ using System.Runtime.InteropServices;
// ビルド番号
// Revision
//
-// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
+// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
diff --git a/ffi/cs/bn256.cs b/ffi/cs/bn256.cs
new file mode 100644
index 0000000..cf87e72
--- /dev/null
+++ b/ffi/cs/bn256.cs
@@ -0,0 +1,479 @@
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace mcl {
+ class BN256 {
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_init();
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_clear([Out] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_setInt([Out] Fr y, int x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_setStr([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_isValid([In] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_isSame([In] Fr x, [In] Fr y);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_isZero([In] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_isOne([In] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_setRand([Out] Fr x);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_setMsg([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_Fr_getStr([Out]StringBuilder buf, long maxBufSize, [In] Fr x);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_neg([Out] Fr y, [In] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_inv([Out] Fr y, [In] Fr x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_add([Out] Fr z, [In] Fr x, [In] Fr y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_sub([Out] Fr z, [In] Fr x, [In] Fr y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_mul([Out] Fr z, [In] Fr x, [In] Fr y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_Fr_div([Out] Fr z, [In] Fr x, [In] Fr y);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_clear([Out] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_setStr([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_isValid([In] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_isSame([In] G1 x, [In] G1 y);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_isZero([In] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_hashAndMapTo([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G1_getStr([Out]StringBuilder buf, long maxBufSize, [In] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_neg([Out] G1 y, [In] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_dbl([Out] G1 y, [In] G1 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_add([Out] G1 z, [In] G1 x, [In] G1 y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_sub([Out] G1 z, [In] G1 x, [In] G1 y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G1_mul([Out] G1 z, [In] G1 x, [In] Fr y);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_clear([Out] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_setStr([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_isValid([In] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_isSame([In] G2 x, [In] G2 y);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_isZero([In] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_hashAndMapTo([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_G2_getStr([Out]StringBuilder buf, long maxBufSize, [In] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_neg([Out] G2 y, [In] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_dbl([Out] G2 y, [In] G2 x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_add([Out] G2 z, [In] G2 x, [In] G2 y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_sub([Out] G2 z, [In] G2 x, [In] G2 y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_G2_mul([Out] G2 z, [In] G2 x, [In] Fr y);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_clear([Out] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_GT_setStr([Out] GT x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_GT_isSame([In] GT x, [In] GT y);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_GT_isZero([In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_GT_isOne([In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern int BN256_GT_getStr([Out]StringBuilder buf, long maxBufSize, [In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_neg([Out] GT y, [In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_inv([Out] GT y, [In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_add([Out] GT z, [In] GT x, [In] GT y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_sub([Out] GT z, [In] GT x, [In] GT y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_mul([Out] GT z, [In] GT x, [In] GT y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_div([Out] GT z, [In] GT x, [In] GT y);
+
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_finalExp([Out] GT y, [In] GT x);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_GT_pow([Out] GT z, [In] GT x, [In] Fr y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_pairing([Out] GT z, [In] G1 x, [In] G2 y);
+ [DllImport("bn256_if.dll")]
+ public static extern void BN256_millerLoop([Out] GT z, [In] G1 x, [In] G2 y);
+
+ [StructLayout(LayoutKind.Sequential)]
+ public class Fr {
+ private ulong v0, v1, v2, v3;
+ public void Clear()
+ {
+ BN256_Fr_clear(this);
+ }
+ public void SetInt(int x)
+ {
+ BN256_Fr_setInt(this, x);
+ }
+ public Fr Clone()
+ {
+ return (Fr)MemberwiseClone();
+ }
+ public void SetStr(string s)
+ {
+ if (BN256_Fr_setStr(this, s) != 0) {
+ throw new ArgumentException("BN256_Fr_setStr", s);
+ }
+ }
+ public bool IsValid()
+ {
+ return BN256_Fr_isValid(this) == 1;
+ }
+ public bool Equals(Fr rhs)
+ {
+ return BN256_Fr_isSame(this, rhs) == 1;
+ }
+ public bool IsZero()
+ {
+ return BN256_Fr_isZero(this) == 1;
+ }
+ public bool IsOne()
+ {
+ return BN256_Fr_isOne(this) == 1;
+ }
+ public void SetRand()
+ {
+ BN256_Fr_setRand(this);
+ }
+ public void SetMsg(String s)
+ {
+ BN256_Fr_setMsg(this, s);
+ }
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ if (BN256_Fr_getStr(sb, sb.Capacity + 1, this) != 0) {
+ throw new ArgumentException("BN256_Fr_getStr");
+ }
+ return sb.ToString();
+ }
+ public static void Neg(Fr y, Fr x)
+ {
+ BN256_Fr_neg(y, x);
+ }
+ public static void Inv(Fr y, Fr x)
+ {
+ BN256_Fr_inv(y, x);
+ }
+ public static void Add(Fr z, Fr y, Fr x)
+ {
+ BN256_Fr_add(z, y, x);
+ }
+ public static void Sub(Fr z, Fr y, Fr x)
+ {
+ BN256_Fr_sub(z, y, x);
+ }
+ public static void Mul(Fr z, Fr y, Fr x)
+ {
+ BN256_Fr_mul(z, y, x);
+ }
+ public static void Div(Fr z, Fr y, Fr x)
+ {
+ BN256_Fr_div(z, y, x);
+ }
+ public static Fr operator -(Fr x)
+ {
+ Fr y = new Fr();
+ Neg(y, x);
+ return y;
+ }
+ public static Fr operator +(Fr x, Fr y)
+ {
+ Fr z = new Fr();
+ Add(z, x, y);
+ return z;
+ }
+ public static Fr operator -(Fr x, Fr y)
+ {
+ Fr z = new Fr();
+ Sub(z, x, y);
+ return z;
+ }
+ public static Fr operator *(Fr x, Fr y)
+ {
+ Fr z = new Fr();
+ Mul(z, x, y);
+ return z;
+ }
+ public static Fr operator /(Fr x, Fr y)
+ {
+ Fr z = new Fr();
+ Div(z, x, y);
+ return z;
+ }
+ }
+ [StructLayout(LayoutKind.Sequential)]
+ public class G1 {
+ private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
+ public void Clear()
+ {
+ BN256_G1_clear(this);
+ }
+ public void setStr(String s)
+ {
+ if (BN256_G1_setStr(this, s) != 0) {
+ throw new ArgumentException("BN256_G1_setStr", s);
+ }
+ }
+ public G1 Clone()
+ {
+ return (G1)MemberwiseClone();
+ }
+ public bool IsValid()
+ {
+ return BN256_G1_isValid(this) == 1;
+ }
+ public bool Equals(G1 rhs)
+ {
+ return BN256_G1_isSame(this, rhs) == 1;
+ }
+ public bool IsZero()
+ {
+ return BN256_G1_isZero(this) == 1;
+ }
+ public void HashAndMapTo(String s)
+ {
+ if (BN256_G1_hashAndMapTo(this, s) != 0) {
+ throw new ArgumentException("BN256_G1_hashAndMapTo", s);
+ }
+ }
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ if (BN256_G1_getStr(sb, sb.Capacity + 1, this) != 0) {
+ throw new ArgumentException("BN256_G1_getStr");
+ }
+ return sb.ToString();
+ }
+ public static void Neg(G1 y, G1 x)
+ {
+ BN256_G1_neg(y, x);
+ }
+ public static void Dbl(G1 y, G1 x)
+ {
+ BN256_G1_dbl(y, x);
+ }
+ public static void Add(G1 z, G1 y, G1 x)
+ {
+ BN256_G1_add(z, y, x);
+ }
+ public static void Sub(G1 z, G1 y, G1 x)
+ {
+ BN256_G1_sub(z, y, x);
+ }
+ public static void Mul(G1 z, G1 y, Fr x)
+ {
+ BN256_G1_mul(z, y, x);
+ }
+ }
+ [StructLayout(LayoutKind.Sequential)]
+ public class 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()
+ {
+ BN256_G2_clear(this);
+ }
+ public void setStr(String s)
+ {
+ if (BN256_G2_setStr(this, s) != 0) {
+ throw new ArgumentException("BN256_G2_setStr", s);
+ }
+ }
+ public G2 Clone()
+ {
+ return (G2)MemberwiseClone();
+ }
+ public bool IsValid()
+ {
+ return BN256_G2_isValid(this) == 1;
+ }
+ public bool Equals(G2 rhs)
+ {
+ return BN256_G2_isSame(this, rhs) == 1;
+ }
+ public bool IsZero()
+ {
+ return BN256_G2_isZero(this) == 1;
+ }
+ public void HashAndMapTo(String s)
+ {
+ if (BN256_G2_hashAndMapTo(this, s) != 0) {
+ throw new ArgumentException("BN256_G2_hashAndMapTo", s);
+ }
+ }
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ if (BN256_G2_getStr(sb, sb.Capacity + 1, this) != 0) {
+ throw new ArgumentException("BN256_G2_getStr");
+ }
+ return sb.ToString();
+ }
+ public static void Neg(G2 y, G2 x)
+ {
+ BN256_G2_neg(y, x);
+ }
+ public static void Dbl(G2 y, G2 x)
+ {
+ BN256_G2_dbl(y, x);
+ }
+ public static void Add(G2 z, G2 y, G2 x)
+ {
+ BN256_G2_add(z, y, x);
+ }
+ public static void Sub(G2 z, G2 y, G2 x)
+ {
+ BN256_G2_sub(z, y, x);
+ }
+ public static void Mul(G2 z, G2 y, Fr x)
+ {
+ BN256_G2_mul(z, y, x);
+ }
+ }
+ [StructLayout(LayoutKind.Sequential)]
+ public class 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()
+ {
+ BN256_GT_clear(this);
+ }
+ public void setStr(String s)
+ {
+ if (BN256_GT_setStr(this, s) != 0) {
+ throw new ArgumentException("BN256_GT_setStr", s);
+ }
+ }
+ public GT Clone()
+ {
+ return (GT)MemberwiseClone();
+ }
+ public bool Equals(GT rhs)
+ {
+ return BN256_GT_isSame(this, rhs) == 1;
+ }
+ public bool IsZero()
+ {
+ return BN256_GT_isZero(this) == 1;
+ }
+ public bool IsOne()
+ {
+ return BN256_GT_isOne(this) == 1;
+ }
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ if (BN256_GT_getStr(sb, sb.Capacity + 1, this) != 0) {
+ throw new ArgumentException("BN256_GT_getStr");
+ }
+ return sb.ToString();
+ }
+ public static void Neg(GT y, GT x)
+ {
+ BN256_GT_neg(y, x);
+ }
+ public static void Inv(GT y, GT x)
+ {
+ BN256_GT_inv(y, x);
+ }
+ public static void Add(GT z, GT y, GT x)
+ {
+ BN256_GT_add(z, y, x);
+ }
+ public static void Sub(GT z, GT y, GT x)
+ {
+ BN256_GT_sub(z, y, x);
+ }
+ public static void Mul(GT z, GT y, GT x)
+ {
+ BN256_GT_mul(z, y, x);
+ }
+ public static void Div(GT z, GT y, GT x)
+ {
+ BN256_GT_div(z, y, x);
+ }
+ public static GT operator -(GT x)
+ {
+ GT y = new GT();
+ Neg(y, x);
+ return y;
+ }
+ public static GT operator +(GT x, GT y)
+ {
+ GT z = new GT();
+ Add(z, x, y);
+ return z;
+ }
+ public static GT operator -(GT x, GT y)
+ {
+ GT z = new GT();
+ Sub(z, x, y);
+ return z;
+ }
+ public static GT operator *(GT x, GT y)
+ {
+ GT z = new GT();
+ Mul(z, x, y);
+ return z;
+ }
+ public static GT operator /(GT x, GT y)
+ {
+ GT z = new GT();
+ Div(z, x, y);
+ return z;
+ }
+ public static void FinalExp(GT y, GT x)
+ {
+ BN256_GT_finalExp(y, x);
+ }
+ public static void Pow(GT z, GT x, Fr y)
+ {
+ BN256_GT_pow(z, x, y);
+ }
+ }
+ public static void Pairing(GT z, G1 x, G2 y)
+ {
+ BN256_pairing(z, x, y);
+ }
+ public static void MillerLoop(GT z, G1 x, G2 y)
+ {
+ BN256_millerLoop(z, x, y);
+ }
+ }
+}
diff --git a/ffi/cs/pairing.csproj b/ffi/cs/bn256.csproj
index 8399e8d..8a1ac88 100644
--- a/ffi/cs/pairing.csproj
+++ b/ffi/cs/bn256.csproj
@@ -7,15 +7,15 @@
<ProjectGuid>{E9D06B1B-EA22-4EF4-BA4B-422F7625966B}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>pairing</RootNamespace>
- <AssemblyName>pairing</AssemblyName>
+ <RootNamespace>bn256</RootNamespace>
+ <AssemblyName>bn256</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
- <OutputPath>bin\x64\Debug\</OutputPath>
+ <OutputPath>..\..\bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DebugType>full</DebugType>
@@ -24,7 +24,7 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
- <OutputPath>bin\x64\Release\</OutputPath>
+ <OutputPath>..\..\bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
@@ -44,7 +44,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="Program.cs" />
+ <Compile Include="bn256.cs" />
+ <Compile Include="bn256_test.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
diff --git a/ffi/cs/pairing.sln b/ffi/cs/bn256.sln
index 6dd97df..64d7750 100644
--- a/ffi/cs/pairing.sln
+++ b/ffi/cs/bn256.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pairing", "pairing.csproj", "{E9D06B1B-EA22-4EF4-BA4B-422F7625966B}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bn256", "bn256.csproj", "{E9D06B1B-EA22-4EF4-BA4B-422F7625966B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/ffi/cs/bn256_test.cs b/ffi/cs/bn256_test.cs
new file mode 100644
index 0000000..6d9e539
--- /dev/null
+++ b/ffi/cs/bn256_test.cs
@@ -0,0 +1,146 @@
+using System;
+
+namespace mcl {
+ using static BN256;
+ class BN256Test {
+ static void Main(string[] args)
+ {
+ try {
+ Console.WriteLine("64bit = {0}", System.Environment.Is64BitProcess);
+ int ret;
+ ret = BN256_init();
+ Console.WriteLine("ret= {0}", ret);
+ ret = BN256_setErrFile("c:/tmp/abc.txt");
+ Console.WriteLine("ret= {0}", ret);
+ TestFr();
+ TestG1();
+ TestG2();
+ TestPairing();
+ } catch (Exception e) {
+ Console.WriteLine("ERR={0}", e);
+ }
+ }
+ static void TestFr()
+ {
+ Console.WriteLine("TestFr");
+ Fr x = new Fr();
+ x.Clear();
+ Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
+ x.SetInt(1);
+ Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
+ x.SetInt(3);
+ Console.WriteLine("x = {0}, isZero = {1}, isOne = {2}", x, x.IsZero(), x.IsOne());
+ x.SetInt(-5);
+ Console.WriteLine("x = {0}", x);
+ x = -x;
+ Console.WriteLine("x = {0}", x);
+ x.SetInt(4);
+ x = x * x;
+ Console.WriteLine("x = {0}", x);
+ Fr y;// = new Fr();
+ // y = x;
+ y = x.Clone();
+ Console.WriteLine("y = {0}", y);
+ Console.WriteLine("x == y {0}", x.Equals(y));
+ x.SetInt(123);
+ Console.WriteLine("y = {0}", y);
+ Console.WriteLine("x == y {0}", x.Equals(y));
+ try {
+ x.SetStr("1234567891234x");
+ Console.WriteLine("x = {0}", x);
+ } catch (Exception e) {
+ Console.WriteLine("exception test {0}", e);
+ }
+ x.SetStr("1234567891234");
+ Console.WriteLine("x = {0}", x);
+ }
+ static void TestG1()
+ {
+ Console.WriteLine("TestG1");
+ G1 P = new G1();
+ P.Clear();
+ Console.WriteLine("P = {0}", P);
+ Console.WriteLine("P is valid {0}", P.IsValid());
+ Console.WriteLine("P is zero {0}", P.IsZero());
+ P.HashAndMapTo("abc");
+ Console.WriteLine("P = {0}", P);
+ Console.WriteLine("P is valid {0}", P.IsValid());
+ Console.WriteLine("P is zero {0}", P.IsZero());
+ G1 Q = new G1();
+ G1.Neg(Q, P);
+ Console.WriteLine("Q = {0}", Q);
+ G1.Add(Q, Q, P);
+ Console.WriteLine("Q = {0}", Q);
+ G1.Dbl(Q, P);
+ G1 R = new G1();
+ G1.Add(R, P, P);
+ Console.WriteLine("P == R {0}", P.Equals(P));
+ Console.WriteLine("P == R {0}", P.Equals(R));
+ Console.WriteLine("Q == R {0}", Q.Equals(R));
+ Console.WriteLine("Q = {0}", Q);
+ Console.WriteLine("R = {0}", R);
+ Fr x = new Fr();
+ x.SetInt(3);
+ G1.Add(R, R, P);
+ G1.Mul(Q, P, x);
+ Console.WriteLine("Q == R {0}", Q.Equals(R));
+ }
+ static void TestG2()
+ {
+ Console.WriteLine("TestG2");
+ G2 P = new G2();
+ P.Clear();
+ Console.WriteLine("P = {0}", P);
+ Console.WriteLine("P is valid {0}", P.IsValid());
+ Console.WriteLine("P is zero {0}", P.IsZero());
+ P.HashAndMapTo("abc");
+ Console.WriteLine("P = {0}", P);
+ Console.WriteLine("P is valid {0}", P.IsValid());
+ Console.WriteLine("P is zero {0}", P.IsZero());
+ G2 Q = new G2();
+ G2.Neg(Q, P);
+ Console.WriteLine("Q = {0}", Q);
+ G2.Add(Q, Q, P);
+ Console.WriteLine("Q = {0}", Q);
+ G2.Dbl(Q, P);
+ G2 R = new G2();
+ G2.Add(R, P, P);
+ Console.WriteLine("P == R {0}", P.Equals(P));
+ Console.WriteLine("P == R {0}", P.Equals(R));
+ Console.WriteLine("Q == R {0}", Q.Equals(R));
+ Console.WriteLine("Q = {0}", Q);
+ Console.WriteLine("R = {0}", R);
+ Fr x = new Fr();
+ x.SetInt(3);
+ G2.Add(R, R, P);
+ G2.Mul(Q, P, x);
+ Console.WriteLine("Q == R {0}", Q.Equals(R));
+ }
+ static void TestPairing()
+ {
+ Console.WriteLine("TestG2");
+ G1 P = new G1();
+ P.setStr("1 -1 1");
+ G2 Q = new G2();
+ Q.HashAndMapTo("1");
+ Fr a = new Fr();
+ Fr b = new Fr();
+ a.SetStr("12345678912345673453");
+ b.SetStr("230498230982394243424");
+ G1 aP = new G1();
+ G2 bQ = new G2();
+ G1.Mul(aP, P, a);
+ G2.Mul(bQ, Q, b);
+ GT e1 = new GT();
+ GT e2 = new GT();
+ GT e3 = new GT();
+ Pairing(e1, P, Q);
+ Pairing(e2, aP, Q);
+ GT.Pow(e3, e1, a);
+ Console.WriteLine("e2.Equals(e3) {0}", e2.Equals(e3));
+ Pairing(e2, P, bQ);
+ GT.Pow(e3, e1, b);
+ Console.WriteLine("e2.Equals(e3) {0}", e2.Equals(e3));
+ }
+ }
+}