From c0bdf71b1325558dfaed67dc9bbce4a6d312fc9c Mon Sep 17 00:00:00 2001 From: vadimdidenkogs Date: Wed, 13 Nov 2019 19:38:01 +0200 Subject: [PATCH] Reformat code --- crypto.go | 1 + crypto_engine.go | 2 +- crypto_test.go | 3 +- curve.go | 3 +- dsa_signer.go | 3 +- ed25519_encode.go | 103 +++++++++++++++++++++-------------------- ed25519_encode_test.go | 21 +++++---- ed25519_model.go | 6 +-- ed25519_model_test.go | 3 +- go.mod | 1 + go.sum | 5 ++ key_modal_test.go | 3 +- key_model.go | 5 +- key_pair.go | 8 ++-- key_pair_test.go | 4 +- math.go | 25 +++++----- math_test.go | 5 +- signature.go | 3 +- signature_test.go | 3 +- signer_test.go | 3 +- 20 files changed, 113 insertions(+), 97 deletions(-) diff --git a/crypto.go b/crypto.go index 5c30ecd..cc7307b 100644 --- a/crypto.go +++ b/crypto.go @@ -7,6 +7,7 @@ package crypto import ( "crypto/sha256" "crypto/subtle" + "golang.org/x/crypto/ripemd160" "golang.org/x/crypto/sha3" ) diff --git a/crypto_engine.go b/crypto_engine.go index 8d64a14..5500578 100644 --- a/crypto_engine.go +++ b/crypto_engine.go @@ -10,7 +10,7 @@ type CryptoEngine interface { CreateDsaSigner(keyPair *KeyPair) DsaSigner // Creates a key generator. CreateKeyGenerator() KeyGenerator - //Creates a block cipher. + // Creates a block cipher. CreateBlockCipher(senderKeyPair *KeyPair, recipientKeyPair *KeyPair) BlockCipher // Creates a key analyzer. CreateKeyAnalyzer() KeyAnalyzer diff --git a/crypto_test.go b/crypto_test.go index 36cba87..101bbb1 100644 --- a/crypto_test.go +++ b/crypto_test.go @@ -2,8 +2,9 @@ package crypto import ( "encoding/hex" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestHashesSha_256(t *testing.T) { diff --git a/curve.go b/curve.go index 0016e03..02b0c98 100644 --- a/curve.go +++ b/curve.go @@ -6,9 +6,8 @@ package crypto import "math/big" -//Curve Interface for getting information for a curve. +// Curve Interface for getting information for a curve. type Curve interface { - /** * Gets the name of the curve. * diff --git a/dsa_signer.go b/dsa_signer.go index 622eb10..e6232b8 100644 --- a/dsa_signer.go +++ b/dsa_signer.go @@ -4,9 +4,8 @@ package crypto -//DsaSigner Interface that supports signing and verification of arbitrarily sized message. +// DsaSigner Interface that supports signing and verification of arbitrarily sized message. type DsaSigner interface { - // Signs the SHA3 hash of an arbitrarily sized message. Sign(mess []byte) (*Signature, error) // Verifies that the signature is valid. diff --git a/ed25519_encode.go b/ed25519_encode.go index 57573e6..05b61a7 100644 --- a/ed25519_encode.go +++ b/ed25519_encode.go @@ -9,8 +9,9 @@ import ( "encoding/hex" "errors" "fmt" - "github.com/proximax-storage/go-xpx-utils" "math/big" + + "github.com/proximax-storage/go-xpx-utils" ) // PrepareForScalarMultiply precomputes the encoded group elements @@ -95,7 +96,7 @@ func Ed25519FieldTWO() *Ed25519FieldElement { func Ed25519FieldP() *big.Int { const Ed25519FieldP = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed" p := (&big.Int{}).Lsh(big.NewInt(1), 255) - //SetBytes(utils.MustHexDecodeString(Ed25519FieldP)) + // SetBytes(utils.MustHexDecodeString(Ed25519FieldP)) return p.Sub(p, big.NewInt(19)) } @@ -148,7 +149,7 @@ func Ed25519FieldElementSqrt(u Ed25519FieldElement, v Ed25519FieldElement) Ed255 return x } -//IsNonZero gets a value indicating whether or not the field element is non-zero. +// IsNonZero gets a value indicating whether or not the field element is non-zero. func (ref Ed25519FieldElement) IsNonZero() bool { return ref.Encode().IsNonZero() @@ -666,7 +667,7 @@ func (ref Ed25519FieldElement) square() Ed25519FieldElement { return f1.multiply(f0) } -//pow2to9 Ccmputes ref field element to the power of (2^9) and returns the result. +// pow2to9 Ccmputes ref field element to the power of (2^9) and returns the result. func (ref Ed25519FieldElement) pow2to9() Ed25519FieldElement { // 2 == 2 * 1 @@ -679,7 +680,7 @@ func (ref Ed25519FieldElement) pow2to9() Ed25519FieldElement { return ref.multiply(f) } -//pow2to252sub4 computes ref field element to the power of (2^252 - 4) and returns the result. +// pow2to252sub4 computes ref field element to the power of (2^252 - 4) and returns the result. func (ref Ed25519FieldElement) pow2to252sub4() Ed25519FieldElement { // 2 == 2 * 1 @@ -960,10 +961,10 @@ func (ref *Ed25519EncodedFieldElement) fourBytesToLong(b []byte, offset int) int // IsNegative return true if ref is in {1,3,5,...,q-2} // Return false if ref is in {0,2,4,...,q-1} -//* Preconditions: -//* |x| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. -//* -//* @return true if ref is in {1,3,5,...,q-2}, false otherwise. +// * Preconditions: +// * |x| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. +// * +// * @return true if ref is in {1,3,5,...,q-2}, false otherwise. func (ref *Ed25519EncodedFieldElement) IsNegative() bool { return (ref.Raw[0] & 1) != 0 @@ -1837,7 +1838,7 @@ func (ref *Ed25519EncodedGroupElement) Decode() (*Ed25519GroupElement, error) { // * If v * ОІ = -u multiply ОІ with i=sqrt(-1). // * Set x := ОІ. // * If sign(x) != bit 255 of s then negate x. -//* @return the affine x-coordinate. +// * @return the affine x-coordinate. func (ref *Ed25519EncodedGroupElement) GetAffineX() (*Ed25519FieldElement, error) { y, err := ref.GetAffineY() @@ -1955,7 +1956,7 @@ const ed25519GroupRawElement = "b0a00e4a271beec478e42fad0618432fa7d7fb3d99004d2b func init() { - //Ed25519Group.GROUP_ORDER + // Ed25519Group.GROUP_ORDER // 2^252 + 27742317777372353535851937790883648493 rInt, ok := (&big.Int{}).SetString(ed25519GroupOrder, 10) @@ -2012,7 +2013,7 @@ func getBasePoint() (*Ed25519GroupElement, error) { * [5] Christiane Pascale Peters: Curves, Codes, and Cryptography (PhD thesis) * [6] Daniel J. Bernstein, Peter Birkner, Tanja Lange and Christiane Peters: Optimizing float64-base elliptic-curve single-scalar multiplication */ -//Ed25519GroupElement +// Ed25519GroupElement type Ed25519GroupElement struct { coordinateSystem CoordinateSystem @@ -2027,10 +2028,10 @@ type Ed25519GroupElement struct { precomputedForSingle [][]*Ed25519GroupElement // Precomputed table for a float64 scalar multiplication precomputedForDouble []*Ed25519GroupElement - //region constructors + // region constructors } -//NewEd25519GroupElement creates a group element for a curve. +// NewEd25519GroupElement creates a group element for a curve. func NewEd25519GroupElement(coordinateSystem CoordinateSystem, x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2047,7 +2048,7 @@ func NewEd25519GroupElement(coordinateSystem CoordinateSystem, } } -//NewEd25519GroupElementAffine creates a new group element using the AFFINE coordinate system. +// NewEd25519GroupElementAffine creates a new group element using the AFFINE coordinate system. func NewEd25519GroupElementAffine( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2055,7 +2056,7 @@ func NewEd25519GroupElementAffine( return NewEd25519GroupElement(AFFINE, x, y, z, nil) } -//NewEd25519GroupElementP2 creates a new group element using the P2 coordinate system. +// NewEd25519GroupElementP2 creates a new group element using the P2 coordinate system. func NewEd25519GroupElementP2( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2063,7 +2064,7 @@ func NewEd25519GroupElementP2( return NewEd25519GroupElement(P2, x, y, z, nil) } -//NewEd25519GroupElementP3 creates a new group element using the P3 coordinate system. +// NewEd25519GroupElementP3 creates a new group element using the P3 coordinate system. func NewEd25519GroupElementP3( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2072,7 +2073,7 @@ func NewEd25519GroupElementP3( return NewEd25519GroupElement(P3, x, y, z, t) } -//NewEd25519GroupElementP1XP1 Creates a new group element using the P1xP1 coordinate system. +// NewEd25519GroupElementP1XP1 Creates a new group element using the P1xP1 coordinate system. func NewEd25519GroupElementP1XP1( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2081,8 +2082,8 @@ func NewEd25519GroupElementP1XP1( return NewEd25519GroupElement(P1xP1, x, y, z, t) } -//NewEd25519GroupElementPrecomputed сreates a new group element using the PRECOMPUTED coordinate system. -//(CoordinateSystem.PRECOMPUTED, yPlusx, yMinusx, xy2d, nil) +// NewEd25519GroupElementPrecomputed сreates a new group element using the PRECOMPUTED coordinate system. +// (CoordinateSystem.PRECOMPUTED, yPlusx, yMinusx, xy2d, nil) func NewEd25519GroupElementPrecomputed( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2091,7 +2092,7 @@ func NewEd25519GroupElementPrecomputed( } // NewEd25519GroupElementCached сreates a new group element using the CACHED coordinate system. -//(CoordinateSystem.CACHED, YPlusX, YMinusX, Z, T2d) +// (CoordinateSystem.CACHED, YPlusX, YMinusX, Z, T2d) func NewEd25519GroupElementCached( x *Ed25519FieldElement, y *Ed25519FieldElement, @@ -2177,14 +2178,14 @@ func (ref *Ed25519GroupElement) toRadix16(encoded *Ed25519EncodedFieldElement) [ /* each e[i] is between 0 and 15 */ /* e[63] is between 0 and 7 */ -//int carry = 0; -//for (i = 0; i < 63; i++) { -//e[i] += carry; -//carry = e[i] + 8; -//carry >>= 4; -//e[i] -= carry << 4; -//} -//e[63] += carry; +// int carry = 0; +// for (i = 0; i < 63; i++) { +// e[i] += carry; +// carry = e[i] + 8; +// carry >>= 4; +// e[i] -= carry << 4; +// } +// e[63] += carry; /** * Calculates a sliding-windows base 2 representation for a given encoded field element a. @@ -2205,7 +2206,7 @@ func (ref *Ed25519GroupElement) toRadix16(encoded *Ed25519EncodedFieldElement) [ for i := 0; i < 256; i++ { r[i] = 1 & (int8(a[i>>3]) >> uint(i&7)) } - //todo: algorimt must be simple! + // todo: algorimt must be simple! // Note: r[i] will always be odd. for i := uint(0); i < 256; i++ { if r[i] != 0 { @@ -2342,14 +2343,14 @@ func (ref *Ed25519GroupElement) toCached() *Ed25519GroupElement { } // toCoordinateSystem convert a Ed25519GroupElement from one coordinate system to another. -//* Supported conversions: -//* - P3 -> P2 -//* - P3 -> CACHED (1 multiply, 1 add, 1 subtract) -//* - P1xP1 -> P2 (3 multiply) -//* - P1xP1 -> P3 (4 multiply) -//* -//* @param newCoordinateSystem The coordinate system to convert to. -//* @return A new group element in the new coordinate system. +// * Supported conversions: +// * - P3 -> P2 +// * - P3 -> CACHED (1 multiply, 1 add, 1 subtract) +// * - P1xP1 -> P2 (3 multiply) +// * - P1xP1 -> P3 (4 multiply) +// * +// * @param newCoordinateSystem The coordinate system to convert to. +// * @return A new group element in the new coordinate system. func (ref *Ed25519GroupElement) toCoordinateSystem(newCoordinateSystem CoordinateSystem) *Ed25519GroupElement { switch ref.coordinateSystem { case P2: @@ -2624,23 +2625,23 @@ func (ref *Ed25519GroupElement) Select(pos int, b int) (*Ed25519GroupElement, er YPlusX := ref.Y.add(*(ref.X)) YMinusX := ref.Y.subtract(*(ref.X)) - //A = (Y1 - X1) * (Y2 - X2) + // A = (Y1 - X1) * (Y2 - X2) A := YMinusX.multiply(*(g.Y)) - //B = (Y1 + X1) * (Y2 + X2) + // B = (Y1 + X1) * (Y2 + X2) B := YPlusX.multiply(*(g.X)) - //C = 2 * d * T1 * T2 + // C = 2 * d * T1 * T2 C := ref.T.multiply(*(g.Z)) - //D = 2 * Z1 * Z2 + // D = 2 * Z1 * Z2 D := ref.Z.add(*(ref.Z)) - //X' = (Y1 + X1) * g.X - (Y1 - X1) * q.Y + // X' = (Y1 + X1) * g.X - (Y1 - X1) * q.Y x := B.subtract(A) - //(Y1 + X1) * g.X + (Y1 - X1) * q.Y + // (Y1 + X1) * g.X + (Y1 - X1) * q.Y y := A.add(B) - //2 * Z1 + T1 * g.Z + // 2 * Z1 + T1 * g.Z z := D.add(C) t := D.subtract(C) @@ -2709,16 +2710,16 @@ func (ref *Ed25519GroupElement) add(g *Ed25519GroupElement) *Ed25519GroupElement panic("NewIllegalArgumentException") } - //!!!B = (Y1 + X1) * (Y2 + X2) + // !!!B = (Y1 + X1) * (Y2 + X2) B := ref.Y.add(*(ref.X)).multiply(*(g.X)) - //!!!A = (Y1 - X1) * (Y2 - X2) + // !!!A = (Y1 - X1) * (Y2 - X2) A := ref.Y.subtract(*(ref.X)).multiply(*(g.Y)) - //C = (2 * d* T2) * T1 + // C = (2 * d* T2) * T1 C := g.T.multiply(*(ref.T)) - //D = 2 * Z1 * Z2 + // D = 2 * Z1 * Z2 ZSquare := ref.Z.multiply(*(g.Z)) D := ZSquare.add(ZSquare) @@ -2829,7 +2830,7 @@ func (ref *Ed25519GroupElement) scalarMultiply(a *Ed25519EncodedFieldElement) (* return h, nil } -//doubleScalarMultiplyVariableTime r = b * B - a * A where +// doubleScalarMultiplyVariableTime r = b * B - a * A where // * a and b are encoded field elements and // * B is ref point. // * A must have been previously precomputed for float64 scalar multiplication. @@ -2879,7 +2880,7 @@ func (ref *Ed25519GroupElement) doubleScalarMultiplyVariableTime( } // SatisfiesCurveEquation Verify that the group element satisfies the curve equation. -//* @return true if the group element satisfies the curve equation, false otherwise. +// * @return true if the group element satisfies the curve equation, false otherwise. func (ref *Ed25519GroupElement) SatisfiesCurveEquation() bool { switch ref.coordinateSystem { diff --git a/ed25519_encode_test.go b/ed25519_encode_test.go index 5e34581..b29d3b2 100644 --- a/ed25519_encode_test.go +++ b/ed25519_encode_test.go @@ -6,11 +6,12 @@ package crypto import ( "fmt" - "github.com/proximax-storage/go-xpx-utils" - "github.com/stretchr/testify/assert" "math/big" "runtime" "testing" + + "github.com/proximax-storage/go-xpx-utils" + "github.com/stretchr/testify/assert" ) const numIter = 1000 @@ -281,7 +282,7 @@ func TestEncodeReturnsCorrectByteArray(t *testing.T) { var test FieldElements for j := uint(0); j < 10; j++ { - //random.nextInt(1 << 28) - (1 << 27); + // random.nextInt(1 << 28) - (1 << 27); test[j] = MathUtils.GetRandomInt64() - (1 << 27) } @@ -395,7 +396,7 @@ func TestSqrtReturnsCorrectResult(t *testing.T) { } } -//end region +// end region // region Ed25519GroupElementTest func TestCanBeCreatedWithP2Coordinates(t *testing.T) { @@ -520,7 +521,7 @@ func TestEncodeReturnsExpectedResult(t *testing.T) { } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestToP2ThrowsIfGroupElementHasPrecompRepresentation(t *testing.T) { defer func() { @@ -535,7 +536,7 @@ func TestToP2ThrowsIfGroupElementHasPrecompRepresentation(t *testing.T) { g.toP2() } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestToP2ThrowsIfGroupElementHasCachedRepresentation(t *testing.T) { /* public */ defer func() { err := recover() @@ -609,7 +610,7 @@ func testRecover(t *testing.T) { } } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestToCachedThrowsIfGroupElementHasP2Representation(t *testing.T) { defer testRecover(t) @@ -632,7 +633,7 @@ func TestToCachedThrowsIfGroupElementHasPrecompRepresentation(t *testing.T) { g.toCached() } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestToCachedThrowsIfGroupElementHasP1P1Representation(t *testing.T) { defer func() { @@ -917,7 +918,7 @@ func TestEd25519EncodedGroupElement_CanBeCreatedFromByteArray(t *testing.T) { assert.Nil(t, err) } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestEd25519EncodedGroupElement_CannotBeCreatedFromArrayWithIncorrectLength(t *testing.T) { _, err := NewEd25519EncodedGroupElement(make([]byte, 30)) @@ -980,7 +981,7 @@ func Test_GetAffineXReturnsExpectedResult(t *testing.T) { } -//(expected = IllegalArgumentException.class) +// (expected = IllegalArgumentException.class) func TestGetAffineXThrowsIfEncodedGroupElementIsInvalid(t *testing.T) { g := NewEd25519GroupElementP2(Ed25519FieldOne(), &Ed25519Field.D, Ed25519FieldOne()) // Ed25519GroupElement diff --git a/ed25519_model.go b/ed25519_model.go index 1f968d1..77e25af 100644 --- a/ed25519_model.go +++ b/ed25519_model.go @@ -13,7 +13,7 @@ import ( "io" ) -//Ed25519SeedCryptoEngine wraps a cryptographic engine ed25519 and seed for this engine +// Ed25519SeedCryptoEngine wraps a cryptographic engine ed25519 and seed for this engine type Ed25519SeedCryptoEngine struct { seed io.Reader } @@ -197,7 +197,7 @@ type Ed25519DsaSigner struct { KeyPair *KeyPair } -//NewEd25519DsaSigner creates a Ed25519 DSA signer. +// NewEd25519DsaSigner creates a Ed25519 DSA signer. func NewEd25519DsaSigner(keyPair *KeyPair) *Ed25519DsaSigner { return &Ed25519DsaSigner{keyPair} } @@ -341,7 +341,7 @@ func (ref *Ed25519DsaSigner) MakeSignatureCanonical(signature *Signature) (*Sign return NewSignature(signature.R, sModQ.Raw) } -//Ed25519KeyGenerator Implementation of the key generator for Ed25519. +// Ed25519KeyGenerator Implementation of the key generator for Ed25519. type Ed25519KeyGenerator struct { seed io.Reader } diff --git a/ed25519_model_test.go b/ed25519_model_test.go index 6290a8e..f18f608 100644 --- a/ed25519_model_test.go +++ b/ed25519_model_test.go @@ -2,9 +2,10 @@ package crypto import ( "encoding/hex" + "testing" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" - "testing" ) const ( diff --git a/go.mod b/go.mod index f2e9e5a..3970969 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/pkg/errors v0.8.1 + github.com/proximax-storage/go-xpx-utils v0.0.0-20190604083640-90d06ff8a19f github.com/stretchr/testify v1.3.0 golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f ) diff --git a/go.sum b/go.sum index 7d7a8d5..2ef5361 100644 --- a/go.sum +++ b/go.sum @@ -4,9 +4,14 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/proximax-storage/go-xpx-utils v0.0.0-20190604083640-90d06ff8a19f h1:/ll8hQJ1jfDreLorR4U1Vn6Z6UL8r+pd8mv4mYq7RUY= +github.com/proximax-storage/go-xpx-utils v0.0.0-20190604083640-90d06ff8a19f/go.mod h1:++1dcQ6EiUK6Yqi5al5OO3QXZPR1y9mX03e5O2qawEw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo= golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/key_modal_test.go b/key_modal_test.go index dca7981..74ba332 100644 --- a/key_modal_test.go +++ b/key_modal_test.go @@ -6,8 +6,9 @@ package crypto import ( "encoding/hex" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) var testPrivatKeyHex = "2275227522752275227522752275227522752275227522752275227522752275" diff --git a/key_model.go b/key_model.go index 62b1431..ef1ba0a 100644 --- a/key_model.go +++ b/key_model.go @@ -6,10 +6,11 @@ package crypto import ( "encoding/hex" - "github.com/pkg/errors" - "github.com/proximax-storage/go-xpx-utils" "math/big" "strings" + + "github.com/pkg/errors" + "github.com/proximax-storage/go-xpx-utils" ) var ErrInvalidSizePrivateKey = errors.New("the length of private key is not 32") diff --git a/key_pair.go b/key_pair.go index e2c2c16..51abde1 100644 --- a/key_pair.go +++ b/key_pair.go @@ -12,12 +12,12 @@ type KeyPair struct { *PublicKey } -//NewRandomKeyPair creates a random key pair. +// NewRandomKeyPair creates a random key pair. func NewRandomKeyPair() (*KeyPair, error) { return NewKeyPairByEngine(CryptoEngines.DefaultEngine) } -//NewKeyPair The public key is calculated from the private key. +// NewKeyPair The public key is calculated from the private key. // The private key must by nil // if crypto engine is nil - default Engine func NewKeyPair(privateKey *PrivateKey, publicKey *PublicKey, engine CryptoEngine) (*KeyPair, error) { @@ -34,12 +34,12 @@ func NewKeyPair(privateKey *PrivateKey, publicKey *PublicKey, engine CryptoEngin return &KeyPair{privateKey, publicKey}, nil } -//NewKeyPairByEngine creates a random key pair that is compatible with the specified engine. +// NewKeyPairByEngine creates a random key pair that is compatible with the specified engine. func NewKeyPairByEngine(engine CryptoEngine) (*KeyPair, error) { return engine.CreateKeyGenerator().GenerateKeyPair() } -//HasPrivateKey Determines if the current key pair has a private key. +// HasPrivateKey Determines if the current key pair has a private key. func (ref *KeyPair) HasPrivateKey() bool { return ref.PrivateKey != nil diff --git a/key_pair_test.go b/key_pair_test.go index 5db48f6..00a9d64 100644 --- a/key_pair_test.go +++ b/key_pair_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -//region basic construction +// region basic construction func TestNewRandomKeyPair_HasPrivateKey(t *testing.T) { kp, err := NewRandomKeyPair() @@ -72,7 +72,7 @@ func TestNewKeyPair(t *testing.T) { } } -//endregion +// endregion func TestNewRandomKeyPair(t *testing.T) { kp1, err := NewRandomKeyPair() diff --git a/math.go b/math.go index d684fde..480e390 100644 --- a/math.go +++ b/math.go @@ -8,9 +8,10 @@ import ( rand2 "crypto/rand" "encoding/binary" "errors" - "github.com/proximax-storage/go-xpx-utils" "io" "math/big" + + "github.com/proximax-storage/go-xpx-utils" ) func bigIntegerZERO() *big.Int { @@ -21,7 +22,7 @@ func bigIntegerONE() *big.Int { return big.NewInt(1) } -//MathUtils Utility class to help with calculations. +// MathUtils Utility class to help with calculations. type mathUtils struct { EXPONENTS []uint D *big.Int @@ -37,19 +38,19 @@ func (ref *mathUtils) random() (random [32]byte) { return } -//* Converts a 2^8 bit representation to a BigInteger. -//* Value: bytes[0] + 2^8 * bytes[1] + ... -//* -//* @param bytes The 2^8 bit representation. -//* @return The BigInteger. +// * Converts a 2^8 bit representation to a BigInteger. +// * Value: bytes[0] + 2^8 * bytes[1] + ... +// * +// * @param bytes The 2^8 bit representation. +// * @return The BigInteger. func (ref *mathUtils) BytesToBigInteger(bytes []byte) *big.Int { - //return utils.BytesToBigInteger(bytes) + // return utils.BytesToBigInteger(bytes) b := bigIntegerZERO() for i, val := range bytes { el := (&big.Int{}).SetUint64(uint64(uint8(val)) & 0xff) - //one := bigIntegerONE() - //one = one.Mul(one, el) + // one := bigIntegerONE() + // one = one.Mul(one, el) b = b.Add(b, el.Lsh(el, uint(i*8))) } @@ -286,7 +287,7 @@ func (ref *mathUtils) ToRepresentation(g *Ed25519GroupElement, newCoorSys Coordi return ref.getNeeCoor(x, y, newCoorSys) case PRECOMPUTED: - //safaty gX for next calculation + // safaty gX for next calculation x := (&big.Int{}).Sub(gX, gY) x = x.Mul(x, gZ.Mul(gZ, big.NewInt(2))).ModInverse(x, Ed25519Field.P).Mod(x, Ed25519Field.P) @@ -327,7 +328,7 @@ func (ref *mathUtils) getNeeCoor(x, y *big.Int, newCoorSys CoordinateSystem) (*E m = m.Sub(y, x) y1 := ref.ToFieldElement(m.Mod(m, Ed25519Field.P)) - //safaty D for next calculation + // safaty D for next calculation m = (&big.Int{}).Mul(ref.D, big.NewInt(2)) t := ref.ToFieldElement(m.Mul(m, x).Mul(m, y).Mod(m, Ed25519Field.P)) diff --git a/math_test.go b/math_test.go index 41f18de..a2d8a09 100644 --- a/math_test.go +++ b/math_test.go @@ -5,8 +5,9 @@ package crypto import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestMathUtils_AddGroupElements_Neutral(t *testing.T) { @@ -25,7 +26,7 @@ func TestMathUtils_AddGroupElements_Neutral(t *testing.T) { } } -//Simple test for verifying that the MathUtils code works as expected. +// Simple test for verifying that the MathUtils code works as expected. func TestMathUtilsWorkAsExpected(t *testing.T) { for i := 0; i < numIter; i++ { diff --git a/signature.go b/signature.go index 9765d60..8d1bae4 100644 --- a/signature.go +++ b/signature.go @@ -7,9 +7,10 @@ package crypto import ( "encoding/hex" "errors" - "github.com/proximax-storage/go-xpx-utils" "math" "math/big" + + "github.com/proximax-storage/go-xpx-utils" ) // Signature include two part of signature diff --git a/signature_test.go b/signature_test.go index f456fb8..688c786 100644 --- a/signature_test.go +++ b/signature_test.go @@ -5,10 +5,11 @@ package crypto import ( - "github.com/stretchr/testify/assert" "math/big" "strconv" "testing" + + "github.com/stretchr/testify/assert" ) const ( diff --git a/signer_test.go b/signer_test.go index 680ae1e..10a2d90 100644 --- a/signer_test.go +++ b/signer_test.go @@ -5,8 +5,9 @@ package crypto import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) var testDataForSigner = []byte("abcdefg")