// ChecksumCombine combines the two uint16 to form their checksum. This is done
// by adding them and the carry.
//
// Note that checksum a must have been computed on an even number of bytes.
183898
func ChecksumCombine(a, b uint16) uint16 {
	v := uint32(a) + uint32(b)
	return uint16(v + v>>16)
}
695898
func (b TCP) SetChecksum(checksum uint16) {
	binary.BigEndian.PutUint16(b[TCPChecksumOffset:], checksum)
}
500264
func AddChecksums(inputs []Trytes, isAddress bool, checksumLength uint64) ([]Trytes, error) {
	withChecksums := make([]Trytes, len(inputs))
	for i, s := range inputs {
		t, err := AddChecksum(s, isAddress, checksumLength)
		if err != nil {
			return nil, err
		}
		withChecksums[i] = t
	}
	return withChecksums, nil
}
823277
func Checksum(input []byte, seed uint64) uint64 {
	n := len(input)
	var h64 uint64

	if n >= 32 {
		v1 := seed + prime64_1 + prime64_2
		v2 := seed + prime64_2
		v3 := seed
		v4 := seed - prime64_1
		p := 0
		for n := n - 32; p <= n; p += 32 {
			sub := input[p:][:32] //BCE hint for compiler
			v1 = rol31(v1+u64(sub[:])*prime64_2) * prime64_1
			v2 = rol31(v2+u64(sub[8:])*prime64_2) * prime64_1
			v3 = rol31(v3+u64(sub[16:])*prime64_2) * prime64_1
			v4 = rol31(v4+u64(sub[24:])*prime64_2) * prime64_1
		}

		h64 = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)

		v1 *= prime64_2
		v2 *= prime64_2
		v3 *= prime64_2
		v4 *= prime64_2

		h64 = (h64^(rol31(v1)*prime64_1))*prime64_1 + prime64_4
		h64 = (h64^(rol31(v2)*prime64_1))*prime64_1 + prime64_4
		h64 = (h64^(rol31(v3)*prime64_1))*prime64_1 + prime64_4
		h64 = (h64^(rol31(v4)*prime64_1))*prime64_1 + prime64_4

		h64 += uint64(n)

		input = input[p:]
		n -= p
	} else {
		h64 = seed + prime64_5 + uint64(n)
	}

	p := 0
	for n := n - 8; p <= n; p += 8 {
		sub := input[p : p+8]
		h64 ^= rol31(u64(sub)*prime64_2) * prime64_1
		h64 = rol27(h64)*prime64_1 + prime64_4
	}
	if p+4 <= n {
		sub := input[p : p+4]
		h64 ^= uint64(u32(sub)) * prime64_1
		h64 = rol23(h64)*prime64_2 + prime64_3
		p += 4
	}
	for ; p < n; p++ {
		h64 ^= uint64(input[p]) * prime64_5
		h64 = rol11(h64) * prime64_1
	}

	h64 ^= h64 >> 33
	h64 *= prime64_2
	h64 ^= h64 >> 29
	h64 *= prime64_3
	h64 ^= h64 >> 32

	return h64
}
296216
func PairingCheck(a []*G1, b []*G2) bool {
	return bn256.PairingCheck(a, b)
}
330863
func xorChecksum(s string) string {
	var checksum uint8
	for i := 0; i < len(s); i++ {
		checksum ^= s[i]
	}
	return fmt.Sprintf("%02X", checksum)
}
972884
func (cr *ChecksumReader) ReadChecksum() ([]byte, error) {
	if cr.datanodes == nil {
		locs := cr.Block.GetLocs()
		datanodes := make([]string, len(locs))
		for i, loc := range locs {
			dn := loc.GetId()
			datanodes[i] = getDatanodeAddress(dn, cr.UseDatanodeHostname)
		}

		cr.datanodes = newDatanodeFailover(datanodes)
	}

	for cr.datanodes.numRemaining() > 0 {
		address := cr.datanodes.next()
		checksum, err := cr.readChecksum(address)
		if err != nil {
			cr.datanodes.recordFailure(err)
			continue
		}

		return checksum, nil
	}

	err := cr.datanodes.lastError()
	if err != nil {
		err = errors.New("No available datanodes for block.")
	}

	return nil, err
}
99783
func checksum(data []byte) ([]byte, error) {
	hash, err := hashDoubleSha256(data)
	if err != nil {
		return nil, err
	}

	return hash[:4], nil
}
197437
func (a *Allocator) checkConsistency(as string) {
	var sKeyList []SubnetKey

	// Retrieve this address space's configuration and bitmasks from the datastore
	a.refresh(as)
	a.Lock()
	aSpace, ok := a.addrSpaces[as]
	a.Unlock()
	if !ok {
		return
	}
	a.updateBitMasks(aSpace)

	aSpace.Lock()
	for sk, pd := range aSpace.subnets {
		if pd.Range != nil {
			continue
		}
		sKeyList = append(sKeyList, sk)
	}
	aSpace.Unlock()

	for _, sk := range sKeyList {
		a.Lock()
		bm := a.addresses[sk]
		a.Unlock()
		if err := bm.CheckConsistency(); err != nil {
			logrus.Warnf("Error while running consistency check for %s: %v", sk, err)
		}
	}
}
1351375
func (l LabelMap) checksum() int32 {
	var checksum uint32
	lb := byteBufferPool.Get().(*bytes.Buffer)
	// make sure the buffer is empty after getting a buffer from our pool
	lb.Reset()
	for key, value := range l {
		// The bytes used for checksumming are the following:
		// <4 bytes: length of key>
		binary.Write(lb, binary.BigEndian, int32(len(key)))
		// <n bytes: key>
		lb.WriteString(key)
		// <4 bytes: length of value>
		binary.Write(lb, binary.BigEndian, int32(len(value)))
		// <n bytes: value>
		lb.WriteString(value)

		// The checksum is calculated by xorring the checksums of all individual
		// labels. This makes the checksum of the labels order independant. This
		// is easier compared to sorting the labels by their key because of two
		// reasons.
		// 1. It saves memory allocations to have the keys in a slice.
		// 2. This method is guaranteed to be locale independant where sorting
		//    of strings might be different on different locale settings. This
		//    would cause indefinite fullsync storms because two ringpops would
		//    never agree on membership checksums.
		checksum = checksum ^ farm.Fingerprint32(lb.Bytes())

		lb.Reset()
	}
	// give the buffer back to the pool
	byteBufferPool.Put(lb)

	var signedChecksum int32
	// This line converts an unsigned integer to a signed integer (32 bits).
	// It is needed to be able to calculate the same value in nodejs for
	// ringpop-node and the integration tests in ringpop-common.
	signedChecksum = int32(checksum>>1)<<1 | int32(checksum&uint32(1))
	return signedChecksum
}
760798
func (s *PutIntentInput) SetChecksum(v string) *PutIntentInput {
	s.Checksum = &v
	return s
}
1045819
func (m Member) checksumString(b *bytes.Buffer) {
	b.WriteString(m.Address)
	b.WriteString(m.Status)
	b.WriteString(strconv.FormatInt(m.Incarnation, 10))
	m.Labels.checksumString(b)
}
1010113
func canMerge16(a, b interval16) bool {
	if int64(a.last())+1 < int64(b.start) {
		return false
	}
	return int64(b.last())+1 >= int64(a.start)
}
642526
func (fr *fuzzReader) updateChecksum(pos int64, val uint32) {
	if pos >= 0 {
		fr.Checksums = append(fr.Checksums, Checksum{pos, val})
	} else {
		fr.Checksums[len(fr.Checksums)-1].Value = val
	}
}
861317
func VersionLabelForChecksum(checksum string) string {
	// Checksums are in the form: {versionLabel}+{hashID}:{hex}
	sepIndex := strings.Index(checksum, "+")
	if sepIndex < 0 {
		return ""
	}
	return checksum[:sepIndex]
}
1068352
func (a Artifact) Checksum(algorithm string) (string, error) {
	log.Debugf("calculating checksum for %s", a.Path)
	file, err := os.Open(a.Path)
	if err != nil {
		return "", errors.Wrap(err, "failed to checksum")
	}
	defer file.Close() // nolint: errcheck
	var h hash.Hash
	switch algorithm {
	case "crc32":
		h = crc32.NewIEEE()
	case "md5":
		h = md5.New()
	case "sha224":
		h = sha256.New224()
	case "sha384":
		h = sha512.New384()
	case "sha256":
		h = sha256.New()
	case "sha1":
		h = sha1.New()
	case "sha512":
		h = sha512.New()
	default:
		return "", fmt.Errorf("invalid algorith: %s", algorithm)
	}
	_, err = io.Copy(h, file)
	if err != nil {
		return "", errors.Wrap(err, "failed to checksum")
	}
	return hex.EncodeToString(h.Sum(nil)), nil
}
517603
func Checksum(data []byte, table *Table) uint16 {
	crc := Init(table)
	crc = Update(crc, data, table)
	return Complete(crc, table)
}