diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-06-01 23:07:31 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-06-01 23:07:31 +0800 |
commit | d6fe92b48dee1b99bf873ec41194be1f951b57bc (patch) | |
tree | 642198637d9b887421764a3427b9ee6cac8c1f78 | |
parent | 96f97676366f98f3365aeb09b6ade98634a5077d (diff) | |
download | tangerine-mcl-d6fe92b48dee1b99bf873ec41194be1f951b57bc.tar.gz tangerine-mcl-d6fe92b48dee1b99bf873ec41194be1f951b57bc.tar.zst tangerine-mcl-d6fe92b48dee1b99bf873ec41194be1f951b57bc.zip |
update C# sample to new api
-rw-r--r-- | ffi/cs/bn256.cs | 488 | ||||
-rw-r--r-- | ffi/cs/bn256_test.cs | 38 | ||||
-rw-r--r-- | include/mcl/bn.h | 2 | ||||
-rw-r--r-- | src/bn_c.cpp | 6 |
4 files changed, 294 insertions, 240 deletions
diff --git a/ffi/cs/bn256.cs b/ffi/cs/bn256.cs index 6203712..d788de4 100644 --- a/ffi/cs/bn256.cs +++ b/ffi/cs/bn256.cs @@ -4,201 +4,235 @@ using System.Runtime.InteropServices; namespace mcl {
class BN256 {
- [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(ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_setInt(ref Fr y, int x);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_setStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_isValid(ref Fr x);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_isEqual(ref Fr x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_isZero(ref Fr x);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_isOne(ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_setRand(ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_init(int curve, int maxUnitSize);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_clear(ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_setInt(ref Fr y, int x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_setDecStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_setHexStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_isValid(ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_isEqual(ref Fr x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_isZero(ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_isOne(ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_setByCSPRNG(ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_setMsg(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_Fr_getStr([Out]StringBuilder buf, int maxBufSize, ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_hashToFr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_getHexStr([Out]StringBuilder buf, long maxBufSize, ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_Fr_getDecStr([Out]StringBuilder buf, long maxBufSize, ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_neg(ref Fr y, ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_inv(ref Fr y, ref Fr x);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_add(ref Fr z, ref Fr x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_sub(ref Fr z, ref Fr x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_mul(ref Fr z, ref Fr x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_Fr_div(ref Fr z, ref Fr x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_neg(ref Fr y, ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_inv(ref Fr y, ref Fr x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_add(ref Fr z, ref Fr x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_sub(ref Fr z, ref Fr x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_mul(ref Fr z, ref Fr x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_Fr_div(ref Fr z, ref Fr x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_clear(ref G1 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_setStr(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_isValid(ref G1 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_isEqual(ref G1 x, ref G1 y);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_isZero(ref G1 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_hashAndMapTo(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_G1_getStr([Out]StringBuilder buf, int maxBufSize, ref G1 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_neg(ref G1 y, ref G1 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_dbl(ref G1 y, ref G1 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_add(ref G1 z, ref G1 x, ref G1 y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_sub(ref G1 z, ref G1 x, ref G1 y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G1_mul(ref G1 z, ref G1 x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_clear(ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G1_setHexStr(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G1_isValid(ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G1_isEqual(ref G1 x, ref G1 y);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G1_isZero(ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_hashAndMapToG1(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern long MBN_G1_getHexStr([Out]StringBuilder buf, long maxBufSize, ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_neg(ref G1 y, ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_dbl(ref G1 y, ref G1 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_add(ref G1 z, ref G1 x, ref G1 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_sub(ref G1 z, ref G1 x, ref G1 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G1_mul(ref G1 z, ref G1 x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_clear(ref G2 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_setStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_isValid(ref G2 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_isEqual(ref G2 x, ref G2 y);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_isZero(ref G2 x);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_hashAndMapTo(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_G2_getStr([Out]StringBuilder buf, int maxBufSize, ref G2 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_neg(ref G2 y, ref G2 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_dbl(ref G2 y, ref G2 x);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_add(ref G2 z, ref G2 x, ref G2 y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_sub(ref G2 z, ref G2 x, ref G2 y);
- [DllImport("bn256.dll")]
- public static extern void BN256_G2_mul(ref G2 z, ref G2 x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_clear(ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G2_setHexStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G2_isValid(ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G2_isEqual(ref G2 x, ref G2 y);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_G2_isZero(ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_hashAndMapToG2(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern long MBN_G2_getHexStr([Out]StringBuilder buf, long maxBufSize, ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_neg(ref G2 y, ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_dbl(ref G2 y, ref G2 x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_add(ref G2 z, ref G2 x, ref G2 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_sub(ref G2 z, ref G2 x, ref G2 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_G2_mul(ref G2 z, ref G2 x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_clear(ref GT x);
- [DllImport("bn256.dll")]
- public static extern int BN256_GT_setStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
- [DllImport("bn256.dll")]
- public static extern int BN256_GT_isEqual(ref GT x, ref GT y);
- [DllImport("bn256.dll")]
- public static extern int BN256_GT_isZero(ref GT x);
- [DllImport("bn256.dll")]
- public static extern int BN256_GT_isOne(ref GT x);
- [DllImport("bn256.dll")]
- public static extern int BN256_GT_getStr([Out]StringBuilder buf, int maxBufSize, ref GT x);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_neg(ref GT y, ref GT x);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_inv(ref GT y, ref GT x);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_add(ref GT z, ref GT x, ref GT y);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_sub(ref GT z, ref GT x, ref GT y);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_mul(ref GT z, ref GT x, ref GT y);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_div(ref GT z, ref GT x, ref GT y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_clear(ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_GT_setDecStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_GT_setHexStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_GT_isEqual(ref GT x, ref GT y);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_GT_isZero(ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern int MBN_GT_isOne(ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern long MBN_GT_getDecStr([Out]StringBuilder buf, long maxBufSize, ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern long MBN_GT_getHexStr([Out]StringBuilder buf, long maxBufSize, ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_neg(ref GT y, ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_inv(ref GT y, ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_add(ref GT z, ref GT x, ref GT y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_sub(ref GT z, ref GT x, ref GT y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_mul(ref GT z, ref GT x, ref GT y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_div(ref GT z, ref GT x, ref GT y);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_finalExp(ref GT y, ref GT x);
- [DllImport("bn256.dll")]
- public static extern void BN256_GT_pow(ref GT z, ref GT x, ref Fr y);
- [DllImport("bn256.dll")]
- public static extern void BN256_pairing(ref GT z, ref G1 x, ref G2 y);
- [DllImport("bn256.dll")]
- public static extern void BN256_millerLoop(ref GT z, ref G1 x, ref G2 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_GT_pow(ref GT z, ref GT x, ref Fr y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_pairing(ref GT z, ref G1 x, ref G2 y);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_finalExp(ref GT y, ref GT x);
+ [DllImport("mclbn256.dll")]
+ public static extern void MBN_millerLoop(ref GT z, ref G1 x, ref G2 y);
- [StructLayout(LayoutKind.Sequential)]
+ public static void init()
+ {
+ const int curveFp254BNb = 0;
+ const int maxUnitSize = 4;
+ if (MBN_init(curveFp254BNb, maxUnitSize) != 0) {
+ throw new InvalidOperationException("MBN_init");
+ }
+ }
+ [StructLayout(LayoutKind.Sequential)]
public struct Fr {
private ulong v0, v1, v2, v3;
public void Clear()
{
- BN256_Fr_clear(ref this);
+ MBN_Fr_clear(ref this);
}
public void SetInt(int x)
{
- BN256_Fr_setInt(ref this, x);
+ MBN_Fr_setInt(ref this, x);
}
- public void SetStr(string s)
+ public void SetDecStr(string s)
{
- if (BN256_Fr_setStr(ref this, s) != 0) {
- throw new ArgumentException("BN256_Fr_setStr", s);
+ if (MBN_Fr_setDecStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_Fr_setDecStr", s);
}
}
- public bool IsValid()
+ public void SetHexStr(string s)
+ {
+ if (MBN_Fr_setHexStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_Fr_setHexStr", s);
+ }
+ }
+ public bool IsValid()
{
- return BN256_Fr_isValid(ref this) == 1;
+ return MBN_Fr_isValid(ref this) == 1;
}
public bool Equals(Fr rhs)
{
- return BN256_Fr_isEqual(ref this, ref rhs) == 1;
+ return MBN_Fr_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
- return BN256_Fr_isZero(ref this) == 1;
+ return MBN_Fr_isZero(ref this) == 1;
}
public bool IsOne()
{
- return BN256_Fr_isOne(ref this) == 1;
+ return MBN_Fr_isOne(ref this) == 1;
}
- public void SetRand()
+ public void SetByCSPRNG()
{
- BN256_Fr_setRand(ref this);
+ MBN_Fr_setByCSPRNG(ref this);
}
- public void SetMsg(String s)
+ public void SetHashOf(String s)
{
- BN256_Fr_setMsg(ref this, s);
+ if (MBN_hashToFr(ref this, s, s.Length) != 0) {
+ throw new InvalidOperationException("MBN_hashToFr:" + s);
+ }
}
- public override string ToString()
+ public string GetDecStr()
{
StringBuilder sb = new StringBuilder(1024);
- if (BN256_Fr_getStr(sb, sb.Capacity + 1, ref this) != 0) {
- return "ERR:BN256_Fr_getStr";
- }
- return sb.ToString();
- }
- public void Neg(Fr x)
- {
- BN256_Fr_neg(ref this, ref x);
+ long size = MBN_Fr_getDecStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_Fr_getDecStr:");
+ }
+ return sb.ToString();
+ }
+ public string GetHexStr()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ long size = MBN_Fr_getHexStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_Fr_getHexStr:");
+ }
+ return sb.ToString();
+ }
+ public void Neg(Fr x)
+ {
+ MBN_Fr_neg(ref this, ref x);
}
public void Inv(Fr x)
{
- BN256_Fr_inv(ref this, ref x);
+ MBN_Fr_inv(ref this, ref x);
}
public void Add(Fr x, Fr y)
{
- BN256_Fr_add(ref this, ref x, ref y);
+ MBN_Fr_add(ref this, ref x, ref y);
}
public void Sub(Fr x, Fr y)
{
- BN256_Fr_sub(ref this, ref x, ref y);
+ MBN_Fr_sub(ref this, ref x, ref y);
}
public void Mul(Fr x, Fr y)
{
- BN256_Fr_mul(ref this, ref x, ref y);
+ MBN_Fr_mul(ref this, ref x, ref y);
}
public void Div(Fr x, Fr y)
{
- BN256_Fr_div(ref this, ref x, ref y);
+ MBN_Fr_div(ref this, ref x, ref y);
}
public static Fr operator -(Fr x)
{
@@ -236,59 +270,60 @@ namespace mcl { private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11;
public void Clear()
{
- BN256_G1_clear(ref this);
+ MBN_G1_clear(ref this);
}
- public void setStr(String s)
+ public void setHexStr(String s)
{
- if (BN256_G1_setStr(ref this, s) != 0) {
- throw new ArgumentException("BN256_G1_setStr", s);
+ if (MBN_G1_setHexStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_G1_setStr:" + s);
}
}
public bool IsValid()
{
- return BN256_G1_isValid(ref this) == 1;
+ return MBN_G1_isValid(ref this) == 1;
}
public bool Equals(G1 rhs)
{
- return BN256_G1_isEqual(ref this, ref rhs) == 1;
+ return MBN_G1_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
- return BN256_G1_isZero(ref this) == 1;
+ return MBN_G1_isZero(ref this) == 1;
}
public void HashAndMapTo(String s)
{
- if (BN256_G1_hashAndMapTo(ref this, s) != 0) {
- throw new ArgumentException("BN256_G1_hashAndMapTo", s);
+ if (MBN_hashAndMapToG1(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_hashAndMapToG1:" + s);
}
}
- public override string ToString()
+ public string GetHexStr()
{
StringBuilder sb = new StringBuilder(1024);
- if (BN256_G1_getStr(sb, sb.Capacity + 1, ref this) != 0) {
- return "ERR:BN256_G1_getStr";
- }
- return sb.ToString();
+ long size = MBN_G1_getHexStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_G1_getHexStr:");
+ }
+ return sb.ToString();
}
public void Neg(G1 x)
{
- BN256_G1_neg(ref this, ref x);
+ MBN_G1_neg(ref this, ref x);
}
public void Dbl(G1 x)
{
- BN256_G1_dbl(ref this, ref x);
+ MBN_G1_dbl(ref this, ref x);
}
public void Add(G1 x, G1 y)
{
- BN256_G1_add(ref this, ref x, ref y);
+ MBN_G1_add(ref this, ref x, ref y);
}
public void Sub(G1 x, G1 y)
{
- BN256_G1_sub(ref this, ref x, ref y);
+ MBN_G1_sub(ref this, ref x, ref y);
}
public void Mul(G1 x, Fr y)
{
- BN256_G1_mul(ref this, ref x, ref y);
+ MBN_G1_mul(ref this, ref x, ref y);
}
}
[StructLayout(LayoutKind.Sequential)]
@@ -297,59 +332,60 @@ namespace mcl { private ulong v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23;
public void Clear()
{
- BN256_G2_clear(ref this);
+ MBN_G2_clear(ref this);
}
public void setStr(String s)
{
- if (BN256_G2_setStr(ref this, s) != 0) {
- throw new ArgumentException("BN256_G2_setStr", s);
+ if (MBN_G2_setHexStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_G2_setHexStr:" + s);
}
}
public bool IsValid()
{
- return BN256_G2_isValid(ref this) == 1;
+ return MBN_G2_isValid(ref this) == 1;
}
public bool Equals(G2 rhs)
{
- return BN256_G2_isEqual(ref this, ref rhs) == 1;
+ return MBN_G2_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
- return BN256_G2_isZero(ref this) == 1;
+ return MBN_G2_isZero(ref this) == 1;
}
public void HashAndMapTo(String s)
{
- if (BN256_G2_hashAndMapTo(ref 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, ref this) != 0) {
- return "ERR:BN256_G2_getStr";
+ if (MBN_hashAndMapToG2(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_hashAndMapToG2:" + s);
}
- return sb.ToString();
}
- public void Neg(G2 x)
+ public string GetHexStr()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ long size = MBN_G2_getHexStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_G2_getHexStr:");
+ }
+ return sb.ToString();
+ }
+ public void Neg(G2 x)
{
- BN256_G2_neg(ref this, ref x);
+ MBN_G2_neg(ref this, ref x);
}
public void Dbl(G2 x)
{
- BN256_G2_dbl(ref this, ref x);
+ MBN_G2_dbl(ref this, ref x);
}
public void Add(G2 x, G2 y)
{
- BN256_G2_add(ref this, ref x, ref y);
+ MBN_G2_add(ref this, ref x, ref y);
}
public void Sub(G2 x, G2 y)
{
- BN256_G2_sub(ref this, ref x, ref y);
+ MBN_G2_sub(ref this, ref x, ref y);
}
public void Mul(G2 x, Fr y)
{
- BN256_G2_mul(ref this, ref x, ref y);
+ MBN_G2_mul(ref this, ref x, ref y);
}
}
[StructLayout(LayoutKind.Sequential)]
@@ -360,57 +396,73 @@ namespace mcl { private ulong v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47;
public void Clear()
{
- BN256_GT_clear(ref this);
+ MBN_GT_clear(ref this);
}
- public void setStr(String s)
+ public void setDecStr(String s)
+ {
+ if (MBN_GT_setDecStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_GT_setDecStr:" + s);
+ }
+ }
+ public void setHexStr(String s)
{
- if (BN256_GT_setStr(ref this, s) != 0) {
- throw new ArgumentException("BN256_GT_setStr", s);
+ if (MBN_GT_setHexStr(ref this, s, s.Length) != 0) {
+ throw new ArgumentException("MBN_GT_setHexStr:" + s);
}
}
public bool Equals(GT rhs)
{
- return BN256_GT_isEqual(ref this, ref rhs) == 1;
+ return MBN_GT_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
- return BN256_GT_isZero(ref this) == 1;
+ return MBN_GT_isZero(ref this) == 1;
}
public bool IsOne()
{
- return BN256_GT_isOne(ref this) == 1;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1024);
- if (BN256_GT_getStr(sb, sb.Capacity + 1, ref this) != 0) {
- return "ERR:BN256_GT_getStr";
- }
- return sb.ToString();
- }
- public void Neg(GT x)
- {
- BN256_GT_neg(ref this, ref x);
+ return MBN_GT_isOne(ref this) == 1;
+ }
+ public string GetDecStr()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ long size = MBN_GT_getDecStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_GT_getDecStr:");
+ }
+ return sb.ToString();
+ }
+ public string GetHexStr()
+ {
+ StringBuilder sb = new StringBuilder(1024);
+ long size = MBN_GT_getHexStr(sb, sb.Capacity, ref this);
+ if (size == 0) {
+ throw new InvalidOperationException("MBN_GT_getHexStr:");
+ }
+ return sb.ToString();
+ }
+ public void Neg(GT x)
+ {
+ MBN_GT_neg(ref this, ref x);
}
public void Inv(GT x)
{
- BN256_GT_inv(ref this, ref x);
+ MBN_GT_inv(ref this, ref x);
}
public void Add(GT x, GT y)
{
- BN256_GT_add(ref this, ref x, ref this);
+ MBN_GT_add(ref this, ref x, ref this);
}
public void Sub(GT x, GT y)
{
- BN256_GT_sub(ref this, ref x, ref this);
+ MBN_GT_sub(ref this, ref x, ref this);
}
public void Mul(GT x, GT y)
{
- BN256_GT_mul(ref this, ref x, ref this);
+ MBN_GT_mul(ref this, ref x, ref this);
}
public void Div(GT x, GT y)
{
- BN256_GT_div(ref this, ref x, ref this);
+ MBN_GT_div(ref this, ref x, ref this);
}
public static GT operator -(GT x)
{
@@ -442,21 +494,21 @@ namespace mcl { z.Div(x, y);
return z;
}
- public void FinalExp(GT x)
- {
- BN256_GT_finalExp(ref this, ref x);
- }
public void Pow(GT x, Fr y)
{
- BN256_GT_pow(ref this, ref x, ref y);
+ MBN_GT_pow(ref this, ref x, ref y);
}
public void Pairing(G1 x, G2 y)
{
- BN256_pairing(ref this, ref x, ref y);
+ MBN_pairing(ref this, ref x, ref y);
}
- public void MillerLoop(G1 x, G2 y)
+ public void FinalExp(GT x)
+ {
+ MBN_finalExp(ref this, ref x);
+ }
+ public void MillerLoop(G1 x, G2 y)
{
- BN256_millerLoop(ref this, ref x, ref y);
+ MBN_millerLoop(ref this, ref x, ref y);
}
}
}
diff --git a/ffi/cs/bn256_test.cs b/ffi/cs/bn256_test.cs index 30c3ce1..379e582 100644 --- a/ffi/cs/bn256_test.cs +++ b/ffi/cs/bn256_test.cs @@ -14,11 +14,10 @@ namespace mcl { {
try {
assert("64bit system", System.Environment.Is64BitProcess);
- int ret;
- ret = BN256_init();
- assert("BN256_init", ret == 0);
- ret = BN256_setErrFile("bn256_test.txt");
- assert("BN256_setErrFile", ret == 0);
+ init();
+ int ret;
+ ret = MBN_setErrFile("MBN_test.txt");
+ assert("MBN_setErrFile", ret == 0);
TestFr();
TestG1();
TestG2();
@@ -37,44 +36,44 @@ namespace mcl { Console.WriteLine("TestFr");
Fr x = new Fr();
x.Clear();
- assert("0", x.ToString() == "0");
+ assert("0", x.GetDecStr() == "0");
assert("0.IzZero", x.IsZero());
assert("!0.IzOne", !x.IsOne());
x.SetInt(1);
- assert("1", x.ToString() == "1");
+ assert("1", x.GetDecStr() == "1");
assert("!1.IzZero", !x.IsZero());
assert("1.IzOne", x.IsOne());
x.SetInt(3);
- assert("3", x.ToString() == "3");
+ assert("3", x.GetDecStr() == "3");
assert("!3.IzZero", !x.IsZero());
assert("!3.IzOne", !x.IsOne());
x.SetInt(-5);
x = -x;
- assert("5", x.ToString() == "5");
+ assert("5", x.GetDecStr() == "5");
x.SetInt(4);
x = x * x;
- assert("16", x.ToString() == "16");
- Fr y;
+ assert("16", x.GetDecStr() == "16");
+ assert("10", x.GetHexStr() == "10");
+ Fr y;
y = x;
assert("x == y", x.Equals(y));
x.SetInt(123);
- assert("123", x.ToString() == "123");
+ assert("123", x.GetDecStr() == "123");
assert("y != x", !x.Equals(y));
try {
- x.SetStr("1234567891234x");
+ x.SetDecStr("1234567891234x");
Console.WriteLine("x = {0}", x);
} catch (Exception e) {
Console.WriteLine("exception test OK\n'{0}'", e);
}
- x.SetStr("1234567891234");
- assert("1234567891234", x.ToString() == "1234567891234");
+ x.SetDecStr("1234567891234");
+ assert("1234567891234", x.GetDecStr() == "1234567891234");
}
static void TestG1()
{
Console.WriteLine("TestG1");
G1 P = new G1();
P.Clear();
- assert("P = 0", P.ToString() == "0");
assert("P.IsValid", P.IsValid());
assert("P.IsZero", P.IsZero());
P.HashAndMapTo("abc");
@@ -101,7 +100,6 @@ namespace mcl { Console.WriteLine("TestG2");
G2 P = new G2();
P.Clear();
- assert("P = 0", P.ToString() == "0");
assert("P is valid", P.IsValid());
assert("P is zero", P.IsZero());
P.HashAndMapTo("abc");
@@ -127,13 +125,13 @@ namespace mcl { {
Console.WriteLine("TestG2");
G1 P = new G1();
- P.setStr("1 -1 1");
+ P.HashAndMapTo("123");
G2 Q = new G2();
Q.HashAndMapTo("1");
Fr a = new Fr();
Fr b = new Fr();
- a.SetStr("12345678912345673453");
- b.SetStr("230498230982394243424");
+ a.SetDecStr("12345678912345673453");
+ b.SetDecStr("230498230982394243424");
G1 aP = new G1();
G2 bQ = new G2();
aP.Mul(P, a);
diff --git a/include/mcl/bn.h b/include/mcl/bn.h index ccb40a7..4f224ac 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -109,7 +109,7 @@ MBN_DLL_API int MBN_Fr_isOne(const MBN_Fr *x); MBN_DLL_API void MBN_Fr_setByCSPRNG(MBN_Fr *x); // hash(s) and set x -MBN_DLL_API void MBN_hashToFr(MBN_Fr *x, const void *buf, size_t bufSize); +MBN_DLL_API int MBN_hashToFr(MBN_Fr *x, const void *buf, size_t bufSize); // return strlen(buf) if sucess else 0 MBN_DLL_API size_t MBN_Fr_getDecStr(char *buf, size_t maxBufSize, const MBN_Fr *x); diff --git a/src/bn_c.cpp b/src/bn_c.cpp index ae67501..f05bac5 100644 --- a/src/bn_c.cpp +++ b/src/bn_c.cpp @@ -193,9 +193,13 @@ void MBN_Fr_setByCSPRNG(MBN_Fr *x) } // hash(buf) and set x -void MBN_hashToFr(MBN_Fr *x, const void *buf, size_t bufSize) +int MBN_hashToFr(MBN_Fr *x, const void *buf, size_t bufSize) + try { cast(x)->setHashOf(buf, bufSize); + return 0; +} catch (std::exception& e) { + return -1; } size_t MBN_Fr_getDecStr(char *buf, size_t maxBufSize, const MBN_Fr *x) |