Commit Graph

266 Commits

Author SHA1 Message Date
Martin Habovstiak 705c9cfbc1 Clarified conversions between `Parity` and integers 2022-02-09 15:33:25 +01: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 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 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
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
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
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 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
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 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
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
sanket1729 837be22e09 Basic derives for Parity 2022-01-06 23:57:26 +05:30
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
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
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 ce4427747d
Move ECDSA functionality into ECDSA 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 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
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 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
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
Tibo-lg 674cc79d87 Add error type for combine keys + test and doc 2021-09-02 21:19:21 +09:00
Dr Maxim Orlovsky 8ee4e05e63
Removing uneeded Secp context arguments 2021-06-20 17:15:10 +02:00
Dr Maxim Orlovsky aa6bdaff3c
Creating SecretKey and PublicKey from BIP-340 KeyPair 2021-06-16 10:56:59 +02: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
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
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
Tibo-lg 7d32182537 Add combine_keys function to PublicKey 2021-04-08 06:28:03 +09: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 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 b31bf2f611
Merge pull request #258 from thomaseizinger/better-wasm-tests
Run more tests in wasm
2020-12-22 06:12:01 +00: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
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
Andrew Poelstra 0ec8fab82c stop explicitly casting references to rawptrs 2020-11-30 02:47:34 +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
Justin Moon 6f8a480974 Implement lexigraphic ordering for PubKey 2020-11-25 19:27:31 -06:00
Tibo-lg 47b33828bc Add serde macros for public keys 2020-11-09 16:00:28 +09:00
Elichai Turkel c69d43d84e Update key.rs to use the new seckey functions 2020-08-26 21:16:19 +00:00
Kohei Taniguchi 02ed0616e4 Add assert for the response of ffi negate interface
The interfaces for negate should always returns 1 as mentioned secp256k1.h L574, L563.
But in the future it might return 0 if the seckey or pubkey is invalid, but our type system doesn't allow that to ever happen.
2020-06-10 09:59:55 +09:00
Kohei Taniguchi d45f709cd4 Add negation support 2020-06-09 10:05:07 +09:00
Rodolfo Araujo a4ccab7431 Libraries. 2019-11-17 22:04:11 -03:00
Elichai Turkel d7461e4cdc
Add zst tests and some other parsing tests 2019-08-21 18:06:02 -04:00
Elichai Turkel ddb8e4fdf2
Explicit checks for empty slices 2019-08-21 13:09:36 -04:00
Elichai Turkel b7e20c5b12
Changed all as_*ptr() to the safer CPtr trait 2019-08-21 13:09:36 -04:00
Elichai Turkel 66ab70f991
Added a C Ptr trait that returns a null pointer for ZSTs 2019-08-21 13:09:20 -04:00
Elichai Turkel 8e701b75b2
Replacing usage of the unsafe `blank` function to the `new` function
Signed-off-by: Elichai Turkel <elichai.turkel@gmail.com>
2019-08-16 14:15:31 -04:00
Elichai Turkel 389e1e2449
Removing usage of `mem::uninitialized()` and deprecating the `blank()` functions
Signed-off-by: Elichai Turkel <elichai.turkel@gmail.com>
2019-08-16 14:15:27 -04:00
Steven Roose 0f25474805
Implement hex human-readable serde for PublicKey 2019-07-23 17:50:56 +02:00
Steven Roose ccac3ead25
Implement fmt::LowerHex for SecretKey and PublicKey 2019-07-23 17:50:54 +02:00
Jonas Nick 3825ed35a7 Add human readable serialization and deserialization to Secret Key 2019-05-14 11:15:18 +00:00
Tim Ruffing ab4320f8cc
Merge pull request #100 from elichai/master
Add no-std support
2019-05-10 23:32:52 +02:00
Andrew Poelstra 90faf434c9
Merge pull request #104 from alekseysidorov/fix_rand
Fix `Rand` trait bounds.
2019-05-01 01:47:43 +00:00
Jonas Nick 91c43e7320 Add as_mut_ptr to PublicKey 2019-04-16 18:48:24 +00:00
Aleksei Sidorov 2536c5a3c4 Fix rand trait bounds. 2019-04-16 16:35:33 +03:00
Elichai Turkel 11ce86681f
Replaced std with core, and made std::error::Error optional through feature 2019-04-14 12:09:36 +03:00
François Garillot bafef68c56
Bumps rand to 0.6.3 & associated updates 2019-02-25 20:09:43 -08:00
Pierre Krieger 9c45944a5f
Remove dependency on libc 2018-12-27 14:39:54 +01:00
Tim Ruffing b470f70e90
Merge pull request #57 from rust-bitcoin/2018-08-no-index
remove `ops::Index` impls for `Signature`
2018-12-03 14:27:08 +01:00
Andrew Poelstra 1f4a4c11a3 change add_*_assign and mul_*_assign to use a byteslice as a tweak, rather than a `SecretKey`
This makes more conceptual sense and does not add any new error paths, since even
valid `SecretKey`s were able to be invalid tweaks.
2018-11-10 13:08:18 +00:00
Andrew Poelstra 7fbaf2f91b remove unsafe ZERO_KEY and deprecated ONE 2018-11-10 12:56:19 +00:00
Andrew Poelstra 93abca5896 remove `without_caps` and every function that used a cap-less context 2018-11-06 22:16:24 +00:00
Andrew Poelstra 572adb2873 add `FromStr` implementation for key types 2018-11-03 15:52:03 +00:00
Andrew Poelstra a810880c54 restore missing `mem` import in key.rs 2018-08-22 20:43:20 +00:00
Andrew Poelstra 0d41c23c21 Revert "remove PublicKey::combine"
This reverts commit 17450e91fd.
2018-08-22 20:18:25 +00:00
Andrew Poelstra c773dfbc8d remove unused import that was causing warning 2018-08-22 16:40:05 +00:00
Andrew Poelstra 2c18a16858
Merge pull request #50 from rust-bitcoin/2018-08-no-combine
remove PublicKey::combine
2018-08-21 18:36:27 +00:00
Andrew Poelstra 1f73a784cb
Merge pull request #52 from rust-bitcoin/2018-08-display-keys
add Display impl for the key types which outputs their hex representation
2018-08-20 18:32:53 +00:00
Andrew Poelstra 93d8249028 improve documentation 2018-08-20 17:56:46 +00:00
Andrew Poelstra 7d2474b10a add Display impl for the key types which outputs their hex representation
Since making PublicKey::serialize return a fixed-size array, this no longer
requires any allocation, so there's no reason not to have it.
2018-08-15 20:40:15 +00:00
Andrew Poelstra 17450e91fd remove PublicKey::combine 2018-08-15 16:29:23 +00:00
Andrew Poelstra 2478930ec1 add feature-gated serde 1.0 support for Signature, SecretKey, PublicKey 2018-07-25 14:44:51 +00:00
Andrew Poelstra c3ec027925 remove `PublicKey::new()` and `PublicKey::is_valid()` 2018-07-24 21:24:19 +00:00
Thomas Eizinger f13cdfa8a4 Replace Result return type with actual value
This introduces the actual breaking API change.
2018-06-08 08:48:02 +08:00
Thomas Eizinger be7134c7f4 Cleanup obsolete code
Remove ContextFlag enum
Remove InvalidContext error-enum variant
Remove unused imports
2018-06-08 08:41:30 +08:00
Thomas Eizinger bb77741e47 Fix unit tests and benchmarks
The new API allows us to remove a bunch of tests which are now checked
by the compiler.
2018-06-08 08:38:06 +08:00
Thomas Eizinger f1a88259fb Introduce generic-based capability handling
Add type parameter to Secp256k1
Add PhantomData for C
Separate into structs and traits
Move constructors to own impl blocks
2018-06-06 12:58:29 +08:00
Aleksey Sidorov 113fe420b9 Add tests to detect regressions. 2018-05-31 13:05:43 +03:00
Aleksey Sidorov b192157ab4 Implement Ord for arrays 2018-05-22 12:33:11 +03:00
Matt Corallo e1878c4522 Remove all serde/rustc-serialize stuff 2018-03-21 18:01:33 -04:00
Andrew Poelstra 2c5f45e165 expose 2-key version of PublicKey::combine 2017-12-21 01:04:07 +00:00
Andrew Poelstra 5c54698bf9 make `serde` optional 2017-12-19 22:40:06 +00:00
Andrew Poelstra 12e9f4b90b make `rustc-serialize` crate optional 2017-12-19 22:36:30 +00:00
Andrew Poelstra 298929600b make `rand` crate optional 2017-12-19 22:36:27 +00:00
Andrew Poelstra dba0d67912 remove arrayvec dependency; rename PublicKey::serialize_vec 2017-12-19 20:36:46 +00:00
Tim Ruffing e71ed3b6d2 Implement serde traits for Signature 2017-07-21 23:58:19 +02:00
Tim Ruffing b1d8b09f25 Add tests for trailing bytes during deserilization 2017-07-21 22:21:37 +02:00
Tim Ruffing 76d160344b Update serde to 1.0 2017-07-21 22:15:00 +02:00
Peter Todd 20c5b903db
Replace unsafe copy_nonoverlapping() with safe copy_from_slice() 2017-05-08 07:31:08 -04:00
Christian Nyumbayire ab0493e46a Use DefaultHasher in place of the deprecated SipHasher 2017-04-27 21:46:28 +02:00
Andrew Poelstra 5b906ec069 expose *_tweak_mul functions 2016-08-20 17:00:39 +00:00
Andrew Poelstra 9e3b93d572 Add ZERO_KEY and ONE_KEY constants
Turns out you cannot initialize constant SecretKeys in any way; these
two constants should cover most sane use cases (other good choices
are the SECG generator and the Alpha CT generator, but these will
wait for a major CT-supporting upgrade, unless demand for them appears.)
2016-08-07 21:33:38 +00:00
Andrew Poelstra 458a3d9417 Update libsecp to latest master, c18b869e58aa4d3bff6958f370f6b643d1223c44 2016-01-14 18:35:54 +00:00
Dawid Ciężarkiewicz 16b36f18e1 key: Add PubKey hashing test. 2016-01-08 20:04:10 -08:00
Dawid Ciężarkiewicz 6686c213dd Implement Hash for PublicKey. 2016-01-05 21:01:40 -08:00
Andrew Poelstra 9a91b69fad Remove side-effect from assertion; add release build to travis 2015-11-18 18:22:16 -06:00
Andrew Poelstra f1e1da1213 Update for breaking changes in underlying rustc.
This should be a major version number since I changed public constants
in the ffi module. I'm not doing so as the invariant "will the constants
be meaningful to the underlying library" has not changed.

