Compute pubkey from privkey, instead of keeping together, initPrivateKey (#13)

* Compute pubkey from privkey, instead of keeping together

* Added initPrivateKey
This commit is contained in:
Yuriy Glukhov 2018-03-31 10:47:16 +03:00 committed by Mamy Ratsimbazafy
parent 07bd05aae4
commit f22ae0a827
3 changed files with 8 additions and 4 deletions

View File

@ -17,7 +17,6 @@ type
PrivateKey* = object PrivateKey* = object
Fraw_key: array[32, byte] Fraw_key: array[32, byte]
Fpublic_key: PublicKey # This is exported publicly through public_key
type type
Scalar256 = distinct array[32, byte] Scalar256 = distinct array[32, byte]
@ -53,7 +52,6 @@ template genAccessors(name: untyped, fieldType, objType: typedesc): untyped =
genAccessors(raw_key, array[64, byte], PublicKey) genAccessors(raw_key, array[64, byte], PublicKey)
genAccessors(raw_key, array[32, byte], PrivateKey) genAccessors(raw_key, array[32, byte], PrivateKey)
genAccessors(public_key, PublicKey, PrivateKey)
## If we hide the fields we need to provide a custom `==` proc ## If we hide the fields we need to provide a custom `==` proc

View File

@ -30,9 +30,11 @@ else:
# ################################ # ################################
# Initialization # Initialization
proc initPrivateKey*(data: array[32, byte]): PrivateKey {.noInit, inline.} =
result.raw_key = data
proc initPrivateKey*(hexString: string): PrivateKey {.noInit.} = proc initPrivateKey*(hexString: string): PrivateKey {.noInit.} =
hexToByteArrayBE(hexString, result.raw_key) hexToByteArrayBE(hexString, result.raw_key)
result.public_key = private_key_to_public_key(result)
proc initPublicKey*(hexString: string): PublicKey {.noInit.} = proc initPublicKey*(hexString: string): PublicKey {.noInit.} =
var b: array[65, byte] var b: array[65, byte]
@ -60,6 +62,9 @@ proc verify_msg*(key: PublicKey, message: string, sig: Signature): bool {.inline
# # ################################ # # ################################
# # Private key interface # # Private key interface
proc public_key*(key: PrivateKey): PublicKey {.inline.} =
private_key_to_public_key(key)
proc sign_msg*(key: PrivateKey, message: openarray[byte]): Signature {.inline.} = proc sign_msg*(key: PrivateKey, message: openarray[byte]): Signature {.inline.} =
let message_hash = keccak256.digest(message) let message_hash = keccak256.digest(message)
ecdsa_sign(key, message_hash) ecdsa_sign(key, message_hash)

View File

@ -8,11 +8,12 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import ./datatypes import ./datatypes
export PublicKey, PrivateKey, Signature, public_key, raw_key, `==`
import ./datatypes_interface import ./datatypes_interface
export datatypes_interface export datatypes_interface
export PublicKey, PrivateKey, Signature, public_key, raw_key, `==`
when defined(backend_native): when defined(backend_native):
import ttmath import ttmath
export ttmath export ttmath