Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit 31056cd

Browse files
jonspockmariano54
authored andcommitted
Use bls namespace , rename files and classes (Chia-Network#35)
* change names of classes * Change file names, fix $HEADERS glob in Cmake * Add bls namespace wrappers * re-use blstmp library in python-bindings instead of rebuilding, remove headers group * update names in README.md * Update variable names in python scripts * Add namespaces where needed
1 parent 2eefbb0 commit 31056cd

29 files changed

+1043
-1023
lines changed

README.md

+48-48
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ uint8_t seed[] = {0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
3434
19, 18, 12, 89, 6, 220, 18, 102, 58, 209,
3535
82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22};
3636

37-
BLSPrivateKey sk = BLSPrivateKey::FromSeed(seed, sizeof(seed));
38-
BLSPublicKey pk = sk.GetPublicKey();
37+
bls::PrivateKey sk = bls::PrivateKey::FromSeed(seed, sizeof(seed));
38+
bls::PublicKey pk = sk.GetPublicKey();
3939

4040
uint8_t msg[] = {100, 2, 254, 88, 90, 45, 23};
4141

42-
BLSSignature sig = sk.Sign(msg, sizeof(msg));
42+
bls::Signature sig = sk.Sign(msg, sizeof(msg));
4343
```
4444
4545
#### Serializing keys and signatures to bytes
4646
```c++
47-
uint8_t skBytes[BLSPrivateKey::PRIVATE_KEY_SIZE]; // 32 byte array
48-
uint8_t pkBytes[BLSPublicKey::PUBLIC_KEY_SIZE]; // 48 byte array
49-
uint8_t sigBytes[BLSSignature::SIGNATURE_SIZE]; // 96 byte array
47+
uint8_t skBytes[bls::PrivateKey::PRIVATE_KEY_SIZE]; // 32 byte array
48+
uint8_t pkBytes[bls::PublicKey::PUBLIC_KEY_SIZE]; // 48 byte array
49+
uint8_t sigBytes[bls::Signature::SIGNATURE_SIZE]; // 96 byte array
5050
5151
sk.Serialize(skBytes); // 32 bytes
5252
pk.Serialize(pkBytes); // 48 bytes
@@ -56,19 +56,19 @@ sig.Serialize(sigBytes); // 96 bytes
5656
#### Loading keys and signatures from bytes
5757
```c++
5858
// Takes array of 32 bytes
59-
sk = BLSPrivateKey::FromBytes(skBytes);
59+
sk = bls::PrivateKey::FromBytes(skBytes);
6060

6161
// Takes array of 48 bytes
62-
pk = BLSPublicKey::FromBytes(pkBytes);
62+
pk = bls::PublicKey::FromBytes(pkBytes);
6363

6464
// Takes array of 96 bytes
65-
sig = BLSSignature::FromBytes(sigBytes);
65+
sig = bls::Signature::FromBytes(sigBytes);
6666
```
6767

6868
#### Verifying signatures
6969
```c++
7070
// Add information required for verification, to sig object
71-
sig.SetAggregationInfo(AggregationInfo::FromMsg(pk, msg, sizeof(msg)));
71+
sig.SetAggregationInfo(bls::AggregationInfo::FromMsg(pk, msg, sizeof(msg)));
7272

7373
bool ok = sig.Verify();
7474
```
@@ -77,51 +77,51 @@ bool ok = sig.Verify();
7777
```c++
7878
// Generate some more private keys
7979
seed[0] = 1;
80-
BLSPrivateKey sk1 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
80+
bls::PrivateKey sk1 = bls::PrivateKey::FromSeed(seed, sizeof(seed));
8181
seed[0] = 2;
82-
BLSPrivateKey sk2 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
82+
bls::PrivateKey sk2 = bls::PrivateKey::FromSeed(seed, sizeof(seed));
8383

8484
// Generate first sig
85-
BLSPublicKey pk1 = sk1.GetPublicKey();
86-
BLSSignature sig1 = sk1.Sign(msg, sizeof(msg));
85+
bls::PublicKey pk1 = sk1.GetPublicKey();
86+
bls::Signature sig1 = sk1.Sign(msg, sizeof(msg));
8787

8888
// Generate second sig
89-
BLSPublicKey pk2 = sk2.GetPublicKey();
90-
BLSSignature sig2 = sk2.Sign(msg, sizeof(msg));
89+
bls::PublicKey pk2 = sk2.GetPublicKey();
90+
bls::Signature sig2 = sk2.Sign(msg, sizeof(msg));
9191

9292
// Aggregate signatures together
93-
vector<BLSSignature> sigs = {sig1, sig2};
94-
BLSSignature aggSig = BLSSignature::Aggregate(sigs);
93+
vector<bls::Signature> sigs = {sig1, sig2};
94+
bls::Signature aggSig = bls::Signature::Aggregate(sigs);
9595

9696
// For same message, public keys can be aggregated into one.
9797
// The signature can be verified the same as a single signature,
9898
// using this public key.
99-
vector<BLSPublicKey> pubKeys = {pk1, pk2};
100-
BLSPublicKey aggPubKey = BLSSignature::Aggregate(pubKeys);
99+
vector<bls::PublicKey> pubKeys = {pk1, pk2};
100+
bls::PublicKey aggPubKey = bls::Signature::Aggregate(pubKeys);
101101
```
102102
103103
#### Aggregate signatures for different messages
104104
```c++
105105
// Generate one more key and message
106106
seed[0] = 3;
107-
BLSPrivateKey sk3 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
108-
BLSPublicKey pk3 = sk3.GetPublicKey();
107+
bls::PrivateKey sk3 = bls::PrivateKey::FromSeed(seed, sizeof(seed));
108+
bls::PublicKey pk3 = sk3.GetPublicKey();
109109
uint8_t msg2[] = {100, 2, 254, 88, 90, 45, 23};
110110
111111
// Generate the signatures, assuming we have 3 private keys
112112
sig1 = sk1.Sign(msg, sizeof(msg));
113113
sig2 = sk2.Sign(msg, sizeof(msg));
114-
BLSSignature sig3 = sk3.Sign(msg2, sizeof(msg2));
114+
bls::Signature sig3 = sk3.Sign(msg2, sizeof(msg2));
115115
116116
// They can be noninteractively combined by anyone
117117
// Aggregation below can also be done by the verifier, to
118118
// make batch verification more efficient
119-
vector<BLSSignature> sigsL = {sig1, sig2};
120-
BLSSignature aggSigL = BLSSignature::Aggregate(sigsL);
119+
vector<bls::Signature> sigsL = {sig1, sig2};
120+
bls::Signature aggSigL = bls::Signature::Aggregate(sigsL);
121121
122122
// Arbitrary trees of aggregates
123-
vector<BLSSignature> sigsFinal = {aggSigL, sig3};
124-
BLSSignature aggSigFinal = BLSSignature::Aggregate(sigsFinal);
123+
vector<bls::Signature> sigsFinal = {aggSigL, sig3};
124+
bls::Signature aggSigFinal = bls::Signature::Aggregate(sigsFinal);
125125
126126
// Serialize the final signature
127127
aggSigFinal.Serialize(sigBytes);
@@ -130,16 +130,16 @@ aggSigFinal.Serialize(sigBytes);
130130
#### Verify aggregate signature for different messages
131131
```c++
132132
// Deserialize aggregate signature
133-
aggSigFinal = BLSSignature::FromBytes(sigBytes);
133+
aggSigFinal = bls::Signature::FromBytes(sigBytes);
134134

135135
// Create aggregation information (or deserialize it)
136-
AggregationInfo a1 = AggregationInfo::FromMsg(pk1, msg, sizeof(msg));
137-
AggregationInfo a2 = AggregationInfo::FromMsg(pk2, msg, sizeof(msg));
138-
AggregationInfo a3 = AggregationInfo::FromMsg(pk3, msg2, sizeof(msg2));
139-
vector<AggregationInfo> infos = {a1, a2};
140-
AggregationInfo a1a2 = AggregationInfo::MergeInfos(infos);
141-
vector<AggregationInfo> infos2 = {a1a2, a3};
142-
AggregationInfo aFinal = AggregationInfo::MergeInfos(infos2);
136+
bls::AggregationInfo a1 = bls::AggregationInfo::FromMsg(pk1, msg, sizeof(msg));
137+
bls::AggregationInfo a2 = bls::AggregationInfo::FromMsg(pk2, msg, sizeof(msg));
138+
bls::AggregationInfo a3 = bls::AggregationInfo::FromMsg(pk3, msg2, sizeof(msg2));
139+
vector<bls::AggregationInfo> infos = {a1, a2};
140+
bls::AggregationInfo a1a2 = bls::AggregationInfo::MergeInfos(infos);
141+
vector<bls::AggregationInfo> infos2 = {a1a2, a3};
142+
bls::AggregationInfo aFinal = bls::AggregationInfo::MergeInfos(infos2);
143143

144144
// Verify final signature using the aggregation info
145145
aggSigFinal.SetAggregationInfo(aFinal);
@@ -148,7 +148,7 @@ ok = aggSigFinal.Verify();
148148
// If you previously verified a signature, you can also divide
149149
// the aggregate signature by the signature you already verified.
150150
ok = aggSigL.Verify();
151-
vector<BLSSignature> cache = {aggSigL};
151+
vector<bls::Signature> cache = {aggSigL};
152152
aggSigFinal = aggSigFinal.DivideBy(cache);
153153

154154
// Final verification is now more efficient
@@ -157,15 +157,15 @@ ok = aggSigFinal.Verify();
157157
158158
#### Aggregate private keys
159159
```c++
160-
vector<BLSPrivateKey> privateKeysList = {sk1, sk2};
161-
vector<BLSPublicKey> pubKeysList = {pk1, pk2};
160+
vector<bls::PrivateKey> privateKeysList = {sk1, sk2};
161+
vector<bls::PublicKey> pubKeysList = {pk1, pk2};
162162
163163
// Create an aggregate private key, that can generate
164164
// aggregate signatures
165-
const BLSPrivateKey aggSk = BLSPrivateKey::Aggregate(
165+
const bls::PrivateKey aggSk = bls::PrivateKey::Aggregate(
166166
privateKeys, pubKeys);
167167
168-
BLSSignature aggSig3 = aggSk.Sign(msg, sizeof(msg));
168+
bls::Signature aggSig3 = aggSk.Sign(msg, sizeof(msg));
169169
```
170170

171171
#### HD keys
@@ -175,21 +175,21 @@ uint8_t seed[] = {1, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
175175
19, 18, 12, 89, 6, 220, 18, 102, 58, 209,
176176
82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22};
177177

178-
ExtendedPrivateKey esk = ExtendedPrivateKey::FromSeed(
178+
bls::ExtendedPrivateKey esk = bls::ExtendedPrivateKey::FromSeed(
179179
seed, sizeof(seed));
180180

181-
ExtendedPublicKey epk = esk.GetExtendedPublicKey();
181+
bls::ExtendedPublicKey epk = esk.GetExtendedPublicKey();
182182

183183
// Use i >= 2^31 for hardened keys
184-
ExtendedPrivateKey skChild = esk.PrivateChild(0)
184+
bls::ExtendedPrivateKey skChild = esk.PrivateChild(0)
185185
.PrivateChild(5);
186186

187-
ExtendedPublicKey pkChild = epk.PublicChild(0)
187+
bls::ExtendedPublicKey pkChild = epk.PublicChild(0)
188188
.PublicChild(5);
189189

190190
// Serialize extended keys
191-
uint8_t buffer1[ExtendedPublicKey::ExtendedPublicKeySize] // 93 bytes
192-
uint8_t buffer2[ExtendedPrivateKey::ExtendedPrivateKeySize] // 77 bytes
191+
uint8_t buffer1[bls::ExtendedPublicKey::ExtendedPublicKeySize] // 93 bytes
192+
uint8_t buffer2[bls::ExtendedPrivateKey::ExtendedPrivateKeySize] // 77 bytes
193193

194194
pkChild.Serialize(buffer1);
195195
skChild.Serialize(buffer2);
@@ -207,12 +207,12 @@ cmake --build . -- -j 6
207207

208208
### Run tests
209209
```bash
210-
./build/runtest
210+
./build/src/runtest
211211
```
212212

213213
### Run benchmarks
214214
```bash
215-
./build/runbench
215+
./build/src/runbench
216216
```
217217

218218
### Link the library to use it

python-bindings/CMakeLists.txt

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0 FATAL_ERROR)
22
set (CMAKE_CXX_STANDARD 11)
3-
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../src/*.h)
4-
source_group("SrcHeaders" FILES ${HEADERS})
53

64
include_directories(
75
${INCLUDE_DIRECTORIES}
@@ -10,19 +8,10 @@ include_directories(
108
${CMAKE_CURRENT_SOURCE_DIR}/../contrib/catch
119
)
1210

13-
pybind11_add_module(blspy
14-
${CMAKE_CURRENT_SOURCE_DIR}/../src/extendedpublickey.cpp
15-
${CMAKE_CURRENT_SOURCE_DIR}/../src/extendedprivatekey.cpp
16-
${CMAKE_CURRENT_SOURCE_DIR}/../src/chaincode.cpp
17-
${CMAKE_CURRENT_SOURCE_DIR}/../src/blssignature.cpp
18-
${CMAKE_CURRENT_SOURCE_DIR}/../src/blspublickey.cpp
19-
${CMAKE_CURRENT_SOURCE_DIR}/../src/blsprivatekey.cpp
20-
${CMAKE_CURRENT_SOURCE_DIR}/../src/bls.cpp
21-
${CMAKE_CURRENT_SOURCE_DIR}/../src/aggregationinfo.cpp
22-
${CMAKE_CURRENT_SOURCE_DIR}/pythonbindings.cpp)
11+
pybind11_add_module(blspy ${CMAKE_CURRENT_SOURCE_DIR}/pythonbindings.cpp)
2312

2413
if (SODIUM_FOUND)
25-
target_link_libraries(blspy PRIVATE relic_s sodium)
14+
target_link_libraries(blspy PRIVATE blstmp relic_s sodium)
2615
else()
27-
target_link_libraries(blspy PRIVATE relic_s)
16+
target_link_libraries(blspy PRIVATE blstmp relic_s)
2817
endif()

python-bindings/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import blspy
2828
seed = bytes([0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
2929
19, 18, 12, 89, 6, 220, 18, 102, 58, 209,
3030
82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22])
31-
sk = BLSPrivateKey.from_seed(seed)
31+
sk = PrivateKey.from_seed(seed)
3232
pk = sk.get_public_key()
3333

3434
msg = bytes([100, 2, 254, 88, 90, 45, 23])
@@ -46,11 +46,11 @@ sig_bytes = sig.serialize() # 96 bytes
4646
#### Loading keys and signatures from bytes
4747
```python
4848
# Takes bytes object of size 32
49-
sk = BLSPrivateKey.from_bytes(sk_bytes)
49+
sk = PrivateKey.from_bytes(sk_bytes)
5050
# Takes bytes object of size 48
51-
pk = BLSPublicKey.from_bytes(pk_bytes)
51+
pk = PublicKey.from_bytes(pk_bytes)
5252
# Takes bytes object of size 96
53-
sig = BLSSignature.from_bytes(sig_bytes)
53+
sig = Signature.from_bytes(sig_bytes)
5454
```
5555

5656
#### Verifying signatures
@@ -64,9 +64,9 @@ ok = BLS.verify(sig)
6464
```python
6565
# Generate some more private keys
6666
seed = bytes([1]) + seed[1:]
67-
sk1 = BLSPrivateKey.from_seed(seed)
67+
sk1 = PrivateKey.from_seed(seed)
6868
seed = bytes([2]) + seed[1:]
69-
sk2 = BLSPrivateKey.from_seed(seed)
69+
sk2 = PrivateKey.from_seed(seed)
7070

7171
# Generate first sig
7272
pk1 = sk1.get_public_key()
@@ -90,7 +90,7 @@ agg_pubkey = BLS.aggregate_pub_keys([pk1, pk2], True)
9090
```python
9191
# Generate one more key and message
9292
seed = bytes([3]) + seed[1:]
93-
sk3 = BLSPrivateKey.from_seed(seed)
93+
sk3 = PrivateKey.from_seed(seed)
9494
pk3 = sk3.get_public_key()
9595
msg2 = bytes([100, 2, 254, 88, 90, 45, 23])
9696

@@ -114,7 +114,7 @@ sig_bytes = agg_sig_final.serialize()
114114
#### Verify aggregate signature for different messages
115115
```python
116116
# Deserialize aggregate signature
117-
agg_sig_final = BLSSignature.from_bytes(sig_bytes)
117+
agg_sig_final = Signature.from_bytes(sig_bytes)
118118

119119
# Create aggregation information (or deserialize it)
120120
a1 = AggregationInfo.from_msg(pk1, msg)

0 commit comments

Comments
 (0)