In general this library's version numbers do not map well to the
underlying library, which is as-yet not versioned at all, so users
need to always be running "the lastest" rust-secp256k1 anyway, and
semantic versioning can't really be used meaninfully. So this is a
bit of a judgement call.
2015-10-25 18:26:08 -05:00
Andrew Poelstra d49db8167e impl Error for error type; remove `Unknown` error variant 2015-10-17 09:49:19 -05:00
Andrew Poelstra 32107132f0 Improve unit tests for bad secp context 2015-10-14 12:21:15 -05:00
Andrew Poelstra c84cfb193f [BREAKING CHANGE] Make PK::from_secret_key() return a Result; change from_ffi functions to From impls
If you try to call PublicKey::from_secret() key with an incapable context it will
now return an error. Before it would pass through to the underlying library which
would terminate the process, something we strive to never expose.

Also change the from_ffi functions on various types to impl's of From to be more
Rustic. We cannot change the from_slice functions because they have error returns.

Also add a Secp256k1::without_caps() function which creates a capability-less
context. I find myself using this in so many places downstream that it seems
appropriate.
2015-10-14 09:38:43 -05:00
Andrew Poelstra ec0e8407f2 Minor documentation fixes 2015-10-09 11:39:42 -05:00
Andrew Poelstra 4012281a2d Fix for upstream API changes 2015-09-20 14:52:29 -05:00
Andrew Poelstra cc68cf8b9e Update serde dep to 0.6 from 0.3 2015-09-20 13:29:09 -05:00
Andrew Poelstra 77f6b6bf21 [API BREAK] Update for new libsecp256k1 API 2015-07-28 11:05:13 -05:00
Andrew Poelstra f8bbc89df6 [API BREAK] expose ability to create contexts without verify or signing caps
There are a lot of cases in rust-bitcoin where we need a `Secp256k1`
which doesn't need any signing or verification capabilities, only
checking the validity of various objects. We can get away with a bare
context (i.e. no precomputation) which can be cheaply created on demand,
avoiding the need to pass around references to Secp256k1 objects everywhere.

