aboutsummaryrefslogtreecommitdiffstats
path: root/include/bls_if.h
blob: f8d174dfbe7cc34f5ad002993292e8045a1c897c (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
158
159
160
161
#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
    @note init() is not thread safe
*/
BLS256_DLL_API void 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);
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);
BLS256_DLL_API void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id);
BLS256_DLL_API void 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);
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);
BLS256_DLL_API void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id);
BLS256_DLL_API void 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);
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);
BLS256_DLL_API void 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