From a8c0b1c33a936549747b68e1d5ea994e7b0767d2 Mon Sep 17 00:00:00 2001 From: Daniel Bourdrez <3641580+bytemare@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:21:34 +0200 Subject: [PATCH] Update (#1) * Update package name, move to new repo, update documentation --------- Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com> --- .github/.golangci.yml | 8 +- .github/CONTRIBUTING.md | 8 +- .github/ISSUE_TEMPLATE/config.yml | 4 +- .github/Makefile | 3 +- .github/SECURITY.md | 2 +- .github/licence-header.tmpl | 2 +- .github/sonar-project.properties | 2 +- LICENSE | 2 +- README.md | 161 ++++++++++++++++-------------- element.go | 7 +- go.mod | 2 +- groups.go | 22 ++-- internal/edwards25519/element.go | 4 +- internal/edwards25519/group.go | 4 +- internal/edwards25519/map.go | 2 +- internal/edwards25519/scalar.go | 4 +- internal/element.go | 2 +- internal/field/field.go | 2 +- internal/group.go | 2 +- internal/misc.go | 2 +- internal/nist/curve.go | 4 +- internal/nist/element.go | 4 +- internal/nist/group.go | 6 +- internal/nist/point.go | 2 +- internal/nist/scalar.go | 6 +- internal/ristretto/element.go | 4 +- internal/ristretto/ristretto.go | 4 +- internal/ristretto/scalar.go | 4 +- internal/scalar.go | 2 +- internal/secp256k1/element.go | 4 +- internal/secp256k1/group.go | 4 +- internal/secp256k1/scalar.go | 4 +- scalar.go | 13 ++- tests/bench_test.go | 2 +- tests/element_test.go | 56 +++++------ tests/encoding_test.go | 16 +-- tests/groups_test.go | 24 ++--- tests/h2c_test.go | 26 ++--- tests/nist_test.go | 2 +- tests/ristretto_hash_test.go | 4 +- tests/ristretto_test.go | 4 +- tests/scalar_test.go | 76 +++++++------- tests/table_test.go | 4 +- tests/utils_test.go | 10 +- 44 files changed, 269 insertions(+), 261 deletions(-) diff --git a/.github/.golangci.yml b/.github/.golangci.yml index 40e4e8a..9cb1800 100644 --- a/.github/.golangci.yml +++ b/.github/.golangci.yml @@ -101,8 +101,8 @@ linters-settings: check-type-assertions: true check-blank: false exclude-functions: - - (*crypto/Element).MarshalBinary - - (*crypto/Scalar).MarshalBinary + - (*ecc/Element).MarshalBinary + - (*ecc/Scalar).MarshalBinary # - io/ioutil.ReadFile # - io.Copy(*bytes.Buffer) # - io.Copy(os.Stdout) @@ -115,7 +115,7 @@ linters-settings: sections: - standard # Standard section: captures all standard packages. - default # Default section: contains all imports that could not be matched to another section type. - - prefix(github.com/bytemare/crypto) # Custom section: groups all imports with the specified Prefix. + - prefix(github.com/bytemare/ecc) # Custom section: groups all imports with the specified Prefix. skip-generated: true # Enable custom order of sections. # If `true`, make the section order the same as the order of `sections`. @@ -147,7 +147,7 @@ linters-settings: gofmt: simplify: true goimports: - local-prefixes: github.com/bytemare/crypto + local-prefixes: github.com/bytemare/ecc gosimple: checks: [ "all" ] govet: diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 76c6639..ad1be8b 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,15 +1,15 @@ # How to contribute to this project -* 🔎 Please ensure your findings have not already been reported by searching on the project repository under [Issues](https://github.com/bytemare/crypto). +* 🔎 Please ensure your findings have not already been reported by searching on the project repository under [Issues](https://github.com/bytemare/ecc). * If you think your findings can be complementary to an existing issue, don't hesitate to join the conversation 😃☕ -* If there's no issue addressing the problem, [open a new one](https://github.com/bytemare/crypto/issues/new). Please be clear in the title and description, and add relevant information. Bonus points if you provide a **code sample** and everything needed to reproduce the issue when expected behaviour is not occurring. +* If there's no issue addressing the problem, [open a new one](https://github.com/bytemare/ecc/issues/new). Please be clear in the title and description, and add relevant information. Bonus points if you provide a **code sample** and everything needed to reproduce the issue when expected behaviour is not occurring. * If possible, use the relevant issue templates. ### Do you have a fix? 🎉 That's awesome! Pull requests are welcome! -* Please [open an issue](https://github.com/bytemare/crypto) beforehand, so we can discuss this. +* Please [open an issue](https://github.com/bytemare/ecc) beforehand, so we can discuss this. * Fork this repo from `main`, and ensure your fork is up-to-date with it when submitting the PR. * If your changes impact the documentation, please update it accordingly. * If you added code that impact tests, please add tests with relevant coverage and test cases. Bonus points for fuzzing. @@ -29,7 +29,7 @@ This project follows the [Benevolent Dictator Governance Model](http://oss-watch ### Licence -By contributing to this project, you agree that your contributions will be licensed under the project's [License](https://github.com/bytemare/crypto/blob/main/LICENSE). +By contributing to this project, you agree that your contributions will be licensed under the project's [License](https://github.com/bytemare/ecc/blob/main/LICENSE). All contributions (including pull requests) must agree to the [Developer Certificate of Origin (DCO) version 1.1](https://developercertificate.org). It states that the contributor has the right to submit the patch for inclusion into the project. Simply submitting a contribution implies this agreement, however, please include the "Signed-off-by" git tag in every commit (this tag is a conventional way to confirm that you agree to the DCO). diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 50d42a0..048e149 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,5 +1,5 @@ blank_issues_enabled: false contact_links: - name: Questions, feature requests, and more 💬 - url: https://github.com/bytemare/crypto/discussions - about: Do you need help? Did you make something with crypto? Do you have an idea? Tell us about it! \ No newline at end of file + url: https://github.com/bytemare/ecc/discussions + about: Do you need help? Did you make something with ecc? Do you have an idea? Tell us about it! \ No newline at end of file diff --git a/.github/Makefile b/.github/Makefile index 7a019ba..92a9e5f 100644 --- a/.github/Makefile +++ b/.github/Makefile @@ -3,8 +3,6 @@ update: @echo "Updating dependencies..." @cd ../ && go get -u ./... @go mod tidy - @echo "Updating Github Actions pins..." - @$(foreach file, $(wildcard workflows/*.yml), pin-github-action $(file);) .PHONY: update-linters update-linters: @@ -12,6 +10,7 @@ update-linters: @go install mvdan.cc/gofumpt@latest @go install github.com/daixiang0/gci@latest @go install github.com/segmentio/golines@latest + @go install github.com/google/addlicense@latest @go install golang.org/x/tools/cmd/goimports@latest @go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest @curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin diff --git a/.github/SECURITY.md b/.github/SECURITY.md index 1d322f0..8f8e52c 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -6,5 +6,5 @@ Only the latest version will be benefit from security fixes. Maintainers of proj ## Reporting a Vulnerability -Vulnerabilities can be reported through Github issues, here: https://github.com/bytemare/crypto/issues. +Vulnerabilities can be reported through Github issues, here: https://github.com/bytemare/ecc/issues. If the issue is sensitive enough that the reporter thinks the discussion needs more confidentiality, we can discuss options there (e.g. On a Security Advisory or per e-mail). diff --git a/.github/licence-header.tmpl b/.github/licence-header.tmpl index 4d3a906..33a3377 100644 --- a/.github/licence-header.tmpl +++ b/.github/licence-header.tmpl @@ -1,6 +1,6 @@ SPDX-License-Identifier: MIT -Copyright (C) 2024 Daniel Bourdrez. All Rights Reserved. +Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree or at diff --git a/.github/sonar-project.properties b/.github/sonar-project.properties index 5e06d5a..82d46de 100644 --- a/.github/sonar-project.properties +++ b/.github/sonar-project.properties @@ -1,5 +1,5 @@ sonar.organization=bytemare -sonar.projectKey=crypto +sonar.projectKey=ecc sonar.sources=. sonar.tests=tests/ sonar.test.exclusions=tests/** diff --git a/LICENSE b/LICENSE index f17d27d..5bf7040 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020-2023 Bytemare +Copyright (c) 2020-2024 Daniel Bourdrez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index afa020f..03a78a7 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,58 @@ -# Prime-order Elliptic Curve Groups -[![CI](https://github.com/bytemare/crypto/actions/workflows/code-scan.yml/badge.svg)](https://github.com/bytemare/crypto/actions/workflows/code-scan.yml) -[![Go Reference](https://pkg.go.dev/badge/github.com/bytemare/crypto.svg)](https://pkg.go.dev/github.com/bytemare/crypto) -[![codecov](https://codecov.io/gh/bytemare/crypto/branch/main/graph/badge.svg?token=5bQfB0OctA)](https://codecov.io/gh/bytemare/crypto) +# Elliptic Curve Groups +[![CI](https://github.com/bytemare/ecc/actions/workflows/code-scan.yml/badge.svg)](https://github.com/bytemare/ecc/actions/workflows/code-scan.yml) +[![Go Reference](https://pkg.go.dev/badge/github.com/bytemare/ecc.svg)](https://pkg.go.dev/github.com/bytemare/ecc) +[![codecov](https://codecov.io/gh/bytemare/ecc/branch/main/graph/badge.svg?token=5bQfB0OctA)](https://codecov.io/gh/bytemare/ecc) ```Go - import "github.com/bytemare/crypto" + import "github.com/bytemare/ecc" ``` -This package exposes abstract operations over opaque prime-order elliptic curve groups and their scalars and elements, +This package exposes abstract operations over opaque (prime-order) elliptic curve groups and their scalars and elements, and support hash-to-curve as per [RFC 9380](https://datatracker.ietf.org/doc/rfc9380). -It is made so you can swap between primitives with no code change and only the Group identifier. -The package serves as an interface to optimized and secure implementations that serve as backends, and to which you -don't need to adapt. - -The following table indexes supported groups with hash-to-curve capability and links each one to the underlying implementations: - -| ID | Name | Backend | -|----|--------------|-------------------------------| -| 1 | Ristretto255 | github.com/gtank/ristretto255 | -| 2 | Decaf448 | not supported | -| 3 | P-256 | filippo.io/nistec | -| 4 | P-384 | filippo.io/nistec | -| 5 | P-521 | filippo.io/nistec | -| 6 | Edwards25519 | filippo.io/edwards25519 | -| 7 | Secp256k1 | github.com/bytemare/secp256k1 | -| 8 | Double-Odd | not yet supported | - -## Prime-order group interface +It makes using different elliptic curves easy, flexible, and without loosing performance or security. You don't have to +care about the parameters. +You can swap between primitives with no code change and only the Group identifier, a byte. +The package is a wrapper to optimized and secure implementations that serve as backends, and to which you +don't need to adapt and learn about. + +The following table shows supported groups with hash-to-curve capability and links each one to the underlying +implementations: + +| ID | Name | Prime-order | Backend | +|----|--------------|-------------------|-------------------------------| +| 1 | Ristretto255 | yes | github.com/gtank/ristretto255 | +| 2 | Decaf448 | not supported | not supported | +| 3 | P-256 | yes | filippo.io/nistec | +| 4 | P-384 | yes | filippo.io/nistec | +| 5 | P-521 | yes | filippo.io/nistec | +| 6 | Edwards25519 | no | filippo.io/edwards25519 | +| 7 | Secp256k1 | yes | github.com/bytemare/secp256k1 | +| 9 | Curve25519 | not yet supported | not yet supported | +| 8 | Double-Odd | not yet supported | not yet supported | + +## Group interface This package exposes types that can handle different implementations under the hood, internally using an interface to the group and its scalars and elements, but you don't need to instantiate or implement anything. Just use the type in the top package. -### Group interface +### Group ```Go // Group abstracts operations in a prime-order group. type Group interface { - NewScalar() Scalar - NewElement() Element - Base() Element + NewScalar() Scalar + NewElement() Element + Base() Element HashFunc() crypto.Hash - HashToScalar(input, dst []byte) Scalar - HashToGroup(input, dst []byte) Element - EncodeToGroup(input, dst []byte) Element - Ciphersuite() string - ScalarLength() int - ElementLength() int - Order() string + HashToScalar(input, dst []byte) Scalar + HashToGroup(input, dst []byte) Element + EncodeToGroup(input, dst []byte) Element + Ciphersuite() string + ScalarLength() int + ElementLength() int + Order() []byte } ``` @@ -57,27 +61,31 @@ type Group interface { ```Go // Scalar interface abstracts common operations on scalars in a prime-order Group. type Scalar interface { - Zero() Scalar - One() Scalar - Random() Scalar - Add(Scalar) Scalar - Subtract(Scalar) Scalar - Multiply(Scalar) Scalar - Pow(Scalar) Scalar - Invert() Scalar - Equal(Scalar) int - LessOrEqual(Scalar) int - IsZero() bool - Set(Scalar) Scalar - SetUInt64(uint64) Scalar - UInt64() (uint64, error) - Copy() Scalar - Encode() []byte - Decode(in []byte) error + Group() Group + Zero() Scalar + One() Scalar + MinusOne() Scalar + Random() Scalar + Add(Scalar) Scalar + Subtract(Scalar) Scalar + Multiply(Scalar) Scalar + Pow(Scalar) Scalar + Invert() Scalar + Equal(Scalar) int + LessOrEqual(Scalar) bool + IsZero() bool + Set(Scalar) Scalar + SetUInt64(uint64) Scalar + UInt64() (uint64, error) + Copy() Scalar + Encode() []byte + Decode(in []byte) error Hex() string HexDecode([]byte) error - encoding.BinaryMarshaler - encoding.BinaryUnmarshaler + MarshalJSON() + UnmarshalJSON() + encoding.BinaryMarshaler + encoding.BinaryUnmarshaler } ``` @@ -85,34 +93,37 @@ type Scalar interface { ```Go // Element interface abstracts common operations on an Element in a prime-order Group. type Element interface { - Base() Element - Identity() Element - Add(Element) Element - Double() Element - Negate() Element - Subtract(Element) Element - Multiply(Scalar) Element - Equal(element Element) int - IsIdentity() bool - Set(Element) Element - Copy() Element - Encode() []byte - XCoordinate() []byte - Decode(data []byte) error - Hex() string - HexDecode([]byte) error - encoding.BinaryMarshaler - encoding.BinaryUnmarshaler + Group() Group + Base() Element + Identity() Element + Add(Element) Element + Double() Element + Negate() Element + Subtract(Element) Element + Multiply(Scalar) Element + Equal(element Element) int + IsIdentity() bool + Set(Element) Element + Copy() Element + Encode() []byte + XCoordinate() []byte + Decode(data []byte) error + Hex() string + HexDecode([]byte) error + MarshalJSON() ([]byte, error) + UnmarshalJSON(data []byte) error + encoding.BinaryMarshaler + encoding.BinaryUnmarshaler } ``` -## Documentation [![Go Reference](https://pkg.go.dev/badge/github.com/bytemare/crypto.svg)](https://pkg.go.dev/github.com/bytemare/crypto) +## Documentation [![Go Reference](https://pkg.go.dev/badge/github.com/bytemare/ecc.svg)](https://pkg.go.dev/github.com/bytemare/ecc) -You can find the documentation and usage examples in [the package doc](https://pkg.go.dev/github.com/bytemare/crypto) and [the project wiki](https://github.com/bytemare/crypto/wiki) . +You can find the documentation and usage examples in [the package doc](https://pkg.go.dev/github.com/bytemare/ecc) and [the project wiki](https://github.com/bytemare/ecc/wiki) . ## Versioning -[SemVer](https://semver.org) is used for versioning. For the versions available, see the [tags on the repository](https://github.com/bytemare/crypto/tags). +[SemVer](https://semver.org) is used for versioning. For the versions available, see the [tags on the repository](https://github.com/bytemare/ecc/tags). ## Contributing diff --git a/element.go b/element.go index 6b37af8..2a30be2 100644 --- a/element.go +++ b/element.go @@ -1,19 +1,18 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at // https://spdx.org/licenses/MIT.html -// Package crypto exposes a prime-order elliptic curve groups with additional hash-to-curve operations. -package crypto +package ecc import ( "fmt" "strings" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Element represents an element on the curve of the prime-order group. diff --git a/go.mod b/go.mod index ea1bab6..473a8d6 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/bytemare/crypto +module github.com/bytemare/ecc go 1.23.1 diff --git a/groups.go b/groups.go index c84ce33..90098fc 100644 --- a/groups.go +++ b/groups.go @@ -1,16 +1,16 @@ // SPDX-License-Group: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at // https://spdx.org/licenses/MIT.html -// Package crypto exposes a prime-order elliptic curve groups with additional hash-to-curve operations. +// Package ecc exposes a prime-order elliptic curve groups with additional hash-to-curve operations. // // It implements the latest hash-to-curve specification to date // (https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/). -package crypto +package ecc import ( "crypto" @@ -18,11 +18,11 @@ import ( "fmt" "sync" - "github.com/bytemare/crypto/internal" - "github.com/bytemare/crypto/internal/edwards25519" - "github.com/bytemare/crypto/internal/nist" - "github.com/bytemare/crypto/internal/ristretto" - "github.com/bytemare/crypto/internal/secp256k1" + "github.com/bytemare/ecc/internal" + "github.com/bytemare/ecc/internal/edwards25519" + "github.com/bytemare/ecc/internal/nist" + "github.com/bytemare/ecc/internal/ristretto" + "github.com/bytemare/ecc/internal/secp256k1" ) // Group identifies prime-order groups over elliptic curves with hash-to-group operations. @@ -47,8 +47,8 @@ const ( // Edwards25519Sha512 identifies the Edwards25519 group with SHA2-512 hash-to-group hashing. Edwards25519Sha512 - // Secp256k1 identifies the SECp256k1 group with SHA2-256 hash-to-group hashing. - Secp256k1 + // Secp256k1Sha256 identifies the SECp256k1 group with SHA2-256 hash-to-group hashing. + Secp256k1Sha256 maxID @@ -171,7 +171,7 @@ func (g Group) init() { g.initGroup(nist.P521) case Edwards25519Sha512: g.initGroup(edwards25519.New) - case Secp256k1: + case Secp256k1Sha256: g.initGroup(secp256k1.New) default: panic("group not recognized") diff --git a/internal/edwards25519/element.go b/internal/edwards25519/element.go index a8c9822..3c321ab 100644 --- a/internal/edwards25519/element.go +++ b/internal/edwards25519/element.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( ed "filippo.io/edwards25519" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Element implements the Element interface for the Edwards25519 group element. diff --git a/internal/edwards25519/group.go b/internal/edwards25519/group.go index 0ab5fb3..25bae04 100644 --- a/internal/edwards25519/group.go +++ b/internal/edwards25519/group.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -15,7 +15,7 @@ import ( ed "filippo.io/edwards25519" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const ( diff --git a/internal/edwards25519/map.go b/internal/edwards25519/map.go index 0fa4e27..1f793ba 100644 --- a/internal/edwards25519/map.go +++ b/internal/edwards25519/map.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/edwards25519/scalar.go b/internal/edwards25519/scalar.go index 06f8bab..706eaf0 100644 --- a/internal/edwards25519/scalar.go +++ b/internal/edwards25519/scalar.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -16,7 +16,7 @@ import ( ed "filippo.io/edwards25519" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const inputLength = 64 diff --git a/internal/element.go b/internal/element.go index afe4798..d31dac5 100644 --- a/internal/element.go +++ b/internal/element.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/field/field.go b/internal/field/field.go index cc5dd9f..7aa1e7d 100644 --- a/internal/field/field.go +++ b/internal/field/field.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/group.go b/internal/group.go index e00e531..da28ef4 100644 --- a/internal/group.go +++ b/internal/group.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/misc.go b/internal/misc.go index 829e354..38d4f5d 100644 --- a/internal/misc.go +++ b/internal/misc.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/nist/curve.go b/internal/nist/curve.go index 7655790..0334d9c 100644 --- a/internal/nist/curve.go +++ b/internal/nist/curve.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "github.com/bytemare/hash2curve" - "github.com/bytemare/crypto/internal/field" + "github.com/bytemare/ecc/internal/field" ) type mapping struct { diff --git a/internal/nist/element.go b/internal/nist/element.go index 53a2d9f..4f6fb67 100644 --- a/internal/nist/element.go +++ b/internal/nist/element.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -16,7 +16,7 @@ import ( "filippo.io/nistec" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const ( diff --git a/internal/nist/group.go b/internal/nist/group.go index 710cd28..22ad2d9 100644 --- a/internal/nist/group.go +++ b/internal/nist/group.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -18,8 +18,8 @@ import ( "filippo.io/nistec" "github.com/bytemare/hash2curve" - "github.com/bytemare/crypto/internal" - "github.com/bytemare/crypto/internal/field" + "github.com/bytemare/ecc/internal" + "github.com/bytemare/ecc/internal/field" ) const ( diff --git a/internal/nist/point.go b/internal/nist/point.go index 19721d7..8a6c6de 100644 --- a/internal/nist/point.go +++ b/internal/nist/point.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/nist/scalar.go b/internal/nist/scalar.go index 806563e..7091047 100644 --- a/internal/nist/scalar.go +++ b/internal/nist/scalar.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -15,8 +15,8 @@ import ( "fmt" "math/big" - "github.com/bytemare/crypto/internal" - "github.com/bytemare/crypto/internal/field" + "github.com/bytemare/ecc/internal" + "github.com/bytemare/ecc/internal/field" ) // Scalar implements the Scalar interface for group scalars. diff --git a/internal/ristretto/element.go b/internal/ristretto/element.go index d330aa4..fbb35c0 100644 --- a/internal/ristretto/element.go +++ b/internal/ristretto/element.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -15,7 +15,7 @@ import ( "github.com/gtank/ristretto255" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Element implements the Element interface for the Ristretto255 group element. diff --git a/internal/ristretto/ristretto.go b/internal/ristretto/ristretto.go index 828ac3a..db6dd08 100644 --- a/internal/ristretto/ristretto.go +++ b/internal/ristretto/ristretto.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -16,7 +16,7 @@ import ( "github.com/bytemare/hash2curve" "github.com/gtank/ristretto255" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const ( diff --git a/internal/ristretto/scalar.go b/internal/ristretto/scalar.go index fea20ff..85afe32 100644 --- a/internal/ristretto/scalar.go +++ b/internal/ristretto/scalar.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -16,7 +16,7 @@ import ( "github.com/gtank/ristretto255" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const canonicalEncodingLength = 32 diff --git a/internal/scalar.go b/internal/scalar.go index ea665f7..72f6077 100644 --- a/internal/scalar.go +++ b/internal/scalar.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/internal/secp256k1/element.go b/internal/secp256k1/element.go index 90ed786..42b8037 100644 --- a/internal/secp256k1/element.go +++ b/internal/secp256k1/element.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C)2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C)2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in theg // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "github.com/bytemare/secp256k1" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Element implements the Element interface for the Secp256k1 group element. diff --git a/internal/secp256k1/group.go b/internal/secp256k1/group.go index 410a967..0486865 100644 --- a/internal/secp256k1/group.go +++ b/internal/secp256k1/group.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C)2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C)2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "github.com/bytemare/secp256k1" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) const ( diff --git a/internal/secp256k1/scalar.go b/internal/secp256k1/scalar.go index 760b8c3..104819d 100644 --- a/internal/secp256k1/scalar.go +++ b/internal/secp256k1/scalar.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C)2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C)2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "github.com/bytemare/secp256k1" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Scalar implements the Scalar interface for Edwards25519 group scalars. diff --git a/scalar.go b/scalar.go index 72d2949..cd3c717 100644 --- a/scalar.go +++ b/scalar.go @@ -1,19 +1,18 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at // https://spdx.org/licenses/MIT.html -// Package crypto exposes a prime-order elliptic curve groups with additional hash-to-curve operations. -package crypto +package ecc import ( "fmt" "strings" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc/internal" ) // Scalar represents a scalar in the prime-order group. @@ -116,12 +115,12 @@ func (s *Scalar) Equal(scalar *Scalar) bool { } // LessOrEqual returns 1 if s <= scalar, and 0 otherwise. -func (s *Scalar) LessOrEqual(scalar *Scalar) int { +func (s *Scalar) LessOrEqual(scalar *Scalar) bool { if scalar == nil { - return 0 + return false } - return s.Scalar.LessOrEqual(scalar.Scalar) + return s.Scalar.LessOrEqual(scalar.Scalar) == 1 } // IsZero returns whether the scalar is 0. diff --git a/tests/bench_test.go b/tests/bench_test.go index e41816f..267113f 100644 --- a/tests/bench_test.go +++ b/tests/bench_test.go @@ -1,6 +1,6 @@ // SPDX-License-Group: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/tests/element_test.go b/tests/element_test.go index 62bc48d..7c0732f 100644 --- a/tests/element_test.go +++ b/tests/element_test.go @@ -1,6 +1,6 @@ // SPDX-License-Group: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -15,8 +15,8 @@ import ( "math/big" "testing" - "github.com/bytemare/crypto" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc" + "github.com/bytemare/ecc/internal" ) const ( @@ -26,7 +26,7 @@ const ( errWrongGroup = "wrong group" ) -func testElementCopySet(t *testing.T, element, other *crypto.Element) { +func testElementCopySet(t *testing.T, element, other *ecc.Element) { // Verify they don't point to the same thing if &element == &other { t.Fatalf("Pointer to the same scalar") @@ -81,19 +81,19 @@ func TestElement_Set(t *testing.T) { } func TestElement_WrongInput(t *testing.T) { - exec := func(f func(*crypto.Element) *crypto.Element, arg *crypto.Element) func() { + exec := func(f func(*ecc.Element) *ecc.Element, arg *ecc.Element) func() { return func() { _ = f(arg) } } - equal := func(f func(*crypto.Element) bool, arg *crypto.Element) func() { + equal := func(f func(*ecc.Element) bool, arg *ecc.Element) func() { return func() { f(arg) } } - mult := func(f func(*crypto.Scalar) *crypto.Element, arg *crypto.Scalar) func() { + mult := func(f func(*ecc.Scalar) *ecc.Element, arg *ecc.Scalar) func() { return func() { f(arg) } @@ -101,14 +101,14 @@ func TestElement_WrongInput(t *testing.T) { testAllGroups(t, func(group *testGroup) { element := group.group.NewElement() - var alternativeGroup crypto.Group + var alternativeGroup ecc.Group switch group.group { // The following is arbitrary, and simply aims at confusing identifiers - case crypto.Ristretto255Sha512, crypto.Edwards25519Sha512: - alternativeGroup = crypto.P256Sha256 - case crypto.P256Sha256, crypto.P384Sha384, crypto.P521Sha512, crypto.Secp256k1: - alternativeGroup = crypto.Ristretto255Sha512 + case ecc.Ristretto255Sha512, ecc.Edwards25519Sha512: + alternativeGroup = ecc.P256Sha256 + case ecc.P256Sha256, ecc.P384Sha384, ecc.P521Sha512, ecc.Secp256k1Sha256: + alternativeGroup = ecc.Ristretto255Sha512 default: t.Fatalf("Invalid group id %d", group.group) } @@ -136,7 +136,7 @@ func TestElement_WrongInput(t *testing.T) { // Specifically test Ristretto if err := testPanic(errWrongGroup, internal.ErrCastScalar, - mult(crypto.Ristretto255Sha512.NewElement().Multiply, crypto.P384Sha384.NewScalar())); err != nil { + mult(ecc.Ristretto255Sha512.NewElement().Multiply, ecc.P384Sha384.NewScalar())); err != nil { t.Fatal(err) } } @@ -178,17 +178,17 @@ func TestElement_Decode_OutOfBounds(t *testing.T) { unmarshallBinaryErr := "element UnmarshalBinary: " errMessage := "" switch group.group { - case crypto.Ristretto255Sha512: + case ecc.Ristretto255Sha512: errMessage = "invalid Ristretto encoding" - case crypto.P256Sha256: + case ecc.P256Sha256: errMessage = "invalid P256 element encoding" - case crypto.P384Sha384: + case ecc.P384Sha384: errMessage = "invalid P384Element encoding" - case crypto.P521Sha512: + case ecc.P521Sha512: errMessage = "invalid P521Element encoding" - case crypto.Edwards25519Sha512: + case ecc.Edwards25519Sha512: errMessage = "edwards25519: invalid point encoding" - case crypto.Secp256k1: + case ecc.Secp256k1Sha256: errMessage = "invalid point encoding" } @@ -208,9 +208,9 @@ func TestElement_Decode_OutOfBounds(t *testing.T) { x.Add(x, big.NewInt(1)) switch group.group { - case crypto.Ristretto255Sha512, crypto.Edwards25519Sha512: + case ecc.Ristretto255Sha512, ecc.Edwards25519Sha512: x.FillBytes(encoded) - case crypto.P256Sha256, crypto.P384Sha384, crypto.P521Sha512, crypto.Secp256k1: + case ecc.P256Sha256, ecc.P384Sha384, ecc.P521Sha512, ecc.Secp256k1Sha256: encoded[0] = byte(2 | y.Bit(0)&1) x.FillBytes(encoded[1:]) default: @@ -324,7 +324,7 @@ func TestElement_Arithmetic(t *testing.T) { }) } -func elementTestEqual(t *testing.T, g crypto.Group) { +func elementTestEqual(t *testing.T, g ecc.Group) { base := g.Base() base2 := g.Base() @@ -343,7 +343,7 @@ func elementTestEqual(t *testing.T, g crypto.Group) { } } -func elementTestAdd(t *testing.T, g crypto.Group) { +func elementTestAdd(t *testing.T, g ecc.Group) { // Verify whether add yields the same element when given nil base := g.Base() cpy := base.Copy() @@ -394,7 +394,7 @@ func elementTestAdd(t *testing.T, g crypto.Group) { } } -func elementTestNegate(t *testing.T, g crypto.Group) { +func elementTestNegate(t *testing.T, g ecc.Group) { // 0 = -0 id := g.NewElement().Identity() negId := g.NewElement().Identity().Negate() @@ -421,7 +421,7 @@ func elementTestNegate(t *testing.T, g crypto.Group) { } } -func elementTestDouble(t *testing.T, g crypto.Group) { +func elementTestDouble(t *testing.T, g ecc.Group) { // Verify whether double works like adding base := g.Base() double := g.Base().Add(g.Base()) @@ -436,7 +436,7 @@ func elementTestDouble(t *testing.T, g crypto.Group) { } } -func elementTestSubstract(t *testing.T, g crypto.Group) { +func elementTestSubstract(t *testing.T, g ecc.Group) { base := g.Base() // Verify whether subtracting yields the same element when given nil. @@ -451,7 +451,7 @@ func elementTestSubstract(t *testing.T, g crypto.Group) { } } -func elementTestMultiply(t *testing.T, g crypto.Group) { +func elementTestMultiply(t *testing.T, g ecc.Group) { scalar := g.NewScalar() // base = base * 1 @@ -488,7 +488,7 @@ func elementTestMultiply(t *testing.T, g crypto.Group) { } } -func elementTestIdentity(t *testing.T, g crypto.Group) { +func elementTestIdentity(t *testing.T, g ecc.Group) { id := g.NewElement() if !id.IsIdentity() { t.Fatal(errExpectedIdentity) diff --git a/tests/encoding_test.go b/tests/encoding_test.go index 5c16d3c..9196b14 100644 --- a/tests/encoding_test.go +++ b/tests/encoding_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2024 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -18,16 +18,16 @@ import ( "strings" "testing" - "github.com/bytemare/crypto" + "github.com/bytemare/ecc" ) type serde interface { Encode() []byte Decode(data []byte) error - MarshalJSON() ([]byte, error) - UnmarshalJSON(data []byte) error Hex() string DecodeHex(h string) error + MarshalJSON() ([]byte, error) + UnmarshalJSON(data []byte) error encoding.BinaryMarshaler encoding.BinaryUnmarshaler } @@ -129,7 +129,7 @@ func (t *encodingTest) run() error { return nil } -func testScalarEncodings(g crypto.Group, f makeEncodeTest) error { +func testScalarEncodings(g ecc.Group, f makeEncodeTest) error { source, receiver := g.NewScalar().Random(), g.NewScalar() t := newEncodingTest(source, receiver) @@ -144,7 +144,7 @@ func testScalarEncodings(g crypto.Group, f makeEncodeTest) error { return nil } -func testElementEncodings(g crypto.Group, f makeEncodeTest) error { +func testElementEncodings(g ecc.Group, f makeEncodeTest) error { source, receiver := g.Base(), g.NewElement() t := newEncodingTest(source, receiver) @@ -159,7 +159,7 @@ func testElementEncodings(g crypto.Group, f makeEncodeTest) error { return nil } -func TestEncoding(t *testing.T) { +func TestScalar_Encoding(t *testing.T) { testAllGroups(t, func(group *testGroup) { g := group.group testDecodeEmpty(t, group.group.NewScalar().Random()) @@ -171,7 +171,7 @@ func TestEncoding(t *testing.T) { }) } -func TestEncoding_Element(t *testing.T) { +func TestElement_Encoding(t *testing.T) { testAllGroups(t, func(group *testGroup) { g := group.group testDecodeEmpty(t, group.group.Base()) diff --git a/tests/groups_test.go b/tests/groups_test.go index a58a9b9..f366d00 100644 --- a/tests/groups_test.go +++ b/tests/groups_test.go @@ -1,6 +1,6 @@ // SPDX-License-Group: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "fmt" "testing" - "github.com/bytemare/crypto" + "github.com/bytemare/ecc" ) const consideredAvailableFmt = "%v is considered available when it must not" @@ -30,12 +30,12 @@ func TestAvailability(t *testing.T) { func TestNonAvailability(t *testing.T) { errInvalidID := errors.New("invalid group identifier") - oob := crypto.Group(0) + oob := ecc.Group(0) if oob.Available() { t.Errorf(consideredAvailableFmt, oob) } - d := crypto.Group(2) // decaf448 + d := ecc.Group(2) // decaf448 if d.Available() { t.Errorf(consideredAvailableFmt, d) } @@ -45,7 +45,7 @@ func TestNonAvailability(t *testing.T) { t.Fatal(err) } - oob = crypto.Secp256k1 + 1 + oob = ecc.Secp256k1Sha256 + 1 if oob.Available() { t.Errorf(consideredAvailableFmt, oob) } @@ -75,13 +75,13 @@ func TestGroup_Base(t *testing.T) { func TestDST(t *testing.T) { app := "app" version := uint8(1) - tests := map[crypto.Group]string{ - crypto.Ristretto255Sha512: app + "-V01-CS01-", - crypto.P256Sha256: app + "-V01-CS03-", - crypto.P384Sha384: app + "-V01-CS04-", - crypto.P521Sha512: app + "-V01-CS05-", - crypto.Edwards25519Sha512: app + "-V01-CS06-", - crypto.Secp256k1: app + "-V01-CS07-", + tests := map[ecc.Group]string{ + ecc.Ristretto255Sha512: app + "-V01-CS01-", + ecc.P256Sha256: app + "-V01-CS03-", + ecc.P384Sha384: app + "-V01-CS04-", + ecc.P521Sha512: app + "-V01-CS05-", + ecc.Edwards25519Sha512: app + "-V01-CS06-", + ecc.Secp256k1Sha256: app + "-V01-CS07-", } testAllGroups(t, func(group *testGroup) { diff --git a/tests/h2c_test.go b/tests/h2c_test.go index 69449e4..042ab57 100644 --- a/tests/h2c_test.go +++ b/tests/h2c_test.go @@ -1,6 +1,6 @@ // SPDX-License-Group: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -22,8 +22,8 @@ import ( "filippo.io/edwards25519" "filippo.io/edwards25519/field" - "github.com/bytemare/crypto" - edwards255192 "github.com/bytemare/crypto/internal/edwards25519" + "github.com/bytemare/ecc" + edwards255192 "github.com/bytemare/ecc/internal/edwards25519" ) const hashToCurveVectorsFileLocation = "h2c" @@ -32,7 +32,7 @@ type h2cVectors struct { Ciphersuite string `json:"ciphersuite"` Dst string `json:"dst"` Vectors []h2cVector `json:"vectors"` - group crypto.Group + group ecc.Group } type h2cVector struct { @@ -53,13 +53,13 @@ type h2cVector struct { U []string `json:"u"` } -func ecFromGroup(g crypto.Group) elliptic.Curve { +func ecFromGroup(g ecc.Group) elliptic.Curve { switch g { - case crypto.P256Sha256: + case ecc.P256Sha256: return elliptic.P256() - case crypto.P384Sha384: + case ecc.P384Sha384: return elliptic.P384() - case crypto.P521Sha512: + case ecc.P521Sha512: return elliptic.P521() default: panic("invalid nist group") @@ -122,14 +122,14 @@ func (v *h2cVector) run(t *testing.T) { var expected string switch v.group { - case crypto.P256Sha256, crypto.P384Sha384, crypto.P521Sha512: + case ecc.P256Sha256, ecc.P384Sha384, ecc.P521Sha512: e := ecFromGroup(v.group) x, y := vectorToBig(v.P.X, v.P.Y) expected = hex.EncodeToString(elliptic.MarshalCompressed(e, x, y)) - case crypto.Edwards25519Sha512: + case ecc.Edwards25519Sha512: p := vectorToEdwards25519(t, v.P.X, v.P.Y) expected = hex.EncodeToString(p.Bytes()) - case crypto.Secp256k1: + case ecc.Secp256k1Sha256: expected = hex.EncodeToString(vectorToSecp256k1(v.P.X, v.P.Y)) } @@ -149,7 +149,7 @@ func (v *h2cVector) run(t *testing.T) { } } -func verifyEncoding(p *crypto.Element, function, expected string) error { +func verifyEncoding(p *ecc.Element, function, expected string) error { if p.Hex() != expected { return fmt.Errorf("Unexpected %s output.\n\tExpected %q\n\tgot %q", function, @@ -169,7 +169,7 @@ func (v *h2cVectors) runCiphersuite(t *testing.T) { } func TestHashToGroupVectors(t *testing.T) { - getGroup := func(ciphersuite string) (crypto.Group, bool) { + getGroup := func(ciphersuite string) (ecc.Group, bool) { for _, group := range testTable { if group.h2c == ciphersuite || group.e2c == ciphersuite { return group.group, true diff --git a/tests/nist_test.go b/tests/nist_test.go index 7a1f185..3f93482 100644 --- a/tests/nist_test.go +++ b/tests/nist_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at diff --git a/tests/ristretto_hash_test.go b/tests/ristretto_hash_test.go index e154dde..21cfb10 100644 --- a/tests/ristretto_hash_test.go +++ b/tests/ristretto_hash_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "fmt" "testing" - "github.com/bytemare/crypto/internal/ristretto" + "github.com/bytemare/ecc/internal/ristretto" ) type ristrettoH2gTestBytes struct { diff --git a/tests/ristretto_test.go b/tests/ristretto_test.go index 4033a29..dc3816a 100644 --- a/tests/ristretto_test.go +++ b/tests/ristretto_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "encoding/hex" "testing" - "github.com/bytemare/crypto/internal/ristretto" + "github.com/bytemare/ecc/internal/ristretto" ) const ( diff --git a/tests/scalar_test.go b/tests/scalar_test.go index d6dd2c3..ba6ca94 100644 --- a/tests/scalar_test.go +++ b/tests/scalar_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -18,8 +18,8 @@ import ( "slices" "testing" - "github.com/bytemare/crypto" - "github.com/bytemare/crypto/internal" + "github.com/bytemare/ecc" + "github.com/bytemare/ecc/internal" ) func TestScalar_Group(t *testing.T) { @@ -32,13 +32,13 @@ func TestScalar_Group(t *testing.T) { } func TestScalar_WrongInput(t *testing.T) { - exec := func(f func(*crypto.Scalar) *crypto.Scalar, arg *crypto.Scalar) func() { + exec := func(f func(*ecc.Scalar) *ecc.Scalar, arg *ecc.Scalar) func() { return func() { f(arg) } } - equal := func(f func(*crypto.Scalar) bool, arg *crypto.Scalar) func() { + equal := func(f func(*ecc.Scalar) bool, arg *ecc.Scalar) func() { return func() { f(arg) } @@ -46,18 +46,18 @@ func TestScalar_WrongInput(t *testing.T) { testAllGroups(t, func(group *testGroup) { scalar := group.group.NewScalar() - methods := []func(arg *crypto.Scalar) *crypto.Scalar{ + methods := []func(arg *ecc.Scalar) *ecc.Scalar{ scalar.Add, scalar.Subtract, scalar.Multiply, scalar.Set, } - var wrongGroup crypto.Group + var wrongGroup ecc.Group switch group.group { // The following is arbitrary, and simply aims at confusing identifiers - case crypto.Ristretto255Sha512, crypto.Edwards25519Sha512, crypto.Secp256k1: - wrongGroup = crypto.P256Sha256 - case crypto.P256Sha256, crypto.P384Sha384, crypto.P521Sha512: - wrongGroup = crypto.Ristretto255Sha512 + case ecc.Ristretto255Sha512, ecc.Edwards25519Sha512, ecc.Secp256k1Sha256: + wrongGroup = ecc.P256Sha256 + case ecc.P256Sha256, ecc.P384Sha384, ecc.P521Sha512: + wrongGroup = ecc.Ristretto255Sha512 // Add a special test for nist groups, using a different field wrongfield := ((group.group + 1) % 3) + 3 @@ -80,7 +80,7 @@ func TestScalar_WrongInput(t *testing.T) { }) } -func testScalarCopySet(t *testing.T, scalar, other *crypto.Scalar) { +func testScalarCopySet(t *testing.T, scalar, other *ecc.Scalar) { // Verify they don't point to the same thing if &scalar == &other { t.Fatalf("Pointer to the same scalar") @@ -125,7 +125,7 @@ func TestScalar_Set(t *testing.T) { }) } -func parseScalar(s *crypto.Scalar) ([]byte, bool) { +func parseScalar(s *ecc.Scalar) ([]byte, bool) { b := s.Encode() b3 := b[8:] b4 := byte(0) @@ -135,7 +135,7 @@ func parseScalar(s *crypto.Scalar) ([]byte, bool) { return b[:8], b4 == 0 } -func testScalarUInt64(t *testing.T, s *crypto.Scalar, expectedValue uint64, expectedError error) { +func testScalarUInt64(t *testing.T, s *ecc.Scalar, expectedValue uint64, expectedError error) { i, err := s.UInt64() if err == nil { @@ -194,7 +194,7 @@ func TestScalar_SetUInt64(t *testing.T) { ref := make([]byte, group.group.ScalarLength()) switch group.group { - case crypto.Ristretto255Sha512, crypto.Edwards25519Sha512: + case ecc.Ristretto255Sha512, ecc.Edwards25519Sha512: binary.LittleEndian.PutUint64(ref, math.MaxUint64) default: binary.BigEndian.PutUint64(ref[group.group.ScalarLength()-8:], math.MaxUint64) @@ -274,7 +274,7 @@ func TestScalar_Arithmetic(t *testing.T) { }) } -func scalarTestZero(t *testing.T, g crypto.Group) { +func scalarTestZero(t *testing.T, g ecc.Group) { zero := g.NewScalar() if !zero.IsZero() { t.Fatal("expected zero scalar") @@ -296,7 +296,7 @@ func scalarTestZero(t *testing.T, g crypto.Group) { } } -func scalarTestOne(t *testing.T, g crypto.Group) { +func scalarTestOne(t *testing.T, g ecc.Group) { one := g.NewScalar().One() m := one.Copy() if !one.Equal(m.Multiply(m)) { @@ -304,7 +304,7 @@ func scalarTestOne(t *testing.T, g crypto.Group) { } } -func scalarTestMinusOne(t *testing.T, g crypto.Group) { +func scalarTestMinusOne(t *testing.T, g ecc.Group) { m1 := g.NewScalar().MinusOne() one := g.NewScalar().One() if !m1.Add(one).IsZero() { @@ -312,14 +312,14 @@ func scalarTestMinusOne(t *testing.T, g crypto.Group) { } } -func scalarTestRandom(t *testing.T, g crypto.Group) { +func scalarTestRandom(t *testing.T, g ecc.Group) { r := g.NewScalar().Random() if r.Equal(g.NewScalar().Zero()) { t.Fatalf("random scalar is zero: %v", r.Hex()) } } -func scalarTestEqual(t *testing.T, g crypto.Group) { +func scalarTestEqual(t *testing.T, g ecc.Group) { zero := g.NewScalar().Zero() zero2 := g.NewScalar().Zero() @@ -343,36 +343,36 @@ func scalarTestEqual(t *testing.T, g crypto.Group) { } } -func scalarTestLessOrEqual(t *testing.T, g crypto.Group) { +func scalarTestLessOrEqual(t *testing.T, g ecc.Group) { zero := g.NewScalar().Zero() one := g.NewScalar().One() two := g.NewScalar().One().Add(one) - if g.NewScalar().Random().LessOrEqual(nil) != 0 { + if g.NewScalar().Random().LessOrEqual(nil) { t.Fatal(errUnExpectedEquality) } - if zero.LessOrEqual(one) != 1 { + if !zero.LessOrEqual(one) { t.Fatal("expected 0 < 1") } - if one.LessOrEqual(two) != 1 { + if !one.LessOrEqual(two) { t.Fatal("expected 1 < 2") } - if one.LessOrEqual(zero) == 1 { + if one.LessOrEqual(zero) { t.Fatal("expected 1 > 0") } - if two.LessOrEqual(one) == 1 { + if two.LessOrEqual(one) { t.Fatal("expected 2 > 1") } - if two.LessOrEqual(two) != 1 { + if !two.LessOrEqual(two) { t.Fatal("expected 2 == 2") } - var r, s *crypto.Scalar + var r, s *ecc.Scalar for { s = g.NewScalar().Random() r = s.Add(g.NewScalar().One()) @@ -381,12 +381,12 @@ func scalarTestLessOrEqual(t *testing.T, g crypto.Group) { } } - if s.LessOrEqual(r) != 1 { + if !s.LessOrEqual(r) { t.Fatalf("expected s < s + 1:") } } -func scalarTestAdd(t *testing.T, g crypto.Group) { +func scalarTestAdd(t *testing.T, g ecc.Group) { r := g.NewScalar().Random() cpy := r.Copy() if !r.Add(nil).Equal(cpy) { @@ -394,7 +394,7 @@ func scalarTestAdd(t *testing.T, g crypto.Group) { } } -func scalarTestSubtract(t *testing.T, g crypto.Group) { +func scalarTestSubtract(t *testing.T, g ecc.Group) { r := g.NewScalar().Random() cpy := r.Copy() if !r.Subtract(nil).Equal(cpy) { @@ -402,14 +402,14 @@ func scalarTestSubtract(t *testing.T, g crypto.Group) { } } -func scalarTestMultiply(t *testing.T, g crypto.Group) { +func scalarTestMultiply(t *testing.T, g ecc.Group) { s := g.NewScalar().Random() if !s.Multiply(nil).IsZero() { t.Fatal("expected zero") } } -func scalarTestPow(t *testing.T, g crypto.Group) { +func scalarTestPow(t *testing.T, g ecc.Group) { // s**nil = 1 s := g.NewScalar().Random() if !s.Pow(nil).Equal(g.NewScalar().One()) { @@ -495,7 +495,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { switch g { // These are in little-endian - case crypto.Ristretto255Sha512, crypto.Edwards25519Sha512: + case ecc.Ristretto255Sha512, ecc.Edwards25519Sha512: e := s.Encode() for i, j := 0, len(e)-1; i < j; i++ { e[i], e[j] = e[j], e[i] @@ -522,10 +522,10 @@ func scalarTestPow(t *testing.T, g crypto.Group) { } } -func bigIntExp(t *testing.T, g crypto.Group, base, exp *big.Int) *crypto.Scalar { +func bigIntExp(t *testing.T, g ecc.Group, base, exp *big.Int) *ecc.Scalar { orderBytes := g.Order() - if g == crypto.Ristretto255Sha512 || g == crypto.Edwards25519Sha512 { + if g == ecc.Ristretto255Sha512 || g == ecc.Edwards25519Sha512 { slices.Reverse(orderBytes) } @@ -535,7 +535,7 @@ func bigIntExp(t *testing.T, g crypto.Group, base, exp *big.Int) *crypto.Scalar b := make([]byte, g.ScalarLength()) r.FillBytes(b) - if g == crypto.Ristretto255Sha512 || g == crypto.Edwards25519Sha512 { + if g == ecc.Ristretto255Sha512 || g == ecc.Edwards25519Sha512 { slices.Reverse(b) } @@ -547,7 +547,7 @@ func bigIntExp(t *testing.T, g crypto.Group, base, exp *big.Int) *crypto.Scalar return result } -func scalarTestInvert(t *testing.T, g crypto.Group) { +func scalarTestInvert(t *testing.T, g ecc.Group) { s := g.NewScalar().Random() sqr := s.Copy().Multiply(s) diff --git a/tests/table_test.go b/tests/table_test.go index aea9300..81ea2ab 100644 --- a/tests/table_test.go +++ b/tests/table_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C)2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C)2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -12,7 +12,7 @@ import ( "crypto" "testing" - group "github.com/bytemare/crypto" + group "github.com/bytemare/ecc" ) func testAllGroups(t *testing.T, f func(*testGroup)) { diff --git a/tests/utils_test.go b/tests/utils_test.go index 66242b4..29fb549 100644 --- a/tests/utils_test.go +++ b/tests/utils_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved. +// Copyright (C) 2020-2024 Daniel Bourdrez. All Rights Reserved. // // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree or at @@ -14,7 +14,7 @@ import ( "fmt" "testing" - "github.com/bytemare/crypto" + "github.com/bytemare/ecc" ) var ( @@ -66,7 +66,7 @@ func testPanic(s string, expectedError error, f func()) error { return nil } -func decodeScalar(t *testing.T, g crypto.Group, input string) *crypto.Scalar { +func decodeScalar(t *testing.T, g ecc.Group, input string) *ecc.Scalar { b, err := hex.DecodeString(input) if err != nil { t.Error(err) @@ -80,14 +80,14 @@ func decodeScalar(t *testing.T, g crypto.Group, input string) *crypto.Scalar { return s } -func decodeElement(t *testing.T, g crypto.Group, input string) *crypto.Element { +func decodeElement(t *testing.T, g ecc.Group, input string) *ecc.Element { b, err := hex.DecodeString(input) if err != nil { t.Error(err) } e := g.NewElement() - if err := e.Decode(b); err != nil { + if err = e.Decode(b); err != nil { t.Error(err) }