Commit Graph

601 Commits

Author SHA1 Message Date
Andrew Poelstra 6599e24010
Merge rust-bitcoin/rust-secp256k1#441: Derive Hash for Signature
ef7f1972a7 Derive Hash for Signature (Tobin C. Harding)

Pull request description:

  In preparation for deriving `Hash` in miniscript, derive `Hash` on the `ecdsa::Signature`.

  ref: https://github.com/rust-bitcoin/rust-miniscript/issues/226

ACKs for top commit:
  apoelstra:
    ACK ef7f1972a7
  elichai:
    ACK ef7f1972a7

Tree-SHA512: 7313f59971444ae18611adbafe86a09478eddd7357f2b7f3ad3bb1761609b6358b156975086f6c318eb2777018b7b2f44386321108939acbcf2d0a522e7e208e
2022-05-09 23:44:17 +00:00
Tibo-lg 997b4b35a9 Fix depreciation warning typos 2022-05-06 16:12:10 +09:00
Tobin C. Harding ef7f1972a7 Derive Hash for Signature
In preparation for deriving `Hash` in miniscript, derive `Hash` on the
`ecdsa::Signature`.
2022-05-06 13:35:23 +10:00
Andrew Poelstra a30e9bb9ff
Merge rust-bitcoin/rust-secp256k1#430: Add convenience methods for keys
f08276adfc Add convenience methods for keys (Tobin Harding)
b4c7fa0d4e Let the compiler work out int size (Tobin Harding)
c612130864 Borrow secret key (Tobin Harding)

Pull request description:

  We have a bunch of `from_<key>` methods for converting between key types. To make the API more ergonomic to use we can add methods that do the same but called on a variable e.g., once applied the following are equivalent:

  - `let pk = PublicKey::from_keypair(kp)`
  - `let pk = kp.public_key()`

  Do this for `SecretKey`, `PublicKey`, `KeyPair`, and `XOnlyKeyPair`.

  Fixes: #428

  ### Note to reviewers

  - `XOnlyPublicKey` -> `PublicKey` logic is made up by me, I could not work out how to get `libsecp256k1` to do this.
  - Please review the tests carefully, they include assumptions based on my current understanding of the cryptography :)

ACKs for top commit:
  sanket1729:
    ACK f08276adfc. Thanks for going through all the iterations.
  apoelstra:
    ACK f08276adfc

Tree-SHA512: 1503a6e570a3958110c6f24cd6d075fe5694b3b32b91a7a9d332c63aa0806198ff10bdd95e7f9de0cf73cbf4e3655c6826bd04e5044d1b019f551471b187c8ea
2022-04-30 16:21:46 +00:00
Andrew Poelstra 37f4f005d1
Merge rust-bitcoin/rust-secp256k1#429: Misc doc fixes
676a9800df Remove unnecessary panic message (sanket1729)
aa50cc6ced Remove Schnorr word from keypairs (sanket1729)

Pull request description:

  Keypairs are pair of EC points that don't have anything to do with the
  signature algorithm

ACKs for top commit:
  apoelstra:
    ACK 676a9800df
  tcharding:
    ACK 676a9800df

Tree-SHA512: ed3e6f5e821d18641234b308b130271dcd2ec0dd6519a0e9d91564ab8e902b82180d7df377f2bcf08cd3ca1df7ce775422e4a3c386637eaff348e58b033de3ea
2022-04-22 16:55:33 +00:00
Tobin Harding f08276adfc Add convenience methods for keys
We have a bunch of `from_<key>` methods for converting between key types.
To improve the API and make it more ergonomic to use we can add methods
that do the same but can be called on the initial key instead of on the
resulting key's type. E.g. once applied the following are equivalent:

- `let pk = PublicKey::from_keypair(kp)`
- `let pk = kp.public_key()`

Do this for `SecretKey`, `PublicKey`, `KeyPair`, and `XOnlyKeyPair`.
2022-04-04 12:58:46 +10:00
Tobin Harding b4c7fa0d4e Let the compiler work out int size
We have two places in the code where we pass a mutable parity integer
to ffi code. At one callsite we tell the compiler explicitly what type
it is (`::secp256k1_sys::types::c_int`) and at the other call site we
let the compiler figure out the type.

Is one way better than the other? I don't know. But letting the compiler
figure it out seems to make the code easier to read.
2022-04-04 12:50:52 +10:00
Tobin Harding c612130864 Borrow secret key
`SecretKey` implements `Copy` and it is fine to take owneship of it; we
have multiple methods called `from_secret_key` and they all borrow the
secret key parameter. Favour consistency over perfection.

Borrow secret key parameter as is done in other `from_secret_key`
methods.
2022-04-04 12:50:52 +10:00
junderw f93ca81348
Add sign_ecdsa_with_noncedata and sign_ecdsa_recoverable_with_noncedata 2022-03-22 21:13:31 +09:00
sanket1729 676a9800df Remove unnecessary panic message
1) All types in rust should have the guarantee that well-formed data is
stored in SecretKey type. Therefore, IMO the panic message is
unnecessary.
2022-03-21 16:37:15 -07:00
sanket1729 aa50cc6ced Remove Schnorr word from keypairs
Keypairs are pair of EC points that don't have anything to do with the
signature algorithm
2022-03-21 16:36:58 -07:00
Tobin Harding de65fb2f1e
Implement de/serialization for SharedSecret
As we do for other keys implement serde de/serialization for the
`SharedSecret`. Includes implementation of `from_slice` method that is
the borrowed version of `from_bytes` as well as a `FromStr`
implementation that parses a hex string.
2022-03-12 07:46:21 +11:00
Dominik Spicher 9be8e74107 Allow SharedSecret to be created from byte array
This was accidentally removed in 8b2edad. See also the discussion
on https://github.com/rust-bitcoin/rust-secp256k1/pull/402
2022-03-10 22:38:25 +01:00
Andrew Poelstra f3d48a298e update "should terminate abnormally" test to trigger a different ARG_CHECK
We can no longer produce non-verification context objects, so instead produce
an invalid public key.
2022-03-08 19:45:43 +00:00
Andrew Poelstra 8294ea3f50 secp256k1-sys: update upstream library
Two API changes needed to be reflected: schnorrsig_sign and schnorrsig_verify.

Also bump both Cargo.toml files
2022-03-08 19:45:41 +00:00
Tobin Harding 7b91f9d8ef
Remove schnorrsig from test names
Recently we moved from using the identifier 'schnorrsig' to 'schnorr',
we omitted to update the tests.

While we are at it use more idiomatic Rust unit test names (i.e., do not
start test name with `test_` because it stutters when the name is read
in output of `cargo test`).
2022-03-04 14:28:29 +00:00
Tobin Harding 4b840ffe87
Remove schnorrsig from helper method
Recently we moved from using the identifier 'schnorrsig' to 'schnorr',
we omitted to update a helper function.
2022-03-04 14:28:28 +00:00
Tobin Harding 79770e17f3
Deprecate SCHNORRSIG_SIGNATURE_SIZE
Recently we moved from using the identifier 'schnorrsig' to 'schnorr',
we omitted to update the schnorr signature size constant.

Deprecate `SCHNORRSIG_SIGNATURE_SIZE` and add
`SCHONORR_SIGNATURE_SIZE`.
2022-03-04 14:28:24 +00:00
Tobin Harding 7a417fd1c5
Deprecate SCHNORRSIG_PUBLIC_KEY_SIZE
Recently we moved from using the identifier 'schnorrsig' to 'schnorr',
we omitted to update the schnorr public key size constant.

Deprecate `SCHNORRSIG_PUBLIC_KEY_SIZE` and add
`SCHONORR_PUBLIC_KEY_SIZE`.
2022-03-04 14:27:58 +00:00
Andrew Poelstra dc90a43e68
Merge rust-bitcoin/rust-secp256k1#403: `Parity` conversion and error handling cleanup
5acf6d23d3 `Parity` conversion and error handling cleanup (Martin Habovstiak)

Pull request description:

  This removes the deprecated `From` conversion, replaces it with
  `TryFrom`, and adds more convenience conversions. A new error type is
  created for the invalid parity error with conversion to catch-all
  `Error`.

  This is intended for an API-breaking version.

ACKs for top commit:
  apoelstra:
    ACK 5acf6d23d3

