csecp256k1

Haskell FFI bindings to bitcoin-core/secp256k1 (docs.ppad.tech/csecp256k1).
git clone git://git.ppad.tech/csecp256k1.git
Log | Files | Refs | README | LICENSE

eckey_impl.h (3958B)


      1 /***********************************************************************
      2  * Copyright (c) 2013, 2014 Pieter Wuille                              *
      3  * Distributed under the MIT software license, see the accompanying    *
      4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
      5  ***********************************************************************/
      6 
      7 #ifndef SECP256K1_ECKEY_IMPL_H
      8 #define SECP256K1_ECKEY_IMPL_H
      9 
     10 #include "eckey.h"
     11 
     12 #include "scalar.h"
     13 #include "field.h"
     14 #include "group.h"
     15 #include "ecmult_gen.h"
     16 
     17 static int haskellsecp256k1_v0_1_0_eckey_pubkey_parse(haskellsecp256k1_v0_1_0_ge *elem, const unsigned char *pub, size_t size) {
     18     if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
     19         haskellsecp256k1_v0_1_0_fe x;
     20         return haskellsecp256k1_v0_1_0_fe_set_b32_limit(&x, pub+1) && haskellsecp256k1_v0_1_0_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
     21     } else if (size == 65 && (pub[0] == SECP256K1_TAG_PUBKEY_UNCOMPRESSED || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
     22         haskellsecp256k1_v0_1_0_fe x, y;
     23         if (!haskellsecp256k1_v0_1_0_fe_set_b32_limit(&x, pub+1) || !haskellsecp256k1_v0_1_0_fe_set_b32_limit(&y, pub+33)) {
     24             return 0;
     25         }
     26         haskellsecp256k1_v0_1_0_ge_set_xy(elem, &x, &y);
     27         if ((pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD) &&
     28             haskellsecp256k1_v0_1_0_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
     29             return 0;
     30         }
     31         return haskellsecp256k1_v0_1_0_ge_is_valid_var(elem);
     32     } else {
     33         return 0;
     34     }
     35 }
     36 
     37 static int haskellsecp256k1_v0_1_0_eckey_pubkey_serialize(haskellsecp256k1_v0_1_0_ge *elem, unsigned char *pub, size_t *size, int compressed) {
     38     if (haskellsecp256k1_v0_1_0_ge_is_infinity(elem)) {
     39         return 0;
     40     }
     41     haskellsecp256k1_v0_1_0_fe_normalize_var(&elem->x);
     42     haskellsecp256k1_v0_1_0_fe_normalize_var(&elem->y);
     43     haskellsecp256k1_v0_1_0_fe_get_b32(&pub[1], &elem->x);
     44     if (compressed) {
     45         *size = 33;
     46         pub[0] = haskellsecp256k1_v0_1_0_fe_is_odd(&elem->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN;
     47     } else {
     48         *size = 65;
     49         pub[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED;
     50         haskellsecp256k1_v0_1_0_fe_get_b32(&pub[33], &elem->y);
     51     }
     52     return 1;
     53 }
     54 
     55 static int haskellsecp256k1_v0_1_0_eckey_privkey_tweak_add(haskellsecp256k1_v0_1_0_scalar *key, const haskellsecp256k1_v0_1_0_scalar *tweak) {
     56     haskellsecp256k1_v0_1_0_scalar_add(key, key, tweak);
     57     return !haskellsecp256k1_v0_1_0_scalar_is_zero(key);
     58 }
     59 
     60 static int haskellsecp256k1_v0_1_0_eckey_pubkey_tweak_add(haskellsecp256k1_v0_1_0_ge *key, const haskellsecp256k1_v0_1_0_scalar *tweak) {
     61     haskellsecp256k1_v0_1_0_gej pt;
     62     haskellsecp256k1_v0_1_0_gej_set_ge(&pt, key);
     63     haskellsecp256k1_v0_1_0_ecmult(&pt, &pt, &haskellsecp256k1_v0_1_0_scalar_one, tweak);
     64 
     65     if (haskellsecp256k1_v0_1_0_gej_is_infinity(&pt)) {
     66         return 0;
     67     }
     68     haskellsecp256k1_v0_1_0_ge_set_gej(key, &pt);
     69     return 1;
     70 }
     71 
     72 static int haskellsecp256k1_v0_1_0_eckey_privkey_tweak_mul(haskellsecp256k1_v0_1_0_scalar *key, const haskellsecp256k1_v0_1_0_scalar *tweak) {
     73     int ret;
     74     ret = !haskellsecp256k1_v0_1_0_scalar_is_zero(tweak);
     75 
     76     haskellsecp256k1_v0_1_0_scalar_mul(key, key, tweak);
     77     return ret;
     78 }
     79 
     80 static int haskellsecp256k1_v0_1_0_eckey_pubkey_tweak_mul(haskellsecp256k1_v0_1_0_ge *key, const haskellsecp256k1_v0_1_0_scalar *tweak) {
     81     haskellsecp256k1_v0_1_0_gej pt;
     82     if (haskellsecp256k1_v0_1_0_scalar_is_zero(tweak)) {
     83         return 0;
     84     }
     85 
     86     haskellsecp256k1_v0_1_0_gej_set_ge(&pt, key);
     87     haskellsecp256k1_v0_1_0_ecmult(&pt, &pt, tweak, &haskellsecp256k1_v0_1_0_scalar_zero);
     88     haskellsecp256k1_v0_1_0_ge_set_gej(key, &pt);
     89     return 1;
     90 }
     91 
     92 #endif /* SECP256K1_ECKEY_IMPL_H */