From 940aba6e5bdebaf958a0215c86ef1b1010f91a2f Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 14 May 2017 19:33:39 +0900 Subject: add C# binding --- ffi/cs/App.config | 6 ++ ffi/cs/Properties/AssemblyInfo.cs | 36 +++++++++++ ffi/cs/bls256.cs | 129 ++++++++++++++++++++++++++++++++++++++ ffi/cs/bls256.csproj | 62 ++++++++++++++++++ ffi/cs/bls256_test.cs | 36 +++++++++++ 5 files changed, 269 insertions(+) create mode 100644 ffi/cs/App.config create mode 100644 ffi/cs/Properties/AssemblyInfo.cs create mode 100644 ffi/cs/bls256.cs create mode 100644 ffi/cs/bls256.csproj create mode 100644 ffi/cs/bls256_test.cs diff --git a/ffi/cs/App.config b/ffi/cs/App.config new file mode 100644 index 0000000..d740e88 --- /dev/null +++ b/ffi/cs/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ffi/cs/Properties/AssemblyInfo.cs b/ffi/cs/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ca0dd6c --- /dev/null +++ b/ffi/cs/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。 +// アセンブリに関連付けられている情報を変更するには、 +// これらの属性値を変更してください。 +[assembly: AssemblyTitle("bls256")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("bls256")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから +// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、 +// その型の ComVisible 属性を true に設定してください。 +[assembly: ComVisible(false)] + +// このプロジェクトが COM に公開される場合、次の GUID が typelib の ID になります +[assembly: Guid("e9d06b1b-ea22-4ef4-ba4b-422f7625966c")] + +// アセンブリのバージョン情報は次の 4 つの値で構成されています: +// +// メジャー バージョン +// マイナー バージョン +// ビルド番号 +// Revision +// +// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を +// 既定値にすることができます: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ffi/cs/bls256.cs b/ffi/cs/bls256.cs new file mode 100644 index 0000000..ccc10e4 --- /dev/null +++ b/ffi/cs/bls256.cs @@ -0,0 +1,129 @@ +using System; +using System.Text; +using System.Runtime.InteropServices; + +namespace mcl { + class BLS256 { +// public static int IoBin = 2; +// public static int IoDec = 10; +// public static int IoHex = 16; +// public static int IoEcComp = 512; // fixed byte representation + [DllImport("bls256.dll")] + public static extern int blsInit(int curve, int maxUnitSize); + [DllImport("bls256.dll")] + public static extern int blsIdIsSame(ref Id lhs, ref Id rhs); + + [DllImport("bls256.dll")] + public static extern int blsIdSetStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern ulong blsIdGetStr(ref Id id, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern void blsIdSet(ref Id id, ref ulong p); + [DllImport("bls256.dll")] + public static extern int blsSecretKeyIsSame(ref SecretKey lhs, ref SecretKey rhs); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyPut(ref SecretKey sec); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyCopy(ref SecretKey dst, ref SecretKey src); + [DllImport("bls256.dll")] + public static extern void blsSecretKeySetArray(ref SecretKey sec, ref ulong p); + [DllImport("bls256.dll")] + public static extern int blsSecretKeySetStr(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern ulong blsSecretKeyGetStr(ref SecretKey sec, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyAdd(ref SecretKey sec, ref SecretKey rhs); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyInit(ref SecretKey sec); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyGetPublicKey(ref SecretKey sec, ref PublicKey pub); + [DllImport("bls256.dll")] + public static extern void blsSecretKeySign(ref SecretKey sec, ref Sign sign, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); + [DllImport("bls256.dll")] + public static extern int blsSecretKeySet(ref SecretKey sec, ref SecretKey msk, ulong k, ref Id id); + [DllImport("bls256.dll")] + public static extern int blsSecretKeyRecover(ref SecretKey sec, ref SecretKey secVec, ref Id idVec, ulong n); + [DllImport("bls256.dll")] + public static extern void blsSecretKeyGetPop(ref SecretKey sec, ref Sign sign); + [DllImport("bls256.dll")] + public static extern int blsPublicKeyIsSame(ref PublicKey lhs, ref PublicKey rhs); + [DllImport("bls256.dll")] + public static extern void blsPublicKeyPut(ref PublicKey pub); + [DllImport("bls256.dll")] + public static extern void blsPublicKeyCopy(ref PublicKey dst, ref PublicKey src); + [DllImport("bls256.dll")] + public static extern int blsPublicKeySetStr(ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern ulong blsPublicKeyGetStr(ref PublicKey pub, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern void blsPublicKeyAdd(ref PublicKey pub, ref PublicKey rhs); + [DllImport("bls256.dll")] + public static extern int blsPublicKeySet(ref PublicKey pub, ref PublicKey mpk, ulong k, ref Id id); + [DllImport("bls256.dll")] + public static extern int blsPublicKeyRecover(ref PublicKey pub, ref PublicKey pubVec, ref Id idVec, ulong n); + [DllImport("bls256.dll")] + public static extern int blsSignIsSame(ref Sign lhs, ref Sign rhs); + [DllImport("bls256.dll")] + public static extern void blsSignPut(ref Sign sign); + [DllImport("bls256.dll")] + public static extern void blsSignCopy(ref Sign dst, ref Sign src); + [DllImport("bls256.dll")] + public static extern int blsSignSetStr(ref Sign sign, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern ulong blsSignGetStr(ref Sign sign, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); + [DllImport("bls256.dll")] + public static extern void blsSignAdd(ref Sign sign, ref Sign rhs); + [DllImport("bls256.dll")] + public static extern int blsSignRecover(ref Sign sign, ref Sign signVec, ref Id idVec, ulong n); + [DllImport("bls256.dll")] + public static extern int blsSignVerify(ref Sign sign, ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); + [DllImport("bls256.dll")] + public static extern int blsSignVerifyPop(ref Sign sign, ref PublicKey pub); + + public static void Init() + { + const int CurveFp254BNb = 0; + const int maxUnitSize = 4; + if (!System.Environment.Is64BitProcess) { + throw new PlatformNotSupportedException("not 64-bit system"); + } + int err = blsInit(CurveFp254BNb, maxUnitSize); + if (err != 0) { + throw new InvalidOperationException("blsInit"); + } + } + + public struct Id { + private ulong v0, v1, v2, v3; + public bool IsSame(Id rhs) + { + return blsIdIsSame(ref this, ref rhs) != 0; + } + public void SetStr(String s, int ioMode) + { + if (blsIdSetStr(ref this, s, (ulong)s.Length, ioMode) != 0) { + throw new InvalidOperationException("blsIdSetStr:" + s); + } + } + public string GetStr(int ioMode) + { + StringBuilder sb = new StringBuilder(1024); + ulong size = blsIdGetStr(ref this, sb, (ulong)sb.Capacity + 1, ioMode); + if (size == 0) { + throw new InvalidOperationException("blsIdGetStr"); + } + return sb.ToString(0, (int)size); + } + } + public struct SecretKey { + private ulong v0, v1, v2, v3; + } + public struct Sign { + private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11; + } + public struct PublicKey { + 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; + } + } +} diff --git a/ffi/cs/bls256.csproj b/ffi/cs/bls256.csproj new file mode 100644 index 0000000..debe31f --- /dev/null +++ b/ffi/cs/bls256.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {E9D06B1B-EA22-4EF4-BA4B-422F7625966C} + Exe + Properties + bls256 + bls256 + v4.5.2 + 512 + true + + + true + ..\..\bin\ + DEBUG;TRACE + false + full + x64 + prompt + MinimumRecommendedRules.ruleset + + + ..\..\bin\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ffi/cs/bls256_test.cs b/ffi/cs/bls256_test.cs new file mode 100644 index 0000000..f649ddc --- /dev/null +++ b/ffi/cs/bls256_test.cs @@ -0,0 +1,36 @@ +using System; + +namespace mcl { + using static BLS256; + class BLS256Test { + static int err = 0; + static void assert(string msg, bool b) + { + if (b) return; + Console.WriteLine("ERR {0}", msg); + err++; + } + static void TestId() + { + Console.WriteLine("TestId"); + Id id = new Id(); + id.SetStr("255", 10); + Console.WriteLine("id={0}", id.GetStr(10)); + Console.WriteLine("id={0}", id.GetStr(16)); + } + static void Main(string[] args) + { + try { + Init(); + TestId(); + if (err == 0) { + Console.WriteLine("all tests succeed"); + } else { + Console.WriteLine("err={0}", err); + } + } catch (Exception e) { + Console.WriteLine("ERR={0}", e); + } + } + } +} -- cgit v1.2.3