parent
3d70b96ae9
commit
e89271b8f5
@ -1,59 +1,162 @@ |
|||||||
// +build amd64
|
// Package sha256 SHA256SSE3, SHA256AVX, SHA256AVX2
|
||||||
|
|
||||||
package sha256 |
package sha256 |
||||||
|
|
||||||
// #include <stdint.h>
|
|
||||||
// void sha256_transform_avx (uint8_t *input_data, uint32_t digest[8], uint64_t num_blks);
|
|
||||||
// void sha256_transform_ssse3 (uint8_t *input_data, uint32_t digest[8], uint64_t num_blks);
|
|
||||||
// void sha256_transform_rorx (uint8_t *input_data, uint32_t digest[8], uint64_t num_blks);
|
|
||||||
// #define SHA256_DIGEST_SIZE 32
|
|
||||||
// #define SHA256_BLOCK_SIZE 64
|
|
||||||
// #define SHA256_H0 0x6a09e667UL
|
|
||||||
// #define SHA256_H1 0xbb67ae85UL
|
|
||||||
// #define SHA256_H2 0x3c6ef372UL
|
|
||||||
// #define SHA256_H3 0xa54ff53aUL
|
|
||||||
// #define SHA256_H4 0x510e527fUL
|
|
||||||
// #define SHA256_H5 0x9b05688cUL
|
|
||||||
// #define SHA256_H6 0x1f83d9abUL
|
|
||||||
// #define SHA256_H7 0x5be0cd19UL
|
|
||||||
import "C" |
|
||||||
import ( |
import ( |
||||||
gosha256 "crypto/sha256" |
"hash" |
||||||
"io" |
"io" |
||||||
|
|
||||||
|
"github.com/minio-io/minio/pkg/utils/cpu" |
||||||
|
) |
||||||
|
|
||||||
|
// The size of a SHA256 checksum in bytes.
|
||||||
|
const Size = 32 |
||||||
|
|
||||||
|
// The blocksize of SHA256 in bytes.
|
||||||
|
const BlockSize = 64 |
||||||
|
|
||||||
|
const ( |
||||||
|
chunk = 64 |
||||||
|
init0 = 0x6A09E667 |
||||||
|
init1 = 0xBB67AE85 |
||||||
|
init2 = 0x3C6EF372 |
||||||
|
init3 = 0xA54FF53A |
||||||
|
init4 = 0x510E527F |
||||||
|
init5 = 0x9B05688C |
||||||
|
init6 = 0x1F83D9AB |
||||||
|
init7 = 0x5BE0CD19 |
||||||
) |
) |
||||||
|
|
||||||
/* |
// digest represents the partial evaluation of a checksum.
|
||||||
func Sha256(buffer []byte) ([]uint32, error) { |
type digest struct { |
||||||
|
h [8]uint32 |
||||||
|
x [chunk]byte |
||||||
|
nx int |
||||||
|
len uint64 |
||||||
|
} |
||||||
|
|
||||||
|
func (d *digest) Reset() { |
||||||
|
d.h[0] = init0 |
||||||
|
d.h[1] = init1 |
||||||
|
d.h[2] = init2 |
||||||
|
d.h[3] = init3 |
||||||
|
d.h[4] = init4 |
||||||
|
d.h[5] = init5 |
||||||
|
d.h[6] = init6 |
||||||
|
d.h[7] = init7 |
||||||
|
d.nx = 0 |
||||||
|
d.len = 0 |
||||||
|
} |
||||||
|
|
||||||
if cpu.HasSSE41() { |
func block(dig *digest, p []byte) { |
||||||
C.sha256_transform_ssse3() |
switch true { |
||||||
return 0, nil |
case cpu.HasAVX2() == true: |
||||||
|
blockAVX2(dig, p) |
||||||
|
case cpu.HasAVX() == true: |
||||||
|
blockAVX(dig, p) |
||||||
|
case cpu.HasSSE41() == true: |
||||||
|
blockSSE(dig, p) |
||||||
} |
} |
||||||
|
} |
||||||
|
|
||||||
|
// New returns a new hash.Hash computing the SHA256 checksum.
|
||||||
|
func New() hash.Hash { |
||||||
|
d := new(digest) |
||||||
|
d.Reset() |
||||||
|
return d |
||||||
|
} |
||||||
|
|
||||||
if cpu.HasAVX() { |
func (d *digest) Size() int { |
||||||
C.sha256_transform_avx() |
return Size |
||||||
return 0, nil |
} |
||||||
|
|
||||||
|
func (d *digest) BlockSize() int { return BlockSize } |
||||||
|
|
||||||
|
func (d *digest) Write(p []byte) (nn int, err error) { |
||||||
|
nn = len(p) |
||||||
|
d.len += uint64(nn) |
||||||
|
if d.nx > 0 { |
||||||
|
n := copy(d.x[d.nx:], p) |
||||||
|
d.nx += n |
||||||
|
if d.nx == chunk { |
||||||
|
block(d, d.x[:]) |
||||||
|
d.nx = 0 |
||||||
|
} |
||||||
|
p = p[n:] |
||||||
|
} |
||||||
|
if len(p) >= chunk { |
||||||
|
n := len(p) &^ (chunk - 1) |
||||||
|
block(d, p[:n]) |
||||||
|
p = p[n:] |
||||||
} |
} |
||||||
|
if len(p) > 0 { |
||||||
|
d.nx = copy(d.x[:], p) |
||||||
|
} |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
func (d0 *digest) Sum(in []byte) []byte { |
||||||
|
// Make a copy of d0 so that caller can keep writing and summing.
|
||||||
|
d := *d0 |
||||||
|
hash := d.checkSum() |
||||||
|
return append(in, hash[:]...) |
||||||
|
} |
||||||
|
|
||||||
|
func (d *digest) checkSum() [Size]byte { |
||||||
|
len := d.len |
||||||
|
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||||
|
var tmp [64]byte |
||||||
|
tmp[0] = 0x80 |
||||||
|
if len%64 < 56 { |
||||||
|
d.Write(tmp[0 : 56-len%64]) |
||||||
|
} else { |
||||||
|
d.Write(tmp[0 : 64+56-len%64]) |
||||||
|
} |
||||||
|
|
||||||
|
// Length in bits.
|
||||||
|
len <<= 3 |
||||||
|
for i := uint(0); i < 8; i++ { |
||||||
|
tmp[i] = byte(len >> (56 - 8*i)) |
||||||
|
} |
||||||
|
d.Write(tmp[0:8]) |
||||||
|
|
||||||
if cpu.HasAVX2() { |
if d.nx != 0 { |
||||||
C.sha256_transform_rorx() |
panic("d.nx != 0") |
||||||
return 0, nil |
|
||||||
} |
} |
||||||
|
|
||||||
|
h := d.h[:] |
||||||
|
|
||||||
|
var digest [Size]byte |
||||||
|
for i, s := range h { |
||||||
|
digest[i*4] = byte(s >> 24) |
||||||
|
digest[i*4+1] = byte(s >> 16) |
||||||
|
digest[i*4+2] = byte(s >> 8) |
||||||
|
digest[i*4+3] = byte(s) |
||||||
|
} |
||||||
|
|
||||||
|
return digest |
||||||
|
} |
||||||
|
|
||||||
|
// Convenience functions
|
||||||
|
|
||||||
|
func Sum256(data []byte) [Size]byte { |
||||||
|
var d digest |
||||||
|
d.Reset() |
||||||
|
d.Write(data) |
||||||
|
return d.checkSum() |
||||||
} |
} |
||||||
*/ |
|
||||||
|
|
||||||
func Sum(reader io.Reader) ([]byte, error) { |
func Sum(reader io.Reader) ([]byte, error) { |
||||||
hash := gosha256.New() |
h := New() |
||||||
var err error |
var err error |
||||||
for err == nil { |
for err == nil { |
||||||
length := 0 |
length := 0 |
||||||
byteBuffer := make([]byte, 1024*1024) |
byteBuffer := make([]byte, 1024*1024) |
||||||
length, err = reader.Read(byteBuffer) |
length, err = reader.Read(byteBuffer) |
||||||
byteBuffer = byteBuffer[0:length] |
byteBuffer = byteBuffer[0:length] |
||||||
hash.Write(byteBuffer) |
h.Write(byteBuffer) |
||||||
} |
} |
||||||
if err != io.EOF { |
if err != io.EOF { |
||||||
return nil, err |
return nil, err |
||||||
} |
} |
||||||
return hash.Sum(nil), nil |
return h.Sum(nil), nil |
||||||
} |
} |
||||||
|
@ -0,0 +1,65 @@ |
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"crypto/sha256" |
||||||
|
"encoding/hex" |
||||||
|
"fmt" |
||||||
|
"io" |
||||||
|
"os" |
||||||
|
"time" |
||||||
|
|
||||||
|
sha256intel "github.com/minio-io/minio/pkg/utils/crypto/sha256" |
||||||
|
) |
||||||
|
|
||||||
|
func SumIntel(reader io.Reader) ([]byte, error) { |
||||||
|
h := sha256intel.New() |
||||||
|
var err error |
||||||
|
for err == nil { |
||||||
|
length := 0 |
||||||
|
byteBuffer := make([]byte, 1024*1024) |
||||||
|
length, err = reader.Read(byteBuffer) |
||||||
|
byteBuffer = byteBuffer[0:length] |
||||||
|
h.Write(byteBuffer) |
||||||
|
} |
||||||
|
if err != io.EOF { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return h.Sum(nil), nil |
||||||
|
} |
||||||
|
|
||||||
|
func Sum(reader io.Reader) ([]byte, error) { |
||||||
|
k := sha256.New() |
||||||
|
var err error |
||||||
|
for err == nil { |
||||||
|
length := 0 |
||||||
|
byteBuffer := make([]byte, 1024*1024) |
||||||
|
length, err = reader.Read(byteBuffer) |
||||||
|
byteBuffer = byteBuffer[0:length] |
||||||
|
k.Write(byteBuffer) |
||||||
|
} |
||||||
|
if err != io.EOF { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return k.Sum(nil), nil |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
fmt.Println("-- start") |
||||||
|
|
||||||
|
file1, _ := os.Open("filename1") |
||||||
|
defer file1.Close() |
||||||
|
stark := time.Now() |
||||||
|
sum, _ := Sum(file1) |
||||||
|
endk := time.Since(stark) |
||||||
|
|
||||||
|
file2, _ := os.Open("filename2") |
||||||
|
defer file2.Close() |
||||||
|
starth := time.Now() |
||||||
|
sumSSE, _ := SumIntel(file2) |
||||||
|
endh := time.Since(starth) |
||||||
|
|
||||||
|
fmt.Println("std(", endk, ")", "ssse3(", endh, ")") |
||||||
|
fmt.Println(hex.EncodeToString(sum), hex.EncodeToString(sumSSE)) |
||||||
|
} |
@ -1,23 +1,112 @@ |
|||||||
|
// SHA256 hash algorithm. See FIPS 180-2.
|
||||||
|
|
||||||
package sha256 |
package sha256 |
||||||
|
|
||||||
import ( |
import ( |
||||||
"bytes" |
"fmt" |
||||||
"encoding/hex" |
"io" |
||||||
"testing" |
"testing" |
||||||
|
|
||||||
. "gopkg.in/check.v1" |
|
||||||
) |
) |
||||||
|
|
||||||
func Test(t *testing.T) { TestingT(t) } |
type sha256Test struct { |
||||||
|
out string |
||||||
|
in string |
||||||
|
} |
||||||
|
|
||||||
|
var golden = []sha256Test{ |
||||||
|
{"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, |
||||||
|
{"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", "a"}, |
||||||
|
{"fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603", "ab"}, |
||||||
|
{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "abc"}, |
||||||
|
{"88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", "abcd"}, |
||||||
|
{"36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c", "abcde"}, |
||||||
|
{"bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721", "abcdef"}, |
||||||
|
{"7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a", "abcdefg"}, |
||||||
|
{"9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab", "abcdefgh"}, |
||||||
|
{"19cc02f26df43cc571bc9ed7b0c4d29224a3ec229529221725ef76d021c8326f", "abcdefghi"}, |
||||||
|
{"72399361da6a7754fec986dca5b7cbaf1c810a28ded4abaf56b2106d06cb78b0", "abcdefghij"}, |
||||||
|
{"a144061c271f152da4d151034508fed1c138b8c976339de229c3bb6d4bbb4fce", "Discard medicine more than two years old."}, |
||||||
|
{"6dae5caa713a10ad04b46028bf6dad68837c581616a1589a265a11288d4bb5c4", "He who has a shady past knows that nice guys finish last."}, |
||||||
|
{"ae7a702a9509039ddbf29f0765e70d0001177914b86459284dab8b348c2dce3f", "I wouldn't marry him with a ten foot pole."}, |
||||||
|
{"6748450b01c568586715291dfa3ee018da07d36bb7ea6f180c1af6270215c64f", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"}, |
||||||
|
{"14b82014ad2b11f661b5ae6a99b75105c2ffac278cd071cd6c05832793635774", "The days of the digital watch are numbered. -Tom Stoppard"}, |
||||||
|
{"7102cfd76e2e324889eece5d6c41921b1e142a4ac5a2692be78803097f6a48d8", "Nepal premier won't resign."}, |
||||||
|
{"23b1018cd81db1d67983c5f7417c44da9deb582459e378d7a068552ea649dc9f", "For every action there is an equal and opposite government program."}, |
||||||
|
{"8001f190dfb527261c4cfcab70c98e8097a7a1922129bc4096950e57c7999a5a", "His money is twice tainted: 'taint yours and 'taint mine."}, |
||||||
|
{"8c87deb65505c3993eb24b7a150c4155e82eee6960cf0c3a8114ff736d69cad5", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"}, |
||||||
|
{"bfb0a67a19cdec3646498b2e0f751bddc41bba4b7f30081b0b932aad214d16d7", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"}, |
||||||
|
{"7f9a0b9bf56332e19f5a0ec1ad9c1425a153da1c624868fda44561d6b74daf36", "size: a.out: bad magic"}, |
||||||
|
{"b13f81b8aad9e3666879af19886140904f7f429ef083286195982a7588858cfc", "The major problem is with sendmail. -Mark Horton"}, |
||||||
|
{"b26c38d61519e894480c70c8374ea35aa0ad05b2ae3d6674eec5f52a69305ed4", "Give me a rock, paper and scissors and I will move the world. CCFestoon"}, |
||||||
|
{"049d5e26d4f10222cd841a119e38bd8d2e0d1129728688449575d4ff42b842c1", "If the enemy is within range, then so are you."}, |
||||||
|
{"0e116838e3cc1c1a14cd045397e29b4d087aa11b0853fc69ec82e90330d60949", "It's well we cannot hear the screams/That we create in others' dreams."}, |
||||||
|
{"4f7d8eb5bcf11de2a56b971021a444aa4eafd6ecd0f307b5109e4e776cd0fe46", "You remind me of a TV show, but that's all right: I watch it anyway."}, |
||||||
|
{"61c0cc4c4bd8406d5120b3fb4ebc31ce87667c162f29468b3c779675a85aebce", "C is as portable as Stonehedge!!"}, |
||||||
|
{"1fb2eb3688093c4a3f80cd87a5547e2ce940a4f923243a79a2a1e242220693ac", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"}, |
||||||
|
{"395585ce30617b62c80b93e8208ce866d4edc811a177fdb4b82d3911d8696423", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"}, |
||||||
|
{"4f9b189a13d030838269dce846b16a1ce9ce81fe63e65de2f636863336a98fe6", "How can you write a big system without C++? -Paul Glick"}, |
||||||
|
} |
||||||
|
|
||||||
|
func TestGolden(t *testing.T) { |
||||||
|
for i := 0; i < len(golden); i++ { |
||||||
|
g := golden[i] |
||||||
|
s := fmt.Sprintf("%x", Sum256([]byte(g.in))) |
||||||
|
if s != g.out { |
||||||
|
t.Fatalf("Sum256 function: sha256(%s) = %s want %s", g.in, s, g.out) |
||||||
|
} |
||||||
|
c := New() |
||||||
|
for j := 0; j < 3; j++ { |
||||||
|
if j < 2 { |
||||||
|
io.WriteString(c, g.in) |
||||||
|
} else { |
||||||
|
io.WriteString(c, g.in[0:len(g.in)/2]) |
||||||
|
c.Sum(nil) |
||||||
|
io.WriteString(c, g.in[len(g.in)/2:]) |
||||||
|
} |
||||||
|
s := fmt.Sprintf("%x", c.Sum(nil)) |
||||||
|
if s != g.out { |
||||||
|
t.Fatalf("sha256[%d](%s) = %s want %s", j, g.in, s, g.out) |
||||||
|
} |
||||||
|
c.Reset() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestSize(t *testing.T) { |
||||||
|
c := New() |
||||||
|
if got := c.Size(); got != Size { |
||||||
|
t.Errorf("Size = %d; want %d", got, Size) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestBlockSize(t *testing.T) { |
||||||
|
c := New() |
||||||
|
if got := c.BlockSize(); got != BlockSize { |
||||||
|
t.Errorf("BlockSize = %d want %d", got, BlockSize) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
var bench = New() |
||||||
|
var buf = make([]byte, 8192) |
||||||
|
|
||||||
type MySuite struct{} |
func benchmarkSize(b *testing.B, size int) { |
||||||
|
b.SetBytes(int64(size)) |
||||||
|
sum := make([]byte, bench.Size()) |
||||||
|
for i := 0; i < b.N; i++ { |
||||||
|
bench.Reset() |
||||||
|
bench.Write(buf[:size]) |
||||||
|
bench.Sum(sum[:0]) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func BenchmarkHash8Bytes(b *testing.B) { |
||||||
|
benchmarkSize(b, 8) |
||||||
|
} |
||||||
|
|
||||||
var _ = Suite(&MySuite{}) |
func BenchmarkHash1K(b *testing.B) { |
||||||
|
benchmarkSize(b, 1024) |
||||||
|
} |
||||||
|
|
||||||
func (s *MySuite) TestSha256Stream(c *C) { |
func BenchmarkHash8K(b *testing.B) { |
||||||
testString := []byte("Test string") |
benchmarkSize(b, 8192) |
||||||
expectedHash, _ := hex.DecodeString("a3e49d843df13c2e2a7786f6ecd7e0d184f45d718d1ac1a8a63e570466e489dd") |
|
||||||
hash, err := Sum(bytes.NewBuffer(testString)) |
|
||||||
c.Assert(err, IsNil) |
|
||||||
c.Assert(bytes.Equal(expectedHash, hash), Equals, true) |
|
||||||
} |
} |
||||||
|
@ -0,0 +1,23 @@ |
|||||||
|
// +build amd64
|
||||||
|
|
||||||
|
package sha256 |
||||||
|
|
||||||
|
// #cgo CFLAGS: -DHAS_SSE41 -DHAS_AVX -DHAS_AVX2
|
||||||
|
// #include <stdint.h>
|
||||||
|
// void sha256_transform_ssse3 (const char *input_data, uint32_t *digest, unsigned long num_blks);
|
||||||
|
// void sha256_transform_avx (const char *input_data, uint32_t *digest, unsigned long num_blks);
|
||||||
|
// void sha256_transform_rorx (const char *input_data, uint32_t *digest, unsigned long num_blks);
|
||||||
|
import "C" |
||||||
|
import "unsafe" |
||||||
|
|
||||||
|
func blockSSE(dig *digest, p []byte) { |
||||||
|
C.sha256_transform_ssse3((*C.char)(unsafe.Pointer(&p[0])), (*C.uint32_t)(unsafe.Pointer(&dig.h[0])), (C.ulong)(len(p)/64)) |
||||||
|
} |
||||||
|
|
||||||
|
func blockAVX(dig *digest, p []byte) { |
||||||
|
C.sha256_transform_avx((*C.char)(unsafe.Pointer(&p[0])), (*C.uint32_t)(unsafe.Pointer(&dig.h[0])), (C.ulong)(len(p)/64)) |
||||||
|
} |
||||||
|
|
||||||
|
func blockAVX2(dig *digest, p []byte) { |
||||||
|
C.sha256_transform_rorx((*C.char)(unsafe.Pointer(&p[0])), (*C.uint32_t)(unsafe.Pointer(&dig.h[0])), (C.ulong)(len(p)/64)) |
||||||
|
} |
Loading…
Reference in new issue