| 1 | // Package md5x16 implements 16-lane parallel MD5 with AVX-512 instructions. |
| 2 | package md5x16 |
| 3 | |
| 4 | import ( |
| 5 | "encoding/binary" |
| 6 | "errors" |
| 7 | "math" |
| 8 | "reflect" |
| 9 | "unsafe" |
| 10 | ) |
| 11 | |
| 12 | //go:generate go run asm.go -out md5x16.s -stubs stub.go |
| 13 | |
| 14 | // Size of a MD5 checksum in bytes. |
| 15 | const Size = 16 |
| 16 | |
| 17 | // BlockSize is the block size of MD5 in bytes. |
| 18 | const BlockSize = 64 |
| 19 | |
| 20 | // Lanes is the maximum number of parallel MD5 computations. |
| 21 | const Lanes = 16 |
| 22 | |
| 23 | // Validate checks whether the preconditions required by Sum() are met. |
| 24 | func Validate(data [Lanes][]byte) error { |
| 25 | _, err := config(data) |
| 26 | return err |
| 27 | } |
| 28 | |
| 29 | // Sum returns the MD5 checksum of up to Lanes data of the same length. |
| 30 | // |
| 31 | // Non-nil inputs must all have the same length, and occupy a memory span not |
| 32 | // exceeding 32 bits. |
| 33 | func Sum(data [Lanes][]byte) [Lanes][Size]byte { |
| 34 | // Determine lane configuration. |
| 35 | cfg, err := config(data) |
| 36 | if err != nil { |
| 37 | panic(err) |
| 38 | } |
| 39 | |
| 40 | // Initialize hash. |
| 41 | var h [4][Lanes]uint32 |
| 42 | for _, l := range cfg.active { |
| 43 | h[0][l] = 0x67452301 |
| 44 | h[1][l] = 0xefcdab89 |
| 45 | h[2][l] = 0x98badcfe |
| 46 | h[3][l] = 0x10325476 |
| 47 | } |
| 48 | |
| 49 | // Consume full blocks. |
| 50 | base, n := cfg.base, cfg.n |
| 51 | for ; n >= BlockSize; n -= BlockSize { |
| 52 | block(&h, base, &cfg.offsets, cfg.mask) |
| 53 | base += BlockSize |
| 54 | } |
| 55 | |
| 56 | // Final block. |
| 57 | var last [Lanes][]byte |
| 58 | var buffer [Lanes * BlockSize]byte |
| 59 | base = dataptr(buffer[:]) |
| 60 | var offsets [Lanes]uint32 |
| 61 | for _, l := range cfg.active { |
| 62 | last[l] = buffer[l*BlockSize : (l+1)*BlockSize] |
| 63 | offsets[l] = uint32(l * BlockSize) |
| 64 | copy(last[l], data[l][cfg.n-n:]) |
| 65 | last[l][n] = 0x80 |
| 66 | } |
| 67 | |
| 68 | if n >= 56 { |
| 69 | block(&h, base, &offsets, cfg.mask) |
| 70 | for i := range buffer { |
| 71 | buffer[i] = 0 |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | for _, l := range cfg.active { |
| 76 | binary.LittleEndian.PutUint64(last[l][56:], uint64(8*cfg.n)) |
| 77 | } |
| 78 | block(&h, base, &offsets, cfg.mask) |
| 79 | |
| 80 | // Write into byte array. |
| 81 | var digest [Lanes][Size]byte |
| 82 | for _, l := range cfg.active { |
| 83 | for i := 0; i < 4; i++ { |
| 84 | binary.LittleEndian.PutUint32(digest[l][4*i:], h[i][l]) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return digest |
| 89 | } |
| 90 | |
| 91 | // lanes represents the configuration of the 16 data lanes of an MD5 |
| 92 | // computation. |
| 93 | type lanes struct { |
| 94 | n int // length of all active (non-nil) lanes |
| 95 | active []int // indexes of active lanes |
| 96 | mask uint16 // mask of active lanes |
| 97 | base uintptr // base pointer |
| 98 | offsets [Lanes]uint32 // offset of data lanes relative to base |
| 99 | } |
| 100 | |
| 101 | // config determines the lane configuration for the provided data. Returns an |
| 102 | // error if there are no active lanes, there's a length mismatch among active |
| 103 | // lanes, or the data spans a memory region larger than 32-bits. |
| 104 | func config(data [Lanes][]byte) (*lanes, error) { |
| 105 | cfg := &lanes{} |
| 106 | |
| 107 | // Populate active lanes, and ensure they're all the same length. |
| 108 | for l, d := range data { |
| 109 | if d != nil { |
| 110 | cfg.active = append(cfg.active, l) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if len(cfg.active) == 0 { |
| 115 | return nil, errors.New("no active lanes") |
| 116 | } |
| 117 | |
| 118 | cfg.n = len(data[cfg.active[0]]) |
| 119 | for _, l := range cfg.active { |
| 120 | cfg.mask |= 1 << l |
| 121 | if len(data[l]) != cfg.n { |
| 122 | return nil, errors.New("length mismatch") |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Compute base pointer and lane offsets. |
| 127 | cfg.base = ^uintptr(0) |
| 128 | for _, l := range cfg.active { |
| 129 | ptr := dataptr(data[l]) |
| 130 | if ptr < cfg.base { |
| 131 | cfg.base = ptr |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for _, l := range cfg.active { |
| 136 | ptr := dataptr(data[l]) |
| 137 | offset := ptr - cfg.base |
| 138 | if offset > math.MaxUint32 { |
| 139 | return nil, errors.New("input data exceed 32-bit memory region") |
| 140 | } |
| 141 | cfg.offsets[l] = uint32(offset) |
| 142 | } |
| 143 | |
| 144 | return cfg, nil |
| 145 | } |
| 146 | |
| 147 | // dataptr extracts the data pointer from the given slice. |
| 148 | func dataptr(data []byte) uintptr { |
| 149 | hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data)) |
| 150 | return hdr.Data |
| 151 | } |