github.com/platonnetwork/platon-go@v0.7.6/cases/tool/win/bls_win/include/mcl/she.h (about)

     1  #pragma once
     2  /**
     3  	@file
     4  	@brief C api of somewhat homomorphic encryption with one-time multiplication, based on prime-order pairings
     5  	@author MITSUNARI Shigeo(@herumi)
     6  	@license modified new BSD license
     7  	http://opensource.org/licenses/BSD-3-Clause
     8  */
     9  #include <mcl/bn.h>
    10  
    11  #ifdef _MSC_VER
    12  #ifdef MCLSHE_DLL_EXPORT
    13  #define MCLSHE_DLL_API __declspec(dllexport)
    14  #else
    15  #define MCLSHE_DLL_API __declspec(dllimport)
    16  #ifndef MCLSHE_NO_AUTOLINK
    17  	#if MCLBN_FP_UNIT_SIZE == 4
    18  		#pragma comment(lib, "mclshe256.lib")
    19  	#elif MCLBN_FP_UNIT_SIZE == 6
    20  		#pragma comment(lib, "mclshe384.lib")
    21  	#else
    22  		#pragma comment(lib, "mclshe512.lib")
    23  	#endif
    24  #endif
    25  #endif
    26  #else
    27  #ifdef __EMSCRIPTEN__
    28  	#define MCLSHE_DLL_API __attribute__((used))
    29  #elif defined(__wasm__)
    30  	#define MCLSHE_DLL_API __attribute__((visibility("default")))
    31  #else
    32  	#define MCLSHE_DLL_API
    33  #endif
    34  #endif
    35  
    36  #ifdef __cplusplus
    37  extern "C" {
    38  #endif
    39  
    40  typedef struct {
    41  	mclBnFr x;
    42  	mclBnFr y;
    43  } sheSecretKey;
    44  
    45  typedef struct {
    46  	mclBnG1 xP;
    47  	mclBnG2 yQ;
    48  } shePublicKey;
    49  
    50  struct shePrecomputedPublicKey;
    51  
    52  typedef struct {
    53  	mclBnG1 S;
    54  	mclBnG1 T;
    55  } sheCipherTextG1;
    56  
    57  typedef struct {
    58  	mclBnG2 S;
    59  	mclBnG2 T;
    60  } sheCipherTextG2;
    61  
    62  typedef struct {
    63  	mclBnGT g[4];
    64  } sheCipherTextGT;
    65  
    66  typedef struct {
    67  	mclBnFr d[4];
    68  } sheZkpBin;
    69  
    70  typedef struct {
    71  	mclBnFr d[4];
    72  } sheZkpEq;
    73  
    74  typedef struct {
    75  	mclBnFr d[7];
    76  } sheZkpBinEq;
    77  /*
    78  	initialize this library
    79  	call this once before using the other functions
    80  	@param curve [in] enum value defined in mcl/bn.h
    81  	@param compiledTimeVar [in] specify MCLBN_COMPILED_TIME_VAR,
    82  	which macro is used to make sure that the values
    83  	are the same when the library is built and used
    84  	@return 0 if success
    85  	@note sheInit() is thread safe and serialized if it is called simultaneously
    86  	but don't call it while using other functions.
    87  */
    88  MCLSHE_DLL_API int sheInit(int curve, int compiledTimeVar);
    89  
    90  // return written byte size if success else 0
    91  MCLSHE_DLL_API mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec);
    92  MCLSHE_DLL_API mclSize shePublicKeySerialize(void *buf, mclSize maxBufSize, const shePublicKey *pub);
    93  MCLSHE_DLL_API mclSize sheCipherTextG1Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG1 *c);
    94  MCLSHE_DLL_API mclSize sheCipherTextG2Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG2 *c);
    95  MCLSHE_DLL_API mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, const sheCipherTextGT *c);
    96  MCLSHE_DLL_API mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const sheZkpBin *zkp);
    97  MCLSHE_DLL_API mclSize sheZkpEqSerialize(void *buf, mclSize maxBufSize, const sheZkpEq *zkp);
    98  MCLSHE_DLL_API mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *zkp);
    99  
   100  // return read byte size if sucess else 0
   101  MCLSHE_DLL_API mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize);
   102  MCLSHE_DLL_API mclSize shePublicKeyDeserialize(shePublicKey* pub, const void *buf, mclSize bufSize);
   103  MCLSHE_DLL_API mclSize sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, mclSize bufSize);
   104  MCLSHE_DLL_API mclSize sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, mclSize bufSize);
   105  MCLSHE_DLL_API mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, mclSize bufSize);
   106  MCLSHE_DLL_API mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mclSize bufSize);
   107  MCLSHE_DLL_API mclSize sheZkpEqDeserialize(sheZkpEq* zkp, const void *buf, mclSize bufSize);
   108  MCLSHE_DLL_API mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSize);
   109  
   110  /*
   111  	set secretKey if system has /dev/urandom or CryptGenRandom
   112  	return 0 if success
   113  */
   114  MCLSHE_DLL_API int sheSecretKeySetByCSPRNG(sheSecretKey *sec);
   115  
   116  MCLSHE_DLL_API void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec);
   117  
   118  /*
   119  	make table to decode DLP
   120  	return 0 if success
   121  */
   122  MCLSHE_DLL_API int sheSetRangeForDLP(mclSize hashSize);
   123  MCLSHE_DLL_API int sheSetRangeForG1DLP(mclSize hashSize);
   124  MCLSHE_DLL_API int sheSetRangeForG2DLP(mclSize hashSize);
   125  MCLSHE_DLL_API int sheSetRangeForGTDLP(mclSize hashSize);
   126  
   127  /*
   128  	set tryNum to decode DLP
   129  */
   130  MCLSHE_DLL_API void sheSetTryNum(mclSize tryNum);
   131  
   132  /*
   133  	decode G1 via GT if use != 0
   134  	@note faster if tryNum >= 300
   135  */
   136  MCLSHE_DLL_API void sheUseDecG1ViaGT(int use);
   137  /*
   138  	decode G2 via GT if use != 0
   139  	@note faster if tryNum >= 100
   140  */
   141  MCLSHE_DLL_API void sheUseDecG2ViaGT(int use);
   142  /*
   143  	load table for DLP
   144  	return read size if success else 0
   145  */
   146  MCLSHE_DLL_API mclSize sheLoadTableForG1DLP(const void *buf, mclSize bufSize);
   147  MCLSHE_DLL_API mclSize sheLoadTableForG2DLP(const void *buf, mclSize bufSize);
   148  MCLSHE_DLL_API mclSize sheLoadTableForGTDLP(const void *buf, mclSize bufSize);
   149  
   150  /*
   151  	save table for DLP
   152  	return written size if success else 0
   153  */
   154  MCLSHE_DLL_API mclSize sheSaveTableForG1DLP(void *buf, mclSize maxBufSize);
   155  MCLSHE_DLL_API mclSize sheSaveTableForG2DLP(void *buf, mclSize maxBufSize);
   156  MCLSHE_DLL_API mclSize sheSaveTableForGTDLP(void *buf, mclSize maxBufSize);
   157  
   158  // return 0 if success
   159  MCLSHE_DLL_API int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, mclInt m);
   160  MCLSHE_DLL_API int sheEncG2(sheCipherTextG2 *c, const shePublicKey *pub, mclInt m);
   161  MCLSHE_DLL_API int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, mclInt m);
   162  MCLSHE_DLL_API int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *ppub, mclInt m);
   163  MCLSHE_DLL_API int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, mclInt m);
   164  MCLSHE_DLL_API int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, mclInt m);
   165  
   166  /*
   167  	enc large integer
   168  	buf[bufSize] is little endian
   169  	bufSize <= (FrBitSize + 63) & ~63
   170  	return 0 if success
   171  */
   172  MCLSHE_DLL_API int sheEncIntVecG1(sheCipherTextG1 *c, const shePublicKey *pub, const void *buf, mclSize bufSize);
   173  MCLSHE_DLL_API int sheEncIntVecG2(sheCipherTextG2 *c, const shePublicKey *pub, const void *buf, mclSize bufSize);
   174  MCLSHE_DLL_API int sheEncIntVecGT(sheCipherTextGT *c, const shePublicKey *pub, const void *buf, mclSize bufSize);
   175  MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize);
   176  MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize);
   177  MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize);
   178  
   179  /*
   180  	m must be 0 or 1
   181  */
   182  MCLSHE_DLL_API int sheEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePublicKey *pub, int m);
   183  MCLSHE_DLL_API int sheEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePublicKey *pub, int m);
   184  MCLSHE_DLL_API int sheEncWithZkpBinEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpBinEq *zkp, const shePublicKey *pub, int m);
   185  MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *ppub, int m);
   186  MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *ppub, int m);
   187  MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpBinEq *zkp, const shePrecomputedPublicKey *ppub, int m);
   188  
   189  /*
   190  	arbitary m
   191  */
   192  MCLSHE_DLL_API int sheEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const shePublicKey *pub, mclInt m);
   193  MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const shePrecomputedPublicKey *ppub, mclInt m);
   194  
   195  /*
   196  	decode c and set m
   197  	return 0 if success
   198  */
   199  MCLSHE_DLL_API int sheDecG1(mclInt *m, const sheSecretKey *sec, const sheCipherTextG1 *c);
   200  MCLSHE_DLL_API int sheDecG2(mclInt *m, const sheSecretKey *sec, const sheCipherTextG2 *c);
   201  MCLSHE_DLL_API int sheDecGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextGT *c);
   202  /*
   203  	verify zkp
   204  	return 1 if valid
   205  */
   206  MCLSHE_DLL_API int sheVerifyZkpBinG1(const shePublicKey *pub, const sheCipherTextG1 *c, const sheZkpBin *zkp);
   207  MCLSHE_DLL_API int sheVerifyZkpBinG2(const shePublicKey *pub, const sheCipherTextG2 *c, const sheZkpBin *zkp);
   208  MCLSHE_DLL_API int sheVerifyZkpEq(const shePublicKey *pub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpEq *zkp);
   209  MCLSHE_DLL_API int sheVerifyZkpBinEq(const shePublicKey *pub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpBinEq *zkp);
   210  MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG1(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c, const sheZkpBin *zkp);
   211  MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG2(const shePrecomputedPublicKey *ppub, const sheCipherTextG2 *c, const sheZkpBin *zkp);
   212  MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpEq(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpEq *zkp);
   213  MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinEq(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpBinEq *zkp);
   214  /*
   215  	decode c via GT and set m
   216  	return 0 if success
   217  */
   218  MCLSHE_DLL_API int sheDecG1ViaGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextG1 *c);
   219  MCLSHE_DLL_API int sheDecG2ViaGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextG2 *c);
   220  
   221  /*
   222  	return 1 if dec(c) == 0
   223  */
   224  MCLSHE_DLL_API int sheIsZeroG1(const sheSecretKey *sec, const sheCipherTextG1 *c);
   225  MCLSHE_DLL_API int sheIsZeroG2(const sheSecretKey *sec, const sheCipherTextG2 *c);
   226  MCLSHE_DLL_API int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c);
   227  
   228  // return 0 if success
   229  // y = -x
   230  MCLSHE_DLL_API int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x);
   231  MCLSHE_DLL_API int sheNegG2(sheCipherTextG2 *y, const sheCipherTextG2 *x);
   232  MCLSHE_DLL_API int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x);
   233  
   234  // return 0 if success
   235  // z = x + y
   236  MCLSHE_DLL_API int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y);
   237  MCLSHE_DLL_API int sheAddG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y);
   238  MCLSHE_DLL_API int sheAddGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y);
   239  
   240  // return 0 if success
   241  // z = x - y
   242  MCLSHE_DLL_API int sheSubG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y);
   243  MCLSHE_DLL_API int sheSubG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y);
   244  MCLSHE_DLL_API int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y);
   245  
   246  // return 0 if success
   247  // z = x * y
   248  MCLSHE_DLL_API int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, mclInt y);
   249  MCLSHE_DLL_API int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, mclInt y);
   250  MCLSHE_DLL_API int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, mclInt y);
   251  /*
   252  	mul large integer
   253  	buf[bufSize] is little endian
   254  	bufSize <= (FrBitSize + 63) & ~63
   255  	return 0 if success
   256  */
   257  MCLSHE_DLL_API int sheMulIntVecG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const void *buf, mclSize bufSize);
   258  MCLSHE_DLL_API int sheMulIntVecG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const void *buf, mclSize bufSize);
   259  MCLSHE_DLL_API int sheMulIntVecGT(sheCipherTextGT *z, const sheCipherTextGT *x, const void *buf, mclSize bufSize);
   260  
   261  // return 0 if success
   262  // z = x * y
   263  MCLSHE_DLL_API int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y);
   264  /*
   265  	sheMul(z, x, y) = sheMulML(z, x, y) + sheFinalExpGT(z)
   266  	@note
   267  	Mul(x1, y1) + ... + Mul(xn, yn) = finalExp(MulML(x1, y1) + ... + MulML(xn, yn))
   268  */
   269  MCLSHE_DLL_API int sheMulML(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y);
   270  MCLSHE_DLL_API int sheFinalExpGT(sheCipherTextGT *y, const sheCipherTextGT *x);
   271  
   272  // return 0 if success
   273  // rerandomize(c)
   274  MCLSHE_DLL_API int sheReRandG1(sheCipherTextG1 *c, const shePublicKey *pub);
   275  MCLSHE_DLL_API int sheReRandG2(sheCipherTextG2 *c, const shePublicKey *pub);
   276  MCLSHE_DLL_API int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub);
   277  
   278  // return 0 if success
   279  // y = convert(x)
   280  MCLSHE_DLL_API int sheConvertG1(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG1 *x);
   281  MCLSHE_DLL_API int sheConvertG2(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG2 *x);
   282  
   283  // return nonzero if success
   284  MCLSHE_DLL_API shePrecomputedPublicKey *shePrecomputedPublicKeyCreate();
   285  // call this function to avoid memory leak
   286  MCLSHE_DLL_API void shePrecomputedPublicKeyDestroy(shePrecomputedPublicKey *ppub);
   287  // return 0 if success
   288  MCLSHE_DLL_API int shePrecomputedPublicKeyInit(shePrecomputedPublicKey *ppub, const shePublicKey *pub);
   289  
   290  #ifdef __cplusplus
   291  }
   292  #endif