Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract secp256k1 to own repo #40

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ linters:
- errname
- errorlint
- execinquery
- exhaustive
#- exhaustive
- exhaustruct
- exportloopref
- forbidigo
Expand All @@ -29,7 +29,7 @@ linters:
#- gochecknoinits
- gocognit
- goconst
#- gocritic
- gocritic
- gocyclo
- godot
- godox
Expand Down Expand Up @@ -130,6 +130,8 @@ linters-settings:
- opinionated
- performance
- style
disabled-checks:
- commentedOutCode
gocyclo:
min-complexity: 15
godox:
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ go 1.20
require (
filippo.io/edwards25519 v1.0.0
filippo.io/nistec v0.0.2
github.com/bytemare/hash2curve v0.1.3
github.com/bytemare/hash2curve v0.2.2
github.com/bytemare/secp256k1 v0.0.0-20230421210201-f2244bd0effa
github.com/gtank/ristretto255 v0.1.2
)

require (
github.com/bytemare/hash v0.1.5 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ filippo.io/nistec v0.0.2 h1:/NIXTUimcHIh0E2DsYucHlICvUisgj28/XEnKSEptUs=
filippo.io/nistec v0.0.2/go.mod h1:84fxC9mi+MhC2AERXI4LSa8cmSVOzrFikg6hZ4IfCyw=
github.com/bytemare/hash v0.1.5 h1:VW+X1YQ2b3chjRFHkRUnO42uclsQjXimdBCPOgIobR4=
github.com/bytemare/hash v0.1.5/go.mod h1:+QmWXTky/2b63ngqM5IYezGydn9UTFDhpX7mLYwYxCA=
github.com/bytemare/hash2curve v0.1.3 h1:BOqV8BF5dF+BbPZgIyoeAVTwd4m7jmw4LwacD1GFBvU=
github.com/bytemare/hash2curve v0.1.3/go.mod h1:Wma3DmJdn8kqiK9j120hkWvC3tQVKS1PyA8ZzyG23BI=
github.com/bytemare/hash2curve v0.2.2 h1:zaGx6Z4/N4Pl9B7aGNtpbZ09vu1NNJGoJRRtHHl8oTw=
github.com/bytemare/hash2curve v0.2.2/go.mod h1:Wma3DmJdn8kqiK9j120hkWvC3tQVKS1PyA8ZzyG23BI=
github.com/bytemare/secp256k1 v0.0.0-20230421210201-f2244bd0effa h1:vrXhOGi25aItUpmJo1ItTZhcv5L7U9/1vdcxL+pKa88=
github.com/bytemare/secp256k1 v0.0.0-20230421210201-f2244bd0effa/go.mod h1:hzquMsr3GXhVcqL9qFX7GGjmcT5dlQldKrArd7tcXHE=
github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc=
github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2 changes: 0 additions & 2 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ func (g Group) init() {
g.initGroup(edwards25519.New)
case Secp256k1:
g.initGroup(secp256k1.New)
case maxID:
fallthrough
default:
panic("group not recognized")
}
Expand Down
34 changes: 17 additions & 17 deletions internal/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func String2Int(s string) big.Int {
panic("invalid string to convert")
}

// Field represents a Gaulois Field.
// Field represents a Galois Field.
type Field struct {
order big.Int
pMinus1div2 big.Int // used in IsSquare
pMinus2 big.Int // used for Field big.Int inversion
exp big.Int
order *big.Int
pMinus1div2 *big.Int // used in IsSquare
pMinus2 *big.Int // used for Field big.Int inversion
exp *big.Int
}

// NewField returns a newly instantiated field for the given prime order.
Expand All @@ -54,10 +54,10 @@ func NewField(prime *big.Int) Field {
exp.Rsh(exp, 2)

return Field{
order: *prime,
pMinus1div2: *pMinus1div2,
pMinus2: *pMinus2,
exp: *exp,
order: prime,
pMinus1div2: pMinus1div2,
pMinus2: pMinus2,
exp: exp,
}
}

Expand All @@ -73,7 +73,7 @@ func (f Field) One() *big.Int {

// Random sets res to a random big.Int in the Field.
func (f Field) Random(res *big.Int) *big.Int {
tmp, err := rand.Int(rand.Reader, &f.order)
tmp, err := rand.Int(rand.Reader, f.order)
if err != nil {
// We can as well not panic and try again in a loop
panic(fmt.Errorf("unexpected error in generating random bytes : %w", err))
Expand All @@ -86,7 +86,7 @@ func (f Field) Random(res *big.Int) *big.Int {

// Order returns the size of the Field.
func (f Field) Order() *big.Int {
return &f.order
return f.order
}

// BitLen of the order.
Expand All @@ -106,18 +106,18 @@ func (f Field) IsZero(e *big.Int) bool {

// Inv sets res to the modular inverse of x mod field order.
func (f Field) Inv(res, x *big.Int) {
f.Exponent(res, x, &f.pMinus2)
f.Exponent(res, x, f.pMinus2)
}

// LegendreSymbol applies the Legendre symbole on (a/p) and returns either {-1, 0, 1} mod field order.
func (f Field) LegendreSymbol(a *big.Int) *big.Int {
var res big.Int
return f.Exponent(&res, a, &f.pMinus1div2)
return f.Exponent(&res, a, f.pMinus1div2)
}

// Exponent returns x^n mod field order.
func (f Field) Exponent(res, x, n *big.Int) *big.Int {
return res.Exp(x, n, &f.order)
return res.Exp(x, n, f.order)
}

// IsSquare returns whether e is a quadratic square.
Expand All @@ -127,12 +127,12 @@ func (f Field) IsSquare(e *big.Int) bool {

// IsEqual returns whether the two fields have the same order.
func (f Field) IsEqual(f2 *Field) bool {
return f.order.Cmp(&f2.order) == 0
return f.order.Cmp(f2.order) == 0
}

// Mod reduces x modulo the field order.
func (f Field) Mod(x *big.Int) *big.Int {
return x.Mod(x, &f.order)
return x.Mod(x, f.order)
}

// Neg sets res to the -x modulo the field order.
Expand Down Expand Up @@ -193,7 +193,7 @@ func (f Field) Sgn0(x *big.Int) int {
}

func (f Field) sqrt3mod4(res, e *big.Int) *big.Int {
return f.Exponent(res, e, &f.exp)
return f.Exponent(res, e, f.exp)
}

// SquareRoot sets res to a square root of e mod the field's order, if such a square root exists.
Expand Down
4 changes: 2 additions & 2 deletions internal/nist/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type Scalar struct {
scalar big.Int
}

func newScalar(field *field.Field) *Scalar {
func newScalar(f *field.Field) *Scalar {
s := &Scalar{
field: field,
field: f,
scalar: big.Int{},
}
s.scalar.Set(s.field.Zero())
Expand Down
173 changes: 0 additions & 173 deletions internal/secp256k1/curve.go

This file was deleted.

Loading