# Need help with /bitcoin-core/secp256k1

**URL:** https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215
**Category:** New to Julia
**Created:** [January 7, 2018, 7:10am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215 "2018-01-07T07:10:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 7:10am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/1 "2018-01-07T07:10:00Z")

</div>

I want to create a public key for private key 7  
There is such code

```julia
struct secp256k1_pubkey
    data::NTuple{64, Cuchar}
end

pubkey = Ref{secp256k1_pubkey}()
private_key = 7
pub = ccall((:secp256k1_ec_pubkey_create, "/usr/local/lib/libsecp256k1.so"), Int, (Pair{Int64, Int64}, Ref{secp256k1_pubkey}, Int32), secp256k1_ctx, pubkey, private_key)

```

in return, I get  
[libsecp256k1] illegal argument: pubkey != NULL

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 7, 2018, 8:12am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/2 "2018-01-07T08:12:28Z")

</div>

Do you have source for that library? Can you give a link to it?

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 8:15am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/3 "2018-01-07T08:15:31Z")

</div>

ScottPJones, hello!  
Oh sure  
[https://github.com/bitcoin-core/secp256k1](https://github.com/bitcoin-core/secp256k1)

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 7, 2018, 8:36am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/4 "2018-01-07T08:36:14Z")

</div>

It looks like you don’t have the calling sequence correct:  
The function wants 3 pointers:

```nohighlight
int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {

```

I don’t know why you are trying to pass a Julia type for the first argument, and `seckey` needs to be a pointer to a `Vector{UInt8}`, you are passing an `Int`, which is likely to cause a core-dump.

Also, it returns a `Cint`, not an `Int` (`Cint` will be 32-bits, `Int` will be 32 or 64 on Julia, 64 for 64-bit platforms)

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 8:54am UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/5 "2018-01-07T08:54:09Z")

</div>

Can you demonstrate the code?  
If I change the type, I get a type conversion error.

```julia
int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
    secp256k1_gej pj;
    secp256k1_ge p;
    secp256k1_scalar sec;
    int overflow;
    int ret = 0;
    VERIFY_CHECK(ctx != NULL);
    ARG_CHECK(pubkey != NULL);
    memset(pubkey, 0, sizeof(*pubkey));
    ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
    ARG_CHECK(seckey != NULL);

    secp256k1_scalar_set_b32(&sec, seckey, &overflow);
    ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
    if (ret) {
        secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
        secp256k1_ge_set_gej(&p, &pj);
        secp256k1_pubkey_save(pubkey, &p);
    }
    secp256k1_scalar_clear(&sec);
    return ret;
}

```

on the first argument the function does not swear  
VERIFY\_CHECK (ctx! = NULL); here we pass  
and here  
ARG\_CHECK (pubkey! = NULL); we get an error

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 1:28pm UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/6 "2018-01-07T13:28:27Z")

</div>

Tell me, do not you exist the standard library of Julia for the ecdsa?

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [January 7, 2018, 2:06pm UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/7 "2018-01-07T14:06:25Z")

</div>

> ARG\_CHECK (pubkey! = NULL); we get an error

This means that you have passed `NULL` for `pubkey`. Let’s see what is `pubkey` in your code:

> pubkey = Ref{secp256k1\_pubkey}()

Well, `Ref` without arguments is that same as `Ref(0)`, so your `pubkey` is indeed `NULL`, while `secp256k1_ec_pubkey_create` clearly needs it to be a pointer to a pre-allocated array of 64 bytes. Try something like this:

```julia
pubkey = Vector{Cuchar}(64) # pre-allocate buffer
private_key = 7
pub = ccall((:secp256k1_ec_pubkey_create, "/usr/local/lib/libsecp256k1.so"), Cint, (Pair{Int64, Int64}, Ptr{Cuchar}, Int32), secp256k1_ctx, pubkey, private_key)

```

Note that I also replaced `Ref{secp256k1_pubkey}` with `Ptr{Cuchar}`. When you invoke `ccall`, Julia should(\*) take a pointer to array data and pass it to the function, so when it’s finished this array will contain the populated data.

(\* - “should” because I always forget all the conversions between Julia and C types, but a quick test will confirm it).

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 2:40pm UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/8 "2018-01-07T14:40:43Z")

</div>

dfdx, thanks, but the mistake is same  
[libsecp256k1] illegal argument: pubkey != NULL

---

<div class="post-metadata">

### Author: ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)
#### Post date: [January 7, 2018, 2:58pm UTC](https://discourse.julialang.org/t/need-help-with-bitcoin-core-secp256k1/8215/9 "2018-01-07T14:58:56Z")

</div>

```julia
pubkey = Vector{Cuchar}(64) 
println(pubkey)

UInt8[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

```

```julia
struct secp256k1_pubkey
    data::NTuple{64, Cuchar}
end

pubkey = Ref{secp256k1_pubkey}()
println(pubkey)
Base.RefValue{secp256k1_pubkey}(secp256k1_pubkey((0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))
UInt8[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

```
