Coder Social home page Coder Social logo

Comments (4)

piersonleo avatar piersonleo commented on June 15, 2024 1

Thanks for the input, Chris!

After looking through the library, I think that the java library is producing the signature in form of compact representation, with the recovery id appended at the end to form a 65 bytes signature.

So I managed to reproduce the same signature as the java library using this

let compactSign = try privateKey.ecdsa.recoverableSignature(for: hashDigest).compactRepresentation

from secp256k1.swift.

csjones avatar csjones commented on June 15, 2024

Hey @piersonleo 👋 Is this the kind of example you're looking for?

let privateKey = try! secp256k1.Signing.PrivateKey()
let preHashedData = try! "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342".bytes

let hashDigest = HashDigest(preHashedData)

let signature = try! privateKey.ecdsa.signature(for: hashDigest)

from secp256k1.swift.

piersonleo avatar piersonleo commented on June 15, 2024

Hi Chris, ah yes this is the example I'm looking for. Thank you.

Kinda unrelated, but I've also been using a secp256k1 library in java, but it seems that swift and java libraries returns a different signature result. Do you perhaps know the reason why it could result to that difference?

Here's a sample of the data and the results I got using both the java library and this library

privateKey string = "187dc06196cb99b99ee19afb876b79defd246cc123fb9e0c617345f77d3737df"
preHashedData = "34b90c9d26d0b19bdb348b31570d39bcda88bac94249f7219444a55bcaef1908"


java public key = "b6f264cc07e01b3e48a3ae3557ed752bc5caae20d6cd048af59d7065b47bada4056ee891d570b926c12b5e19af3c6e071b2d0b029f9dc4061391604178235fcf"
java signature result = "54c1d0eb6babd0124987d69d1870efaefbda5abb1ce10cd225a9aacab1b59de641f2c815a7a98db233ad3570a078c4d17a43f0ebb3f04e83e1c16ba260fbecc501"

swift public key = "03b6f264cc07e01b3e48a3ae3557ed752bc5caae20d6cd048af59d7065b47bada4"
swift signature result = "e69db5b1caaaa925d20ce11cbb5adafbaeef70189dd6874912d0ab6bebd0c154c5ecfb60a26bc1e1834ef0b3ebf0437ad1c478a07035ad33b28da9a715c8f241"

When i try to recover the public key and verify both of the signature result using the golang ecdsa library the java signature passed the verification while the swift signature fails.

from secp256k1.swift.

csjones avatar csjones commented on June 15, 2024

You're welcome Pierson 😁

Looking at the java library, my understanding is it leverages libsodium (this package uses libsecp256k1) and I'm not exactly sure how make these ends meet because subtle differences could end up with completely different outputs.

For example, the java public key you posted is uncompressed but doesn't include 04 which denotes key format. If we switch the swift public key format from compressed, 03, to uncompressed - we will get the expected public key.

let expectedPrivateKey = "187dc06196cb99b99ee19afb876b79defd246cc123fb9e0c617345f77d3737df"
let privateKeyBytes = try! expectedPrivateKey.bytes
let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes, format: .uncompressed)
        
print(String(bytes: Array(privateKey.publicKey.rawRepresentation))) // 04b6f264cc07e01b3e48a3ae3557ed752bc5caae20d6cd048af59d7065b47bada4056ee891d570b926c12b5e19af3c6e071b2d0b029f9dc4061391604178235fcf

After completing the sanity check, we would want to start debugging the signatures themselves. First thing to notice is that the signatures are different character lengths. This makes me think of questions like, "What are the steps the java library used to create the signature?" and whether you'd get the result you're looking for by using the recovery signature API.

let recoverableSignature = try! privateKey.ecdsa.recoverableSignature(for: hashDigest)

Hope that helps!

from secp256k1.swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.