diff --git a/eth_keys/libsecp256k1.nim b/eth_keys/libsecp256k1.nim index 76e8312..1ccee4a 100644 --- a/eth_keys/libsecp256k1.nim +++ b/eth_keys/libsecp256k1.nim @@ -318,3 +318,17 @@ proc signRawMessage*(data: openarray[byte], seckey: PrivateKey, nil, nil) != 1: return(EthKeysStatus.Error) return(EthKeysStatus.Success) + +proc isZeroKey*(seckey: PrivateKey): bool = + ## Check if private key `seckey` contains only 0 bytes. + result = true + for i in seckey.data: + if i != byte(0): + result = false + +proc isZeroKey*(pubkey: PublicKey): bool = + ## Check if public key `pubkey` contains only 0 bytes. + result = true + for i in pubkey.data: + if i != byte(0): + result = false diff --git a/tests/tests.nim b/tests/tests.nim index c94ee3f..33de596 100644 --- a/tests/tests.nim +++ b/tests/tests.nim @@ -253,3 +253,15 @@ suite "ECC/ECDSA/ECDHE tests suite": ecdhAgree(aliceSecret, bobPublic, secret1) == Success ecdhAgree(bobSecret, alicePublic, secret2) == Success secret1 == secret2 + + test "isZeroKey() checks": + var seckey1: PrivateKey + var pubkey1: PublicKey + var seckey2 = newPrivateKey() + var pubkey2 = seckey2.getPublicKey() + + check: + seckey1.isZeroKey() == true + pubkey1.isZeroKey() == true + seckey2.isZeroKey() == false + pubkey2.isZeroKey() == false