diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-06 22:15:21 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-06 22:15:21 +0800 |
commit | 745969978b67bed981f547f4026f1a9db7e9130d (patch) | |
tree | 5eece33c0b60e3ea4084a94051377b5b7c985dfd | |
parent | 7ad2f9a9c3ceb82f8dbcd23c9d0743a1f5e18419 (diff) | |
download | tangerine-mcl-745969978b67bed981f547f4026f1a9db7e9130d.tar.gz tangerine-mcl-745969978b67bed981f547f4026f1a9db7e9130d.tar.zst tangerine-mcl-745969978b67bed981f547f4026f1a9db7e9130d.zip |
use unsafe fixed for Fr
-rw-r--r-- | ffi/cs/bn256.cs | 101 | ||||
-rw-r--r-- | ffi/cs/bn256.csproj | 3 | ||||
-rw-r--r-- | ffi/cs/bn256_test.cs | 4 |
3 files changed, 53 insertions, 55 deletions
diff --git a/ffi/cs/bn256.cs b/ffi/cs/bn256.cs index 95048e1..296bf60 100644 --- a/ffi/cs/bn256.cs +++ b/ffi/cs/bn256.cs @@ -4,18 +4,19 @@ using System.Runtime.InteropServices; namespace mcl {
class BN256 {
+ public const int N = 4;
[DllImport("bn256.dll")]
public static extern int BN256_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport("bn256.dll")]
public static extern int BN256_init();
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_clear([Out] Fr x);
+ public static extern void BN256_Fr_clear(out Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_setInt([Out] Fr y, int x);
+ public static extern void BN256_Fr_setInt(out Fr y, int x);
[DllImport("bn256.dll")]
- public static extern int BN256_Fr_setStr([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ public static extern int BN256_Fr_setStr(out Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
[DllImport("bn256.dll")]
- public static extern int BN256_Fr_isValid([In] Fr x);
+ public static extern int BN256_Fr_isValid(ref Fr x);
[DllImport("bn256.dll")]
public static extern int BN256_Fr_isSame([In] Fr x, [In] Fr y);
[DllImport("bn256.dll")]
@@ -23,25 +24,25 @@ namespace mcl { [DllImport("bn256.dll")]
public static extern int BN256_Fr_isOne([In] Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_setRand([Out] Fr x);
+ public static extern void BN256_Fr_setRand(out Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_setMsg([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
+ public static extern void BN256_Fr_setMsg(out Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
[DllImport("bn256.dll")]
public static extern int BN256_Fr_getStr([Out]StringBuilder buf, int maxBufSize, [In] Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_neg([Out] Fr y, [In] Fr x);
+ public static extern void BN256_Fr_neg(out Fr y, [In] Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_inv([Out] Fr y, [In] Fr x);
+ public static extern void BN256_Fr_inv(out Fr y, [In] Fr x);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_add([Out] Fr z, [In] Fr x, [In] Fr y);
+ public static extern void BN256_Fr_add(out Fr z, [In] Fr x, [In] Fr y);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_sub([Out] Fr z, [In] Fr x, [In] Fr y);
+ public static extern void BN256_Fr_sub(out Fr z, [In] Fr x, [In] Fr y);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_mul([Out] Fr z, [In] Fr x, [In] Fr y);
+ public static extern void BN256_Fr_mul(out Fr z, [In] Fr x, [In] Fr y);
[DllImport("bn256.dll")]
- public static extern void BN256_Fr_div([Out] Fr z, [In] Fr x, [In] Fr y);
+ public static extern void BN256_Fr_div(out Fr z, [In] Fr x, [In] Fr y);
[DllImport("bn256.dll")]
public static extern void BN256_G1_clear([Out] G1 x);
@@ -128,15 +129,15 @@ namespace mcl { 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 struct Fr {
+ private unsafe fixed ulong v[N];
public void Clear()
{
- BN256_Fr_clear(this);
+ BN256_Fr_clear(out this);
}
public void SetInt(int x)
{
- BN256_Fr_setInt(this, x);
+ BN256_Fr_setInt(out this, x);
}
public Fr Clone()
{
@@ -144,15 +145,15 @@ namespace mcl { }
public void SetStr(string s)
{
- if (BN256_Fr_setStr(this, s) != 0) {
+ if (BN256_Fr_setStr(out this, s) != 0) {
throw new ArgumentException("BN256_Fr_setStr", s);
}
}
public bool IsValid()
{
- return BN256_Fr_isValid(this) == 1;
+ return BN256_Fr_isValid(ref this) == 1;
}
- public bool Equals(Fr rhs)
+ public bool Equals([In] Fr rhs)
{
return BN256_Fr_isSame(this, rhs) == 1;
}
@@ -166,11 +167,11 @@ namespace mcl { }
public void SetRand()
{
- BN256_Fr_setRand(this);
+ BN256_Fr_setRand(out this);
}
public void SetMsg(String s)
{
- BN256_Fr_setMsg(this, s);
+ BN256_Fr_setMsg(out this, s);
}
public override string ToString()
{
@@ -180,58 +181,58 @@ namespace mcl { }
return sb.ToString();
}
- public static void Neg(Fr y, Fr x)
+ public static void Neg(out Fr y, [In] Fr x)
{
- BN256_Fr_neg(y, x);
+ BN256_Fr_neg(out y, x);
}
- public static void Inv(Fr y, Fr x)
+ public static void Inv(out Fr y, [In] Fr x)
{
- BN256_Fr_inv(y, x);
+ BN256_Fr_inv(out y, x);
}
- public static void Add(Fr z, Fr y, Fr x)
+ public static void Add(out Fr z, [In] Fr x, [In] Fr y)
{
- BN256_Fr_add(z, y, x);
+ BN256_Fr_add(out z, x, y);
}
- public static void Sub(Fr z, Fr y, Fr x)
+ public static void Sub(out Fr z, [In] Fr x, [In] Fr y)
{
- BN256_Fr_sub(z, y, x);
+ BN256_Fr_sub(out z, x, y);
}
- public static void Mul(Fr z, Fr y, Fr x)
+ public static void Mul(out Fr z, [In] Fr x, [In] Fr y)
{
- BN256_Fr_mul(z, y, x);
+ BN256_Fr_mul(out z, x, y);
}
- public static void Div(Fr z, Fr y, Fr x)
+ public static void Div(out Fr z, [In] Fr x, [In] Fr y)
{
- BN256_Fr_div(z, y, x);
+ BN256_Fr_div(out z, x, y);
}
- public static Fr operator -(Fr x)
+ public static Fr operator -([In] Fr x)
{
- Fr y = new Fr();
- Neg(y, x);
+ Fr y;
+ Neg(out y, x);
return y;
}
- public static Fr operator +(Fr x, Fr y)
+ public static Fr operator +([In] Fr x, [In] Fr y)
{
- Fr z = new Fr();
- Add(z, x, y);
+ Fr z;
+ Add(out z, x, y);
return z;
}
- public static Fr operator -(Fr x, Fr y)
+ public static Fr operator -([In] Fr x, [In] Fr y)
{
- Fr z = new Fr();
- Sub(z, x, y);
+ Fr z;
+ Sub(out z, x, y);
return z;
}
- public static Fr operator *(Fr x, Fr y)
+ public static Fr operator *([In] Fr x, [In] Fr y)
{
- Fr z = new Fr();
- Mul(z, x, y);
+ Fr z;
+ Mul(out z, x, y);
return z;
}
- public static Fr operator /(Fr x, Fr y)
+ public static Fr operator /([In] Fr x, [In] Fr y)
{
- Fr z = new Fr();
- Div(z, x, y);
+ Fr z;
+ Div(out z, x, y);
return z;
}
}
@@ -380,10 +381,6 @@ namespace mcl { 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;
diff --git a/ffi/cs/bn256.csproj b/ffi/cs/bn256.csproj index 8a1ac88..bca329b 100644 --- a/ffi/cs/bn256.csproj +++ b/ffi/cs/bn256.csproj @@ -17,7 +17,7 @@ <DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
- <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
@@ -32,6 +32,7 @@ <ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
diff --git a/ffi/cs/bn256_test.cs b/ffi/cs/bn256_test.cs index 6a21b99..c66d1ee 100644 --- a/ffi/cs/bn256_test.cs +++ b/ffi/cs/bn256_test.cs @@ -35,7 +35,7 @@ namespace mcl { static void TestFr()
{
Console.WriteLine("TestFr");
- Fr x = new Fr();
+ Fr x;
x.Clear();
assert("0", x.ToString() == "0");
assert("0.IzZero", x.IsZero());
@@ -55,7 +55,7 @@ namespace mcl { x = x * x;
assert("16", x.ToString() == "16");
Fr y;
- y = x.Clone();
+ y = x;
assert("x == y", x.Equals(y));
x.SetInt(123);
assert("123", x.ToString() == "123");
|