Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Dec 16, 2024
1 parent 454b164 commit 7dba449
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 67 deletions.
21 changes: 10 additions & 11 deletions elliptic/secp/secp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,30 @@ func Test_All(t *testing.T) {
testCurve(t, P192r1())
})

/*
t.Run("P112r1", func(t *testing.T) {
testCurve(t, P112r1())
t.Run("P160r1", func(t *testing.T) {
testCurve(t, P160r1())
})
t.Run("P112r2", func(t *testing.T) {
testCurve(t, P112r2())
t.Run("P160r2", func(t *testing.T) {
testCurve(t, P160r2())
})
*/

t.Run("P128r1", func(t *testing.T) {
testCurve(t, P128r1())
})

/*
t.Run("P128r2", func(t *testing.T) {
testCurve(t, P128r2())
})
*/

t.Run("P160r1", func(t *testing.T) {
testCurve(t, P160r1())
/*
t.Run("P112r1", func(t *testing.T) {
testCurve(t, P112r1())
})
t.Run("P160r2", func(t *testing.T) {
testCurve(t, P160r2())
t.Run("P112r2", func(t *testing.T) {
testCurve(t, P112r2())
})
*/
}

func Test_Add(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions mode/eax/eax.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func (e *eax) pad(plaintext, B, P []byte) []byte {
// if |M| in {n, 2n, 3n, ...}
blockSize := e.block.BlockSize()
if len(plaintext) != 0 && len(plaintext)%blockSize == 0 {
return bytes.RightXor(plaintext, B)
return bytes.RightXOR(plaintext, B)
}

// else return (M || 1 || 0^(n−1−(|M| % n))) xor→ P
ending := make([]byte, blockSize-len(plaintext)%blockSize)
ending[0] = 0x80
padded := append(plaintext, ending...)
return bytes.RightXor(padded, P)
return bytes.RightXOR(padded, P)
}
48 changes: 24 additions & 24 deletions mode/ocb/ocb.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ func (o *ocb) crypt(instruction int, Y, nonce, adata, X []byte) []byte {

// Stretch = Ktop || ((lower half of Ktop) XOR (lower half of Ktop << 8))
xorHalves := make([]byte, blockSize/2)
byteutil.XorBytes(xorHalves, Ktop[:blockSize/2], Ktop[1:1+blockSize/2])
byteutil.XORBytes(xorHalves, Ktop[:blockSize/2], Ktop[1:1+blockSize/2])

stretch := append(Ktop, xorHalves...)
bottom := int(nonce[len(nonce)-1] & 63)
offset := make([]byte, len(stretch))

byteutil.ShiftBytesLeftN(offset, stretch, bottom)
byteutil.ShiftLeftN(offset, stretch, bottom)
offset = offset[:blockSize]

//
Expand All @@ -202,20 +202,20 @@ func (o *ocb) crypt(instruction int, Y, nonce, adata, X []byte) []byte {
o.mask.extendTable(index)
}

byteutil.XorBytesMut(offset, o.mask.L[bits.TrailingZeros(uint(i+1))])
byteutil.XORBytesMut(offset, o.mask.L[bits.TrailingZeros(uint(i+1))])
blockX := X[i*blockSize : (i+1)*blockSize]
blockY := Y[i*blockSize : (i+1)*blockSize]
byteutil.XorBytes(blockY, blockX, offset)
byteutil.XORBytes(blockY, blockX, offset)

switch instruction {
case enc:
o.block.Encrypt(blockY, blockY)
byteutil.XorBytesMut(blockY, offset)
byteutil.XorBytesMut(checksum, blockX)
byteutil.XORBytesMut(blockY, offset)
byteutil.XORBytesMut(checksum, blockX)
case dec:
o.block.Decrypt(blockY, blockY)
byteutil.XorBytesMut(blockY, offset)
byteutil.XorBytesMut(checksum, blockY)
byteutil.XORBytesMut(blockY, offset)
byteutil.XORBytesMut(checksum, blockY)
}
}

Expand All @@ -224,41 +224,41 @@ func (o *ocb) crypt(instruction int, Y, nonce, adata, X []byte) []byte {
//
tag := make([]byte, blockSize)
if len(X)%blockSize != 0 {
byteutil.XorBytesMut(offset, o.mask.lAst)
byteutil.XORBytesMut(offset, o.mask.lAst)
pad := make([]byte, blockSize)
o.block.Encrypt(pad, offset)

chunkX := X[blockSize*m:]
chunkY := Y[blockSize*m : len(X)]
byteutil.XorBytes(chunkY, chunkX, pad[:len(chunkX)])
byteutil.XORBytes(chunkY, chunkX, pad[:len(chunkX)])

// P_* || bit(1) || zeroes(127) - len(P_*)
switch instruction {
case enc:
paddedY := append(chunkX, byte(128))
paddedY = append(paddedY, make([]byte, blockSize-len(chunkX)-1)...)
byteutil.XorBytesMut(checksum, paddedY)
byteutil.XORBytesMut(checksum, paddedY)
case dec:
paddedX := append(chunkY, byte(128))
paddedX = append(paddedX, make([]byte, blockSize-len(chunkY)-1)...)
byteutil.XorBytesMut(checksum, paddedX)
byteutil.XORBytesMut(checksum, paddedX)
}

byteutil.XorBytes(tag, checksum, offset)
byteutil.XorBytesMut(tag, o.mask.lDol)
byteutil.XORBytes(tag, checksum, offset)
byteutil.XORBytesMut(tag, o.mask.lDol)

o.block.Encrypt(tag, tag)

byteutil.XorBytesMut(tag, o.hash(adata))
byteutil.XORBytesMut(tag, o.hash(adata))

copy(Y[blockSize*m+len(chunkY):], tag[:o.tagSize])
} else {
byteutil.XorBytes(tag, checksum, offset)
byteutil.XorBytesMut(tag, o.mask.lDol)
byteutil.XORBytes(tag, checksum, offset)
byteutil.XORBytesMut(tag, o.mask.lDol)

o.block.Encrypt(tag, tag)

byteutil.XorBytesMut(tag, o.hash(adata))
byteutil.XORBytesMut(tag, o.hash(adata))

copy(Y[blockSize*m:], tag[:o.tagSize])
}
Expand Down Expand Up @@ -291,28 +291,28 @@ func (o *ocb) hash(adata []byte) []byte {
o.mask.extendTable(index)
}

byteutil.XorBytesMut(offset, o.mask.L[index])
byteutil.XorBytesMut(chunk, offset)
byteutil.XORBytesMut(offset, o.mask.L[index])
byteutil.XORBytesMut(chunk, offset)

o.block.Encrypt(chunk, chunk)

byteutil.XorBytesMut(sum, chunk)
byteutil.XORBytesMut(sum, chunk)
}

//
// Process any final partial block; compute final hash value
//
if len(A)%blockSize != 0 {
byteutil.XorBytesMut(offset, o.mask.lAst)
byteutil.XORBytesMut(offset, o.mask.lAst)
// Pad block with 1 || 0 ^ 127 - bitlength(a)
ending := make([]byte, blockSize-len(A)%blockSize)
ending[0] = 0x80
encrypted := append(A[blockSize*m:], ending...)
byteutil.XorBytesMut(encrypted, offset)
byteutil.XORBytesMut(encrypted, offset)

o.block.Encrypt(encrypted, encrypted)

byteutil.XorBytesMut(sum, encrypted)
byteutil.XORBytesMut(sum, encrypted)
}

return sum
Expand Down
10 changes: 3 additions & 7 deletions pubkey/bip0340/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ func BatchVerify(pub []*PublicKey, m, sig [][]byte, hashFunc Hasher) bool {
Rx[i], Ry[i] = elliptic.UnmarshalCompressed(curve, rBytes)

if Rx[i] == nil || Ry[i] == nil {
rBytes = append([]byte{byte(2)}, pad(r[i].Bytes(), 32)...)
Rx[i], Ry[i] = elliptic.UnmarshalCompressed(curve, rBytes)

if Rx[i] == nil || Ry[i] == nil {
return false
}
return false
}
}

Expand Down Expand Up @@ -193,7 +188,8 @@ func affYFromX(curve elliptic.Curve, x *big.Int) (*big.Int, *big.Int) {
/* Now compute the two possible square roots
* realizing y^2 = x^3 + ax + b
*/
y1.ModSqrt(y2, y1)
// y1.ModSqrt(y2, y1)
y1.ModSqrt(y2, params.P)

return y1, y2
}
Expand Down
28 changes: 14 additions & 14 deletions tool/bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func GfnDouble(input []byte) []byte {

// If the first bit is zero, return 2L = L << 1
// Else return (L << 1) xor 0^120 10000111
shifted := ShiftBytesLeft(input)
shifted := ShiftLeft(input)
shifted[15] ^= ((input[0] >> 7) * 0x87)
return shifted
}

// ShiftBytesLeft outputs the byte array corresponding to x << 1 in binary.
func ShiftBytesLeft(x []byte) []byte {
// ShiftLeft outputs the byte array corresponding to x << 1 in binary.
func ShiftLeft(x []byte) []byte {
l := len(x)
dst := make([]byte, l)
for i := 0; i < l-1; i++ {
Expand All @@ -43,8 +43,8 @@ func ShiftBytesLeft(x []byte) []byte {
return dst
}

// ShiftBytesLeftN puts in dst the byte array corresponding to x << n in binary.
func ShiftBytesLeftN(dst, x []byte, n int) {
// ShiftLeftN puts in dst the byte array corresponding to x << n in binary.
func ShiftLeftN(dst, x []byte, n int) {
// Erase first n / 8 bytes
copy(dst, x[n/8:])

Expand All @@ -60,18 +60,18 @@ func ShiftBytesLeftN(dst, x []byte, n int) {
dst = append(dst, make([]byte, n/8)...)
}

// XorBytesMut assumes equal input length, replaces X with X XOR Y
func XorBytesMut(X, Y []byte) {
// XORBytesMut assumes equal input length, replaces X with X XOR Y
func XORBytesMut(X, Y []byte) {
subtle.XORBytes(X, X, Y)
}

// XorBytes assumes equal input length, puts X XOR Y into Z
func XorBytes(Z, X, Y []byte) {
// XORBytes assumes equal input length, puts X XOR Y into Z
func XORBytes(Z, X, Y []byte) {
subtle.XORBytes(Z, X, Y)
}

// RightXor XORs smaller input (assumed Y) at the right of the larger input (assumed X)
func RightXor(X, Y []byte) []byte {
// RightXOR XORs smaller input (assumed Y) at the right of the larger input (assumed X)
func RightXOR(X, Y []byte) []byte {
offset := len(X) - len(Y)
xored := make([]byte, len(X))

Expand All @@ -83,7 +83,7 @@ func RightXor(X, Y []byte) []byte {
}

// split bytes with n length
func BytesSplit(buf []byte, size int) [][]byte {
func SplitSize(buf []byte, size int) [][]byte {
var chunk []byte

chunks := make([][]byte, 0, len(buf)/size+1)
Expand All @@ -101,7 +101,7 @@ func BytesSplit(buf []byte, size int) [][]byte {
}

// string to bytes
func StringToBytes(str string) []byte {
func FromString(str string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Expand All @@ -111,6 +111,6 @@ func StringToBytes(str string) []byte {
}

// bytes to string
func BytesToString(buf []byte) string {
func ToString(buf []byte) string {
return *(*string)(unsafe.Pointer(&buf))
}
18 changes: 9 additions & 9 deletions tool/bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
"github.com/deatil/go-cryptobin/tool/test"
)

func Test_BytesSplit(t *testing.T) {
func Test_SplitSize(t *testing.T) {
assertEqual := test.AssertEqualT(t)

data := BytesSplit([]byte("1234567ytghyuj"), 5)
data := SplitSize([]byte("1234567ytghyuj"), 5)
check := [][]byte{
[]byte("12345"),
[]byte("67ytg"),
[]byte("hyuj"),
}

assertEqual(data, check, "Test_BytesSplit")
assertEqual(data, check, "Test_SplitSize")
}

func Test_StringToBytes(t *testing.T) {
func Test_FromString(t *testing.T) {
assertEqual := test.AssertEqualT(t)

data := StringToBytes("1234567ytghyuj")
data := FromString("1234567ytghyuj")
check := []byte("1234567ytghyuj")

assertEqual(data, check, "Test_StringToBytes")
assertEqual(data, check, "Test_FromString")
}

func Test_BytesToString(t *testing.T) {
func Test_ToString(t *testing.T) {
assertEqual := test.AssertEqualT(t)

data := BytesToString([]byte("1234567ytghyuj"))
data := ToString([]byte("1234567ytghyuj"))
check := "1234567ytghyuj"

assertEqual(data, check, "Test_BytesToString")
assertEqual(data, check, "Test_ToString")
}

0 comments on commit 7dba449

Please sign in to comment.