Skip to content
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
8 changes: 8 additions & 0 deletions bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ type blsBLS12381Algo struct {
algo sign.SigningAlgorithm
}

// init the BLS12-381 curve context
func init() {
// register the BLS context on the BLS 12-381 curve instance in the `sign` package
if err := sign.RegisterSigner(sign.BLSBLS12381, &blsBLS12381Algo{algo: sign.BLSBLS12381}); err != nil {
panic(err)
}
}

// NewExpandMsgXOFKMAC128 returns a new expand_message_xof instance for
// the hash-to-curve function, hashing data to G1 on BLS12 381.
// This instance must only be used to generate signatures (and not PoP),
Expand Down
5 changes: 3 additions & 2 deletions bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@

"github.com/onflow/crypto/hash"
"github.com/onflow/crypto/sign"
"github.com/onflow/crypto/sign/internal"

Check failure on line 36 in bls_test.go

View workflow job for this annotation

GitHub Actions / Go code checks

use of internal package github.com/onflow/crypto/sign/internal not allowed (typecheck)

Check failure on line 36 in bls_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

use of internal package github.com/onflow/crypto/sign/internal not allowed

Check failure on line 36 in bls_test.go

View workflow job for this annotation

GitHub Actions / C code checks

use of internal package github.com/onflow/crypto/sign/internal not allowed
)

// TestBLSMainMethods is a sanity check of main signature scheme methods (keyGen, sign, verify)
func TestBLSMainMethods(t *testing.T) {
// test the key generation seed lengths
testKeyGenSeed(t, sign.BLSBLS12381, KeyGenSeedMinLen, KeyGenSeedMaxLen)
internal.TestKeyGenSeed(t, sign.BLSBLS12381, KeyGenSeedMinLen, KeyGenSeedMaxLen)
// test the consistency with different inputs
hasher := NewExpandMsgXOFKMAC128("test tag")
testGenSignVerify(t, sign.BLSBLS12381, hasher)
internal.TestGenSignVerify(t, sign.BLSBLS12381, hasher)

// specific signature test for BLS:
// Test a signature with a point encoded with a coordinate x not reduced mod p.
Expand Down
112 changes: 0 additions & 112 deletions common.go

This file was deleted.

57 changes: 57 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Flow Crypto
*
* Copyright Flow Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"errors"

"github.com/onflow/crypto/internal"
)

// IsInvalidInputsError checks if the input error is of an invalidInputsError type
// invalidInputsError is returned when the API is provided invalid inputs.
// Some specific errors are assigned specific sentinel errors for a simpler error check
// while the remaining input errors trigger an invalidInputsError.
func IsInvalidInputsError(err error) bool {
var target *internal.InvalidInputsError
return errors.As(err, &target)
}

// InvalidInputsErrorf constructs a new invalidInputsError
var InvalidInputsErrorf = internal.InvalidInputsErrorf

// IsNilHasherError checks if the input error wraps the internal errNilHasher,
// which is returned when a nil hasher is used.
func IsNilHasherError(err error) bool {
return errors.Is(err, ErrNilHasher)
}

// ErrNilHasher is returned when a nil hasher is used
var ErrNilHasher = internal.ErrNilHasher

// IsInvalidHasherSizeError checks if the input error is of an invalidHasherSizeError type.
// invalidHasherSizeError is an error returned when a crypto API is called with a hasher
// with an output size not suited with the cryptographic operation.
func IsInvalidHasherSizeError(err error) bool {
var target *internal.InvalidHasherSizeError
return errors.As(err, &target)
}

// InvalidHasherSizeErrorf constructs a new invalidHasherSizeError
var InvalidHasherSizeErrorf = internal.InvalidHasherSizeErrorf
80 changes: 80 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Flow Crypto
*
* Copyright Flow Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package internal

import (
"crypto/rand"
"errors"
"fmt"
)

// Minimum targeted bits of security.
// This is used as a reference but it doesn't mean all implemented primitives provide this minimum.
const SecurityBits = 128

// TODO: update this code to make sure
// the function isn't removed by the compiler
// https://github.com/golang/go/issues/21865
func Overwrite(data []byte) {
_, err := rand.Read(data) // checking err is enough
if err != nil {
// zero the buffer if randomizing failed
for i := 0; i < len(data); i++ {
data[i] = 0
}
}
}

// InvalidInputsError is an error returned when a crypto API receives invalid inputs.
// It allows a function caller differentiate unexpected program errors from errors caused by
// invalid inputs.
type InvalidInputsError struct {
error
}

func (e InvalidInputsError) Unwrap() error {
return e.error
}

// InvalidHasherSizeError is an error returned when a crypto API is called with a hasher
// with an output size not suited with the cryptographic operation.
type InvalidHasherSizeError struct {
error
}

func (e InvalidHasherSizeError) Unwrap() error {
return e.error
}

// ErrNilHasher is returned when a nil hasher is used
var ErrNilHasher = errors.New("hasher cannot be nil")

// InvalidInputsErrorf constructs a new InvalidInputsError
func InvalidInputsErrorf(msg string, args ...interface{}) error {
return &InvalidInputsError{
error: fmt.Errorf(msg, args...),
}
}

// InvalidHasherSizeErrorf constructs a new InvalidHasherSizeError
func InvalidHasherSizeErrorf(msg string, args ...interface{}) error {
return &InvalidHasherSizeError{
error: fmt.Errorf(msg, args...),
}
}
Loading
Loading