aboutsummaryrefslogtreecommitdiffstats
path: root/include/bls/bls.h
blob: cb8414767bb3fbad11707fddf62ba0de97e7ca4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#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
*/
#include <mcl/bn.h>

#ifdef _MSC_VER
#ifdef BLS_DLL_EXPORT
#define BLS_DLL_API __declspec(dllexport)
#else
#define BLS_DLL_API __declspec(dllimport)
#ifndef BLS_NO_AUTOLINK
    #if MBN_FP_UNIT_SIZE == 4
        #pragma comment(lib, "bls256.lib")
    #endif
#endif
#endif
#else
#define BLS_DLL_API
#endif

#ifdef __cplusplus
extern "C" {
#endif

enum {
    blsCurveFp254BNb = 0,
    blsCurveFp382_1 = 1,
    blsCurveFp382_2 = 2
};

typedef struct {
    uint64_t buf[MBN_FP_UNIT_SIZE];
} blsId;

typedef struct {
    uint64_t buf[MBN_FP_UNIT_SIZE];
} blsSecretKey;

typedef struct {
    uint64_t buf[MBN_FP_UNIT_SIZE * 2 * 3];
} blsPublicKey;

typedef struct {
    uint64_t buf[MBN_FP_UNIT_SIZE * 3];
} blsSignature;

/*
    initialize this library
    call this once before using the other method
    return 0 if success
    @note init() is not thread safe
*/
BLS_DLL_API int blsInit(int curve, int maxUnitSize);
BLS_DLL_API size_t blsGetOpUnitSize(void);
// return strlen(buf) if success else 0
BLS_DLL_API int blsGetCurveOrder(char *buf, size_t maxBufSize);
BLS_DLL_API int blsGetFieldOrder(char *buf, size_t maxBufSize);

// return 1 if same else 0
BLS_DLL_API int blsIdIsEqual(const blsId *lhs, const blsId *rhs);

// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
// return 0 if success
BLS_DLL_API int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize);
BLS_DLL_API int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize);
BLS_DLL_API int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize);

/*
    return written byte size if success else 0
*/
BLS_DLL_API size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id);
/*
    return strlen(buf) if success else 0
    buf is '\0' terminated
*/
BLS_DLL_API size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id);
BLS_DLL_API size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id);

// return 1 if same else 0
BLS_DLL_API int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey *rhs);

// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
// return 0 if success
BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize);
BLS_DLL_API int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize);
BLS_DLL_API int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize);
/*
    return written byte size if success else 0
*/
BLS_DLL_API size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec);
/*
    hash buf and set
*/
BLS_DLL_API int blsHashToSecretKey(blsSecretKey *sec, const void *buf, size_t bufSize);
/*
    set secretKey if system has /dev/urandom or CryptGenRandom
    return 0 if success else -1
*/
BLS_DLL_API int blsSecretKeySetByCSPRNG(blsSecretKey *sec);
/*
    return strlen(buf) if success else 0
    buf is '\0' terminated
*/
BLS_DLL_API size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec);
BLS_DLL_API size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec);
BLS_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs);

BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec);
BLS_DLL_API void blsSign(blsSignature *sig, const blsSecretKey *sec, const char *m, size_t size);
// return 0 if success
BLS_DLL_API int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id);
// return 0 if success
BLS_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n);
BLS_DLL_API void blsGetPop(blsSignature *sig, const blsSecretKey *sec);

// return 1 if same else 0
BLS_DLL_API int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs);
// return 0 if success
BLS_DLL_API int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize);
/*
    return written byte size if success else 0
*/
BLS_DLL_API size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub);
BLS_DLL_API int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize);
BLS_DLL_API size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub);
BLS_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs);
// return 0 if success
BLS_DLL_API int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id);
// return 0 if success
BLS_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n);

// return 1 if same else 0
BLS_DLL_API int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs);

// return 0 if success
BLS_DLL_API int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize);
/*
    return written byte size if success else 0
*/
BLS_DLL_API size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig);
BLS_DLL_API int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize);
BLS_DLL_API size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig);
BLS_DLL_API void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs);
// return 0 if success
BLS_DLL_API int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, size_t n);
BLS_DLL_API int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const char *m, size_t size);

BLS_DLL_API int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub);

#ifdef __cplusplus
}
#endif