Tree-SHA512: 49b73fc90455c172012b46f36eafa7d256b940f4b431b4eedb577ab07d9402eae40af931e00b3c409bbe502dbcac064a742e874a5e8bedd8d0cbe92a468ae4f6
2022-03-01 13:01:15 +00:00
Martin Habovstiak 5acf6d23d3 `Parity` conversion and error handling cleanup
This removes the deprecated `From` conversion and adds a new error
type for the invalid parity error with a conversion to the catch-all
`Error`.
2022-02-28 20:59:51 +01:00
Tobin Harding cf6badf96a
Obfuscate SharedSecret when printing
Currently printing the `SharedSecret` using `Display` or `Debug` prints
the real secret, this is sub-optimal. We have a solution for other
secrets in the project where printing is obfuscated and we provide a
`display_secret` method for explicitly printing.

Mirror the logic for other secrets and obfuscate the `SharedSecret` when printing.
2022-02-28 07:22:17 +00:00
Tobin Harding e4be664d97
Improve rustdocs for displaying secrets
Improve rustdocs on `display_secret` by doing:

- Minor improvements to the rustdocs to aid readability in the editor.
- Do not guarantee (`assert_eq!`) debug output
2022-02-28 07:16:41 +00:00
Tobin Harding 5c7c76eb74
Rename serialize_secret -> secret_bytes
The `serialize_secret` method is a getter method, it does not do any
serialisation. However we use the method on secret keys and key types so
in order for the name to be uniform use the descriptive name
`secret_bytes`.

Rename `serialize_secret` to be `secret_bytes`.
2022-02-28 07:11:24 +00:00
Tobin Harding 4ded2c0478
Use byte instead of i
The identifier `i` is predominantly used for indexing an array but we
are using it as a place holder for the iterated value of an array that
is then printed. The identifier `byte` is more descriptive.

Done in preparation for adding similar code to the `ecdh` module.
2022-02-24 19:48:23 +00:00
Tobin Harding 91106f5685
Remove magic number
In array initialisation we use magic number 64, this is the secret bytes
length multiplied by 2.

Please note; we still use the magic number 32, left as such because it
is used in various ways and its not immediately clear that using a
single const would be any more descriptive.

Use `SECRET_KEY_SIZE * 2` instead of magic number 64.
2022-02-24 19:48:19 +00:00
Tobin Harding 6dca99631f
Mention bitcoin_hashes in obfuscated secret msg
Hashing the debug output for secrets can be done with `bitcoin_hashes`
not just `std`. Mention this in the obfuscated string output when
neither are available.
2022-02-24 19:48:15 +00:00
Andrew Poelstra 8b2edad041
Merge rust-bitcoin/rust-secp256k1#402: Limit SharedSecret to 32 byte buffer
5603d71ad3 Limit SharedSecret to 32 byte buffer (Tobin Harding)
d5eeb099ad Use more intuitive local var numbering (Tobin Harding)
834f63c26c Separate new_with_hash into public function (Tobin Harding)

Pull request description:

  Currently `SharedSecret` provides a way to get a shared secret using SHA256 _as well as_ a way to use a custom hash function to get the shared secret. Internally `SharedSecret` uses a 256 byte buffer, this is a tad wasteful. We would like to keep the current functionality but reduce memory usage.

  - Patch 1: Pulls the `new_with_hash` logic out into a standalone public function that just returns the 64 bytes representing the x,y co-ordinates of the computed shared secret point. Callers are then responsible for hashing this point to get the shared secret (idea by @Kixunil, thanks).
  - Patch 2: Does trivial refactor
  - Patch 3: Uses a 32 byte buffer internally for `SharedSecret`. This is basically a revert of the work @elichai did to add the custom hashing logic. @elichai please holla if you are not happy with me walking all over this code :)

  ### Note to reviewers

  Secret obfuscation is done on top of this in https://github.com/rust-bitcoin/rust-secp256k1/pull/396, they could be reviewed in order if this work is of interest to you.

ACKs for top commit:
  apoelstra:
    ACK 5603d71ad3

Tree-SHA512: 48982a4a6a700a111e4c1d5d21d62503d34f433d8cb303d11ff018d2f2be2467fa806107018db16b6d0fcc5ff1a0325dd5790c62c47831c7cd2141a1b6f9467d
2022-02-24 15:17:44 +00:00
Andrew Poelstra c7d6cdbaba
Merge rust-bitcoin/rust-secp256k1#401: Breaking: changed Parity serialization to u8
e6cb588a23 Breaking: changed `Parity` serialization to `u8` (Martin Habovstiak)

Pull request description:

  Serializing the value as `u8` is more compact but this is a breaking
  change.

  `Visitor` was renamed to avoid hungarian notation and maybe allow other
  integers in the future.

  For next major version, depends on #400

ACKs for top commit:
  tcharding:
    tACK e6cb588
  apoelstra:
    ACK e6cb588a23

Tree-SHA512: 1432a2f3c913c3a7eaec5228fd2dd4e8320d828128bec71812cbf56dd8950c969ed22c69867402eb9e820127868d29b291f3374c6e15de0a3ff2341420c4bbab
2022-02-24 15:16:21 +00:00
Tobin Harding 5603d71ad3
Limit SharedSecret to 32 byte buffer
The `SharedSecret` uses sha256 to hash the secret, this implies the
secret is 32 bytes of data.

Currently we use a buffer of 256 bytes, this is unnecessary.

Change the implementation of `SharedSecret` to use a 32 byte buffer.
2022-02-21 13:33:17 +00:00
Tobin Harding d5eeb099ad
Use more intuitive local var numbering
In test code we use multiple pub/sec keys. It is more intuitive if the
'secret 1' is generated by the owner of secret key 1.

Refactor only, no logic changes.
2022-02-21 13:11:30 +00:00
Tobin Harding 834f63c26c
Separate new_with_hash into public function
In preparation for simplifying the `SharedSecret` internals pull the
`new_with_hash` function logic out into a standalone public function
that provides similar functionality without use of the `SharedSecret`
struct. Function now returns the 64 bytes of data representing a shared
point on the curve, callers are expected to the hash these bytes to get
a shared secret.
2022-02-18 09:51:06 +00:00
Andrew Poelstra 2a25e5eae8 restore `global-context-less-secure` feature 2022-02-16 23:46:52 +00:00
Tobin Harding 4c9bab9f6e
Remove explicit mention of feature requirements
We are using `cfg_attr` to instruct the rustdocs build system to
highlight feature requirements for functions, there is no need to
explicitly mention feature requirements in the text.
2022-02-11 07:47:52 +00:00
Tobin Harding 806eaca5f1
Use feature std with rand-std
Recently we fixed a bunch of feature gates to use `rand-std` instead
of `rand` but in doing so did not notice that the same feature gates
were using `alloc` which is meaningless if `std` is enabled.

Feature gate on `std` if we are using `rand-std`.
2022-02-11 07:44:16 +00:00
Andrew Poelstra df7520e951
Merge rust-bitcoin/rust-secp256k1#340: Improve documentation
c73eb2f391 Use 'extra' instead of 'cheap' (Tobin Harding)
c79eb976ca Remove unnecessary explanation (Tobin Harding)
f95e91a6da Use isn't instead of shouldn't (Tobin Harding)
c9e6ca1680 Use rust-bitcoin module doc style (Tobin Harding)
3fa6762437 Add link to referenced commit (Tobin Harding)
f5e68f3ba7 Add ticks around code snippet (Tobin Harding)
d25431c1da Use 3rd person tense for function docs (Tobin Harding)
c3be285c1d Fix size constant docs (Tobin Harding)
5e07e7596b Add period to sentences (Tobin Harding)
269bde042f Remove unnecessary capitalisation (Tobin Harding)

Pull request description:

  In a continued effort to find my feet around here, and inspired by issue #128 I've done a codebase wide audit of the docs (primarily just rustdocs but I glanced at `//` docs as well). Each change is in a separate commit so can be removed if resistance is met. (_"resistance is futile"_).

  I've based the stylistic decisions on [work done](https://github.com/rust-bitcoin/rust-bitcoin/pull/704) in rust-bitcoin.

  I believe the only controversial change is the last (commit: da161c9 Use rust-bitcoin module doc style), please review that one carefully.

ACKs for top commit:
  apoelstra:
    ACK c73eb2f391

