Skip to content

Conversation

@elichai
Copy link
Contributor

@elichai elichai commented Apr 30, 2020

Resolves #184

I didn't add a sign/verify/recover example yet because I'd prefer that the example included hashing a message,
but we don't provide a hash function so I decided to start only with key generation and ECDH, and get feedback.

I did not check the example on windows, but did try to implement it correctly, if anyone has access to a windows machine(and knows how to build on windows hehe) please test this :)

P.S. Should the usage examples be MIT or CC0(Public Domain)?

@elichai elichai changed the title Add usage example Add usage examples Apr 30, 2020
Copy link
Contributor

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Alternatively the module-specific examples could be in their respective module directory. But I think either way is fine.

@elichai
Copy link
Contributor Author

elichai commented Apr 30, 2020

Alternatively the module-specific examples could be in their respective module directory. But I think either way is fine.

I thought about it, but I think that for newcomers it's easier to see an examples directory at the top level and start from there.

@real-or-random
Copy link
Contributor

real-or-random commented May 1, 2020

My feeling is this entire issue of secret key handling deserves a broader discussion.

We don't have a key generation function and Core won't need one. If we're now targeting different users more, maybe it's time to have one. But it's hard, see all the discussion points above. And it really depends on the user's platform. So actually I'd be happy with good examples and some references to correct methods to get randomness on different OSes.

On the other hand, nothing should stop us from exporting a cleaning function in #636, even if we don't have one for key gen.

What are other libraries doing for key generation?

size_t len;
secp256k1_pubkey pubkey;
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
while (1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to loop here and instead bail out if we hit an invalid key.

The probability of hitting an invalid key is negligible if your randomness is good.
So this loop is only relevant if your randomness is broken. And then it hides the fact that your randomness is broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see. I still think that my argument holds up. Maybe @sipa can chime in for the code in Core.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the probability of an invalid key is negligible (something like 1/2^128?), I feel that if the verify function is provided in the library then it could still be informative to include in the examples.

if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
	printf("Invalid secret key\n");
	return 1;
}

* Which is why we create a context for signing(SECP256K1_CONTEXT_SIGN).
* (The docs for `secp256k1_ecdh` don't require any special context, just some context) */

secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context should be randomized here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@elichai elichai force-pushed the 2020-04-examples branch from 485c68b to b7077be Compare July 30, 2020 08:53
@jonasnick
Copy link
Contributor

Would be cool to revive this PR since it's really helpful, well-documented and provides best practices for getting randomness on different platforms. In particular, I don't think this PR should be blocked on the result of the key generation discussion. @elichai if you want to add examples for the schnorrsig module, feel free to do it, but we can also add this in a different PR.

@elichai elichai force-pushed the 2020-04-examples branch 2 times, most recently from 7e16921 to 0be44b4 Compare November 9, 2020 16:12
@elichai
Copy link
Contributor Author

elichai commented Nov 9, 2020

Rebased, and replaced the keygen example with a schnorr and ecdsa examples.
Please review and comment if you think something I've done isn't best practice or some comment's wording isn't good enough

@real-or-random
Copy link
Contributor

P.S. Should the usage examples be MIT or CC0(Public Domain)?

I think we should make them CC0 to avoid any doubt.

@ssocolow
Copy link

As a new user, simple example programs are really great. Thanks!

@elichai elichai force-pushed the 2020-04-examples branch from 0be44b4 to a00271b Compare July 4, 2021 14:42
@elichai
Copy link
Contributor Author

elichai commented Jul 4, 2021

Rebased and fixed review comments,
Feel free to suggest alternative phrasings to the comments I wrote (English is my 2nd language)

@elichai elichai force-pushed the 2020-04-examples branch from a00271b to 4cc6984 Compare July 7, 2021 10:19
@elichai
Copy link
Contributor Author

elichai commented Jul 7, 2021

Rebasing on master again so the CI will work

@elichai elichai force-pushed the 2020-04-examples branch from 4cc6984 to 99fbf68 Compare July 7, 2021 10:20
@scgbckbone
Copy link

Tested ACK

  • ubuntu 20.04 Linux workstation 5.13.0-28-generic #31~20.04.1-Ubuntu SMP Wed Jan 19 14:08:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  • windows 10 MINGW64_NT-10.0-19044 DESKTOP-UV75MOO 3.3.3-341.x86_64 2022-01-18 13:00 UTC x86_64 Msys

@elichai
Copy link
Contributor Author

elichai commented Feb 22, 2022

Fixed comments, and squashed

Copy link
Contributor

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK beecb9a

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elichai Can you quickly fix the copyright and the msg_hash comment?

I think then this is good to go. We had many eyes on it and I can address the other two comments in another PR.

Comment on lines 1 to 8
/*************************************************************************
* Copyright (c) 2020-2021 Elichai Turkel *
* Distributed under the CC0 software license, see the accompanying file *
* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
*************************************************************************/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry interrupting with legal stuff and not noticing earlier... I think it's weird to say "Copyright" and CC0 at the same time. And CC0 is strictly speaking not a software license. https://wiki.creativecommons.org/wiki/CC0_FAQ#May_I_apply_CC0_to_computer_software.3F_If_so.2C_is_there_a_recommended_implementation.3F provides some boilerplate.

Here's a suggestion, slightly adopted from the above to make sure it applies only to a single file.

Suggested change
/*************************************************************************
* Copyright (c) 2020-2021 Elichai Turkel *
* Distributed under the CC0 software license, see the accompanying file *
* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
*************************************************************************/
/*************************************************************************
* Written in 2020-2022 by Elichai Turkel *
* To the extent possible under law, the author(s) have dedicated all *
* copyright and related and neighboring rights to the software in this *
* file to the public domain worldwide. This software is distributed *
* without any warranty. For the CC0 Public Domain Dedication, see *
* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
*************************************************************************/

Copy link
Contributor Author

@elichai elichai Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I hate dealing with licenses. fixed

examples/ecdsa.c Outdated
Comment on lines 82 to 83
/* Generate an ECDSA signature, note that even though here `msg_hash` is set
* to zeros, it MUST contain a hash, otherwise ECDSA is easily broken.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msg_hash is not set to zeros.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! fixed

Comment on lines +125 to +134
/* It's best practice to try to clear secrets from memory after using them.
* This is done because some bugs can allow an attacker to leak memory, for
* example through "out of bounds" array access (see Heartbleed), Or the OS
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
*
* TODO: Prevent these writes from being optimized out, as any good compiler
* will remove any writes that aren't used. */
memset(seckey, 0, sizeof(seckey));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea of having a TODO here. I mean we spent days getting the randomness right, then this feels wrong.

We could copy https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/support/cleanse.cpp#L14 and then really call the file util.h or refer to it...

In the interest of moving this forward, we suggest to leave this as is and I'm happy to open a further PR that fixes it.

return_val = secp256k1_context_randomize(ctx, randomize);
assert(return_val);

/*** Key Generation ***/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be good to add a header to every code section in this part, e.g., "Party 1: " and "Party 2:" or similar, to make sure.

This would require splitting the key generation loop but I think that's really better than. Real code wouldn't work like this, so I think we should avoid it in example code.

This is also something I can do in a follow-up PR.

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7c9502c

As I said, I can create a follow-up PR.

The one CI failure for the sage prover is harmless and only because just sage on CI was introduced after the last rebase of this PR. It will work on master.

Copy link
Contributor

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7c9502c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation should provide example callers.