aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2019-03-29 14:31:02 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2019-03-29 14:31:02 +0800
commit38044ada41d699d43d72d6ddd54ed2f9de2ab583 (patch)
tree5b316fcb4a9ab67de3181195d3176666e3fdbd48
parent385d37561ead4e3ed6bde6d0b302ab9e89800e02 (diff)
downloaddexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar.gz
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar.bz2
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar.lz
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar.xz
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.tar.zst
dexon-bls-38044ada41d699d43d72d6ddd54ed2f9de2ab583.zip
[cs] format source
-rw-r--r--ffi/cs/App.config6
-rw-r--r--ffi/cs/bls.cs514
-rw-r--r--ffi/cs/bls.csproj3
-rw-r--r--ffi/cs/bls_test.cs248
-rw-r--r--ffi/cs/readme-ja.md36
5 files changed, 447 insertions, 360 deletions
diff --git a/ffi/cs/App.config b/ffi/cs/App.config
index d740e88..e89424b 100644
--- a/ffi/cs/App.config
+++ b/ffi/cs/App.config
@@ -1,6 +1,6 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
-</configuration> \ No newline at end of file
+</configuration>
diff --git a/ffi/cs/bls.cs b/ffi/cs/bls.cs
index 417550a..0afb9ca 100644
--- a/ffi/cs/bls.cs
+++ b/ffi/cs/bls.cs
@@ -2,8 +2,10 @@
using System.Text;
using System.Runtime.InteropServices;
-namespace mcl {
- class BLS {
+namespace mcl
+{
+ class BLS
+ {
public const int BN254 = 0;
public const int BLS12_381 = 5;
@@ -17,289 +19,325 @@ namespace mcl {
public const int PUBLICKEY_UNIT_SIZE = FP_UNIT_SIZE * 3 * 2;
public const int SIGNATURE_UNIT_SIZE = FP_UNIT_SIZE * 3;
+ public const int ID_SERIALIZE_SIZE = FR_UNIT_SIZE * 8;
+ public const int SECRETKEY_SERIALIZE_SIZE = FR_UNIT_SIZE * 8;
+ public const int PUBLICKEY_SERIALIZE_SIZE = FP_UNIT_SIZE * 8 * 2;
+ public const int SIGNATURE_SERIALIZE_SIZE = FP_UNIT_SIZE * 8;
+
public const string dllName = FP_UNIT_SIZE == 4 ? "bls256.dll" : "bls384_256.dll";
- [DllImport(dllName)]
- public static extern int blsInit(int curveType, int compiledTimeVar);
+ [DllImport(dllName)]
+ public static extern int blsInit(int curveType, int compiledTimeVar);
- [DllImport(dllName)] public static extern void blsIdSetInt(ref Id id, int x);
- [DllImport(dllName)] public static extern int blsIdSetDecStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern int blsIdSetHexStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern ulong blsIdGetDecStr([Out]StringBuilder buf, ulong maxBufSize, in Id id);
- [DllImport(dllName)] public static extern ulong blsIdGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in Id id);
+ [DllImport(dllName)] public static extern void blsIdSetInt(ref Id id, int x);
+ [DllImport(dllName)] public static extern int blsIdSetDecStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ [DllImport(dllName)] public static extern int blsIdSetHexStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsIdGetDecStr([Out]StringBuilder buf, ulong maxBufSize, in Id id);
+ [DllImport(dllName)] public static extern ulong blsIdGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in Id id);
- [DllImport(dllName)] public static extern ulong blsIdSerialize([Out]StringBuilder buf, ulong maxBufSize, in Id id);
- [DllImport(dllName)] public static extern ulong blsSecretKeySerialize([Out]StringBuilder buf, ulong maxBufSize, in SecretKey sec);
- [DllImport(dllName)] public static extern ulong blsPublicKeySerialize([Out]StringBuilder buf, ulong maxBufSize, in PublicKey pub);
- [DllImport(dllName)] public static extern ulong blsSignatureSerialize([Out]StringBuilder buf, ulong maxBufSize, in Signature sig);
- [DllImport(dllName)] public static extern int blsIdDeserialize(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern int blsSecretKeyDeserialize(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern int blsPublicKeyDeserialize(ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern int blsSignatureDeserialize(ref Signature sig, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsIdSerialize([Out]byte[] buf, ulong maxBufSize, in Id id);
+ [DllImport(dllName)] public static extern ulong blsSecretKeySerialize([Out]byte[] buf, ulong maxBufSize, in SecretKey sec);
+ [DllImport(dllName)] public static extern ulong blsPublicKeySerialize([Out]byte[] buf, ulong maxBufSize, in PublicKey pub);
+ [DllImport(dllName)] public static extern ulong blsSignatureSerialize([Out]byte[] buf, ulong maxBufSize, in Signature sig);
+ [DllImport(dllName)] public static extern ulong blsIdDeserialize(ref Id id, [In]byte[] buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsSecretKeyDeserialize(ref SecretKey sec, [In]byte[] buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsPublicKeyDeserialize(ref PublicKey pub, [In]byte[] buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsSignatureDeserialize(ref Signature sig, [In]byte[] buf, ulong bufSize);
[DllImport(dllName)] public static extern int blsIdIsEqual(in Id lhs, in Id rhs);
[DllImport(dllName)] public static extern int blsSecretKeyIsEqual(in SecretKey lhs, in SecretKey rhs);
[DllImport(dllName)] public static extern int blsPublicKeyIsEqual(in PublicKey lhs, in PublicKey rhs);
- [DllImport(dllName)] public static extern int blsSignatureIsEqual(in Signature lhs, in Signature rhs);
+ [DllImport(dllName)] public static extern int blsSignatureIsEqual(in Signature lhs, in Signature rhs);
// add
[DllImport(dllName)] public static extern void blsSecretKeyAdd(ref SecretKey sec, in SecretKey rhs);
[DllImport(dllName)] public static extern void blsPublicKeyAdd(ref PublicKey pub, in PublicKey rhs);
- [DllImport(dllName)] public static extern void blsSignatureAdd(ref Signature sig, in Signature rhs);
+ [DllImport(dllName)] public static extern void blsSignatureAdd(ref Signature sig, in Signature rhs);
// hash buf and set
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- /*
+ /*
set secretKey if system has /dev/urandom or CryptGenRandom
return 0 if success else -1
*/
- [DllImport(dllName)] public static extern int blsSecretKeySetByCSPRNG(ref SecretKey sec);
+ [DllImport(dllName)] public static extern int blsSecretKeySetByCSPRNG(ref SecretKey sec);
[DllImport(dllName)] public static extern void blsGetPublicKey(ref PublicKey pub, in SecretKey sec);
- [DllImport(dllName)] public static extern void blsGetPop(ref Signature sig, in SecretKey sec);
+ [DllImport(dllName)] public static extern void blsGetPop(ref Signature sig, in SecretKey sec);
- // return 0 if success
- [DllImport(dllName)] public static extern int blsSecretKeyShare(ref SecretKey sec, in SecretKey msk, ulong k, in Id id);
- [DllImport(dllName)] public static extern int blsPublicKeyShare(ref PublicKey pub, in PublicKey mpk, ulong k, in Id id);
+ // return 0 if success
+ [DllImport(dllName)] public static extern int blsSecretKeyShare(ref SecretKey sec, in SecretKey msk, ulong k, in Id id);
+ [DllImport(dllName)] public static extern int blsPublicKeyShare(ref PublicKey pub, in PublicKey mpk, ulong k, in Id id);
- [DllImport(dllName)] public static extern int blsSecretKeyRecover(ref SecretKey sec, in SecretKey secVec, in Id idVec, ulong n);
- [DllImport(dllName)] public static extern int blsPublicKeyRecover(ref PublicKey pub, in PublicKey pubVec, in Id idVec, ulong n);
- [DllImport(dllName)] public static extern int blsSignatureRecover(ref Signature sig, in Signature sigVec, in Id idVec, ulong n);
+ [DllImport(dllName)] public static extern int blsSecretKeyRecover(ref SecretKey sec, in SecretKey secVec, in Id idVec, ulong n);
+ [DllImport(dllName)] public static extern int blsPublicKeyRecover(ref PublicKey pub, in PublicKey pubVec, in Id idVec, ulong n);
+ [DllImport(dllName)] public static extern int blsSignatureRecover(ref Signature sig, in Signature sigVec, in Id idVec, ulong n);
- [DllImport(dllName)] public static extern void blsSign(ref Signature sig, in SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
+ [DllImport(dllName)] public static extern void blsSign(ref Signature sig, in SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
- // return 1 if valid
- [DllImport(dllName)] public static extern int blsVerify(in Signature sig, in PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
- [DllImport(dllName)] public static extern int blsVerifyPop(in Signature sig, in PublicKey pub);
+ // return 1 if valid
+ [DllImport(dllName)] public static extern int blsVerify(in Signature sig, in PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
+ [DllImport(dllName)] public static extern int blsVerifyPop(in Signature sig, in PublicKey pub);
- //////////////////////////////////////////////////////////////////////////
- // the following apis will be removed
+ //////////////////////////////////////////////////////////////////////////
+ // the following apis will be removed
- // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
- [DllImport(dllName)] public static extern int blsIdSetLittleEndian(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- /*
+ // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
+ [DllImport(dllName)] public static extern int blsIdSetLittleEndian(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ /*
return written byte size if success else 0
*/
- [DllImport(dllName)] public static extern ulong blsIdGetLittleEndian([Out]StringBuilder buf, ulong maxBufSize, in Id id);
+ [DllImport(dllName)] public static extern ulong blsIdGetLittleEndian([Out]StringBuilder buf, ulong maxBufSize, in Id id);
- // return 0 if success
- // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
- [DllImport(dllName)] public static extern int blsSecretKeySetLittleEndian(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern int blsSecretKeySetDecStr(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ // return 0 if success
+ // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
+ [DllImport(dllName)] public static extern int blsSecretKeySetLittleEndian(ref SecretKey sec, [In]byte[] buf, ulong bufSize);
+ [DllImport(dllName)] public static extern int blsSecretKeySetDecStr(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
[DllImport(dllName)] public static extern int blsSecretKeySetHexStr(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
/*
return written byte size if success else 0
*/
- [DllImport(dllName)] public static extern ulong blsSecretKeyGetLittleEndian([Out]StringBuilder buf, ulong maxBufSize, in SecretKey sec);
- /*
+ [DllImport(dllName)] public static extern ulong blsSecretKeyGetLittleEndian([Out]byte[] buf, ulong maxBufSize, in SecretKey sec);
+ /*
return strlen(buf) if success else 0
buf is '\0' terminated
*/
[DllImport(dllName)] public static extern ulong blsSecretKeyGetDecStr([Out]StringBuilder buf, ulong maxBufSize, in SecretKey sec);
[DllImport(dllName)] public static extern ulong blsSecretKeyGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in SecretKey sec);
- [DllImport(dllName)] public static extern int blsPublicKeySetHexStr(ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern ulong blsPublicKeyGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in PublicKey pub);
- [DllImport(dllName)] public static extern int blsSignatureSetHexStr(ref Signature sig, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
- [DllImport(dllName)] public static extern ulong blsSignatureGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in Signature sig);
+ [DllImport(dllName)] public static extern int blsPublicKeySetHexStr(ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsPublicKeyGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in PublicKey pub);
+ [DllImport(dllName)] public static extern int blsSignatureSetHexStr(ref Signature sig, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
+ [DllImport(dllName)] public static extern ulong blsSignatureGetHexStr([Out]StringBuilder buf, ulong maxBufSize, in Signature sig);
- public static void Init(int curveType = BN254)
- {
- if (!System.Environment.Is64BitProcess) {
- throw new PlatformNotSupportedException("not 64-bit system");
- }
- int err = blsInit(curveType, COMPILED_TIME_VAR);
- if (err != 0) {
- throw new ArgumentException("blsInit");
- }
- }
+ public static void Init(int curveType = BN254) {
+ if (!System.Environment.Is64BitProcess) {
+ throw new PlatformNotSupportedException("not 64-bit system");
+ }
+ int err = blsInit(curveType, COMPILED_TIME_VAR);
+ if (err != 0) {
+ throw new ArgumentException("blsInit");
+ }
+ }
[StructLayout(LayoutKind.Sequential)]
- public unsafe struct Id {
+ public unsafe struct Id
+ {
private fixed ulong v[ID_UNIT_SIZE];
- public bool IsEqual(in Id rhs)
- {
+ public byte[] Serialize() {
+ byte[] buf = new byte[ID_SERIALIZE_SIZE];
+ ulong n = blsIdSerialize(buf, (ulong)buf.Length, this);
+ if (n == 0) {
+ throw new ArithmeticException("blsIdSerialize");
+ }
+ return buf;
+ }
+ public void Deserialize(byte[] buf) {
+ ulong n = blsIdDeserialize(ref this, buf, (ulong)buf.Length);
+ if (n == 0) {
+ throw new ArithmeticException("blsIdDeserialize");
+ }
+ }
+ public bool IsEqual(in Id rhs) {
return blsIdIsEqual(this, rhs) != 0;
}
- public void SetDecStr(in String s)
- {
- if (blsIdSetDecStr(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsIdSetDecSt:" + s);
- }
- }
- public void SetHexStr(in String s)
- {
- if (blsIdSetHexStr(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsIdSetHexStr:" + s);
- }
- }
- public void SetInt(int x)
- {
- blsIdSetInt(ref this, x);
- }
- public string GetDecStr()
- {
- StringBuilder sb = new StringBuilder(1024);
- ulong size = blsIdGetDecStr(sb, (ulong)sb.Capacity, this);
- if (size == 0) {
- throw new ArgumentException("blsIdGetDecStr");
- }
- return sb.ToString(0, (int)size);
- }
- public string GetHexStr()
- {
- StringBuilder sb = new StringBuilder(1024);
- ulong size = blsIdGetHexStr(sb, (ulong)sb.Capacity, this);
- if (size == 0) {
- throw new ArgumentException("blsIdGetHexStr");
- }
- return sb.ToString(0, (int)size);
- }
- }
+ public void SetDecStr(string s) {
+ if (blsIdSetDecStr(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsIdSetDecSt:" + s);
+ }
+ }
+ public void SetHexStr(string s) {
+ if (blsIdSetHexStr(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsIdSetHexStr:" + s);
+ }
+ }
+ public void SetInt(int x) {
+ blsIdSetInt(ref this, x);
+ }
+ public string GetDecStr() {
+ StringBuilder sb = new StringBuilder(1024);
+ ulong size = blsIdGetDecStr(sb, (ulong)sb.Capacity, this);
+ if (size == 0) {
+ throw new ArgumentException("blsIdGetDecStr");
+ }
+ return sb.ToString(0, (int)size);
+ }
+ public string GetHexStr() {
+ StringBuilder sb = new StringBuilder(1024);
+ ulong size = blsIdGetHexStr(sb, (ulong)sb.Capacity, this);
+ if (size == 0) {
+ throw new ArgumentException("blsIdGetHexStr");
+ }
+ return sb.ToString(0, (int)size);
+ }
+ }
[StructLayout(LayoutKind.Sequential)]
- public unsafe struct SecretKey {
+ public unsafe struct SecretKey
+ {
private fixed ulong v[SECRETKEY_UNIT_SIZE];
- public bool IsEqual(in SecretKey rhs)
- {
- return blsSecretKeyIsEqual(this, rhs) != 0;
- }
- public void SetHexStr(in String s)
- {
- if (blsSecretKeySetHexStr(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsSecretKeySetHexStr:" + s);
- }
- }
- public string GetHexStr()
- {
- StringBuilder sb = new StringBuilder(1024);
- ulong size = blsSecretKeyGetHexStr(sb, (ulong)sb.Capacity, this);
- if (size == 0) {
- throw new ArgumentException("mclBnFr_getStr");
- }
- return sb.ToString(0, (int)size);
- }
- public void Add(in SecretKey rhs)
- {
- blsSecretKeyAdd(ref this, rhs);
- }
- public void SetByCSPRNG()
- {
- blsSecretKeySetByCSPRNG(ref this);
- }
- public void SetHashOf(in string s)
- {
- if (blsHashToSecretKey(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsHashToSecretKey");
- }
- }
- public PublicKey GetPublicKey()
- {
- PublicKey pub = new PublicKey();
- blsGetPublicKey(ref pub, this);
- return pub;
- }
- public Signature Signature(String m)
- {
- Signature Signature = new Signature();
- blsSign(ref Signature, this, m, (ulong)m.Length);
- return Signature;
- }
+ public byte[] Serialize() {
+ byte[] buf = new byte[SECRETKEY_SERIALIZE_SIZE];
+ ulong n = blsSecretKeySerialize(buf, (ulong)buf.Length, this);
+ if (n == 0) {
+ throw new ArithmeticException("blsSecretKeySerialize");
+ }
+ return buf;
+ }
+ public void Deserialize(byte[] buf) {
+ ulong n = blsSecretKeyDeserialize(ref this, buf, (ulong)buf.Length);
+ if (n == 0) {
+ throw new ArithmeticException("blsSecretKeyDeserialize");
+ }
+ }
+ public bool IsEqual(in SecretKey rhs) {
+ return blsSecretKeyIsEqual(this, rhs) != 0;
+ }
+ public void SetHexStr(string s) {
+ if (blsSecretKeySetHexStr(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsSecretKeySetHexStr:" + s);
+ }
+ }
+ public string GetHexStr() {
+ StringBuilder sb = new StringBuilder(1024);
+ ulong size = blsSecretKeyGetHexStr(sb, (ulong)sb.Capacity, this);
+ if (size == 0) {
+ throw new ArgumentException("mclBnFr_getStr");
+ }
+ return sb.ToString(0, (int)size);
+ }
+ public void Add(in SecretKey rhs) {
+ blsSecretKeyAdd(ref this, rhs);
+ }
+ public void SetByCSPRNG() {
+ blsSecretKeySetByCSPRNG(ref this);
+ }
+ public void SetHashOf(string s) {
+ if (blsHashToSecretKey(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsHashToSecretKey");
+ }
+ }
+ public PublicKey GetPublicKey() {
+ PublicKey pub = new PublicKey();
+ blsGetPublicKey(ref pub, this);
+ return pub;
+ }
+ public Signature Signature(string m) {
+ Signature Signature = new Signature();
+ blsSign(ref Signature, this, m, (ulong)m.Length);
+ return Signature;
+ }
+ }
+ // secretKey = sum_{i=0}^{msk.Length - 1} msk[i] * id^i
+ public static SecretKey ShareSecretKey(in SecretKey[] msk, in Id id) {
+ SecretKey sec = new SecretKey();
+ if (blsSecretKeyShare(ref sec, msk[0], (ulong)msk.Length, id) != 0) {
+ throw new ArgumentException("GetSecretKeyForId:" + id.ToString());
+ }
+ return sec;
+ }
+ public static SecretKey RecoverSecretKey(in SecretKey[] secs, in Id[] ids) {
+ SecretKey sec = new SecretKey();
+ if (blsSecretKeyRecover(ref sec, secs[0], ids[0], (ulong)secs.Length) != 0) {
+ throw new ArgumentException("Recover");
+ }
+ return sec;
}
- // secretKey = sum_{i=0}^{msk.Length - 1} msk[i] * id^i
- public static SecretKey ShareSecretKey(in SecretKey[] msk, in Id id)
- {
- SecretKey sec = new SecretKey();
- if (blsSecretKeyShare(ref sec, msk[0], (ulong)msk.Length, id) != 0) {
- throw new ArgumentException("GetSecretKeyForId:" + id.ToString());
- }
- return sec;
- }
- public static SecretKey RecoverSecretKey(in SecretKey[] secs, in Id[] ids)
- {
- SecretKey sec = new SecretKey();
- if (blsSecretKeyRecover(ref sec, secs[0], ids[0], (ulong)secs.Length) != 0) {
- throw new ArgumentException("Recover");
- }
- return sec;
- }
[StructLayout(LayoutKind.Sequential)]
- public unsafe struct PublicKey {
+ public unsafe struct PublicKey
+ {
private fixed ulong v[PUBLICKEY_UNIT_SIZE];
- public bool IsEqual(in PublicKey rhs)
- {
- return blsPublicKeyIsEqual(this, rhs) != 0;
- }
- public void SetStr(in String s)
- {
- if (blsPublicKeySetHexStr(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsPublicKeySetStr:" + s);
- }
- }
- public string GetHexStr()
- {
- StringBuilder sb = new StringBuilder(1024);
- ulong size = blsPublicKeyGetHexStr(sb, (ulong)sb.Capacity, this);
- if (size == 0) {
- throw new ArgumentException("blsPublicKeyGetStr");
- }
- return sb.ToString(0, (int)size);
- }
- public void Add(in PublicKey rhs)
- {
- blsPublicKeyAdd(ref this, rhs);
- }
- public bool Verify(in Signature sig, in string m)
- {
- return blsVerify(sig, this, m, (ulong)m.Length) == 1;
- }
- }
- // publicKey = sum_{i=0}^{mpk.Length - 1} mpk[i] * id^i
- public static PublicKey SharePublicKey(in PublicKey[] mpk, in Id id)
- {
- PublicKey pub = new PublicKey();
- if (blsPublicKeyShare(ref pub, mpk[0], (ulong)mpk.Length, id) != 0) {
- throw new ArgumentException("GetPublicKeyForId:" + id.ToString());
- }
- return pub;
- }
- public static PublicKey RecoverPublicKey(in PublicKey[] pubs, in Id[] ids)
- {
- PublicKey pub = new PublicKey();
- if (blsPublicKeyRecover(ref pub, pubs[0], ids[0], (ulong)pubs.Length) != 0) {
- throw new ArgumentException("Recover");
- }
- return pub;
- }
+ public byte[] Serialize() {
+ byte[] buf = new byte[PUBLICKEY_SERIALIZE_SIZE];
+ ulong n = blsPublicKeySerialize(buf, (ulong)buf.Length, this);
+ if (n == 0) {
+ throw new ArithmeticException("blsPublicKeySerialize");
+ }
+ return buf;
+ }
+ public void Deserialize(byte[] buf) {
+ ulong n = blsPublicKeyDeserialize(ref this, buf, (ulong)buf.Length);
+ if (n == 0) {
+ throw new ArithmeticException("blsPublicKeyDeserialize");
+ }
+ }
+ public bool IsEqual(in PublicKey rhs) {
+ return blsPublicKeyIsEqual(this, rhs) != 0;
+ }
+ public void SetStr(string s) {
+ if (blsPublicKeySetHexStr(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsPublicKeySetStr:" + s);
+ }
+ }
+ public string GetHexStr() {
+ StringBuilder sb = new StringBuilder(1024);
+ ulong size = blsPublicKeyGetHexStr(sb, (ulong)sb.Capacity, this);
+ if (size == 0) {
+ throw new ArgumentException("blsPublicKeyGetStr");
+ }
+ return sb.ToString(0, (int)size);
+ }
+ public void Add(in PublicKey rhs) {
+ blsPublicKeyAdd(ref this, rhs);
+ }
+ public bool Verify(in Signature sig, string m) {
+ return blsVerify(sig, this, m, (ulong)m.Length) == 1;
+ }
+ }
+ // publicKey = sum_{i=0}^{mpk.Length - 1} mpk[i] * id^i
+ public static PublicKey SharePublicKey(in PublicKey[] mpk, in Id id) {
+ PublicKey pub = new PublicKey();
+ if (blsPublicKeyShare(ref pub, mpk[0], (ulong)mpk.Length, id) != 0) {
+ throw new ArgumentException("GetPublicKeyForId:" + id.ToString());
+ }
+ return pub;
+ }
+ public static PublicKey RecoverPublicKey(in PublicKey[] pubs, in Id[] ids) {
+ PublicKey pub = new PublicKey();
+ if (blsPublicKeyRecover(ref pub, pubs[0], ids[0], (ulong)pubs.Length) != 0) {
+ throw new ArgumentException("Recover");
+ }
+ return pub;
+ }
[StructLayout(LayoutKind.Sequential)]
- public unsafe struct Signature {
+ public unsafe struct Signature
+ {
private fixed ulong v[SIGNATURE_UNIT_SIZE];
- public bool IsEqual(in Signature rhs)
- {
- return blsSignatureIsEqual(this, rhs) != 0;
- }
- public void SetStr(in String s)
- {
- if (blsSignatureSetHexStr(ref this, s, (ulong)s.Length) != 0) {
- throw new ArgumentException("blsSignatureSetStr:" + s);
- }
- }
- public string GetHexStr()
- {
- StringBuilder sb = new StringBuilder(1024);
- ulong size = blsSignatureGetHexStr(sb, (ulong)sb.Capacity, this);
- if (size == 0) {
- throw new ArgumentException("blsSignatureGetStr");
- }
- return sb.ToString(0, (int)size);
- }
- public void Add(in Signature rhs)
- {
- blsSignatureAdd(ref this, rhs);
- }
- }
- public static Signature RecoverSign(in Signature[] signs, in Id[] ids)
- {
- Signature Signature = new Signature();
- if (blsSignatureRecover(ref Signature, signs[0], ids[0], (ulong)signs.Length) != 0) {
- throw new ArgumentException("Recover");
- }
- return Signature;
- }
+ public byte[] Serialize() {
+ byte[] buf = new byte[SIGNATURE_SERIALIZE_SIZE];
+ ulong n = blsSignatureSerialize(buf, (ulong)buf.Length, this);
+ if (n == 0) {
+ throw new ArithmeticException("blsSignatureSerialize");
+ }
+ return buf;
+ }
+ public void Deserialize(byte[] buf) {
+ ulong n = blsSignatureDeserialize(ref this, buf, (ulong)buf.Length);
+ if (n == 0) {
+ throw new ArithmeticException("blsSignatureDeserialize");
+ }
+ }
+ public bool IsEqual(in Signature rhs) {
+ return blsSignatureIsEqual(this, rhs) != 0;
+ }
+ public void SetStr(string s) {
+ if (blsSignatureSetHexStr(ref this, s, (ulong)s.Length) != 0) {
+ throw new ArgumentException("blsSignatureSetStr:" + s);
+ }
+ }
+ public string GetHexStr() {
+ StringBuilder sb = new StringBuilder(1024);
+ ulong size = blsSignatureGetHexStr(sb, (ulong)sb.Capacity, this);
+ if (size == 0) {
+ throw new ArgumentException("blsSignatureGetStr");
+ }
+ return sb.ToString(0, (int)size);
+ }
+ public void Add(in Signature rhs) {
+ blsSignatureAdd(ref this, rhs);
+ }
+ }
+ public static Signature RecoverSign(in Signature[] signs, in Id[] ids) {
+ Signature Signature = new Signature();
+ if (blsSignatureRecover(ref Signature, signs[0], ids[0], (ulong)signs.Length) != 0) {
+ throw new ArgumentException("Recover");
+ }
+ return Signature;
+ }
}
}
diff --git a/ffi/cs/bls.csproj b/ffi/cs/bls.csproj
index f2cbee1..13492d3 100644
--- a/ffi/cs/bls.csproj
+++ b/ffi/cs/bls.csproj
@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>bls</RootNamespace>
<AssemblyName>bls</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PublishUrl>publish\</PublishUrl>
@@ -39,6 +39,7 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<LangVersion>7.2</LangVersion>
+ <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>..\..\bin\</OutputPath>
diff --git a/ffi/cs/bls_test.cs b/ffi/cs/bls_test.cs
index f443c7e..e021e19 100644
--- a/ffi/cs/bls_test.cs
+++ b/ffi/cs/bls_test.cs
@@ -1,116 +1,131 @@
using System;
-namespace mcl {
- using static BLS;
- class BLSTest {
- 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 id1 = new Id();
- id1.SetDecStr("255");
- assert("GetStr(10)", id1.GetDecStr() == "255");
- assert("GetStr(16)", id1.GetHexStr() == "ff");
- Id id2 = new Id();
+namespace mcl
+{
+ using static BLS;
+ class BLSTest
+ {
+ 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 id1;
+ id1.SetDecStr("255");
+ assert("GetStr(10)", id1.GetDecStr() == "255");
+ assert("GetStr(16)", id1.GetHexStr() == "ff");
+ Id id2;
id2.SetInt(255);
assert("IsEqual", id1.IsEqual(id2));
- }
- static void TestSecretKey()
- {
- Console.WriteLine("TestSecretKey");
- SecretKey sec = new SecretKey();
- sec.SetHexStr("ff");
- assert("GetHexStr()", sec.GetHexStr() == "ff");
- {
- SecretKey sec2 = new SecretKey();
- sec.SetHexStr("321");
- sec2.SetHexStr("4000");
- sec.Add(sec2);
- assert("sec.Add", sec.GetHexStr() == "4321");
- sec.SetByCSPRNG();
- Console.WriteLine("sec.Init={0}", sec.GetHexStr());
- }
- }
- static void TestPublicKey()
- {
- Console.WriteLine("TestPublicKey");
- SecretKey sec = new SecretKey();
- sec.SetByCSPRNG();
- PublicKey pub = sec.GetPublicKey();
- String s = pub.GetHexStr();
- Console.WriteLine("pub={0}", s);
- PublicKey pub2 = new PublicKey();
- pub2.SetStr(s);
- assert("pub.SetStr", pub.IsEqual(pub2));
- }
- static void TestSign()
- {
- Console.WriteLine("TestSign");
- SecretKey sec = new SecretKey();
- sec.SetByCSPRNG();
- PublicKey pub = sec.GetPublicKey();
- String m = "abc";
- Signature sig = sec.Signature(m);
+ }
+ static void TestSecretKey() {
+ Console.WriteLine("TestSecretKey");
+ SecretKey sec;
+ sec.SetHexStr("ff");
+ assert("GetHexStr()", sec.GetHexStr() == "ff");
+ {
+ SecretKey sec2;
+ sec.SetHexStr("321");
+ sec2.SetHexStr("4000");
+ sec.Add(sec2);
+ assert("sec.Add", sec.GetHexStr() == "4321");
+ sec.SetByCSPRNG();
+ Console.WriteLine("sec.Init={0}", sec.GetHexStr());
+ }
+ {
+ SecretKey sec2;
+ byte[] buf = sec.Serialize();
+ sec2.Deserialize(buf);
+ assert("serialize", sec2.IsEqual(sec));
+ }
+ }
+ static void TestPublicKey() {
+ Console.WriteLine("TestPublicKey");
+ SecretKey sec;
+ sec.SetByCSPRNG();
+ PublicKey pub = sec.GetPublicKey();
+ string s = pub.GetHexStr();
+ Console.WriteLine("pub={0}", s);
+ {
+ PublicKey pub2;
+ pub2.SetStr(s);
+ assert("pub.SetStr", pub.IsEqual(pub2));
+ }
+ {
+ PublicKey pub2;
+ byte[] buf = pub.Serialize();
+ pub2.Deserialize(buf);
+ assert("serialize", pub2.IsEqual(pub));
+ }
+ }
+ static void TestSign() {
+ Console.WriteLine("TestSign");
+ SecretKey sec = new SecretKey();
+ sec.SetByCSPRNG();
+ PublicKey pub = sec.GetPublicKey();
+ string m = "abc";
+ Signature sig = sec.Signature(m);
Console.WriteLine("sig={0}", sig.GetHexStr());
- assert("verify", pub.Verify(sig, m));
- assert("not verify", !pub.Verify(sig, m + "a"));
- }
- static void TestSharing()
- {
- Console.WriteLine("TestSharing");
- int k = 5;
- SecretKey[] msk = new SecretKey[k];
- PublicKey[] mpk = new PublicKey[k];
- // make master secretkey
- for (int i = 0; i < k; i++) {
- msk[i].SetByCSPRNG();
- mpk[i] = msk[i].GetPublicKey();
- }
- int n = 30;
- Id[] ids = new Id[n];
- SecretKey[] secs = new SecretKey[n];
- PublicKey[] pubs = new PublicKey[n];
- for (int i = 0; i < n; i++) {
- ids[i].SetInt(i * i + 123);
- secs[i] = ShareSecretKey(msk, ids[i]);
- pubs[i] = SharePublicKey(mpk, ids[i]);
- assert("share publicKey", secs[i].GetPublicKey().IsEqual(pubs[i]));
- }
- string m = "doremi";
- for (int i = 0; i < n; i++) {
- Signature Signature = secs[i].Signature(m);
- assert("Signature.Verify", pubs[i].Verify(Signature, m));
- }
- {
- int[] idxTbl = { 0, 2, 5, 8, 10 };
- assert("idxTbl.Length=k", idxTbl.Length == k);
- Id[] subIds = new Id[k];
- SecretKey[] subSecs = new SecretKey[k];
- PublicKey[] subPubs = new PublicKey[k];
- Signature[] subSigns = new Signature[k];
- for (int i = 0; i < k; i++) {
- int idx = idxTbl[i];
- subIds[i] = ids[idx];
- subSecs[i] = secs[idx];
- subPubs[i] = pubs[idx];
- subSigns[i] = secs[idx].Signature(m);
- }
- SecretKey sec = RecoverSecretKey(subSecs, subIds);
- PublicKey pub = RecoverPublicKey(subPubs, subIds);
- assert("check pub", pub.IsEqual(sec.GetPublicKey()));
- Signature Signature = RecoverSign(subSigns, subIds);
- assert("Signature.verify", pub.Verify(Signature, m));
- }
- }
- static void Main(string[] args)
- {
- try {
+ assert("verify", pub.Verify(sig, m));
+ assert("not verify", !pub.Verify(sig, m + "a"));
+ {
+ Signature sig2;
+ byte[] buf = sig.Serialize();
+ sig2.Deserialize(buf);
+ assert("serialize", sig2.IsEqual(sig));
+ }
+ }
+ static void TestSharing() {
+ Console.WriteLine("TestSharing");
+ int k = 5;
+ SecretKey[] msk = new SecretKey[k];
+ PublicKey[] mpk = new PublicKey[k];
+ // make master secretkey
+ for (int i = 0; i < k; i++) {
+ msk[i].SetByCSPRNG();
+ mpk[i] = msk[i].GetPublicKey();
+ }
+ int n = 30;
+ Id[] ids = new Id[n];
+ SecretKey[] secs = new SecretKey[n];
+ PublicKey[] pubs = new PublicKey[n];
+ for (int i = 0; i < n; i++) {
+ ids[i].SetInt(i * i + 123);
+ secs[i] = ShareSecretKey(msk, ids[i]);
+ pubs[i] = SharePublicKey(mpk, ids[i]);
+ assert("share publicKey", secs[i].GetPublicKey().IsEqual(pubs[i]));
+ }
+ string m = "doremi";
+ for (int i = 0; i < n; i++) {
+ Signature Signature = secs[i].Signature(m);
+ assert("Signature.Verify", pubs[i].Verify(Signature, m));
+ }
+ {
+ int[] idxTbl = { 0, 2, 5, 8, 10 };
+ assert("idxTbl.Length=k", idxTbl.Length == k);
+ Id[] subIds = new Id[k];
+ SecretKey[] subSecs = new SecretKey[k];
+ PublicKey[] subPubs = new PublicKey[k];
+ Signature[] subSigns = new Signature[k];
+ for (int i = 0; i < k; i++) {
+ int idx = idxTbl[i];
+ subIds[i] = ids[idx];
+ subSecs[i] = secs[idx];
+ subPubs[i] = pubs[idx];
+ subSigns[i] = secs[idx].Signature(m);
+ }
+ SecretKey sec = RecoverSecretKey(subSecs, subIds);
+ PublicKey pub = RecoverPublicKey(subPubs, subIds);
+ assert("check pub", pub.IsEqual(sec.GetPublicKey()));
+ Signature Signature = RecoverSign(subSigns, subIds);
+ assert("Signature.verify", pub.Verify(Signature, m));
+ }
+ }
+ static void Main(string[] args) {
+ try {
int[] curveTypeTbl = { BN254, BLS12_381 };
foreach (int curveType in curveTypeTbl) {
Console.WriteLine("curveType={0}", curveType);
@@ -120,18 +135,15 @@ namespace mcl {
TestPublicKey();
TestSign();
TestSharing();
- if (err == 0)
- {
+ if (err == 0) {
Console.WriteLine("all tests succeed");
- }
- else
- {
+ } else {
Console.WriteLine("err={0}", err);
}
}
- } catch (Exception e) {
- Console.WriteLine("ERR={0}", e);
- }
- }
- }
+ } catch (Exception e) {
+ Console.WriteLine("ERR={0}", e);
+ }
+ }
+ }
}
diff --git a/ffi/cs/readme-ja.md b/ffi/cs/readme-ja.md
new file mode 100644
index 0000000..73c4a3e
--- /dev/null
+++ b/ffi/cs/readme-ja.md
@@ -0,0 +1,36 @@
+# BLS署名のC#バインディング
+
+# 必要環境
+
+* Visual Studio 2017(x64) or later
+* C# 7.2 or later
+* .NET Framework 4.5.2 or later
+
+# DLLのビルド方法
+
+Visual Studio 2017の64bit用コマンドプロンプトを開いて
+```
+md work
+cd work
+git clone https://github.com/herumi/cybozulib_ext
+git clone https://github.com/herumi/mcl
+git clone https://github.com/herumi/bls
+cd bls
+mklib dll
+```
+としてbls/binにDLLを作成する。
+
+# サンプルのビルド方法
+
+bls/ffi/cs/bls.slnを開いて実行する。
+
+* 注意 bls256.slnは古いため使わないでください。
+
+# ライセンス
+
+modified new BSD License
+http://opensource.org/licenses/BSD-3-Clause
+
+# 著者
+
+光成滋生 MITSUNARI Shigeo(herumi@nifty.com)