Tree-SHA512: 5ea215de3fd23ca2a4f25d8f8d59a85a299044fe495269c43b621291ea50c58856fa8544e36cc109b7bdb1a7a59bcab8711f30113572ddce4509d3b06ff0d3b6
2022-02-10 15:42:30 +00:00
Tobin Harding c73eb2f391
Use 'extra' instead of 'cheap'
The word 'extra' better describes the sidechannel resistance gained by
re-randomising the context.
2022-02-10 09:57:15 +00:00
Tobin Harding c79eb976ca
Remove unnecessary explanation
The nested pub inside a private module is easy to understand, we do not
need an explanation.
2022-02-10 09:55:41 +00:00
Tobin Harding f95e91a6da
Use isn't instead of shouldn't
This definitely isn't possible, change the phrase.
2022-02-10 09:54:35 +00:00
Martin Habovstiak e6cb588a23 Breaking: changed `Parity` serialization to `u8`
Serializing the value as `u8` is more compact but this is a breaking
change.

`Visitor` was renamed to avoid hungarian notation and maybe allow other
integers in the future.
2022-02-09 20:46:20 +01:00
Martin Habovstiak 662843e73b Improved error handling in `Parity` serde impl
* Fixes error message to be according to the trait documentation
* Uses `unexpected_value` to provide more information about the error
2022-02-09 20:42:12 +01:00
Andrew Poelstra 8bf29271de
Merge rust-bitcoin/rust-secp256k1#399: Clarified conversions between `Parity` and integers
705c9cfbc1 Clarified conversions between `Parity` and integers (Martin Habovstiak)

Pull request description:

  This was discussed in https://github.com/rust-bitcoin/rust-secp256k1/pull/390#issuecomment-1033018430

ACKs for top commit:
  apoelstra:
    ACK 705c9cfbc1

Tree-SHA512: 3ba2ec566099c3c6d1c6f830e4959312b818b8766d924e3d995e6b23bd196ab747cc03d46f494ef451569188b0163f53e3236cacd20bfae9118ee76bcdbc9c02
2022-02-09 18:36:19 +00:00
Andrew Poelstra f97e41ae21
Merge rust-bitcoin/rust-secp256k1#398: Implement LowerHex and Display for Message
a209836a99 Implement LowerHex and Display for Message (Tobin Harding)

Pull request description:

  Implement `fmt::LowerHex` for `Message`. Implement `Display` by calling `LowerHex`.

  Resolves: #251

ACKs for top commit:
  apoelstra:
    ACK a209836a99

Tree-SHA512: 64eeafc57ea2814108228d8427cd650076eb3cbb85ae14a7c5a6f39f5e20ca9b83b4ccc27c201668fd57a34fde0a37be4098aa5c602208a81a2018293b40b64d
2022-02-09 18:33:34 +00:00
Andrew Poelstra bc278fa351
Merge rust-bitcoin/rust-secp256k1#393: Add custom Debug impl for RecoverableSignature
4c43d5e20f Add custom Debug impl for RecoverableSignature (Tobin Harding)

Pull request description:

  Currently when debug printing the `RecoverableSignature` we do so byte by byte, this means that the output differs depending on the endianess of the machine. If instead we serialize the signature in compact form then the output is the same irrespective of the endianess.

  With this applied the following two commands now pass:

  ```
  cargo test test_debug_output --features=recovery
  ```
  ```
  cross test --target powerpc-unknown-linux-gnu test_debug_output --features=recovery
  ```

  Fixes: #375

ACKs for top commit:
  apoelstra:
    ACK 4c43d5e20f

Tree-SHA512: 073c2e0e23ce41a2b35f1b1193b07a755b726bf565d61e6bcb23b6bdaab31ba3591f31aa92230b07f7dfc018de0401eba09a6858dc261e66dacb331355f40d76
2022-02-09 18:31:33 +00:00
Andrew Poelstra b8615e2132
Merge rust-bitcoin/rust-secp256k1#397: Fix example dependency list
3c9dd2fb32 Fix example dependency list (Tobin Harding)

Pull request description:

  Example relies on `rand-std` not plain `rand` dependency.

  I do not understand why the following command passes without this patch
  applied

  ```
  cargo test --no-default-features --features=std,rand,bitcoin_hashes
  ```

  But if we put the same code in a standalone binary it fails as expected?

  Since the running of this test is _unusual_ and it is primarily meant as
  an entry point example to the library, remove the mention of "alloc"
  feature and just depend upon "std".

  Fixes: #395

ACKs for top commit:
  apoelstra:
    ACK 3c9dd2fb32

Tree-SHA512: 8e7ec7ac846e2916c29b74c7485650e5242ae1141c12c69b50d74efdfee71c11a52cd454231d2a7cdd6f8f683d3ba4369f9bf898a6b9351dc92c2a4e2bd626cd
2022-02-09 18:26:58 +00:00
Martin Habovstiak 705c9cfbc1 Clarified conversions between `Parity` and integers 2022-02-09 15:33:25 +01:00
Tobin Harding a209836a99
Implement LowerHex and Display for Message
Implement `fmt::LowerHex` for `Message`. Implement `Display` by calling
`LowerHex`.

Resolves: #251
2022-02-09 09:22:07 +00:00
Tobin Harding eb453b8227
Add global context API
Our API often involves a `Secp256k1` parameter, when users enable the
`global-context` feature they must then pass `SECP256K1` into these
functions. This is kind of clunky since the global is by definition
available everywhere.

Make the API more ergonomic for `global-context` builds by adding
various API functions/methods that use the global context implicitly.
2022-02-09 09:09:06 +00:00
Tobin Harding 3ecb5e41b3
Refactor from_secret_key definition
The `from_secret_key` method definition currently uses non-standard
indentation.

Improve uniformity by using 'standard' indentation.
2022-02-09 09:04:13 +00:00
Tobin Harding e2d47a29e2
Remove unnecessary import statement
The global context is already in scope in tests since we use a glob
import. No clue why Clippy does not warn for this.

Remove unnecessary import statement in test function.
2022-02-09 08:53:31 +00:00
Tobin Harding d79989bc95
Remove erroneous duplicate feature
When we removed the "global-context-less-secure" a duplicate feature
snuck in, remove it.
2022-02-09 08:53:31 +00:00
Tobin Harding c9e6ca1680
Use rust-bitcoin module doc style
Recently we introduced uniform styling for module docs over in
`rust-bitcoin` repo. We can do the same here but its a bit controversial
because it removes the heading from module docs and every single public
module in rust-secp256k1 uses a heading. Instead we use a full
sentences. Also makes uniform the trailing `//!`.
2022-02-09 07:31:55 +00:00
Tobin Harding 3fa6762437
Add link to referenced commit
To save devs looking up the commit themselves add a link to it in the
rustdoc.
2022-02-09 07:31:55 +00:00
Tobin Harding f5e68f3ba7
Add ticks around code snippet
For added clarity add ticks around words that are code.
2022-02-09 07:31:54 +00:00
Tobin Harding d25431c1da
Use 3rd person tense for function docs
As is typical in the Rust ecosystem use the third person tense when
documenting functions. E.g.,

```
/// Creates a new Foo.
```
As opposed to

```
/// Create a new Foo.
```
2022-02-09 07:31:54 +00:00
Tobin Harding c3be285c1d
Fix size constant docs
Make all the various size constant docs uniform by using form 'The size
...' and also by ending with a period.
2022-02-09 07:31:54 +00:00
Tobin Harding 5e07e7596b
Add period to sentences
Add the terminating period to all docs sentences. (Also one instance of
capitialize initial character in sentence.)
2022-02-09 07:31:53 +00:00
Tobin Harding 269bde042f
Remove unnecessary capitalisation
'context' does not need need a capital letter in the middle of a
sentence.
2022-02-09 07:29:45 +00:00
Tobin Harding 3c9dd2fb32
Fix example dependency list
Example relies on `rand-std` not plain `rand` dependency.

I do not understand why the following command passes without this patch
applied

```
cargo test --no-default-features --features=std,rand,bitcoin_hashes
```

But if we put the same code in a standalone binary it fails as expected?

