{"id":13563093,"url":"https://github.com/dustinxie/ecc","last_synced_at":"2025-04-13T15:34:37.198Z","repository":{"id":57572203,"uuid":"342141639","full_name":"dustinxie/ecc","owner":"dustinxie","description":"Golang native implementation of the secp256k1 elliptic curve","archived":false,"fork":false,"pushed_at":"2021-05-11T00:09:15.000Z","size":130,"stargazers_count":33,"open_issues_count":2,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T06:22:51.620Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dustinxie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-25T06:10:06.000Z","updated_at":"2025-02-13T17:28:21.000Z","dependencies_parsed_at":"2022-08-24T05:31:58.317Z","dependency_job_id":null,"html_url":"https://github.com/dustinxie/ecc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinxie%2Fecc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinxie%2Fecc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinxie%2Fecc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustinxie%2Fecc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dustinxie","download_url":"https://codeload.github.com/dustinxie/ecc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248736016,"owners_count":21153520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:01:15.126Z","updated_at":"2025-04-13T15:34:37.166Z","avatar_url":"https://github.com/dustinxie.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# secp256k1\nGolang native implementation of the secp256k1 elliptic curve\n\n[![LICENSE](https://img.shields.io/badge/License-Apache%202.0-turquise.svg)](LICENSE)\n[![Go version](https://img.shields.io/badge/Go-1.15-turquise.svg)]()\n[![Go Report card](https://goreportcard.com/badge/github.com/dustinxie/ecc)](https://goreportcard.com/report/github.com/dustinxie/ecc)\n[![Go Reference](https://pkg.go.dev/badge/github.com/dustinxie/ecc.svg)](https://pkg.go.dev/github.com/dustinxie/ecc)\n---\n## Features\n- Based on Golang's native `crypto/ecdsa` and `crypto/elliptic` package, no\nexternal dependency at all \n- Full compatible with the secp256k1 signature in [go-ethereum](https://github.com/ethereum/go-ethereum)\n\n## Motivation\nGolang's `elliptic.Curve` implements the short-form Weierstrass curve y² = x³ +\nax + b, but only with a = -3, which are the case for NIST-recommended curves\nP224, P256,P384, and P521. For a general curve with a != -3, one would have to\nrely on external packages, which is quite an inconvenience.\n\nFor example, a very popular curve is secp256k1 with equation y² = x³ + 7, used\nby many crypto projects such as Bitcoin and Ethereum. In order to use it, one\nwould usually need to import for example [go-ethereum](https://github.com/ethereum/go-ethereum),\nwhich is a very large package with many dependencies.\n\nThis package provides a secp256k1 implementation solely based on Golang's native\ncode. No external dependency is introduced.\n\n## How to use\nPackage's `P256k1()` method returns a `elliptic.Curve` that implements the\nsecp256k1 curve, use it the same way as you would use other curves in the\n`ecdsa` package.\n\nOr use package's `SignBytes()` and `VerifyBytes()` API that signs/verifies the\nsignature as a byte-stream. See example below:\n```go\npackage anyname\n\nimport (\n\t\"crypto/ecdsa\"\n\t\"crypto/rand\"\n\t\"crypto/sha256\"\n\t\"fmt\"\n\t\n\t\"github.com/dustinxie/ecc\"\n)\n\nfunc signVerify(msg []byte) error {\n\t// generate secp256k1 private key\n\tp256k1 := ecc.P256k1()\n\tprivKey, err := ecdsa.GenerateKey(p256k1, rand.Reader)\n\tif err != nil {\n\t\t// handle error\n\t\treturn err\n\t}\n\t\n\t// sign message\n\thash := sha256.Sum256(msg)\n\tsig, err := ecc.SignBytes(privKey, hash[:], ecc.Normal)\n\tif err != nil {\n\t\treturn err\n\t}\n\t\n\t// verify message\n\tif !ecc.VerifyBytes(\u0026privKey.PublicKey, hash[:], sig, ecc.Normal) {\n\t\treturn fmt.Errorf(\"failed to verify secp256k1 signature\")\n\t}\n\treturn nil\n}\n```\n\n### Signing options\nThe package provides 2 additional signing options:\n- To tackle the ECDSA signature malleability issue (see \"Rationale\" in\n[here](https://eips.ethereum.org/EIPS/eip-2)), pass the flag `LowerS` to\nsigning API. This ensures the resulting `s` value in the signature is less\nthan or equal to half of N (the order of the curve)\n```go\n// generate 64-byte signature R || S, with s \u003c= N/2\nsig, err := ecc.SignBytes(privKey, hash, ecc.LowerS)\nif err != nil {\n\treturn err\n}\n\nif !ecc.VerifyBytes(\u0026privKey.PublicKey, hash, sig, ecc.LowerS) {\n\treturn fmt.Errorf(\"failed to verify secp256k1 signature\")\n}\nreturn nil\n```\n- To return the one-byte recovery ID that can be used to recover public key from\nthe signature, pass the flag `RecID` to signing API\n```go\n// generate 65-byte signature R || S || V\nsig, err := ecc.SignBytes(privKey, hash, ecc.RecID)\nif err != nil {\n\treturn err\n}\n\nif !ecc.VerifyBytes(\u0026privKey.PublicKey, hash, sig, ecc.RecID) {\n\treturn fmt.Errorf(\"failed to verify secp256k1 signature\")\n}\n```\nthe resulting 65-byte signature allows you to recover public key from it:\n```go\npubKey, err := RecoverPubkey(\"P-256k1\", hash, sig)\nif err != nil {\n\treturn err\n}\n\nif !pubKey.Equal(\u0026privKey.PublicKey) {\n\treturn fmt.Errorf(\"recovered public key not equal to signing public key\")\n}\nreturn nil\n```\nThe recommendation is to always enable `ecc.LowerS` option when signing any\nmessage. And finally, you can pass both flags to signing API:\n```go\nsig, err := ecc.SignBytes(privKey, hash, ecc.LowerS | ecc.RecID)\n```\n\n### Full Ethereum compatibility\nPackage also provides the following 3 API that are fully compatible with the\nofficial [go-ethereum](https://github.com/ethereum/go-ethereum). They are\nactually just a wrapper of our API using proper options.\n```go\nfunc SignEthereum(hash []byte, priv *ecdsa.PrivateKey) ([]byte, error)\n\nfunc VerifyEthereum(pubkey, hash, sig []byte, isHomestead bool) bool\n\nfunc RecoverEthereum(hash, sig []byte) ([]byte, error)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustinxie%2Fecc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdustinxie%2Fecc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustinxie%2Fecc/lists"}