API break because the following functions can now fail (given an insufficiently
capable context) and therefore now return a Result:

    Secp256k1::generate_keypair
    Secp256k1::sign
    Secp256k1::sign_compact
2015-04-13 22:15:52 -05:00
Andrew Poelstra fb75373b47 [API BREAK] Remove Rng from Secp256k1 and associated code
The Rng was only used for key generation, and for BIP32 users not even then;
thus hauling around a Rng is a waste of space in addition to causing a
massive amount of syntactic noise. For example rust-bitcoin almost always
uses `()` as the Rng; having `Secp256k1` default to a `Secp256k1<Fortuna>`
then means even more syntactic noise, rather than less.

Now key generation functions take a Rng as a parameter, and the rest can
forget about having a Rng. This also means that the Secp256k1 context
never needs a mutable reference and can be easily put into an Arc if so
desired.
2015-04-12 15:54:22 -05:00
Andrew Poelstra 83823379e4 [minor API BREAK] Add unit tests to cover all error cases
This comes with a couple bugfixes and the following API changes:

  - Secp256k1::sign and ::sign_compact no longer return Result;
    it is impossible to trigger their failure modes with safe
    code since the `Message` and `SecretKey` types validate when
    they are created.

  - constants::MAX_COMPACT_SIGNATURE_SIZE loses the MAX_; signatures
    are always constant size

  - the Debug output for everything is now hex-encoded rather than
    being a list of base-10 ints. It's just easier to read this way.

