diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-14 18:33:39 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-14 18:33:39 +0800 |
commit | 940aba6e5bdebaf958a0215c86ef1b1010f91a2f (patch) | |
tree | f76238feb12f4980fd4b6cf5a5c1104453ac0604 | |
parent | 5858d08066c633e1ca2df7fb4929427b9bf594e4 (diff) | |
download | dexon-bls-940aba6e5bdebaf958a0215c86ef1b1010f91a2f.tar.gz dexon-bls-940aba6e5bdebaf958a0215c86ef1b1010f91a2f.tar.zst dexon-bls-940aba6e5bdebaf958a0215c86ef1b1010f91a2f.zip |
add C# binding
-rw-r--r-- | ffi/cs/App.config | 6 | ||||
-rw-r--r-- | ffi/cs/Properties/AssemblyInfo.cs | 36 | ||||
-rw-r--r-- | ffi/cs/bls256.cs | 129 | ||||
-rw-r--r-- | ffi/cs/bls256.csproj | 62 | ||||
-rw-r--r-- | ffi/cs/bls256_test.cs | 36 |
5 files changed, 269 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <startup>
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+ </startup>
+</configuration>
\ 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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{E9D06B1B-EA22-4EF4-BA4B-422F7625966C}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>bls256</RootNamespace>
+ <AssemblyName>bls256</AssemblyName>
+ <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+ <DebugSymbols>true</DebugSymbols>
+ <OutputPath>..\..\bin\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
+ <DebugType>full</DebugType>
+ <PlatformTarget>x64</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+ <OutputPath>..\..\bin\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <Optimize>true</Optimize>
+ <DebugType>pdbonly</DebugType>
+ <PlatformTarget>x64</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
+ <Prefer32Bit>true</Prefer32Bit>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="bls256.cs" />
+ <Compile Include="bls256_test.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ 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); + } + } + } +} |