dustinxie / ecc

Golang native implementation of the secp256k1 elliptic curve
MIT License
29 stars 7 forks source link

How to implement these curve for ecdh? #2

Open LuThaiHoc opened 6 days ago

LuThaiHoc commented 6 days ago

Thanks for the project, I succeed verify with additional curve, I tried to using additional curve for key exchange ecdh, how to do it? I following ecdh interface, implement its function but not work:

type Curve interface {
    // GenerateKey generates a random PrivateKey.
    //
    // Most applications should use [crypto/rand.Reader] as rand. Note that the
    // returned key does not depend deterministically on the bytes read from rand,
    // and may change between calls and/or between versions.
    GenerateKey(rand io.Reader) (*PrivateKey, error)

    // NewPrivateKey checks that key is valid and returns a PrivateKey.
    //
    // For NIST curves, this follows SEC 1, Version 2.0, Section 2.3.6, which
    // amounts to decoding the bytes as a fixed length big endian integer and
    // checking that the result is lower than the order of the curve. The zero
    // private key is also rejected, as the encoding of the corresponding public
    // key would be irregular.
    //
    // For X25519, this only checks the scalar length.
    NewPrivateKey(key []byte) (*PrivateKey, error)

    // NewPublicKey checks that key is valid and returns a PublicKey.
    //
    // For NIST curves, this decodes an uncompressed point according to SEC 1,
    // Version 2.0, Section 2.3.4. Compressed encodings and the point at
    // infinity are rejected.
    //
    // For X25519, this only checks the u-coordinate length. Adversarially
    // selected public keys can cause ECDH to return an error.
    NewPublicKey(key []byte) (*PublicKey, error)

    // ecdh performs an ECDH exchange and returns the shared secret. It's exposed
    // as the PrivateKey.ECDH method.
    //
    // The private method also allow us to expand the ECDH interface with more
    // methods in the future without breaking backwards compatibility.
    ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error)

    // privateKeyToPublicKey converts a PrivateKey to a PublicKey. It's exposed
    // as the PrivateKey.PublicKey method.
    //
    // This method always succeeds: for X25519, the zero key can't be
    // constructed due to clamping; for NIST curves, it is rejected by
    // NewPrivateKey.
    privateKeyToPublicKey(*PrivateKey) *PublicKey
}