Since the running of this test is _unusual_ and it is primarily meant as
an entry point example to the library, remove the mention of "alloc"
feature and just depend upon "std".
2022-02-09 06:35:54 +00:00
Tobin Harding 4c43d5e20f
Add custom Debug impl for RecoverableSignature
Currently when debug printing the `RecoverableSignature` we do so byte
by byte, this means that the output differs depending on the endianess
of the machine. If instead we serialize the signature in compact form
then the output is the same irrespective of the endianess.

With this applied the following two commands now pass:

```
cargo test test_debug_output --features=recovery

```
cross test --target powerpc-unknown-linux-gnu test_debug_output --features=recovery
```

Fixes: #375
2022-02-08 08:14:30 +00:00
Andrew Poelstra ecb62612b5
Merge rust-bitcoin/rust-secp256k1#391: Add example to SharedSecret
b3503ba148 Add example to SharedSecret (Tobin Harding)

Pull request description:

  Currently the rustdoc on `SharedSecret` is wildly incorrect (possibly a cut'n'pasta error).

  Fix the rustdoc for `SharedSecret` and add an examples section to assist testing the public API.

  Fixes: #249

ACKs for top commit:
  apoelstra:
    ACK b3503ba148

Tree-SHA512: 650092388099bb415c11ea335ca6b64c90094f1a51ceecc403911316ee62da0279488af6fa66e00ee5269c129f06d4641085f8ab9be91c98d24a7a4449d235c2
2022-02-04 14:14:48 +00:00
Tobin Harding b3503ba148 Add example to SharedSecret
Currently the rustdoc on `SharedSecret` is wildly incorrect (possibly a
cut'n'pasta error).

Fix the rustdoc for `SharedSecret` and add an examples section to assist
testing the public API.

Fixes: 249
2022-02-04 11:55:06 +11:00
Tobin Harding 8339ca5706 Add documentation guiding users towards randomization
Now that we opportunistically randomize the context on creation if
`rand-std` is enabled it would be nice to encourage users who do not
wish to use `rand-std` to randomize the context. We already have an API
to do this but it requires a separate call to do so. Instead of adding a
bunch of additional constructors elect to add documentation to the
current constructors guiding users towards randomization.
2022-02-04 08:36:15 +11:00
Tobin Harding cf1496b64e Add documentation about rand-std feature
We recently implemented opportunistic randomization of the context
object if the the `rand-std` feature is enabled. Both for the global
context and also for signing context constructors.

Add documentation about `rand-std` feature in relation to the context
object.
2022-02-04 08:36:15 +11:00
Tobin Harding 1693d51ce7 Randomize context on creation
Randomize context on creation if `rand-std` feature is enabled.
2022-02-04 08:36:03 +11:00
Tobin Harding a0465ea279 Remove feature global-context-less-secure
Instead of providing a mechanism for users to opt out of randomization
we can just feature gate the call site i.e., opportunistically randomize
the global context on creation if `rand-std` feature is enabled.
2022-02-04 08:34:39 +11:00
Andrew Poelstra 7a3736a0f9
Merge rust-bitcoin/rust-secp256k1#389: On key-generation example (documentation), "rand" should be "rand-std" instead
2732891359 Change rand to rand-std in lib.rs documentation (Vincent Liao)

Pull request description:

  I copy-pasted the key-generation example written on the documentation, but it didn't work. It only worked when I used the feature `rand-std` instead of `rand`.

  To reproduce, boot up a new Rust project, and add this to main.rs:

  ```
  use secp256k1::rand::rngs::OsRng;
  use secp256k1::{Secp256k1, Message};
  use secp256k1::hashes::sha256;

  let secp = Secp256k1::new();
  let mut rng = OsRng::new().expect("OsRng");
  let (secret_key, public_key) = secp.generate_keypair(&mut rng);
  let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());

  let sig = secp.sign_ecdsa(&message, &secret_key);
  assert!(secp.verify_ecdsa(&message, &sig, &public_key).is_ok());
  ```

  Using this dependencies causes error: `secp256k1 = {version="0.21.2", features=["rand", "bitcoin_hashes"]}`. After replacing `rand` with `rand-std`, it works.

ACKs for top commit:
  apoelstra:
    ACK 2732891
  tcharding:
    tACK 2732891359

Tree-SHA512: 6b5436bc71bab7535e432e119679bc6bcb11d2575b609e039cc25c122ae92b528f95a673e9c643a6cfa2ee3a663f7efdd61731b6084261c52a220448b6f72d12
2022-02-03 15:10:37 +00:00
Tobin Harding c30026d08b Fix typo 'epected' -> 'expected'
Fix minor spelling mistake in code comments.
2022-02-03 08:36:36 +11:00
Tobin Harding f3688ecf56 Use rand-std in key rustdoc examples
Seems there is a bug in cargo, the tests in `key.rs` run successfully
but AFAICT they should fail. Here is an example, running `cargo test
--features=rand` should make this test fail but it doesn't?
```
/// Secret 256-bit key used as `x` in an ECDSA signature.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # #[cfg(all(feature = "rand", any(feature =  "alloc", feature = "std")))] {
/// use secp256k1::{rand, Secp256k1, SecretKey};
///
/// let secp = Secp256k1::new();
/// let secret_key = SecretKey::new(&mut rand::thread_rng());
/// # }
/// ```

Anywho, use the correct feature gate: `rand-std`.
2022-02-01 15:21:30 +11:00
Tobin Harding ae3e06f95b Fix lint warnings in test code
Various combinations of features trigger lint warnings for unused code,
all warnings are caused by incorrect feature gating.

Correct feature gating to remove Clippy warnings during testing.
2022-02-01 15:21:30 +11:00
Tobin Harding c01cd8f1f3 Enable running tests without default features
Currently various features fail to build when enabled without default
features. This is because many tests need feature gating.

Feature gating the import statements quickly turns into spaghetti when
trying to cover all combinations of two features correctly, instead just
allow unused imports on `tests` modules where needed.

Add correct feature requirements to the examples so they also can be run
without default features.

Improve the CI script by doing:

- Add `std` to the feature matrix.
- Add `--no-default-features` to test runs in the CI script.
2022-02-01 15:20:44 +11:00
Tobin Harding 433c350424 Add multiple implementations of Debug for secrets
The `Debug` implementation for secrets is feature gated on `std` because
it uses a hasher from `std`. If `bitcoin_hashes` is enabled we can use
it for hashing. If neither `std` nor `bitcoin_hashes` is enabled fall
back to outputting:

<secret requires std or bitcoin_hashes feature to display>

Remove the docs conditional since we now implement `Debug` always.
2022-02-01 14:51:40 +11:00
Vincent Liao 2732891359 Change rand to rand-std in lib.rs documentation 2022-01-27 23:45:56 +07:00
Andrew Poelstra f7d637e6aa
Merge rust-bitcoin/rust-secp256k1#376: Add examples to `key` module
aa828f01a5 Improve documentation in the key module (Tobin Harding)
9e46d6f122 Add examples to types and methods in key module (Tobin Harding)
a7f3d9bcfd Improve key module docs (Tobin Harding)
6d23614467 Improve lib.rs rustdocs (Tobin Harding)
4c4268f1ad Improve docs on method generate_keypair (Tobin Harding)

Pull request description:

  This PR is an initial attempt to more thoroughly test our public API.

  Add examples to various types/methods/functions in the key module.

  I'm not entirely sure when is enough, do we want an example on every single public method, function, and type or is this overkill. In this PR I tried to find a balance by doing ever method/function that took an argument that is a custom type from this lib. I think this should be extended to include return values too though ...

  Thanks to @thomaseizinger for the idea!

  First 2 patches are docs improvements to `lib.rs`.

ACKs for top commit:
  apoelstra:
    ACK aa828f01a5

Tree-SHA512: 9383ad263469f98ce7e988d47edc1482a09a0ce82f43d3991bd80aabdf621430f4a3c86be4debf33232dcb1d60d3e81f2c6d930ea7de7aa0e34b037accd7bc98
2022-01-25 12:34:45 +00:00
Andrew Poelstra 6b515755ab
Merge rust-bitcoin/rust-secp256k1#369: Trivial doc: Warn that serde differs from consensus encoding
52560a9205 Warn that serde differs from consensus encoding (Martin Habovstiak)

Pull request description:

  Addresses https://github.com/rust-bitcoin/rust-bitcoin/pull/756#discussion_r780844146

ACKs for top commit:
  apoelstra:
    ACK 52560a9205

