github.com/platonnetwork/platon-go@v0.7.6/cases/tool/win/bls_win/include/bls/bls.h (about) 1 #pragma once 2 /** 3 @file 4 @brief C interface of bls.hpp 5 @author MITSUNARI Shigeo(@herumi) 6 @license modified new BSD license 7 http://opensource.org/licenses/BSD-3-Clause 8 */ 9 #define MCLBN_NO_AUTOLINK 10 #include <mcl/bn.h> 11 12 #ifdef BLS_SWAP_G 13 /* 14 error if BLS_SWAP_G is inconsistently used between library and exe 15 */ 16 #undef MCLBN_COMPILED_TIME_VAR 17 #define MCLBN_COMPILED_TIME_VAR ((MCLBN_FR_UNIT_SIZE) * 10 + (MCLBN_FP_UNIT_SIZE) + 100) 18 #endif 19 20 #ifdef _MSC_VER 21 #ifdef BLS_DONT_EXPORT 22 #define BLS_DLL_API 23 #else 24 #ifdef BLS_DLL_EXPORT 25 #define BLS_DLL_API __declspec(dllexport) 26 #else 27 #define BLS_DLL_API __declspec(dllimport) 28 #endif 29 #endif 30 #ifndef BLS_NO_AUTOLINK 31 #if MCLBN_FP_UNIT_SIZE == 4 32 #pragma comment(lib, "bls256.lib") 33 #elif (MCLBN_FP_UNIT_SIZE == 6) && (MCLBN_FR_UNIT_SIZE == 4) 34 #pragma comment(lib, "bls384_256.lib") 35 #elif (MCLBN_FP_UNIT_SIZE == 6) && (MCLBN_FR_UNIT_SIZE == 6) 36 #pragma comment(lib, "bls384.lib") 37 #endif 38 #endif 39 #elif defined(__EMSCRIPTEN__) && !defined(BLS_DONT_EXPORT) 40 #define BLS_DLL_API __attribute__((used)) 41 #elif defined(__wasm__) && !defined(BLS_DONT_EXPORT) 42 #define BLS_DLL_API __attribute__((visibility("default"))) 43 #else 44 #define BLS_DLL_API 45 #endif 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 typedef struct { 52 mclBnFr v; 53 } blsId; 54 55 typedef struct { 56 mclBnFr v; 57 } blsSecretKey; 58 59 typedef struct { 60 #ifdef BLS_SWAP_G 61 mclBnG1 v; 62 #else 63 mclBnG2 v; 64 #endif 65 } blsPublicKey; 66 67 typedef struct { 68 #ifdef BLS_SWAP_G 69 mclBnG2 v; 70 #else 71 mclBnG1 v; 72 #endif 73 } blsSignature; 74 75 /* 76 initialize this library 77 call this once before using the other functions 78 @param curve [in] enum value defined in mcl/bn.h 79 @param compiledTimeVar [in] specify MCLBN_COMPILED_TIME_VAR, 80 which macro is used to make sure that the values 81 are the same when the library is built and used 82 @return 0 if success 83 @note blsInit() is not thread safe 84 */ 85 BLS_DLL_API int blsInit(int curve, int compiledTimeVar); 86 /* 87 set ETH serialization mode for BLS12-381 88 @param ETHserialization [in] 1:enable, 0:disable 89 @note ignore the flag if curve is not BLS12-381 90 */ 91 BLS_DLL_API void blsSetETHserialization(int ETHserialization); 92 93 BLS_DLL_API void blsIdSetInt(blsId *id, int x); 94 95 // sec = buf & (1 << bitLen(r)) - 1 96 // if (sec >= r) sec &= (1 << (bitLen(r) - 1)) - 1 97 // always return 0 98 BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize); 99 // return 0 if success (bufSize <= 64) else -1 100 // set (buf mod r) to sec 101 BLS_DLL_API int blsSecretKeySetLittleEndianMod(blsSecretKey *sec, const void *buf, mclSize bufSize); 102 103 BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec); 104 105 // calculate the has of m and sign the hash 106 BLS_DLL_API void blsSign(blsSignature *sig, const blsSecretKey *sec, const void *m, mclSize size); 107 108 // return 1 if valid 109 BLS_DLL_API int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, mclSize size); 110 111 // return written byte size if success else 0 112 BLS_DLL_API mclSize blsIdSerialize(void *buf, mclSize maxBufSize, const blsId *id); 113 BLS_DLL_API mclSize blsSecretKeySerialize(void *buf, mclSize maxBufSize, const blsSecretKey *sec); 114 BLS_DLL_API mclSize blsPublicKeySerialize(void *buf, mclSize maxBufSize, const blsPublicKey *pub); 115 BLS_DLL_API mclSize blsSignatureSerialize(void *buf, mclSize maxBufSize, const blsSignature *sig); 116 117 // return read byte size if success else 0 118 BLS_DLL_API mclSize blsIdDeserialize(blsId *id, const void *buf, mclSize bufSize); 119 BLS_DLL_API mclSize blsSecretKeyDeserialize(blsSecretKey *sec, const void *buf, mclSize bufSize); 120 BLS_DLL_API mclSize blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, mclSize bufSize); 121 BLS_DLL_API mclSize blsSignatureDeserialize(blsSignature *sig, const void *buf, mclSize bufSize); 122 123 // return 1 if same else 0 124 BLS_DLL_API int blsIdIsEqual(const blsId *lhs, const blsId *rhs); 125 BLS_DLL_API int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey *rhs); 126 BLS_DLL_API int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs); 127 BLS_DLL_API int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs); 128 129 // return 0 if success 130 BLS_DLL_API int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id); 131 BLS_DLL_API int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id); 132 133 BLS_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n); 134 BLS_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n); 135 BLS_DLL_API int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n); 136 137 // add 138 BLS_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs); 139 BLS_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs); 140 BLS_DLL_API void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs); 141 142 /* 143 verify whether a point of an elliptic curve has order r 144 This api affetcs setStr(), deserialize() for G2 on BN or G1/G2 on BLS12 145 @param doVerify [in] does not verify if zero(default 1) 146 Signature = G1, PublicKey = G2 147 */ 148 BLS_DLL_API void blsSignatureVerifyOrder(int doVerify); 149 BLS_DLL_API void blsPublicKeyVerifyOrder(int doVerify); 150 // deserialize under VerifyOrder(true) = deserialize under VerifyOrder(false) + IsValidOrder 151 BLS_DLL_API int blsSignatureIsValidOrder(const blsSignature *sig); 152 BLS_DLL_API int blsPublicKeyIsValidOrder(const blsPublicKey *pub); 153 154 #ifndef BLS_MINIMUM_API 155 156 /* 157 verify X == sY by checking e(X, sQ) = e(Y, Q) 158 @param X [in] 159 @param Y [in] 160 @param pub [in] pub = sQ 161 @return 1 if e(X, pub) = e(Y, Q) else 0 162 */ 163 BLS_DLL_API int blsVerifyPairing(const blsSignature *X, const blsSignature *Y, const blsPublicKey *pub); 164 165 /* 166 sign the hash 167 use the low (bitSize of r) - 1 bit of h 168 return 0 if success else -1 169 NOTE : return false if h is zero or c1 or -c1 value for BN254. see hashTest() in test/bls_test.hpp 170 */ 171 BLS_DLL_API int blsSignHash(blsSignature *sig, const blsSecretKey *sec, const void *h, mclSize size); 172 // return 1 if valid 173 BLS_DLL_API int blsVerifyHash(const blsSignature *sig, const blsPublicKey *pub, const void *h, mclSize size); 174 175 /* 176 verify aggSig with pubVec[0, n) and hVec[0, n) 177 e(aggSig, Q) = prod_i e(hVec[i], pubVec[i]) 178 return 1 if valid 179 @note do not check duplication of hVec 180 */ 181 BLS_DLL_API int blsVerifyAggregatedHashes(const blsSignature *aggSig, const blsPublicKey *pubVec, const void *hVec, size_t sizeofHash, mclSize n); 182 183 // sub 184 BLS_DLL_API void blsSecretKeySub(blsSecretKey *sec, const blsSecretKey *rhs); 185 BLS_DLL_API void blsPublicKeySub(blsPublicKey *pub, const blsPublicKey *rhs); 186 BLS_DLL_API void blsSignatureSub(blsSignature *sig, const blsSignature *rhs); 187 188 // not thread safe version (old blsInit) 189 BLS_DLL_API int blsInitNotThreadSafe(int curve, int compiledTimeVar); 190 191 BLS_DLL_API mclSize blsGetOpUnitSize(void); 192 // return strlen(buf) if success else 0 193 BLS_DLL_API int blsGetCurveOrder(char *buf, mclSize maxBufSize); 194 BLS_DLL_API int blsGetFieldOrder(char *buf, mclSize maxBufSize); 195 196 // return serialized secretKey size 197 BLS_DLL_API int blsGetSerializedSecretKeyByteSize(void); 198 // return serialized publicKey size 199 BLS_DLL_API int blsGetSerializedPublicKeyByteSize(void); 200 // return serialized signature size 201 BLS_DLL_API int blsGetSerializedSignatureByteSize(void); 202 203 // return bytes for serialized G1(=Fp) 204 BLS_DLL_API int blsGetG1ByteSize(void); 205 206 // return bytes for serialized Fr 207 BLS_DLL_API int blsGetFrByteSize(void); 208 209 // get a generator of PublicKey 210 BLS_DLL_API void blsGetGeneratorOfPublicKey(blsPublicKey *pub); 211 212 // return 0 if success 213 BLS_DLL_API int blsIdSetDecStr(blsId *id, const char *buf, mclSize bufSize); 214 BLS_DLL_API int blsIdSetHexStr(blsId *id, const char *buf, mclSize bufSize); 215 216 /* 217 return strlen(buf) if success else 0 218 buf is '\0' terminated 219 */ 220 BLS_DLL_API mclSize blsIdGetDecStr(char *buf, mclSize maxBufSize, const blsId *id); 221 BLS_DLL_API mclSize blsIdGetHexStr(char *buf, mclSize maxBufSize, const blsId *id); 222 223 // hash buf and set SecretKey 224 BLS_DLL_API int blsHashToSecretKey(blsSecretKey *sec, const void *buf, mclSize bufSize); 225 // hash buf and set Signature 226 BLS_DLL_API int blsHashToSignature(blsSignature *sig, const void *buf, mclSize bufSize); 227 #ifndef MCL_DONT_USE_CSPRNG 228 /* 229 set secretKey if system has /dev/urandom or CryptGenRandom 230 return 0 if success else -1 231 */ 232 BLS_DLL_API int blsSecretKeySetByCSPRNG(blsSecretKey *sec); 233 /* 234 set user-defined random function for setByCSPRNG 235 @param self [in] user-defined pointer 236 @param readFunc [in] user-defined function, 237 which writes random bufSize bytes to buf and returns bufSize if success else returns 0 238 @note if self == 0 and readFunc == 0 then set default random function 239 @note not threadsafe 240 */ 241 BLS_DLL_API void blsSetRandFunc(void *self, unsigned int (*readFunc)(void *self, void *buf, unsigned int bufSize)); 242 #endif 243 244 BLS_DLL_API void blsGetPop(blsSignature *sig, const blsSecretKey *sec); 245 246 BLS_DLL_API int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub); 247 ////////////////////////////////////////////////////////////////////////// 248 // the following apis will be removed 249 250 // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r 251 BLS_DLL_API int blsIdSetLittleEndian(blsId *id, const void *buf, mclSize bufSize); 252 /* 253 return written byte size if success else 0 254 */ 255 BLS_DLL_API mclSize blsIdGetLittleEndian(void *buf, mclSize maxBufSize, const blsId *id); 256 257 // return 0 if success 258 BLS_DLL_API int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, mclSize bufSize); 259 BLS_DLL_API int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, mclSize bufSize); 260 /* 261 return written byte size if success else 0 262 */ 263 BLS_DLL_API mclSize blsSecretKeyGetLittleEndian(void *buf, mclSize maxBufSize, const blsSecretKey *sec); 264 /* 265 return strlen(buf) if success else 0 266 buf is '\0' terminated 267 */ 268 BLS_DLL_API mclSize blsSecretKeyGetDecStr(char *buf, mclSize maxBufSize, const blsSecretKey *sec); 269 BLS_DLL_API mclSize blsSecretKeyGetHexStr(char *buf, mclSize maxBufSize, const blsSecretKey *sec); 270 BLS_DLL_API int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, mclSize bufSize); 271 BLS_DLL_API mclSize blsPublicKeyGetHexStr(char *buf, mclSize maxBufSize, const blsPublicKey *pub); 272 BLS_DLL_API int blsSignatureSetHexStr(blsSignature *sig, const char *buf, mclSize bufSize); 273 BLS_DLL_API mclSize blsSignatureGetHexStr(char *buf, mclSize maxBufSize, const blsSignature *sig); 274 275 /* 276 Diffie Hellman key exchange 277 out = sec * pub 278 */ 279 BLS_DLL_API void blsDHKeyExchange(blsPublicKey *out, const blsSecretKey *sec, const blsPublicKey *pub); 280 281 #endif // BLS_MINIMUM_API 282 283 #ifdef __cplusplus 284 } 285 #endif