Skip to content

Commit

Permalink
Use bls namespace , rename files and classes (dashpay#35)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jonspock authored and mariano54 committed Oct 2, 2018
1 parent 2eefbb0 commit 31056cd
Show file tree
Hide file tree
Showing 29 changed files with 1,043 additions and 1,023 deletions.
96 changes: 48 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ uint8_t seed[] = {0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
19, 18, 12, 89, 6, 220, 18, 102, 58, 209,
82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22};

BLSPrivateKey sk = BLSPrivateKey::FromSeed(seed, sizeof(seed));
BLSPublicKey pk = sk.GetPublicKey();
bls::PrivateKey sk = bls::PrivateKey::FromSeed(seed, sizeof(seed));
bls::PublicKey pk = sk.GetPublicKey();

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

BLSSignature sig = sk.Sign(msg, sizeof(msg));
bls::Signature sig = sk.Sign(msg, sizeof(msg));
```
#### Serializing keys and signatures to bytes
```c++
uint8_t skBytes[BLSPrivateKey::PRIVATE_KEY_SIZE]; // 32 byte array
uint8_t pkBytes[BLSPublicKey::PUBLIC_KEY_SIZE]; // 48 byte array
uint8_t sigBytes[BLSSignature::SIGNATURE_SIZE]; // 96 byte array
uint8_t skBytes[bls::PrivateKey::PRIVATE_KEY_SIZE]; // 32 byte array
uint8_t pkBytes[bls::PublicKey::PUBLIC_KEY_SIZE]; // 48 byte array
uint8_t sigBytes[bls::Signature::SIGNATURE_SIZE]; // 96 byte array
sk.Serialize(skBytes); // 32 bytes
pk.Serialize(pkBytes); // 48 bytes
Expand All @@ -56,19 +56,19 @@ sig.Serialize(sigBytes); // 96 bytes
#### Loading keys and signatures from bytes
```c++
// Takes array of 32 bytes
sk = BLSPrivateKey::FromBytes(skBytes);
sk = bls::PrivateKey::FromBytes(skBytes);

// Takes array of 48 bytes
pk = BLSPublicKey::FromBytes(pkBytes);
pk = bls::PublicKey::FromBytes(pkBytes);

// Takes array of 96 bytes
sig = BLSSignature::FromBytes(sigBytes);
sig = bls::Signature::FromBytes(sigBytes);
```

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

bool ok = sig.Verify();
```
Expand All @@ -77,51 +77,51 @@ bool ok = sig.Verify();
```c++
// Generate some more private keys
seed[0] = 1;
BLSPrivateKey sk1 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
bls::PrivateKey sk1 = bls::PrivateKey::FromSeed(seed, sizeof(seed));
seed[0] = 2;
BLSPrivateKey sk2 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
bls::PrivateKey sk2 = bls::PrivateKey::FromSeed(seed, sizeof(seed));

// Generate first sig
BLSPublicKey pk1 = sk1.GetPublicKey();
BLSSignature sig1 = sk1.Sign(msg, sizeof(msg));
bls::PublicKey pk1 = sk1.GetPublicKey();
bls::Signature sig1 = sk1.Sign(msg, sizeof(msg));

// Generate second sig
BLSPublicKey pk2 = sk2.GetPublicKey();
BLSSignature sig2 = sk2.Sign(msg, sizeof(msg));
bls::PublicKey pk2 = sk2.GetPublicKey();
bls::Signature sig2 = sk2.Sign(msg, sizeof(msg));

// Aggregate signatures together
vector<BLSSignature> sigs = {sig1, sig2};
BLSSignature aggSig = BLSSignature::Aggregate(sigs);
vector<bls::Signature> sigs = {sig1, sig2};
bls::Signature aggSig = bls::Signature::Aggregate(sigs);

// For same message, public keys can be aggregated into one.
// The signature can be verified the same as a single signature,
// using this public key.
vector<BLSPublicKey> pubKeys = {pk1, pk2};
BLSPublicKey aggPubKey = BLSSignature::Aggregate(pubKeys);
vector<bls::PublicKey> pubKeys = {pk1, pk2};
bls::PublicKey aggPubKey = bls::Signature::Aggregate(pubKeys);
```
#### Aggregate signatures for different messages
```c++
// Generate one more key and message
seed[0] = 3;
BLSPrivateKey sk3 = BLSPrivateKey::FromSeed(seed, sizeof(seed));
BLSPublicKey pk3 = sk3.GetPublicKey();
bls::PrivateKey sk3 = bls::PrivateKey::FromSeed(seed, sizeof(seed));
bls::PublicKey pk3 = sk3.GetPublicKey();
uint8_t msg2[] = {100, 2, 254, 88, 90, 45, 23};
// Generate the signatures, assuming we have 3 private keys
sig1 = sk1.Sign(msg, sizeof(msg));
sig2 = sk2.Sign(msg, sizeof(msg));
BLSSignature sig3 = sk3.Sign(msg2, sizeof(msg2));
bls::Signature sig3 = sk3.Sign(msg2, sizeof(msg2));
// They can be noninteractively combined by anyone
// Aggregation below can also be done by the verifier, to
// make batch verification more efficient
vector<BLSSignature> sigsL = {sig1, sig2};
BLSSignature aggSigL = BLSSignature::Aggregate(sigsL);
vector<bls::Signature> sigsL = {sig1, sig2};
bls::Signature aggSigL = bls::Signature::Aggregate(sigsL);
// Arbitrary trees of aggregates
vector<BLSSignature> sigsFinal = {aggSigL, sig3};
BLSSignature aggSigFinal = BLSSignature::Aggregate(sigsFinal);
vector<bls::Signature> sigsFinal = {aggSigL, sig3};
bls::Signature aggSigFinal = bls::Signature::Aggregate(sigsFinal);
// Serialize the final signature
aggSigFinal.Serialize(sigBytes);
Expand All @@ -130,16 +130,16 @@ aggSigFinal.Serialize(sigBytes);
#### Verify aggregate signature for different messages
```c++
// Deserialize aggregate signature
aggSigFinal = BLSSignature::FromBytes(sigBytes);
aggSigFinal = bls::Signature::FromBytes(sigBytes);

// Create aggregation information (or deserialize it)
AggregationInfo a1 = AggregationInfo::FromMsg(pk1, msg, sizeof(msg));
AggregationInfo a2 = AggregationInfo::FromMsg(pk2, msg, sizeof(msg));
AggregationInfo a3 = AggregationInfo::FromMsg(pk3, msg2, sizeof(msg2));
vector<AggregationInfo> infos = {a1, a2};
AggregationInfo a1a2 = AggregationInfo::MergeInfos(infos);
vector<AggregationInfo> infos2 = {a1a2, a3};
AggregationInfo aFinal = AggregationInfo::MergeInfos(infos2);
bls::AggregationInfo a1 = bls::AggregationInfo::FromMsg(pk1, msg, sizeof(msg));
bls::AggregationInfo a2 = bls::AggregationInfo::FromMsg(pk2, msg, sizeof(msg));
bls::AggregationInfo a3 = bls::AggregationInfo::FromMsg(pk3, msg2, sizeof(msg2));
vector<bls::AggregationInfo> infos = {a1, a2};
bls::AggregationInfo a1a2 = bls::AggregationInfo::MergeInfos(infos);
vector<bls::AggregationInfo> infos2 = {a1a2, a3};
bls::AggregationInfo aFinal = bls::AggregationInfo::MergeInfos(infos2);

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

// Final verification is now more efficient
Expand All @@ -157,15 +157,15 @@ ok = aggSigFinal.Verify();
#### Aggregate private keys
```c++
vector<BLSPrivateKey> privateKeysList = {sk1, sk2};
vector<BLSPublicKey> pubKeysList = {pk1, pk2};
vector<bls::PrivateKey> privateKeysList = {sk1, sk2};
vector<bls::PublicKey> pubKeysList = {pk1, pk2};
// Create an aggregate private key, that can generate
// aggregate signatures
const BLSPrivateKey aggSk = BLSPrivateKey::Aggregate(
const bls::PrivateKey aggSk = bls::PrivateKey::Aggregate(
privateKeys, pubKeys);
BLSSignature aggSig3 = aggSk.Sign(msg, sizeof(msg));
bls::Signature aggSig3 = aggSk.Sign(msg, sizeof(msg));
```

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

ExtendedPrivateKey esk = ExtendedPrivateKey::FromSeed(
bls::ExtendedPrivateKey esk = bls::ExtendedPrivateKey::FromSeed(
seed, sizeof(seed));

ExtendedPublicKey epk = esk.GetExtendedPublicKey();
bls::ExtendedPublicKey epk = esk.GetExtendedPublicKey();

// Use i >= 2^31 for hardened keys
ExtendedPrivateKey skChild = esk.PrivateChild(0)
bls::ExtendedPrivateKey skChild = esk.PrivateChild(0)
.PrivateChild(5);

ExtendedPublicKey pkChild = epk.PublicChild(0)
bls::ExtendedPublicKey pkChild = epk.PublicChild(0)
.PublicChild(5);

// Serialize extended keys
uint8_t buffer1[ExtendedPublicKey::ExtendedPublicKeySize] // 93 bytes
uint8_t buffer2[ExtendedPrivateKey::ExtendedPrivateKeySize] // 77 bytes
uint8_t buffer1[bls::ExtendedPublicKey::ExtendedPublicKeySize] // 93 bytes
uint8_t buffer2[bls::ExtendedPrivateKey::ExtendedPrivateKeySize] // 77 bytes

pkChild.Serialize(buffer1);
skChild.Serialize(buffer2);
Expand All @@ -207,12 +207,12 @@ cmake --build . -- -j 6

### Run tests
```bash
./build/runtest
./build/src/runtest
```

### Run benchmarks
```bash
./build/runbench
./build/src/runbench
```

### Link the library to use it
Expand Down
17 changes: 3 additions & 14 deletions python-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0 FATAL_ERROR)
set (CMAKE_CXX_STANDARD 11)
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../src/*.h)
source_group("SrcHeaders" FILES ${HEADERS})

include_directories(
${INCLUDE_DIRECTORIES}
Expand All @@ -10,19 +8,10 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../contrib/catch
)

pybind11_add_module(blspy
${CMAKE_CURRENT_SOURCE_DIR}/../src/extendedpublickey.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/extendedprivatekey.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/chaincode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/blssignature.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/blspublickey.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/blsprivatekey.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/bls.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/aggregationinfo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pythonbindings.cpp)
pybind11_add_module(blspy ${CMAKE_CURRENT_SOURCE_DIR}/pythonbindings.cpp)

if (SODIUM_FOUND)
target_link_libraries(blspy PRIVATE relic_s sodium)
target_link_libraries(blspy PRIVATE blstmp relic_s sodium)
else()
target_link_libraries(blspy PRIVATE relic_s)
target_link_libraries(blspy PRIVATE blstmp relic_s)
endif()
16 changes: 8 additions & 8 deletions python-bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import blspy
seed = bytes([0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
19, 18, 12, 89, 6, 220, 18, 102, 58, 209,
82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22])
sk = BLSPrivateKey.from_seed(seed)
sk = PrivateKey.from_seed(seed)
pk = sk.get_public_key()

msg = bytes([100, 2, 254, 88, 90, 45, 23])
Expand All @@ -46,11 +46,11 @@ sig_bytes = sig.serialize() # 96 bytes
#### Loading keys and signatures from bytes
```python
# Takes bytes object of size 32
sk = BLSPrivateKey.from_bytes(sk_bytes)
sk = PrivateKey.from_bytes(sk_bytes)
# Takes bytes object of size 48
pk = BLSPublicKey.from_bytes(pk_bytes)
pk = PublicKey.from_bytes(pk_bytes)
# Takes bytes object of size 96
sig = BLSSignature.from_bytes(sig_bytes)
sig = Signature.from_bytes(sig_bytes)
```

#### Verifying signatures
Expand All @@ -64,9 +64,9 @@ ok = BLS.verify(sig)
```python
# Generate some more private keys
seed = bytes([1]) + seed[1:]
sk1 = BLSPrivateKey.from_seed(seed)
sk1 = PrivateKey.from_seed(seed)
seed = bytes([2]) + seed[1:]
sk2 = BLSPrivateKey.from_seed(seed)
sk2 = PrivateKey.from_seed(seed)

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

Expand All @@ -114,7 +114,7 @@ sig_bytes = agg_sig_final.serialize()
#### Verify aggregate signature for different messages
```python
# Deserialize aggregate signature
agg_sig_final = BLSSignature.from_bytes(sig_bytes)
agg_sig_final = Signature.from_bytes(sig_bytes)

# Create aggregation information (or deserialize it)
a1 = AggregationInfo.from_msg(pk1, msg)
Expand Down
Loading

0 comments on commit 31056cd

Please sign in to comment.