Tree-SHA512: ee56c13dcb0493318f5b59306d6375adb202a5812c4673f0b9abe71fb809b927a74c3fd61aa20fe28fd2176c67f495214bf4da546380dfe9c83b933b02835fff
2022-01-24 15:58:21 +00:00
Tobin Harding aa828f01a5 Improve documentation in the key module
We recently patched much of the docs in the `key` module, lets attempt
to attain perfection.

Improve docs by doing:

- Use full stops
- Use 100 character column width
- Use plural third person tense
- Use plural for section headings
- Fix any grammar mistakes
- Use code ticks and links as appropriate
2022-01-24 14:48:46 +11:00
Tobin Harding 9e46d6f122 Add examples to types and methods in key module
Done in an effort to better test our public API.

Add tests in the `Examples` section as is idiomatic in the Rust
ecosystem.

Make other minor improvements to any rusdocs we touch:
- Use full stops
- Use 100 character column width
- Use plural third person tense
- Use plural for section headings
2022-01-24 14:48:46 +11:00
Tobin Harding a7f3d9bcfd Improve key module docs
Use 'standard' stlye, standard is defined as
- No markdown heading
- Full sentence (capital first letter and full stop)
- Trailing empty comment line
2022-01-24 14:43:44 +11:00
Tobin Harding 6d23614467 Improve lib.rs rustdocs
Improve the main docs by doing:

- Remove unneeded `self` from use statement
- Add code ticks to `bitcoin_hashes`
2022-01-24 14:43:44 +11:00
Tobin Harding 4c4268f1ad Improve docs on method generate_keypair
Improve method docs by doing:

- Remove 'batch' comment
- Remove mention of required features, docs already show this
- Use links to types as well as ticks
2022-01-24 14:43:44 +11:00
Tobin Harding 6fad20ef0c Fix the mess around Parity
Recently we made a wee mess with the `Parity` opaque type. Let's fix it
up by doing:

- Use an enum with variants `Even` and `Odd`.
- Add explicit conversion methods to/from u8 and i32
- Implement `BitXor`

Note: This patch is an API breaking change that does _not_ follow the
deprecation guidelines. Rust does not allow deprecating `From` impl
blocks AFAICT.
2022-01-23 10:33:51 +11:00
Andrew Poelstra a9f0ec2258
Merge rust-bitcoin/rust-secp256k1#381: Fix typo in documentation
47411ce73d Fix typo in documentation (Tobin Harding)

Pull request description:

  Docs reference a function name but there is a typo.

  'grund' -> 'grind'.

ACKs for top commit:
  apoelstra:
    ACK 47411ce73d

Tree-SHA512: e6724f1c7972625d59be0ae9de358295c9280e2e126e7322d706cbdca342c1189552b2fdeef9370ff4f85ea8ef185ef4447f6693979d5e8548fcfa2df41a491e
2022-01-19 18:48:04 +00:00
Tobin Harding 47411ce73d Fix typo in documentation
Docs reference a function name but there is a typo.

'grund' -> 'grind'.
2022-01-19 15:22:37 +11:00
elsirion 1877e4db33
Add serde impl for KeyPair
The impl is added as a module instead of being a direct implementation
since it uses the global context and users should be aware that.
2022-01-18 20:10:11 +01:00
Tobin Harding c7a8bbb772 Use the new recover_ecdsa in bench function
We recently deprecated `recover` in favour of `recover_ecdsa` but missed
one call site in benches.
2022-01-14 13:19:36 +11:00
Andrew Poelstra 7b8392057c
Merge rust-bitcoin/rust-secp256k1#374: Do trivial Clippy fixes
72f5e0441e Do not use deprecated functions (Tobin Harding)
3840ce914b Add Debug/Copy/Clone derives (Tobin Harding)
63ae716e6f Add clippy.toml (Tobin Harding)

Pull request description:

  Add `clippy.toml` and fix a few trivial Clippy warnings.

ACKs for top commit:
  elichai:
    ACK 72f5e04
  apoelstra:
    ACK 72f5e0441e

Tree-SHA512: 5310832d9a4d864a118fef41e275304b912d1c8997060e1eaba95e12700a0b551e846309f9765e10a9a886a03f65b90fcda31d6bf92188791b3c73f97a14e18d
2022-01-12 18:29:23 +00:00
Tobin Harding 72f5e0441e Do not use deprecated functions
Recently we deprecated a bunch of methods/functions. We are still
calling them in test code. Found by Clippy.

Use the shiny new methods/functions instead of the deprecated ones.
2022-01-12 18:56:08 +11:00
Tobin Harding 3840ce914b Add Debug/Copy/Clone derives
Clear Clippy warnings by adding derives to `GlobalContext`.
2022-01-12 18:55:06 +11:00
Tobin Harding 97524b2da7 Deprecate generate_schnorrsig_keypair
We have deprecated all other functions that use the identifier
'schnorrsig' but we missed `generate_schnorrsig_keypair`.

This function is purely a helper function and serves no real purpose
other than to reduce two lines of code to a single line. Downstream
users can write this function themselves if they need it.

Also, we recently added a new public method to `KeyPair` to get the
public key in a slightly more ergonomic fashion. Use `kp.public_key()`
when replacing usage of now deprecated `generate_schnorrsig_keypair`
function.
2022-01-12 18:23:40 +11:00
Tobin Harding 389abddcc7 Add method KeyPair::public_key
Currently to get the `XOnlyPublicKey` from a `KeyPair` users must do
`XOnlyPublicKey::from_keypair(&kp)`. While this does the job we can make
the lib more ergonomic by providing a method directly on `KeyPair` that
calls through to `XOnlyPublicKey::from_keypair`.

Add method `KeyPair::public_key(&self)`.
2022-01-12 18:14:19 +11:00
Martin Habovstiak 52560a9205 Warn that serde differs from consensus encoding
Addresses https://github.com/rust-bitcoin/rust-bitcoin/pull/756#discussion_r780844146
2022-01-10 00:20:06 +01:00
Andrew Poelstra d068fd7d41
Merge rust-bitcoin/rust-secp256k1#365: Fixed docs(rs)
314e8755df Clarify `global-context` feature (Martin Habovstiak)
d52ab85dd5 Added missing features to docs.rs config (Martin Habovstiak)

Pull request description:

  Sadly, I missed two details in #353: features missing in docs.rs configuration and `global-context` being a bit confusing.
  This PR fixes those, see commit messages for details.

ACKs for top commit:
  apoelstra:
    ACK 314e8755df

Tree-SHA512: 01bed8ae2f30adcbdd436b514f08a084492d7f4e1a739ca62e6d8b8547e379c01faeda3522733c27ab615acbb4c6cff60e13906cc88a0d2b90e439e7da517466
2022-01-07 17:31:42 +00:00
Andrew Poelstra fe76cada92
Merge rust-bitcoin/rust-secp256k1#366: Remove capital letter in middle of docs sentence
656f19407b Remove capital letter in middle of docs sentence (Tobin Harding)

Pull request description:

  (Candidate for most trivial patch of all time.)

  Seems to be a typo, change the 'L' to an 'l'.

ACKs for top commit:
  real-or-random:
    ACK 656f19407b

Tree-SHA512: 06a4712868c3195a8465b9cf7bd39e55a30e37574086ca27cb032e0109a8fe053411426a15bcb354642bf78e6420b6fa2789ca487c6cc499f741a11220d5dc22
2022-01-07 17:28:19 +00:00
Tobin Harding 656f19407b Remove capital letter in middle of docs sentence
Seems to be a typo, change the 'L' to an 'l'.
2022-01-07 15:05:00 +11:00
Martin Habovstiak 314e8755df Clarify `global-context` feature
Previously only `global-context-less-secure` was shown in the doc even
though `global-context` may also work. This was strictly correct because
`global-context` implies `global-context-less-secure` which is also
documented but people could miss it or forget about it and then worry
about security or worse, enable less secure feature.

Calling out both fetures seems useful, even important and thankfully
doesn't seem to cause too much noise in the docs.
2022-01-07 01:17:52 +01:00
Tobin Harding 69f44d9301 Manually implement Debug for SerializedSignature
Currently we have an implementation of `Debug` (also used by `Display`)
for `Signature` that first converts the sig to a `SerializedSignature`
then prints it as hex.

