aboutsummaryrefslogtreecommitdiffstats
path: root/include/bls
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-05-28 14:56:35 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-05-28 14:56:35 +0800
commitd6de132a2d40e79eaa944999882c484015c3261a (patch)
tree5a6c3ca1f25b601dca3bd3d33868da23b5da9b2c /include/bls
parentfc086647711e278f3b3eecc2d5c87a882fa944e8 (diff)
downloaddexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar.gz
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar.bz2
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar.lz
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar.xz
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.tar.zst
dexon-bls-d6de132a2d40e79eaa944999882c484015c3261a.zip
move include/* to include/bls*
Diffstat (limited to 'include/bls')
-rw-r--r--include/bls/bls.hpp274
-rw-r--r--include/bls/bls_if.h169
2 files changed, 443 insertions, 0 deletions
diff --git a/include/bls/bls.hpp b/include/bls/bls.hpp
new file mode 100644
index 0000000..7e4eaad
--- /dev/null
+++ b/include/bls/bls.hpp
@@ -0,0 +1,274 @@
+#pragma once
+/**
+ @file
+ @brief BLS threshold signature on BN curve
+ @author MITSUNARI Shigeo(@herumi)
+ @license modified new BSD license
+ http://opensource.org/licenses/BSD-3-Clause
+*/
+#ifndef BLS_MAX_OP_UNIT_SIZE
+ #error "define BLS_MAX_OP_UNIT_SIZE 4(or 6)"
+#endif
+#include <vector>
+#include <string>
+#include <iosfwd>
+#include <stdint.h>
+
+#ifdef _MSC_VER
+ #pragma comment(lib, "mcl.lib")
+ #pragma comment(lib, "bls.lib")
+#endif
+
+namespace bls {
+
+enum {
+ CurveFp254BNb = 0,
+ CurveFp382_1 = 1,
+ CurveFp382_2 = 2
+};
+
+// same value with IoMode of mcl/op.hpp
+enum {
+ IoBin = 2, // binary number
+ IoDec = 10, // decimal number
+ IoHex = 16, // hexadecimal number
+ IoEcComp = 512 // fixed byte representation
+};
+
+namespace impl {
+
+struct SecretKey;
+struct PublicKey;
+struct Sign;
+struct Id;
+
+} // bls::impl
+
+/*
+ BLS signature
+ e : G2 x G1 -> Fp12
+ Q in G2 ; fixed global parameter
+ H : {str} -> G1
+ s : secret key
+ sQ ; public key
+ s H(m) ; signature of m
+ verify ; e(sQ, H(m)) = e(Q, s H(m))
+*/
+
+/*
+ initialize this library
+ call this once before using the other method
+ @param curve [in] type of curve
+ @param maxUnitSize [in] 4 or 6 (specify same value used in compiling for validation)
+ @note init() is not thread safe
+*/
+void init(int curve = CurveFp254BNb, int maxUnitSize = BLS_MAX_OP_UNIT_SIZE);
+size_t getOpUnitSize();
+void getCurveOrder(std::string& str);
+void getFieldOrder(std::string& str);
+
+class SecretKey;
+class PublicKey;
+class Sign;
+class Id;
+
+/*
+ the value of secretKey and Id must be less than
+ r = 0x2523648240000001ba344d8000000007ff9f800000000010a10000000000000d
+ sizeof(uint64_t) * keySize byte
+*/
+const size_t keySize = BLS_MAX_OP_UNIT_SIZE;
+
+typedef std::vector<SecretKey> SecretKeyVec;
+typedef std::vector<PublicKey> PublicKeyVec;
+typedef std::vector<Sign> SignVec;
+typedef std::vector<Id> IdVec;
+
+class Id {
+ uint64_t self_[BLS_MAX_OP_UNIT_SIZE];
+ friend class PublicKey;
+ friend class SecretKey;
+ template<class T, class G> friend struct WrapArray;
+ impl::Id& getInner() { return *reinterpret_cast<impl::Id*>(self_); }
+ const impl::Id& getInner() const { return *reinterpret_cast<const impl::Id*>(self_); }
+public:
+ Id(unsigned int id = 0);
+ bool operator==(const Id& rhs) const;
+ bool operator!=(const Id& rhs) const { return !(*this == rhs); }
+ friend std::ostream& operator<<(std::ostream& os, const Id& id);
+ friend std::istream& operator>>(std::istream& is, Id& id);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
+ bool isZero() const;
+ /*
+ set p[0, .., keySize)
+ @note the value must be less than r
+ */
+ void set(const uint64_t *p);
+
+};
+
+/*
+ s ; secret key
+*/
+class SecretKey {
+ uint64_t self_[BLS_MAX_OP_UNIT_SIZE];
+ template<class T, class G> friend struct WrapArray;
+ impl::SecretKey& getInner() { return *reinterpret_cast<impl::SecretKey*>(self_); }
+ const impl::SecretKey& getInner() const { return *reinterpret_cast<const impl::SecretKey*>(self_); }
+public:
+ SecretKey() : self_() {}
+ bool operator==(const SecretKey& rhs) const;
+ bool operator!=(const SecretKey& rhs) const { return !(*this == rhs); }
+ friend std::ostream& operator<<(std::ostream& os, const SecretKey& sec);
+ friend std::istream& operator>>(std::istream& is, SecretKey& sec);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
+ /*
+ initialize secretKey with random number
+ */
+ void init();
+ /*
+ set secretKey with p[0, .., keySize)
+ @note the value must be less than r
+ */
+ void set(const uint64_t *p);
+ void getPublicKey(PublicKey& pub) const;
+ // constant time sign
+ void sign(Sign& sign, const std::string& m) const;
+ /*
+ make Pop(Proof of Possesion)
+ pop = prv.sign(pub)
+ */
+ void getPop(Sign& pop) const;
+ /*
+ make [s_0, ..., s_{k-1}] to prepare k-out-of-n secret sharing
+ */
+ void getMasterSecretKey(SecretKeyVec& msk, size_t k) const;
+ /*
+ set a secret key for id > 0 from msk
+ */
+ void set(const SecretKeyVec& msk, const Id& id)
+ {
+ set(msk.data(), msk.size(), id);
+ }
+ /*
+ recover secretKey from k secVec
+ */
+ void recover(const SecretKeyVec& secVec, const IdVec& idVec);
+ /*
+ add secret key
+ */
+ void add(const SecretKey& rhs);
+
+ // the following methods are for C api
+ /*
+ the size of msk must be k
+ */
+ void set(const SecretKey *msk, size_t k, const Id& id);
+ void recover(const SecretKey *secVec, const Id *idVec, size_t n);
+};
+
+/*
+ sQ ; public key
+*/
+class PublicKey {
+ uint64_t self_[BLS_MAX_OP_UNIT_SIZE * 2 * 3];
+ friend class SecretKey;
+ friend class Sign;
+ template<class T, class G> friend struct WrapArray;
+ impl::PublicKey& getInner() { return *reinterpret_cast<impl::PublicKey*>(self_); }
+ const impl::PublicKey& getInner() const { return *reinterpret_cast<const impl::PublicKey*>(self_); }
+public:
+ PublicKey() : self_() {}
+ bool operator==(const PublicKey& rhs) const;
+ bool operator!=(const PublicKey& rhs) const { return !(*this == rhs); }
+ friend std::ostream& operator<<(std::ostream& os, const PublicKey& pub);
+ friend std::istream& operator>>(std::istream& is, PublicKey& pub);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
+ /*
+ set public for id from mpk
+ */
+ void set(const PublicKeyVec& mpk, const Id& id)
+ {
+ set(mpk.data(), mpk.size(), id);
+ }
+ /*
+ recover publicKey from k pubVec
+ */
+ void recover(const PublicKeyVec& pubVec, const IdVec& idVec);
+ /*
+ add public key
+ */
+ void add(const PublicKey& rhs);
+
+ // the following methods are for C api
+ void set(const PublicKey *mpk, size_t k, const Id& id);
+ void recover(const PublicKey *pubVec, const Id *idVec, size_t n);
+};
+
+/*
+ s H(m) ; sign
+*/
+class Sign {
+ uint64_t self_[BLS_MAX_OP_UNIT_SIZE * 3];
+ friend class SecretKey;
+ template<class T, class G> friend struct WrapArray;
+ impl::Sign& getInner() { return *reinterpret_cast<impl::Sign*>(self_); }
+ const impl::Sign& getInner() const { return *reinterpret_cast<const impl::Sign*>(self_); }
+public:
+ Sign() : self_() {}
+ bool operator==(const Sign& rhs) const;
+ bool operator!=(const Sign& rhs) const { return !(*this == rhs); }
+ friend std::ostream& operator<<(std::ostream& os, const Sign& s);
+ friend std::istream& operator>>(std::istream& is, Sign& s);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
+ bool verify(const PublicKey& pub, const std::string& m) const;
+ /*
+ verify self(pop) with pub
+ */
+ bool verify(const PublicKey& pub) const;
+ /*
+ recover sign from k signVec
+ */
+ void recover(const SignVec& signVec, const IdVec& idVec);
+ /*
+ add signature
+ */
+ void add(const Sign& rhs);
+
+ // the following methods are for C api
+ void recover(const Sign* signVec, const Id *idVec, size_t n);
+};
+
+/*
+ make master public key [s_0 Q, ..., s_{k-1} Q] from msk
+*/
+inline void getMasterPublicKey(PublicKeyVec& mpk, const SecretKeyVec& msk)
+{
+ const size_t n = msk.size();
+ mpk.resize(n);
+ for (size_t i = 0; i < n; i++) {
+ msk[i].getPublicKey(mpk[i]);
+ }
+}
+
+/*
+ make pop from msk and mpk
+*/
+inline void getPopVec(SignVec& popVec, const SecretKeyVec& msk)
+{
+ const size_t n = msk.size();
+ popVec.resize(n);
+ for (size_t i = 0; i < n; i++) {
+ msk[i].getPop(popVec[i]);
+ }
+}
+
+inline Sign operator+(const Sign& a, const Sign& b) { Sign r(a); r.add(b); return r; }
+inline PublicKey operator+(const PublicKey& a, const PublicKey& b) { PublicKey r(a); r.add(b); return r; }
+inline SecretKey operator+(const SecretKey& a, const SecretKey& b) { SecretKey r(a); r.add(b); return r; }
+
+} //bls
diff --git a/include/bls/bls_if.h b/include/bls/bls_if.h
new file mode 100644
index 0000000..7d617d9
--- /dev/null
+++ b/include/bls/bls_if.h
@@ -0,0 +1,169 @@
+#pragma once
+/**
+ @file
+ @brief C interface of bls.hpp
+ @author MITSUNARI Shigeo(@herumi)
+ @license modified new BSD license
+ http://opensource.org/licenses/BSD-3-Clause
+*/
+#ifndef BLS_MAX_OP_UNIT_SIZE
+ #error "define BLS_MAX_OP_UNIT_SIZE 4(or 6)"
+#endif
+
+#include <stdint.h> // for uint64_t, uint8_t
+#include <stdlib.h> // for size_t
+
+#ifdef _MSC_VER
+#ifdef BLS256_DLL_EXPORT
+#define BLS256_DLL_API __declspec(dllexport)
+#else
+#define BLS256_DLL_API __declspec(dllimport)
+#ifndef BLS_NO_AUTOLINK
+ #if BLS_MAX_OP_UNIT_SIZE == 4
+ #pragma comment(lib, "bls_if256.lib")
+ #endif
+#endif
+#endif
+#else
+#define BLS256_DLL_API
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+ blsCurveFp254BNb = 0,
+ blsCurveFp382_1 = 1,
+ blsCurveFp382_2 = 2
+};
+
+// same value with bls.hpp
+enum {
+ blsIoBin = 2, // binary number
+ blsIoDec = 10, // decimal number
+ blsIoHex = 16, // hexadecimal number
+ blsIoEcComp = 512 // fixed byte representation
+};
+
+typedef struct {
+ uint64_t buf[BLS_MAX_OP_UNIT_SIZE];
+} blsId;
+
+typedef struct {
+ uint64_t buf[BLS_MAX_OP_UNIT_SIZE];
+} blsSecretKey;
+
+typedef struct {
+ uint64_t buf[BLS_MAX_OP_UNIT_SIZE * 2 * 3];
+} blsPublicKey;
+
+typedef struct {
+ uint64_t buf[BLS_MAX_OP_UNIT_SIZE * 3];
+} blsSign;
+
+/*
+ initialize this library
+ call this once before using the other method
+ return 0 if success
+ @note init() is not thread safe
+*/
+BLS256_DLL_API int blsInit(int curve, int maxUnitSize);
+BLS256_DLL_API size_t blsGetOpUnitSize(void);
+// return strlen(buf) if success else 0
+BLS256_DLL_API int blsGetCurveOrder(char *buf, size_t maxBufSize);
+BLS256_DLL_API int blsGetFieldOrder(char *buf, size_t maxBufSize);
+
+BLS256_DLL_API blsId *blsIdCreate(void);
+BLS256_DLL_API void blsIdDestroy(blsId *id);
+// return 1 if same else 0
+BLS256_DLL_API int blsIdIsSame(const blsId *lhs, const blsId *rhs);
+BLS256_DLL_API void blsIdPut(const blsId *id);
+BLS256_DLL_API void blsIdCopy(blsId *dst, const blsId *src);
+
+// return 0 if success
+BLS256_DLL_API int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode);
+
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+BLS256_DLL_API size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode);
+/*
+ access p[0], ..., p[3] if 256-bit curve
+ access p[0], ..., p[5] if 384-bit curve
+*/
+BLS256_DLL_API void blsIdSet(blsId *id, const uint64_t *p);
+
+BLS256_DLL_API blsSecretKey* blsSecretKeyCreate(void);
+BLS256_DLL_API void blsSecretKeyDestroy(blsSecretKey *sec);
+// return 1 if same else 0
+BLS256_DLL_API int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs);
+
+BLS256_DLL_API void blsSecretKeyPut(const blsSecretKey *sec);
+BLS256_DLL_API void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src);
+BLS256_DLL_API void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p);
+// return 0 if success
+BLS256_DLL_API int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+BLS256_DLL_API size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode);
+BLS256_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs);
+
+BLS256_DLL_API void blsSecretKeyInit(blsSecretKey *sec);
+BLS256_DLL_API void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub);
+BLS256_DLL_API void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, size_t size);
+// return 0 if success
+BLS256_DLL_API int blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id);
+// return 0 if success
+BLS256_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n);
+BLS256_DLL_API void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign);
+
+BLS256_DLL_API blsPublicKey *blsPublicKeyCreate(void);
+BLS256_DLL_API void blsPublicKeyDestroy(blsPublicKey *pub);
+// return 1 if same else 0
+BLS256_DLL_API int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs);
+BLS256_DLL_API void blsPublicKeyPut(const blsPublicKey *pub);
+BLS256_DLL_API void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src);
+// return 0 if success
+BLS256_DLL_API int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+BLS256_DLL_API size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode);
+BLS256_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs);
+// return 0 if success
+BLS256_DLL_API int blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id);
+// return 0 if success
+BLS256_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n);
+
+BLS256_DLL_API blsSign *blsSignCreate(void);
+BLS256_DLL_API void blsSignDestroy(blsSign *sign);
+// return 1 if same else 0
+BLS256_DLL_API int blsSignIsSame(const blsSign *lhs, const blsSign *rhs);
+BLS256_DLL_API void blsSignPut(const blsSign *sign);
+BLS256_DLL_API void blsSignCopy(blsSign *dst, const blsSign *src);
+// return 0 if success
+BLS256_DLL_API int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+BLS256_DLL_API size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode);
+BLS256_DLL_API void blsSignAdd(blsSign *sign, const blsSign *rhs);
+// return 0 if success
+BLS256_DLL_API int blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n);
+BLS256_DLL_API int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size);
+
+BLS256_DLL_API int blsSignVerifyPop(const blsSign *sign, const blsPublicKey *pub);
+
+#ifdef __cplusplus
+}
+#endif