A mid-level rust secp256k1 elliptic curve cryptography library that's optimized for fun! Here, fun means:
Fun does not mean (yet -- please help!):
The goal is for this library to let researchers experiment with ideas, have them work on Bitcoin and to enjoy it! High-level libraries like rust-secp256k1 make it difficult to implement exotic cryptographic schemes correctly and efficiently. Low-level libraries like parity/libsecp256k1 make it possible but the resulting code is often error prone and difficult to read.
toml
[dependencies]
secp256kfun = "0.7"
This library is ready for production as long what you are trying to produce is fun and amusement!. If you want to engineer something solid that a lot of people's money will depend on, this library is a very very risky choice. Here are some alternatives:
Here's the distinguishing features of this library.
Both secp256k1 points and scalars have a notional zero element.
Unfortunately, in things surrounding Bitcoin, the zero scalar and zero point are illegal values in most cases.
secp256kfun
solves these difficulties using marker types.
Points and Scalars are marked with Zero
or NonZero
at compile time (by default, NonZero
).
So if you declare your function with a NonZero
type, passing a Zero
type will be a compile time error as shown below:
```rust,compilefail use secp256kfun::{marker::*, Scalar, Point,G,g}; // a randomly selected Scalar will never be zero (statistically unreachable) let x = Scalar::random(&mut rand::threadrng()); dbg!(&x); // Scalar<.., NonZero> // Multiplying a NonZero scalar by G (which is also NonZero) results in a NonZero point let X = g!(x * G); dbg!(&X) // Point<..,NonZero> let Y = g!(-x * G) // An addition can lead to a zero so the result is marked Zero let sum = g!(X + Y); dbg!(&sum); // Point<.., Zero> // Now let's say I naively decide to use this value as my public key... let publickey = sum.normalize(); // BOOM! This is a compile time Error! 🎉 sendpubkeytobob(&public_key);
fn sendpubkeytobob(publickey: &Point) { unimplemented!() } ```
This gives us:
shell
error[E0308]: mismatched types
--> src/lib.rs:77:20
|
17 | send_pubkey_to_bob(&public_key);
| ^^^^^^^^^^^ expected struct `secp256kfun::marker::NonZero`, found struct `secp256kfun::marker::Zero`
To fix this, the library forces you to manually mark the value as NonZero
and then deal with the case that it is Zero
.
rust,compile_fail
match sum.normalize().mark::<NonZero>() {
Some(public_key) => send_pubkey_to_bob(&public_key), // it was actually NonZero
None => .. // deal with the case it is Zero
}
Or you can declare that you are confident that it can never be
If a cryptogrpahic function's execution time should be independent of its secret inputs. Otherwise, information about those inputs may leak to anyone that can measure its execution time.
In secp256kfun we try and solve this problem by allowing you to mark different inputs as Public
or Secret
.
Depending on the marking the rust compiler may choose different low level operations.
Choosing faster but variable time operations for Public
inputs and slower safer constant time ones for things marked as Secret
.
In other words, the caller can decide which input are
For example, below we have a pedersen_commitment
function which is called by the committing party with a secret value and by the verifying party when the secret value is finally revealed.
Note that we only have to write the function once and the caller decides by marking whether the function should run in constant time or variable time.
```rust use secp256kfun::{marker::*, Point, Scalar, g};
/// commit to a secret value x with publicly known A and B.
fn pedersencommit(
A: &Point
// public setup let A = secp256kfun::G; // use the standard basepoint for one of the points let B = Point::random(&mut rand::thread_rng());
// Alice commits to her secret value x with randomness r let r = Scalar::random(&mut rand::threadrng()); let x = Scalar::from(42); let commitment = pedersencommit(A, &B, &r, &x);
// Imagine Later on, Bob receives the public opening (r,x) for commitment. He
// doesn't care about leaking these values via execution time so he marks them
// as public.
let r = r.mark::
// Now he'll compute the commitment in faster variable time and check it // against the original asserteq!(commitment, pedersencommit(A, &B, &r, &x)); ```
As of v0.7.0
marking things correctly does very little since we changed the arithmetic backend to [k256] (it's always going to be constant time).
However this situation may improve in future versions.
g!
and s!
(used above) to clearly express group operations.serde
serialization/deserialization for binary and hex for human-readable formats (enable with serde
feature hex requires alloc
feature as well).no_std
supportlibsecp_compat
adds From
implementations to and from rust-secp256k1 types.proptest
implementations of core types with the proptest
feature