kcov v26 now reports 100% test coverage; however, this does not
guarantee that test coverage is actually complete. Patches are
always welcome for improved unit tests.
2015-04-12 10:51:15 -05:00
Andrew Poelstra 6b39f97f51 Add `Secp256k1::new_deterministic` constructor for applications which don't need randomness 2015-04-11 13:28:15 -05:00
Andrew Poelstra 96e1844c25 Change inline assertions to debug_asserts
All of these were things that are (should be) guaranteed true no matter
what input is given to the API, barring unsafe operations on the data.
2015-04-11 13:07:43 -05:00
Andrew Poelstra 9e717d4219 Add `Secp256k1::with_rng`, parameterize `Secp256k1` over its RNG.
Now that you can't create secret keys by directly passing a Rng to
`SecretKey::new`, we need a way to allow user-chosed randomness.
We add it to the `Secp256k1`.
2015-04-11 12:53:30 -05:00
Andrew Poelstra e52faee98f [API BREAK] update for libsecp256k1 "explicit context" API break
Rather than have global initialization functions, which required
expensive synchronization on the part of the Rust library,
libsecp256k1 now carries its context in thread-local data which
must be passed to every function.

What this means for the rust-secp256k1 API is:
  - Most functions on `PublicKey` and `SecretKey` now require a
    `Secp256k1` to be given to them.

  - `Secp256k1::verify` and `::verify_raw` now take a `&self`

  - `SecretKey::new` now takes a `Secp256k1` rather than a Rng; a
    future commit will allow specifying the Rng in the `Secp256k1`
    so that functionality is not lost.

  - The FFI functions have all changed to take a context argument

  - `secp256k1::init()` is gone, as is the dependency on std::sync

  - There is a `ffi::Context` type which must be handled carefully
    by anyone using it directly (hopefully nobody :))
