More localized noSideEffect overrides
This commit is contained in:
parent
f1bdb572f4
commit
a9d5cba699
@ -223,7 +223,6 @@ func fromRaw*(T: type SkSecretKey, data: openArray[byte]): SkResult[T] =
|
|||||||
if len(data) < SkRawSecretKeySize:
|
if len(data) < SkRawSecretKeySize:
|
||||||
return err(static(&"secp: raw private key should be {SkRawSecretKeySize} bytes"))
|
return err(static(&"secp: raw private key should be {SkRawSecretKeySize} bytes"))
|
||||||
|
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
if secp256k1_ec_seckey_verify(secp256k1_context_no_precomp, data.ptr0) != 1:
|
if secp256k1_ec_seckey_verify(secp256k1_context_no_precomp, data.ptr0) != 1:
|
||||||
return err("secp: invalid private key")
|
return err("secp: invalid private key")
|
||||||
|
|
||||||
@ -266,7 +265,6 @@ func fromRaw*(T: type SkPublicKey, data: openArray[byte]): SkResult[T] =
|
|||||||
return err("secp: public key format not recognised")
|
return err("secp: public key format not recognised")
|
||||||
|
|
||||||
var key {.noinit.}: secp256k1_pubkey
|
var key {.noinit.}: secp256k1_pubkey
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
if secp256k1_ec_pubkey_parse(
|
if secp256k1_ec_pubkey_parse(
|
||||||
secp256k1_context_no_precomp, addr key, data.ptr0, csize_t(length)) != 1:
|
secp256k1_context_no_precomp, addr key, data.ptr0, csize_t(length)) != 1:
|
||||||
return err("secp: cannot parse public key")
|
return err("secp: cannot parse public key")
|
||||||
@ -281,8 +279,6 @@ func fromHex*(T: type SkPublicKey, data: string): SkResult[T] =
|
|||||||
func toRaw*(pubkey: SkPublicKey): array[SkRawPublicKeySize, byte] =
|
func toRaw*(pubkey: SkPublicKey): array[SkRawPublicKeySize, byte] =
|
||||||
## Serialize Secp256k1 `public key` ``key`` to raw uncompressed form
|
## Serialize Secp256k1 `public key` ``key`` to raw uncompressed form
|
||||||
var length = csize_t(len(result))
|
var length = csize_t(len(result))
|
||||||
|
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ec_pubkey_serialize(
|
let res = secp256k1_ec_pubkey_serialize(
|
||||||
secp256k1_context_no_precomp, result.ptr0, addr length,
|
secp256k1_context_no_precomp, result.ptr0, addr length,
|
||||||
unsafeAddr pubkey.data, SECP256K1_EC_UNCOMPRESSED)
|
unsafeAddr pubkey.data, SECP256K1_EC_UNCOMPRESSED)
|
||||||
@ -294,7 +290,6 @@ func toHex*(pubkey: SkPublicKey): string =
|
|||||||
func toRawCompressed*(pubkey: SkPublicKey): array[SkRawCompressedPublicKeySize, byte] =
|
func toRawCompressed*(pubkey: SkPublicKey): array[SkRawCompressedPublicKeySize, byte] =
|
||||||
## Serialize Secp256k1 `public key` ``key`` to raw compressed form
|
## Serialize Secp256k1 `public key` ``key`` to raw compressed form
|
||||||
var length = csize_t(len(result))
|
var length = csize_t(len(result))
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ec_pubkey_serialize(
|
let res = secp256k1_ec_pubkey_serialize(
|
||||||
secp256k1_context_no_precomp, result.ptr0, addr length,
|
secp256k1_context_no_precomp, result.ptr0, addr length,
|
||||||
unsafeAddr pubkey.data, SECP256K1_EC_COMPRESSED)
|
unsafeAddr pubkey.data, SECP256K1_EC_COMPRESSED)
|
||||||
@ -309,7 +304,6 @@ func fromRaw*(T: type SkSignature, data: openArray[byte]): SkResult[T] =
|
|||||||
return err(static(&"secp: signature must be {SkRawSignatureSize} bytes"))
|
return err(static(&"secp: signature must be {SkRawSignatureSize} bytes"))
|
||||||
|
|
||||||
var sig {.noinit.}: secp256k1_ecdsa_signature
|
var sig {.noinit.}: secp256k1_ecdsa_signature
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
if secp256k1_ecdsa_signature_parse_compact(
|
if secp256k1_ecdsa_signature_parse_compact(
|
||||||
secp256k1_context_no_precomp, addr sig, data.ptr0) != 1:
|
secp256k1_context_no_precomp, addr sig, data.ptr0) != 1:
|
||||||
return err("secp: cannot parse signaure")
|
return err("secp: cannot parse signaure")
|
||||||
@ -323,7 +317,6 @@ func fromDer*(T: type SkSignature, data: openarray[byte]): SkResult[T] =
|
|||||||
return err("secp: DER signature too short")
|
return err("secp: DER signature too short")
|
||||||
|
|
||||||
var sig {.noinit.}: secp256k1_ecdsa_signature
|
var sig {.noinit.}: secp256k1_ecdsa_signature
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
if secp256k1_ecdsa_signature_parse_der(
|
if secp256k1_ecdsa_signature_parse_der(
|
||||||
secp256k1_context_no_precomp, addr sig, data.ptr0, csize_t(len(data))) != 1:
|
secp256k1_context_no_precomp, addr sig, data.ptr0, csize_t(len(data))) != 1:
|
||||||
return err("secp: cannot parse DER signature")
|
return err("secp: cannot parse DER signature")
|
||||||
@ -337,7 +330,6 @@ func fromHex*(T: type SkSignature, data: string): SkResult[T] =
|
|||||||
|
|
||||||
func toRaw*(sig: SkSignature): array[SkRawSignatureSize, byte] =
|
func toRaw*(sig: SkSignature): array[SkRawSignatureSize, byte] =
|
||||||
## Serialize signature to compact binary form
|
## Serialize signature to compact binary form
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ecdsa_signature_serialize_compact(
|
let res = secp256k1_ecdsa_signature_serialize_compact(
|
||||||
secp256k1_context_no_precomp, result.ptr0, unsafeAddr sig.data)
|
secp256k1_context_no_precomp, result.ptr0, unsafeAddr sig.data)
|
||||||
doAssert res == 1, "Can't fail, per documentation"
|
doAssert res == 1, "Can't fail, per documentation"
|
||||||
@ -350,7 +342,6 @@ func toDer*(sig: SkSignature, data: var openarray[byte]): int =
|
|||||||
## this is more than `data.len`, `data` is not written to.
|
## this is more than `data.len`, `data` is not written to.
|
||||||
var buffer: array[SkDerSignatureMaxSize, byte]
|
var buffer: array[SkDerSignatureMaxSize, byte]
|
||||||
var plength = csize_t(len(buffer))
|
var plength = csize_t(len(buffer))
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ecdsa_signature_serialize_der(
|
let res = secp256k1_ecdsa_signature_serialize_der(
|
||||||
secp256k1_context_no_precomp, buffer.ptr0, addr plength,
|
secp256k1_context_no_precomp, buffer.ptr0, addr plength,
|
||||||
unsafeAddr sig.data)
|
unsafeAddr sig.data)
|
||||||
@ -375,7 +366,6 @@ func fromRaw*(T: type SkRecoverableSignature, data: openArray[byte]): SkResult[T
|
|||||||
|
|
||||||
let recid = cint(data[64])
|
let recid = cint(data[64])
|
||||||
var sig {.noinit.}: secp256k1_ecdsa_recoverable_signature
|
var sig {.noinit.}: secp256k1_ecdsa_recoverable_signature
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
if secp256k1_ecdsa_recoverable_signature_parse_compact(
|
if secp256k1_ecdsa_recoverable_signature_parse_compact(
|
||||||
secp256k1_context_no_precomp, addr sig, data.ptr0, recid) != 1:
|
secp256k1_context_no_precomp, addr sig, data.ptr0, recid) != 1:
|
||||||
return err("secp: invalid recoverable signature")
|
return err("secp: invalid recoverable signature")
|
||||||
@ -390,7 +380,6 @@ func fromHex*(T: type SkRecoverableSignature, data: string): SkResult[T] =
|
|||||||
func toRaw*(sig: SkRecoverableSignature): array[SkRawRecoverableSignatureSize, byte] =
|
func toRaw*(sig: SkRecoverableSignature): array[SkRawRecoverableSignatureSize, byte] =
|
||||||
## Converts recoverable signature to compact binary form
|
## Converts recoverable signature to compact binary form
|
||||||
var recid = cint(0)
|
var recid = cint(0)
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
let res = secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
||||||
secp256k1_context_no_precomp, result.ptr0, addr recid, unsafeAddr sig.data)
|
secp256k1_context_no_precomp, result.ptr0, addr recid, unsafeAddr sig.data)
|
||||||
doAssert res == 1, "can't fail, per documentation"
|
doAssert res == 1, "can't fail, per documentation"
|
||||||
@ -461,7 +450,6 @@ func recover*(sig: SkRecoverableSignature, msg: SkMessage): SkResult[SkPublicKey
|
|||||||
func ecdh*(seckey: SkSecretKey, pubkey: SkPublicKey): SkEcdhSecret =
|
func ecdh*(seckey: SkSecretKey, pubkey: SkPublicKey): SkEcdhSecret =
|
||||||
## Calculate ECDH shared secret.
|
## Calculate ECDH shared secret.
|
||||||
var secret {.noinit.}: array[SkEdchSecretSize, byte]
|
var secret {.noinit.}: array[SkEdchSecretSize, byte]
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ecdh(
|
let res = secp256k1_ecdh(
|
||||||
secp256k1_context_no_precomp, secret.ptr0, unsafeAddr pubkey.data,
|
secp256k1_context_no_precomp, secret.ptr0, unsafeAddr pubkey.data,
|
||||||
seckey.data.ptr0)
|
seckey.data.ptr0)
|
||||||
@ -473,7 +461,6 @@ func ecdhRaw*(seckey: SkSecretKey, pubkey: SkPublicKey): SkEcdhRawSecret =
|
|||||||
## Calculate ECDH shared secret, ethereum style
|
## Calculate ECDH shared secret, ethereum style
|
||||||
# TODO - deprecate: https://github.com/status-im/nim-eth/issues/222
|
# TODO - deprecate: https://github.com/status-im/nim-eth/issues/222
|
||||||
var secret {.noinit.}: array[SkEcdhRawSecretSize, byte]
|
var secret {.noinit.}: array[SkEcdhRawSecretSize, byte]
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ecdh_raw(
|
let res = secp256k1_ecdh_raw(
|
||||||
secp256k1_context_no_precomp, secret.ptr0, unsafeAddr pubkey.data,
|
secp256k1_context_no_precomp, secret.ptr0, unsafeAddr pubkey.data,
|
||||||
seckey.data.ptr0)
|
seckey.data.ptr0)
|
||||||
@ -519,7 +506,6 @@ proc default*(T: type SkEcdhSecret): T {.error: "loophole".}
|
|||||||
proc default*(T: type SkEcdhRawSecret): T {.error: "loophole".}
|
proc default*(T: type SkEcdhRawSecret): T {.error: "loophole".}
|
||||||
|
|
||||||
func tweakAdd*(secretKey: var SkSecretKey, tweak: openArray[byte]): SkResult[void] =
|
func tweakAdd*(secretKey: var SkSecretKey, tweak: openArray[byte]): SkResult[void] =
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ec_privkey_tweak_add(secp256k1_context_no_precomp, secretKey.data.ptr0, tweak.ptr0)
|
let res = secp256k1_ec_privkey_tweak_add(secp256k1_context_no_precomp, secretKey.data.ptr0, tweak.ptr0)
|
||||||
if res != 1:
|
if res != 1:
|
||||||
err("Tweak out of range, or invalid private key")
|
err("Tweak out of range, or invalid private key")
|
||||||
@ -527,9 +513,9 @@ func tweakAdd*(secretKey: var SkSecretKey, tweak: openArray[byte]): SkResult[voi
|
|||||||
ok()
|
ok()
|
||||||
|
|
||||||
func tweakMul*(secretKey: var SkSecretKey, tweak: openArray[byte]): SkResult[void] =
|
func tweakMul*(secretKey: var SkSecretKey, tweak: openArray[byte]): SkResult[void] =
|
||||||
{.noSideEffect.}: # secp256k1_context_no_precomp is actually const, see above
|
|
||||||
let res = secp256k1_ec_privkey_tweak_mul(secp256k1_context_no_precomp, secretKey.data.ptr0, tweak.ptr0)
|
let res = secp256k1_ec_privkey_tweak_mul(secp256k1_context_no_precomp, secretKey.data.ptr0, tweak.ptr0)
|
||||||
if res != 1:
|
if res != 1:
|
||||||
err("Tweak out of range, or equal to zero")
|
err("Tweak out of range, or equal to zero")
|
||||||
else:
|
else:
|
||||||
ok()
|
ok()
|
||||||
|
|
||||||
|
@ -74,11 +74,18 @@ const
|
|||||||
|
|
||||||
var secp256k1_context_no_precomp_imp {.
|
var secp256k1_context_no_precomp_imp {.
|
||||||
importc: "secp256k1_context_no_precomp".}: ptr secp256k1_context
|
importc: "secp256k1_context_no_precomp".}: ptr secp256k1_context
|
||||||
let secp256k1_context_no_precomp* = secp256k1_context_no_precomp_imp
|
|
||||||
|
|
||||||
var secp256k1_ecdh_hash_function_default_imp {.
|
var secp256k1_ecdh_hash_function_default_imp {.
|
||||||
importc: "secp256k1_ecdh_hash_function_default".}: secp256k1_ecdh_hash_function
|
importc: "secp256k1_ecdh_hash_function_default".}: secp256k1_ecdh_hash_function
|
||||||
let secp256k1_ecdh_hash_function_default* =
|
|
||||||
|
template secp256k1_context_no_precomp*: ptr secp256k1_context =
|
||||||
|
# This is really a constant
|
||||||
|
{.noSideEffect.}:
|
||||||
|
secp256k1_context_no_precomp_imp
|
||||||
|
|
||||||
|
template secp256k1_ecdh_hash_function_default*: secp256k1_ecdh_hash_function =
|
||||||
|
# This is really a constant
|
||||||
|
{.noSideEffect.}:
|
||||||
secp256k1_ecdh_hash_function_default_imp
|
secp256k1_ecdh_hash_function_default_imp
|
||||||
|
|
||||||
proc secp256k1_context_create*(
|
proc secp256k1_context_create*(
|
||||||
@ -282,12 +289,11 @@ proc secp256k1_ecdsa_recoverable_signature_parse_compact*(
|
|||||||
sig: ptr secp256k1_ecdsa_recoverable_signature;
|
sig: ptr secp256k1_ecdsa_recoverable_signature;
|
||||||
input64: ptr cuchar, recid: cint): cint {.secp.}
|
input64: ptr cuchar, recid: cint): cint {.secp.}
|
||||||
|
|
||||||
proc secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
func secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
||||||
pubkey: ptr secp256k1_pubkey;
|
pubkey: ptr secp256k1_pubkey;
|
||||||
privkey: ptr cuchar,
|
privkey: ptr cuchar,
|
||||||
hashfp: secp256k1_ecdh_hash_function,
|
hashfp: secp256k1_ecdh_hash_function,
|
||||||
data: pointer
|
data: pointer): cint {.secp.}
|
||||||
): cint {.secp.}
|
|
||||||
## Compute an EC Diffie-Hellman secret in constant time
|
## Compute an EC Diffie-Hellman secret in constant time
|
||||||
## Returns: 1: exponentiation was successful
|
## Returns: 1: exponentiation was successful
|
||||||
## 0: scalar was invalid (zero or overflow)
|
## 0: scalar was invalid (zero or overflow)
|
||||||
@ -301,10 +307,9 @@ proc secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
|||||||
|
|
||||||
template secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
template secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
||||||
pubkey: ptr secp256k1_pubkey;
|
pubkey: ptr secp256k1_pubkey;
|
||||||
privkey: ptr cuchar
|
privkey: ptr cuchar): cint =
|
||||||
): cint =
|
|
||||||
secp256k1_ecdh(ctx, output32, pubkey, privkey,
|
secp256k1_ecdh(ctx, output32, pubkey, privkey,
|
||||||
secp256k1_ecdh_hash_function_default, nil)
|
secp256k1_ecdh_hash_function_default(), nil)
|
||||||
|
|
||||||
proc secp256k1_ecdh_raw*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
proc secp256k1_ecdh_raw*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
||||||
pubkey: ptr secp256k1_pubkey;
|
pubkey: ptr secp256k1_pubkey;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user