We would like to have an implementation of `Debug` for
`SerializedSignature`, this cannot be derived because of the `data: [u8;
field]`. We can manually implement `Debug` for `SerializedSignature`
exactly as it is currently done for `Signature` and call this new
implementation from `Signature::fmt()`.

This code path is already tested in `lib.rs` in the test function
`signature_display`.
2022-01-07 10:42:34 +11:00
Tobin Harding 26921a31b8 Add lints to catch missing traits
Rustc can warn us when we forget to add `Copy` and `Deubg` trait
implementations to types.

Add lint directives to enable warnings for missing `Copy` and `Debug`
implementations. Use the newly emitted warnings to find types that do
not implement our 'standard' traits. These 'standard' traits are defined
as the set of attributes that it has been found beneficial to
opportunistically add to all types, these are

- Copy
- Clone
- Debug
- PartialEq and Eq
- PartialOrd and Ord
- Hash
2022-01-07 10:28:10 +11:00
Tobin Harding 35556e22f2 Remove useless call to format
Clippy emits:

 warning: useless use of `format!`

As suggested, remove the useless call to `format!`.
2022-01-07 10:18:49 +11:00
Tobin Harding 0ad414a982 Remove unneeded return statements
Clippy emits a few warnings:

 warning: unneeded `return` statement

As suggested, remove the unneeded return statements.
2022-01-07 10:18:49 +11:00
sanket1729 837be22e09 Basic derives for Parity 2022-01-06 23:57:26 +05:30
sanket1729 7059192de9 Wildcard export from key module 2022-01-06 23:08:01 +05:30
Andrew Poelstra 74e8fc7699
Merge rust-bitcoin/rust-secp256k1#360: Re-export Parity struct
e595b39510 Re-export Parity struct (sanket1729)

Pull request description:

  pub struct Parity is under a private module key and not re-exported in lib.rs . It is therefore not
  possible to use it downstream.

ACKs for top commit:
  elichai:
    ACK e595b39510
  apoelstra:
    ACK e595b39510

Tree-SHA512: 2573689f9a08505c8dfe8f79cd921d5a2742a2a2f4f92cf4066fe6557c765c756531d13560fa4fe6461f094b0c11a52aca30b44542eb77eda7dd1ebd24d3b155
2022-01-06 15:46:18 +00:00
Andrew Poelstra 88f6baee73
Merge rust-bitcoin/rust-secp256k1#353: Documented features
18f74d5242 Clarify what does "less security" mean (Martin Habovstiak)
94c55b4d09 Fixed typos/grammar mistakes (Martin Habovštiak)
1bf05523f0 Documented features (Martin Habovstiak)

Pull request description:

  This documents the Cargo features making sure docs.rs shows warning for
  feature-gated items. They are also explicitly spelled out in the crate
  documentation.

  The PR is similar in spirit to https://github.com/rust-bitcoin/rust-bitcoin/pull/633

ACKs for top commit:
  apoelstra:
    ACK 18f74d5242

Tree-SHA512: 8aac3fc5fd8ee887d6b13606d66b3d11ce44662afb92228c4f8da6169e3f70ac6a005b328f427a91d307f8d36d091dcf24bfe4d17dfc034d02b578258719a90a
2022-01-06 15:42:32 +00:00
sanket1729 e595b39510 Re-export Parity struct
pub struct Parity is under a private module key. It is therefore not
possible to use it downstream.
2022-01-06 09:08:59 +05:30
Martin Habovstiak 18f74d5242 Clarify what does "less security" mean 2022-01-04 23:20:45 +01:00
Martin Habovštiak 94c55b4d09
Fixed typos/grammar mistakes
Co-authored-by: Tobin C. Harding <me@tobin.cc>
2022-01-04 23:16:28 +01:00
Martin Habovstiak 1bf05523f0 Documented features
This documents the Cargo features making sure docs.rs shows warning for
feature-gated items. They are also explicitly spelled out in the crate
documentation.
2022-01-04 16:55:01 +01:00
Tobin Harding ede114fb1a Improve docs on tweak_add_check method
It is not immediately apparent what 'err == 1' means, one must determine
that the FFI function call returns 1 for success. We can help readers of
the code by adding a 'Return' section to the method documentation.

Add trailing full stop to method docs initial line also.
2022-01-04 09:19:11 +11:00
Tobin Harding fbc64c7725 Add opaque parity type
Two functions in the FFI secp code return and accept a parity int.
Currently we are manually converting this to a bool. Doing so forces
readers of the code to think what the bool means even though
understanding this bool is not needed since in is just passed back down
to the FFI code. We can abstract this away by using an opaque type to
hold the original int and not converting it to a boolean value.

Add 'Return' and 'Error' sections to `tweak_add_assign` while fixing the
docs to describe the new opaque parity type.
2022-01-04 09:19:05 +11:00
Tobin Harding 1b768b2749 Make tweak_add_assign return statements uniform
We have two `tweak_add_assign` methods (one for keypair and one for
x-only pubkey). Both check the return value from a FFI function call.
We can make both sites uniform to _slightly_ reduce cognitive load when
reading the code.

Use C style code to make it obvious to readers that this is basically C
code.
2022-01-04 09:01:34 +11:00
Tobin Harding edafb88f8c Move key unit tests to key module
There are currently two unit tests in the `schnorr` module that are
testing keys from the `key` module. This is possible because the tests
are only testing the public interface, none the less they are better
placed in the `key` module.
2022-01-04 08:23:05 +11:00
Tobin Harding e3d21a3d87 Clean up test imports with key module
The import statements can be simplified by using an import
wildcard (`super::*`). While we are at it put them in std, external
crate, this crate order.
2022-01-04 08:18:44 +11:00
sanket1729 eab549c46c Re-export types to reduce breaking changes downstream 2022-01-03 08:55:54 +05:30
Andrew Poelstra 50034ccb18
Merge rust-bitcoin/rust-secp256k1#342: Change context objects for verification methods
21aa914ad2 Change context objects for schnorr sig methods (sanket1729)

Pull request description:

  - The current schnorrsig verify methods should operate on verify context
  as is done throughout the bitcoin core
  - Finally, and importantly the XonlyPublicKey::from_keypair now operates
  without any context parameter.

ACKs for top commit:
  apoelstra:
    ACK 21aa914ad2

Tree-SHA512: 035338f19839805a080eb262ae7b93ab187dabb63086c8b7f6015f3a6006986604dc2c6f329a99a20ddfa78c1ee518f44cd5eee2f73810fbdc83ff8df7d12506
2021-12-24 14:34:07 +00:00
sanket1729 21aa914ad2 Change context objects for schnorr sig methods
- The current schnorrsig verify methods should operate on verify context
as is done throughout the bitcoin core
- Scondly, and importantly the XonlyPublicKey::from_keypair now operates
without any context objects.
2021-12-24 00:54:06 +05:30
Dr Maxim Orlovsky de77518d3a
Serde serialization for KeyPair 2021-11-12 21:18:53 +01:00
Thomas Eizinger d244b4d747
Fix typo in docs 2021-11-11 13:43:51 +11:00
Thomas Eizinger c5c95513f2
Move helper function below usage
This is not C89. We can declare the more important things first.
2021-11-11 13:43:50 +11:00
Thomas Eizinger ce4427747d
Move ECDSA functionality into ECDSA module 2021-11-11 13:43:50 +11:00
Thomas Eizinger e0c3bb28c4
Rename schnorr functions on `Secp256k1` to match naming of ecdsa
The naming scheme we employ is `{sign,verify, ...}_{ecdsa,schnorr}`.
2021-11-11 13:43:50 +11:00
Thomas Eizinger 760559c70e
Rename `schnorrsig` module to `schnorr`
Schnorr is commenly known as a signature algorithm, we don't need
to restate that in the name of the module.
2021-11-11 13:43:50 +11:00
Thomas Eizinger d4fb819d80
Move `XOnlyPublicKey` to `key` module 2021-11-11 13:43:49 +11:00
Thomas Eizinger 87d936a765
Rename `schnorr::PublicKey` to `schnorr::XOnlyPublicKey`
The public key is unrelated to the signature algorithm. It will
be moved out of the module in another commit. For ease of review,
the renamed is kept separate.
2021-11-11 13:43:49 +11:00
Thomas Eizinger 2e0e731664
Move `KeyPair` to `key` module
The `KeyPair` type is semantically unrelated to the schnorr signature
algorithm.
2021-11-11 13:43:48 +11:00
Thomas Eizinger c47ead9967
Move `Signature` and `SerializedSignature` to new `ecdsa` module
With the introduction of Schnorr signatures, exporting a `Signature`
type without any further qualification is ambiguous. To minimize the
ambiguity, the `ecdsa` module is public which should encourage users
to refer to its types as `ecdsa::Signature` and `ecdsa::SerializedSignature`.

