Comments (7)
So to confirm, the issue is that you are not generating an identical signature to bitcoin-core/libsecp256k1, correct?
Does the signature still verify?
One possible cause is how libsecp256k1 implements RFC6979 deterministic generation of the ECDSA k
value: it uses Keccak256 to hash the message, but then uses HMAC-SHA-256 to derive k
.
By default k256::ecdsa::recoverable::Signature
will use Keccak256 for both.
If that's really the issue, it's possible although a bit involved to wire everything up to use Keccak256 for message hashing, and SHA-256 for computing k
. However, note that this doesn't really accomplish anything other than producing the same signature for producing the same signature's sake. Both signatures will still verify.
from elliptic-curves.
The first issue is indeed that the signature is not identical, yes.
I want to test a bit more with Ethereum transactions, because I might have spotted issues there as well, but cannot say for sure now.
If that helps, I tried with web3py (following this) and it produces the same signature as secp256k1
.
from elliptic-curves.
If you really want to compute an identical signature, you'll need to use the SignPrimitive
trait.
I haven't verified this actually works, but it should be roughly the following:
use k256::{
ecdsa::hazmat::{SignPrimitive, rfc6979_generate_k},
elliptic_curve::ops::Reduce,
NonZeroScalar, Secp256k1, U256
};
use sha2::Sha256;
use sha3::Keccak256;
// Get `NonZeroScalar` for the signing key somehow
let secret_key = NonZeroScalar::new(...);
// Get message to be signed somehow
let msg = b"...";
let z = <Scalar as Reduce<U256>>::from_be_digest_reduced(Keccak256::new_with_prefix(msg));
let k = rfc6979_generate_k::<Secp256k1, Sha256>(&secret_key, &z, &[]);
let (signature, recid) = secret_key.try_sign_prehashed(&k, z)?;
from elliptic-curves.
Got it, thank you very much.
I will test a bit more and get back here, but at least I understand why the signature "appears to be" different.
from elliptic-curves.
If that works, I can potentially tweak the implementation in k256
to do that by default, although it's a bit silly that it's using two different hash functions
from elliptic-curves.
Confirmed everything is good as it is. The signatures are indeed different (and thank you for the explanation about why that's the case) but both work just fine for all purposes I could test.
Thanks again, I will close the issue.
from elliptic-curves.
@MicheleMinelli see how we solved it with a Sha256Proxy here gakonst/ethers-rs#975 (I tried to remove it in the PR but ended up back to the same problem)
from elliptic-curves.
Related Issues (20)
- Update crates to the newtype `fiat-crypto` codegen
- Request: Instructions to reproduce fiat-crypto files HOT 2
- Using ECDSA key pair for ECDH HOT 1
- Prepend the TapSigHash tag when doing k256 Schnorr signatures? HOT 1
- bp256+bp384: tracking issue for `arithmetic` feature HOT 3
- Numerous unused variable cause Rust difficulties HOT 3
- Bug(deps) p521 crate requires ecdsa 16.8 HOT 2
- BIP340 Schnorr should accept arbitrary length messages HOT 6
- p256::Scalar: implement Reduce<U512> HOT 1
- PKCS8: Cannot parse PrivateKey HOT 2
- Asymmetry in default features: only p384 has `ecdh` by default HOT 2
- Poseidon Support for k256 Hash To Curve HOT 1
- Document use with the PKCS#8 HOT 1
- Pre-release of k256 v0.14? HOT 3
- k256 does not compile without allocator HOT 5
- primeorder 0.13.4 update breaks p384 0.13.0 builds HOT 4
- k256 disrepency in secp256k1 key recover HOT 12
- How to convert a VerifyingKey of type FieldElement10x26 to a FieldElement5x52 type
- k256: v0.13.2 breaking verification HOT 9
- Bitwise operations for scalars HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elliptic-curves.