forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cgosecp256k1 verification (cosmos#11298)
## Description Closes: cosmos#10747 - update secp256k1 cgo fork, - debug verify bytes ``` benchmark old ns/op new ns/op delta BenchmarkKeyGeneration-10 407 413 +1.35% BenchmarkSigning-10 95099 36754 -61.35% BenchmarkVerification-10 215551 48053 -77.71% benchmark old allocs new allocs delta BenchmarkKeyGeneration-10 2 2 +0.00% BenchmarkSigning-10 83 4 -95.18% BenchmarkVerification-10 74 1 -98.65% benchmark old bytes new bytes delta BenchmarkKeyGeneration-10 96 96 +0.00% BenchmarkSigning-10 5283 196 -96.29% BenchmarkVerification-10 3537 32 -99.10% ``` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
1 parent
efdedf3
commit 361c837
Showing
23 changed files
with
461 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# secp256k1 | ||
|
||
This package is copied from https://github.com/ethereum/go-ethereum/tree/729bf365b5f17325be9107b63b233da54100eec6/crypto/secp256k1 | ||
This package is copied from https://github.com/ethereum/go-ethereum/tree/8fddf27a989e246659fd018ea9be37b2b4f55326/crypto/secp256k1 | ||
|
||
Unlike the rest of go-ethereum it is MIT licensed so compatible with our Apache2.0 license. We opt to copy in here rather than depend on go-ethereum to avoid issues with vendoring of the GPL parts of that repository by downstream. | ||
Unlike the rest of go-ethereum it is [3-clause BSD](https://opensource.org/licenses/BSD-3-Clause) licensed so compatible with our Apache2.0 license. We opt to copy in here rather than depend on go-ethereum to avoid issues with vendoring of the GPL parts of that repository by downstream. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build dummy | ||
// +build dummy | ||
|
||
// This file is part of a workaround for `go mod vendor` which won't vendor | ||
// C files if there's no Go file in the same directory. | ||
// This would prevent the crypto/secp256k1/libsecp256k1/include/secp256k1.h file to be vendored. | ||
// | ||
// This Go file imports the c directory where there is another dummy.go file which | ||
// is the second part of this workaround. | ||
// | ||
// These two files combined make it so `go mod vendor` behaves correctly. | ||
// | ||
// See this issue for reference: https://github.com/golang/go/issues/26366 | ||
|
||
package secp256k1 | ||
|
||
import ( | ||
_ "github.com/ethereum/go-ethereum/crypto/secp256k1/libsecp256k1/include" | ||
_ "github.com/ethereum/go-ethereum/crypto/secp256k1/libsecp256k1/src" | ||
_ "github.com/ethereum/go-ethereum/crypto/secp256k1/libsecp256k1/src/modules/recovery" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/contrib/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package contrib |
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package libsecp256k1 |
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/include/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/src/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package src |
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/src/modules/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package module |
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/src/modules/ecdh/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package ecdh |
7 changes: 7 additions & 0 deletions
7
crypto/keys/secp256k1/internal/secp256k1/libsecp256k1/src/modules/recovery/dummy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build dummy | ||
|
||
// Package c contains only a C file. | ||
// | ||
// This Go file is part of a workaround for `go mod vendor`. | ||
// Please see the file crypto/secp256k1/dummy.go for more information. | ||
package recovery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
crypto/keys/secp256k1/internal/secp256k1/scalar_mult_cgo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
//go:build !gofuzz && cgo | ||
// +build !gofuzz,cgo | ||
|
||
package secp256k1 | ||
|
||
import ( | ||
"math/big" | ||
"unsafe" | ||
) | ||
|
||
/* | ||
#include "libsecp256k1/include/secp256k1.h" | ||
extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); | ||
*/ | ||
import "C" | ||
|
||
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) { | ||
// Ensure scalar is exactly 32 bytes. We pad always, even if | ||
// scalar is 32 bytes long, to avoid a timing side channel. | ||
if len(scalar) > 32 { | ||
panic("can't handle scalars > 256 bits") | ||
} | ||
// NOTE: potential timing issue | ||
padded := make([]byte, 32) | ||
copy(padded[32-len(scalar):], scalar) | ||
scalar = padded | ||
|
||
// Do the multiplication in C, updating point. | ||
point := make([]byte, 64) | ||
readBits(Bx, point[:32]) | ||
readBits(By, point[32:]) | ||
|
||
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0])) | ||
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0])) | ||
res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr) | ||
|
||
// Unpack the result and clear temporaries. | ||
x := new(big.Int).SetBytes(point[:32]) | ||
y := new(big.Int).SetBytes(point[32:]) | ||
for i := range point { | ||
point[i] = 0 | ||
} | ||
for i := range padded { | ||
scalar[i] = 0 | ||
} | ||
if res != 1 { | ||
return nil, nil | ||
} | ||
return x, y | ||
} |
14 changes: 14 additions & 0 deletions
14
crypto/keys/secp256k1/internal/secp256k1/scalar_mult_nocgo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
//go:build gofuzz || !cgo | ||
// +build gofuzz !cgo | ||
|
||
package secp256k1 | ||
|
||
import "math/big" | ||
|
||
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) { | ||
panic("ScalarMult is not available when secp256k1 is built without cgo") | ||
} |
Oops, something went wrong.