To reduce ambiguity in the APIs on `Secp256k1`, we deprecate several
fucntions and introduce new variants that explicitly mention the use of
the ECDSA signature algorithm.

Due to the move of `Signature` and `SerializedSignature` to a new module,
this patch is a breaking change. The impact is minimal though and fixing the
compile errors encourages a qualified naming of the type.
2021-11-11 13:43:48 +11:00
Thomas Eizinger 49c7e21486
Prefer `use super::*` import over manually picking items
Tests are usually placed next to the code they are testing. As such,
importing `super::*` is a good starting point.
2021-11-11 13:38:54 +11:00
Thomas Eizinger 52d0554423
Fully qualify Error to simplify imports 2021-11-11 13:38:52 +11:00
Thomas Eizinger 8e96abae39
Make `key` module private
We re-export all structs residing in that module. There is no reason
to expose the internal module structure of the library publicly.
2021-11-11 13:38:49 +11:00
Andrew Poelstra 476ced62fe
Merge rust-bitcoin/rust-secp256k1#307: Fixing documentation for BIP 340-related functions
931b560dc7 Fixing documentation for BIP 340-related functions (Dr Maxim Orlovsky)

Pull request description:

ACKs for top commit:
  thomaseizinger:
    ACK 931b560dc7
  apoelstra:
    ACK 931b560dc7

Tree-SHA512: d78fbda138e636c04c6f356e2a41fcdd0270eea710a9af6c965ff90acfd6740bd650a8909638c558badd52d307cbdcd914fa25846236b064ca8c38faccec8be8
2021-11-02 18:39:09 +00:00
Andrew Poelstra 6a774bd47c
Merge rust-bitcoin/rust-secp256k1#334: Use explicit u8 when assigning a byte slice
24d6f62603 Use explicit u8 when assigning a byte slice (junderw)

Pull request description:

  Is there a way to tell the compiler to not allow `[0; 64]` and require that either the type is explicitly given to the variable, or that each member uses explicit `0u8` notation?

  I noticed the usage was a mix of explicit and implicit, so I changed all to explicit.

ACKs for top commit:
  apoelstra:
    ACK 24d6f62603

Tree-SHA512: f7796dcc3ae240983257bef0f25bd0df741943f75d86e9bca7c45076af179d96ce213bd9c339a01f721f7dc9b96a0a4a56ef2cf44339f4c91d208103b7659d9f
2021-11-02 18:21:14 +00:00
junderw 24d6f62603
Use explicit u8 when assigning a byte slice 2021-10-17 10:07:51 +09:00
Dr Maxim Orlovsky 931b560dc7
Fixing documentation for BIP 340-related functions 2021-09-27 15:20:33 +02:00
Dr Maxim Orlovsky 6810c2b547
Dedicated display_secret fn for secret-containing types
Debug-print secrets as tagged hashes

Refactoring Display/Debug for secret values with display_secret
2021-09-27 14:03:58 +02:00
Elichai Turkel 635a6ae441
Add to_hex converter and add tests for hex conversion 2021-09-27 12:50:08 +02:00
Andrew Poelstra 24a9c9c765
Merge pull request #304 from p2pderivatives/combine-keys-test-and-doc
Add error type for combine keys + test and doc
2021-09-24 22:37:19 +00:00
Andrew Poelstra cd62343407
Merge rust-bitcoin/rust-secp256k1#326: Bump bitcoin_hashes to version 0.10
bc42529a16 Rename `secp256k1::bitcoin_hashes` module to `secp256k1::hashes` (Thomas Eizinger)
ae1f8f4609 Bump bitcoin_hashes to version 0.10 (Thomas Eizinger)

Pull request description:

  Requires for interoperability of the `ThirtyTwoByteHash` trait with
  rust-bitcoin.

ACKs for top commit:
  apoelstra:
    ACK bc42529a16

Tree-SHA512: 85fcb284ff82b543a0c3ea2b568351b3af938a26ac42c6a975480ae97def84e4f0795105bd4572f930a7bf82654eba416cf0c5e25f62809e2ea331443ffb5807
2021-09-09 13:19:49 +00:00
Andrew Poelstra c72e7cc743
Merge pull request #308 from LNP-BP/extrakeys/keypair-ser
Adding KeyPair::serialize_sec. Closes #298
2021-09-08 21:37:32 +00:00
Thomas Eizinger bc42529a16
Rename `secp256k1::bitcoin_hashes` module to `secp256k1::hashes` 2021-09-08 15:46:38 +10:00
Tibo-lg 674cc79d87 Add error type for combine keys + test and doc 2021-09-02 21:19:21 +09:00
Elichai Turkel ea4eae8a20
Merge pull request #315 from LNP-BP/fix/feat-warning
Fixing warning in context mod under feature-specific compilation
2021-06-30 15:32:35 +03:00
sanket1729 5bac4e4bac Change error enum for KeyPair::from_seckey_slice
Also does another nit in the file which removes a trailing whitespace
2021-06-27 17:20:23 -07:00
Dr Maxim Orlovsky 593a550e3f
Fixing warning in context mod under feature-specific compilation 2021-06-27 20:09:41 +02:00
Dr Maxim Orlovsky 8ee4e05e63
Removing uneeded Secp context arguments 2021-06-20 17:15:10 +02:00
Dr Maxim Orlovsky 7e2f7fef72
Adding KeyPair::serialize_sec. Closes #298 2021-06-19 14:02:48 +02:00
Dr Maxim Orlovsky aa6bdaff3c
Creating SecretKey and PublicKey from BIP-340 KeyPair 2021-06-16 10:56:59 +02:00
Andrew Poelstra bb25ed4715
Merge pull request #282 from TheBlueMatt/2021-02-redo-fuzz
Reduce cryptography usage in --cfg=fuzzing
2021-06-09 15:01:52 +00:00
Matt Corallo 940a51c2c6 Reduce cryptography usage in --cfg=fuzzing
This reduces the usage of real cryptography in --cfg=fuzzing,
specifically replacing the secret->public key derivation with a
simple copy and ECDH with XOR of the public and private parts
(plus a stream of 1s to make a test pass that expected non-0
output).

It leaves secret tweak addition/multiplication as-is.

It also changes the context creation to over-allocate and store
the context flags at the end of the context buffer, allowing us
to easily test context flags in each function.

While it would be nice to have something fancier (eg XOR-based),
its not immediately obvious how to accomplish this, and better to
fix the issues I have than spend too much time on it.

Fixes #271.

This partially reverts b811ec133a
2021-06-08 19:17:08 +00:00
Matt Corallo f2e4b297e1 Do not test secret->public derivation or pk validity in fuzzing cfg
In the next commit the secret->public key derivation in fuzzing cfg
is changed to be simpler, as well as the validity rules of public
keys relaxed.

This adds a new test to ensure random keys can be added, not just
the hard-coded keys test that exists today.
2021-06-08 19:17:07 +00:00
Devrandom b5ff47a1a8 New alloc feature
Allows use of `Secp256k1::new` and related API if an allocator is available
2021-06-08 20:41:49 +02:00
Andrew Poelstra e89fd5d269
Merge pull request #291 from p2pderivatives/add-combine-keys
Add combine_keys function to PublicKey
2021-06-08 18:09:35 +00:00
Andrew Poelstra a5dfd09e94
Merge pull request #294 from LNP-BP/feat/from_secret_key
Adding schnorrsig::KeyPair::from_secret_key convenience function
2021-06-08 18:05:20 +00:00
Matt Corallo ce930ab6b7 Add a global-context-less-secure feature which skips randomization
This is useful for us downstream as we wish to target WASM with a
global context, and using rand in such a build doesn't seem like a
safe idea.
2021-06-08 01:48:50 +00:00
sanket1729 6265b2558a Fix SecretKey FromStr bug
Secret::from_str did not check if the secret key
was a valid one or not.
2021-04-26 14:17:39 -07:00
Elichai Turkel 3c2bee31f2
Merge pull request #270 from thomaseizinger/serde-owned-types
Rebased version of `[Alternative] Allow deserializing from owned types` + support for new schnorr module
2021-04-21 19:06:29 +03:00
Dr Maxim Orlovsky bf9a276342
Adding schnorrsig::KeyPair::from_secret_key convenience function 2021-04-12 15:25:50 +02:00
Tibo-lg 7d32182537 Add combine_keys function to PublicKey 2021-04-08 06:28:03 +09:00
Tim Ruffing 1eb2c32df7 Manually test that panicking from C will abort the process
Panicking from C is not UB in newer rust versions and will reliably
trigger an abort (without unwinding). In older rust versions, it is
technically UB but empirically it seems to "just work" (and what should
it realistically do except crashing, which is what we intent).