2015-04-11 12:52:54 -05:00
Andrew Poelstra 609f658bee [API BREAK] Remove `Sequence` iterator over secret keys
Y'know, I can't for the life of me think what this was supposed to
be used for. Given that the library did not compile for several
months until last week, I assume there are no users, let alone
users of such a weird feature.
2015-04-11 12:24:04 -05:00
Andrew Poelstra ac61baf040 Add support for serde (de)serialization; add unit tests 2015-04-10 00:32:12 -05:00
Andrew Poelstra 1591bba3f9 Update bindings to current secp256k1 library
rust-secp256k1 was based off of https://github.com/sipa/secp256k1,
which has been inactive nearly as long as this repository (prior to
a couple days ago anyway). The correct repository is

   https://github.com/bitcoin/secp256k1

This is a major breaking change to the library for one reason: there
are no longer any Nonce types in the safe interface. The signing functions
do not take a nonce; this is generated internally.

This also means that I was able to drop all my RFC6979 code, since
libsecp256k1 has its own implementation.

If you need to generate your own nonces, you need to create an unsafe
function of type `ffi::NonceFn`, then pass it to the appropriate
functions in the `ffi` module. There is no safe interface for doing
this, deliberately: there is basically no need to directly fiddle
with nonces ever.
2015-04-06 00:13:38 -05:00
Andrew Poelstra f6585616b1 Add `Display` impl to `Error`; cleanup `Result` mess 2015-04-05 20:27:43 -05:00
Andrew Poelstra 5b3858e0ce Simplify Index implementations 2015-04-05 12:16:56 -05:00
Andrew Poelstra e2daaf875d Update for language changes (rustc beta is out !!) 2015-04-04 12:20:38 -05:00
Andrew Poelstra 42dfa752ce More slicing 2015-03-25 20:52:09 -05:00
Andrew Poelstra d858d7f7e6 Slicing fixes 2015-03-25 20:36:57 -05:00
Andrew Poelstra 7bd24615f6 Change std::rand to just rand::, though there is still a 'unimplemented trait' error :/ 2015-03-25 18:22:24 -05:00
Andrew Poelstra 825e77519f for in range(a,b) -> for in a..b 2015-03-25 14:42:05 -05:00
Andrew Poelstra d0519f0b3a IoResult -> io::Result, copy_nonoverlapping_memory -> copy_nonoverlapping 2015-03-25 14:10:02 -05:00
Andrew Poelstra 1e24549ef5 Show -> Debug 2015-03-25 13:59:54 -05:00
Andrew Poelstra 5a6c6c8d0a Fix for secp256k1 ffi changes
All tests pass, compile now
2015-01-17 10:38:16 -06:00
Andrew Poelstra d495d9ca06 Update for rustc changes
We can compile now, but not link -- there have been too many changes
in libsecp256k1 behind the scenes. Next commit :)
2015-01-17 10:13:45 -06:00
Andrew Poelstra 9cab4e023d Revert "Overhaul interface to use zero-on-free SecretKeys"
This reverts commit 9889090784.

This is not ready for primetime -- the move prevention also prevents
reborrowing, which makes secret keys nearly unusable.
2014-09-12 08:28:35 -05:00
Andrew Poelstra 9889090784 Overhaul interface to use zero-on-free SecretKeys
Using the `secretdata` library, we can store SecretKeys in such a way
that they cannot be moved or copied, and their memory is zeroed out on
drop. This gives us some assurance that in the case of memory unsafety,
there is not secret key data lying around anywhere that we don't expect.

Unfortunately, it means that we cannot construct secret keys and then
return them, which forces the interface to change a fair bit. I removed
the `generate_keypair` function from Secp256k1, then `generate_nonce`
for symmetry, then dropped the `Secp256k1` struct entirely because it
turned out that none of the remaining functions used the `self` param.

So here we are. I bumped the version number. Sorry about this.
2014-09-12 08:28:26 -05:00
Andrew Poelstra eabe57e403 Also make PublicKey encodable 2014-09-04 20:21:09 -05:00
Andrew Poelstra 17daebf15d Implement deterministic nonce generation with HMAC-SHA512
Testing was done against python-ecdsa; python code in the test case
comments.
2014-09-04 16:21:35 -05:00