diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-02-26 18:09:28 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-02-26 18:09:28 +0800 |
commit | 101d2470b1933a011ebb96daf93bfb111732553f (patch) | |
tree | c4f518ffdace71ae65139771f6713435ffb09b61 | |
parent | 39cebb6f207be1932f41a45921bd0bf0f40de9d0 (diff) | |
download | dexon-mcl-101d2470b1933a011ebb96daf93bfb111732553f.tar.gz dexon-mcl-101d2470b1933a011ebb96daf93bfb111732553f.tar.zst dexon-mcl-101d2470b1933a011ebb96daf93bfb111732553f.zip |
ffi for C#/Python(TBD)
-rw-r--r-- | ffi/cs/App.config | 6 | ||||
-rw-r--r-- | ffi/cs/Program.cs | 172 | ||||
-rw-r--r-- | ffi/cs/Properties/AssemblyInfo.cs | 36 | ||||
-rw-r--r-- | ffi/cs/pairing.csproj | 61 | ||||
-rw-r--r-- | ffi/cs/pairing.sln | 22 | ||||
-rw-r--r-- | ffi/python/pairing.py | 80 |
6 files changed, 377 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/Program.cs b/ffi/cs/Program.cs new file mode 100644 index 0000000..7f7c893 --- /dev/null +++ b/ffi/cs/Program.cs @@ -0,0 +1,172 @@ +using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace bn256 {
+ class X {
+ [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 void BN256_Fr_copy([Out] Fr y, [In] Fr 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);
+
+ [StructLayout(LayoutKind.Sequential)]
+ public class Fr {
+ private ulong v0;
+ private ulong v1;
+ private ulong v2;
+ private ulong 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_isZero(this) == 1;
+ }
+ public void SetRand()
+ {
+ BN256_Fr_setRand(this);
+ }
+ public void SsetMsg(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;
+ }
+ }
+
+ static void Main(string[] args)
+ {
+ 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);
+
+ Fr x = new Fr();
+ x.Clear();
+ Console.WriteLine("x = {0}", x);
+ x.SetInt(3);
+ Console.WriteLine("x = {0}", x);
+ 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);
+ x.SetInt(123);
+ Console.WriteLine("y = {0}", y);
+ }
+ }
+}
diff --git a/ffi/cs/Properties/AssemblyInfo.cs b/ffi/cs/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0b87e98 --- /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("pairing")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("pairing")]
+[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-422f7625966b")]
+
+// アセンブリのバージョン情報は次の 4 つの値で構成されています:
+//
+// メジャー バージョン
+// マイナー バージョン
+// ビルド番号
+// Revision
+//
+// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
+// 既定値にすることができます:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/ffi/cs/pairing.csproj b/ffi/cs/pairing.csproj new file mode 100644 index 0000000..8399e8d --- /dev/null +++ b/ffi/cs/pairing.csproj @@ -0,0 +1,61 @@ +<?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-422F7625966B}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>pairing</RootNamespace>
+ <AssemblyName>pairing</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>
+ <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\x64\Release\</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="Program.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/pairing.sln b/ffi/cs/pairing.sln new file mode 100644 index 0000000..6dd97df --- /dev/null +++ b/ffi/cs/pairing.sln @@ -0,0 +1,22 @@ +
+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}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x64 = Debug|x64
+ Release|x64 = Release|x64
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {E9D06B1B-EA22-4EF4-BA4B-422F7625966B}.Debug|x64.ActiveCfg = Debug|x64
+ {E9D06B1B-EA22-4EF4-BA4B-422F7625966B}.Debug|x64.Build.0 = Debug|x64
+ {E9D06B1B-EA22-4EF4-BA4B-422F7625966B}.Release|x64.ActiveCfg = Release|x64
+ {E9D06B1B-EA22-4EF4-BA4B-422F7625966B}.Release|x64.Build.0 = Release|x64
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/ffi/python/pairing.py b/ffi/python/pairing.py new file mode 100644 index 0000000..ffdbf4f --- /dev/null +++ b/ffi/python/pairing.py @@ -0,0 +1,80 @@ +from ctypes import * +from ctypes.wintypes import LPWSTR, LPCSTR, LPVOID + +g_lib = None + +def BN256_init(): + global g_lib + g_lib = cdll.LoadLibrary("../../bin/bn256_if.dll") + ret = g_lib.BN256_init() + if ret: + print "ERR BN256_init" + +class Fr(Structure): + _fields_ = [("v", c_ulonglong * 4)] + def setInt(self, v): + g_lib.BN256_Fr_setInt(self.v, v) + def setStr(self, s): + ret = g_lib.BN256_Fr_setStr(self.v, c_char_p(s)) + if ret: + print("ERR Fr:setStr") + def __str__(self): + svLen = 1024 + sv = create_string_buffer('\0' * svLen) + ret = g_lib.BN256_Fr_getStr(sv, svLen, self.v) + if ret: + print("ERR Fr:getStr") + return sv.value + def isZero(self, rhs): + return g_lib.BN256_Fr_isZero(self.v) != 0 + def isOne(self, rhs): + return g_lib.BN256_Fr_isOne(self.v) != 0 + def __eq__(self, rhs): + return g_lib.BN256_Fr_isSame(self.v, rhs.v) != 0 + def __ne__(self, rhs): + return not(P == Q) + def __add__(self, rhs): + ret = Fr() + g_lib.BN256_Fr_add(ret.v, self.v, rhs.v) + return ret + def __sub__(self, rhs): + ret = Fr() + g_lib.BN256_Fr_sub(ret.v, self.v, rhs.v) + return ret + def __mul__(self, rhs): + ret = Fr() + g_lib.BN256_Fr_mul(ret.v, self.v, rhs.v) + return ret + def __div__(self, rhs): + ret = Fr() + g_lib.BN256_Fr_div(ret.v, self.v, rhs.v) + return ret + def __neg__(self): + ret = Fr() + g_lib.BN256_Fr_neg(ret.v, self.v) + return ret + +def Fr_add(z, x, y): + g_lib.BN256_Fr_add(z.v, x.v, y.v) + +def Fr_sub(z, x, y): + g_lib.BN256_Fr_sub(z.v, x.v, y.v) + +def Fr_mul(z, x, y): + g_lib.BN256_Fr_mul(z.v, x.v, y.v) + +def Fr_div(z, x, y): + g_lib.BN256_Fr_div(z.v, x.v, y.v) + +BN256_init() + +P = Fr() +Q = Fr() +print P == Q +print P != Q +P.setInt(5) +Q.setStr("34982034824") +print Q +R = Fr() +Fr_add(R, P, Q) +print R |