Since there's potentially no unwinding, we can't test this behavior
using [should_panic]. This PR will instead check the libtest output
explicitly in our CI tests.

Fixes #228.
2021-03-24 21:54:02 +01:00
Dr Maxim Orlovsky 519857cd83
More standard derives for Error 2021-01-30 12:35:41 +01:00
Thomas Eizinger c2fd5cefa4
Remove `visit_bytes` implementation from `FromStrVisitor`
This visitor is meant to deserialize strings using `FromStr` not
bytes.
2021-01-13 09:57:09 +11:00
Thomas Eizinger e6e23e9dd6
Rename `HexVisitor` to `FromStrVisitor`
The visitor works with all types that implement `FromStr`. Whether or
not that ends up being hex encoding depends on the implementation
of `FromStr`.
2021-01-13 09:54:33 +11:00
Thomas Eizinger 18890d3b86
Extend schnorr-sig serde test suite with owned and slice variants 2021-01-12 11:52:24 +11:00
Thomas Eizinger b4040f0a82
Update test names to reflect what they are doing
These tests are testing more than just the signature serialization.
2021-01-12 11:52:06 +11:00
Elichai Turkel 1f08a313e5
Replace serde macros with generic visitor module
Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
Co-authored-by: Sebastian Geisler <sebastian@blockstream.io>
2021-01-12 11:51:29 +11:00
Andrew Poelstra 65ff814a39 rename `rust_secp_fuzz` to `fuzzing` 2021-01-11 19:14:42 +00:00
Matt Corallo 1f043216a0 Add ability to randomize context without the `rand` feature.
There is little reason to pull in the `rand` dep just for the `Rng`
trait for users who want to randomize contexts. We should expose a
randomize function that just takes 32 bytes.
2020-12-29 16:45:07 -05:00
Andrew Poelstra d1714ce0ac fuzz: disable sig-grinding tests, prevent spin-looping 2020-12-28 18:55:33 +00:00
Andrew Poelstra 96862b6a74 fuzz: implement recoverable signatures, get all tests passing, run them in CI 2020-12-28 18:42:46 +00:00
Andrew Poelstra b811ec133a fuzz: only replace signing and verification, leave everything else alone
We can now run unit tests with the fuzz feature on, and they'll pass,
which is some assurance that fuzzing with the feature on won't lead to
spurious failures due to the fuzz harness inadequately simulating message
signing.
2020-12-23 17:59:52 +00:00
Andrew Poelstra 40c31342f2 ffi: make function types nullable 2020-12-23 17:59:52 +00:00
Andrew Poelstra a2c25f2e83
Merge pull request #259 from p2pderivatives/implement-low-r-signing
Implement low r signing
2020-12-22 23:28:34 +00:00
Andrew Poelstra b31bf2f611
Merge pull request #258 from thomaseizinger/better-wasm-tests
Run more tests in wasm
2020-12-22 06:12:01 +00:00
Tibo-lg 1d166d061b Implement low r signing 2020-12-22 14:50:37 +09:00
Tobin Harding a584643486 Use ManuallyDrop
Suggested by clippy, we need to use ManuallyDrop for these types in
order to correctly free up the memory.
2020-12-22 14:56:17 +11:00
Tobin Harding c38136b6bc Use for loop instead of map
Currently we are misusing `map` on an iterator to loop `n` times,
additionally the assertion is pointless. Use a for loop and assert
against the length of the set.
2020-12-22 14:56:17 +11:00
Tobin Harding c92b946493 Remove unnecessary clone
Type is `Copy`, no need for clone.
2020-12-22 14:56:17 +11:00
Tobin Harding ef23cb8167 Return Ok directly
Clippy emits warning:

	warning: passing a unit value to a function

Just return `Ok(())` after calling `fill_bytes`.
2020-12-22 14:56:17 +11:00
Tobin Harding 34ad4110f1 Remove unused error return value
This helper never returns an error, remove the `Result` return type.
Found by clippy.
2020-12-22 14:56:17 +11:00
Tobin Harding ed29f12216
Remove unnecessary return statements
Found by clippy. We don't need a `return` for the final statement.
2020-12-22 13:38:02 +11:00
Tobin Harding 4e87e6fe94
Implement is_empty method
Clippy warns of missing `is_empty`, trivially implement it by calling
through to `self.data.is_empty()`.
2020-12-22 13:37:25 +11:00
Thomas Eizinger 8b8e482f79
Run more tests in wasm
Instead of repeating ourselves in defining one big test for the wasm
target, we can override the `test` attribute with the `wasm-bindgen-test`
one and therefore automatically run all (supported) tests in wasm.

Unfortunately, wasm doesn't support catching panics yet which means we
have to disable the `test_panic_raw_ctx` test.
2020-12-22 10:30:56 +11:00
Elichai Turkel 767246a282
Make preallocated use AlignedType 2020-12-18 12:48:19 +02:00
Elichai Turkel fd206ab57c
Replace use of boxes with global allocator 2020-12-18 12:48:19 +02:00
Andrew Poelstra ea027ce258 schnorrsig: change tweak_add_check to return a bool, take a fixed-length array 2020-12-09 16:49:48 +00:00
Andrew Poelstra 0ec8fab82c stop explicitly casting references to rawptrs 2020-11-30 02:47:34 +00:00
Andrew Poelstra 37049d743e schnorrsig: expose tweak_check_add API 2020-11-27 18:42:14 +00:00
Andrew Poelstra 12b0abbcf8 make `ffi::Type::new` functions all unsafe, expand documentation 2020-11-27 18:42:12 +00:00
Andrew Poelstra be9a78f39e
Merge pull request #237 from p2pderivatives/add-bip340-schnorr
Add bip340 schnorr
2020-11-27 18:04:44 +00:00
Andrew Poelstra 9083babbe2
Merge pull request #248 from justinmoon/pubkey-ordering
Implement lexigraphic ordering for PubKey
2020-11-26 03:03:17 +00:00
Justin Moon 6f8a480974 Implement lexigraphic ordering for PubKey 2020-11-25 19:27:31 -06:00
Andrew Poelstra 1859ddc28a switch from travis to github workflows 2020-11-10 23:58:03 +00:00
Tibo-lg 0c937d04f5 Add key::PublicKey to schnorrsig::PublicKey conversion 2020-11-10 14:15:34 +09:00
Tibo-lg 09b04560bc Add schnorrsig module 2020-11-10 13:43:34 +09:00
Tibo-lg 47b33828bc Add serde macros for public keys 2020-11-09 16:00:28 +09:00
Elichai Turkel 9e5a351ea7
remove redundant code after MSRV bump 2020-08-27 22:51:36 +03:00
Andrew Poelstra 19f5138925 Update MSRV to 1.29; increase version to 0.19 and -sys version to 0.3 2020-08-27 14:18:06 +00:00
Elichai Turkel c69d43d84e Update key.rs to use the new seckey functions 2020-08-26 21:16:19 +00:00
Andrew Poelstra 5c451f78c6
Merge pull request #207 from sorpaas/sp-message-zero
Allow all-zero messages
2020-08-26 18:02:51 +00:00
Andrew Poelstra 3692c94a0f
Merge pull request #220 from rantan/add_negate_support
Add negate support
2020-08-26 17:43:46 +00:00
Sebastian Geisler 2046a40905 Randomize context on initialization
Signed-off-by: Sebastian Geisler <sebastian@blockstream.io>
2020-08-03 12:07:26 +02:00
Sebastian Geisler a959de4174 Add an optional global, static context 2020-07-08 16:05:42 +02:00