func VerifySStxAmounts(sstxAmts []int64, sstxCalcAmts []int64) error {
	if len(sstxCalcAmts) != len(sstxAmts) {
		errStr := fmt.Sprintf("SStx verify error: number of calculated " +
			"sstx output values was not equivalent to the number of sstx " +
			"commitment outputs")
		return stakeRuleError(ErrVerSStxAmts, errStr)
	}
	for idx, amt := range sstxCalcAmts {
		if !(amt == sstxAmts[idx]) {
			errStr := fmt.Sprintf("SStx verify error: at index %v incongruent "+
				"amt %v in SStx calculated reward and amt %v in "+
				"SStx", idx, amt, sstxAmts[idx])
			return stakeRuleError(ErrVerSStxAmts, errStr)
		}
	}
	return nil
}
VerifySStxAmounts compares a list of calculated amounts for ticket commitments
to the list of commitment amounts from the actual SStx.
package stake
import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"math"
	"math/big"
	"github.com/HcashOrg/hcashd/chaincfg"
	"github.com/HcashOrg/hcashd/chaincfg/chainec"
	"github.com/HcashOrg/hcashd/chaincfg/chainhash"
	"github.com/HcashOrg/hcashd/txscript"
	"github.com/HcashOrg/hcashd/wire"
	"github.com/HcashOrg/hcashutil"
)
type TxType int
const (
	TxTypeRegular TxType = iota
	TxTypeSStx
	TxTypeSSGen
	TxTypeSSRtx
)
const (
	MaxInputsPerSStx = 64
	MaxOutputsPerSStx = MaxInputsPerSStx*2 + 1
	NumInputsPerSSGen = 2  
	MaxOutputsPerSSGen = MaxInputsPerSStx + 2
	NumInputsPerSSRtx = 1
	MaxOutputsPerSSRtx = MaxInputsPerSStx
	SStxPKHMinOutSize = 32
	SStxPKHMaxOutSize = 77
	SSGenBlockReferenceOutSize = 38 + 4
	SSGenVoteBitsOutputMinSize = 4
	SSGenVoteBitsOutputMaxSize = 77
	MaxSingleBytePushLength = 75
	SSGenVoteBitsExtendedMaxSize = MaxSingleBytePushLength - 2
	SStxVoteReturnFractionMask = 0x003f
	SStxRevReturnFractionMask = 0x3f00
	SStxVoteFractionFlag = 0x0040
	SStxRevFractionFlag = 0x4000
	VoteConsensusVersionAbsent = 0
)
var (
	validSStxAddressOutMinPrefix = []byte{0x6a, 0x1e}
	validSSGenReferenceOutPrefix = []byte{0x6a, 0x28}
	validSSGenVoteOutMinPrefix = []byte{0x6a, 0x02}
	zeroHash = &chainhash.Hash{}
	rangeLimitMax = uint16(63)
)
type VoteBits struct {
	Bits         uint16
	ExtendedBits []byte
}
func isNullOutpoint(tx *wire.MsgTx) bool {
	nullInOP := tx.TxIn[0].PreviousOutPoint
	if nullInOP.Index == math.MaxUint32 && nullInOP.Hash.IsEqual(zeroHash) &&
		nullInOP.Tree == wire.TxTreeRegular {
		return true
	}
	return false
}
func isNullFraudProof(tx *wire.MsgTx) bool {
	txIn := tx.TxIn[0]
	switch {
	case txIn.BlockHeight != wire.NullBlockHeight:
		return false
	case txIn.BlockIndex != wire.NullBlockIndex:
		return false
	}
	return true
}
func IsStakeBase(tx *wire.MsgTx) bool {
	if len(tx.TxIn) != 2 {
		return false
	}
	if !isNullOutpoint(tx) {
		return false
	}
	if !isNullFraudProof(tx) {
		return false
	}
	return true
}
type MinimalOutput struct {
	PkScript []byte
	Value    int64
	Version  uint16
}
func ConvertToMinimalOutputs(tx *wire.MsgTx) []*MinimalOutput {
	minOuts := make([]*MinimalOutput, len(tx.TxOut))
	for i, txOut := range tx.TxOut {
		minOuts[i] = &MinimalOutput{
			PkScript: txOut.PkScript,
			Value:    txOut.Value,
			Version:  txOut.Version,
		}
	}
	return minOuts
}
func SStxStakeOutputInfo(outs []*MinimalOutput) ([]bool, [][]byte, []int64,
	[]int64, [][]bool, [][]uint16) {
	expectedInLen := len(outs) / 2
	isP2SH := make([]bool, expectedInLen)
	addresses := make([][]byte, expectedInLen)
	amounts := make([]int64, expectedInLen)
	changeAmounts := make([]int64, expectedInLen)
	allSpendRules := make([][]bool, expectedInLen)
	allSpendLimits := make([][]uint16, expectedInLen)
	for idx, out := range outs {
		if (idx > 0) && (idx%2 != 0) {
			amtEncoded := make([]byte, 8, 8)
			copy(amtEncoded, out.PkScript[22:30])
			isP2SH[idx/2] = !(amtEncoded[7]&(1<<7) == 0)  
			amtEncoded[7] &= ^uint8(1 << 7)               
			addresses[idx/2] = out.PkScript[2:22]
			amounts[idx/2] = int64(binary.LittleEndian.Uint64(amtEncoded))
			spendRules := make([]bool, 2, 2)
			spendLimits := make([]uint16, 2, 2)
			feeLimitUint16 := binary.LittleEndian.Uint16(out.PkScript[30:32])
			spendRules[0] = (feeLimitUint16 & SStxVoteFractionFlag) ==
				SStxVoteFractionFlag
			spendRules[1] = (feeLimitUint16 & SStxRevFractionFlag) ==
				SStxRevFractionFlag
			allSpendRules[idx/2] = spendRules
			spendLimits[0] = feeLimitUint16 & SStxVoteReturnFractionMask
			spendLimits[1] = feeLimitUint16 & SStxRevReturnFractionMask
			spendLimits[1] >>= 8
			allSpendLimits[idx/2] = spendLimits
		}
		if (idx > 0) && (idx%2 == 0) {
			changeAmounts[(idx/2)-1] = out.Value
		}
	}
	return isP2SH, addresses, amounts, changeAmounts, allSpendRules,
		allSpendLimits
}
func TxSStxStakeOutputInfo(tx *wire.MsgTx) ([]bool, [][]byte, []int64, []int64,
	[][]bool, [][]uint16) {
	return SStxStakeOutputInfo(ConvertToMinimalOutputs(tx))
}
func AddrFromSStxPkScrCommitment(pkScript []byte,
	params *chaincfg.Params) (hcashutil.Address, error) {
	if len(pkScript) < SStxPKHMinOutSize {
		return nil, stakeRuleError(ErrSStxBadCommitAmount, "short read "+
			"of sstx commit pkscript")
	}
	amtEncoded := make([]byte, 8, 8)
	copy(amtEncoded, pkScript[22:30])
	isP2SH := !(amtEncoded[7]&(1<<7) == 0)  
	hashBytes := pkScript[2:22]
	var err error
	var addr hcashutil.Address
	if isP2SH {
		addr, err = hcashutil.NewAddressScriptHashFromHash(hashBytes, params)
	} else {
		addr, err = hcashutil.NewAddressPubKeyHash(hashBytes, params,
			chainec.ECTypeSecp256k1)
	}
	return addr, err
}
func AmountFromSStxPkScrCommitment(pkScript []byte) (hcashutil.Amount, error) {
	if len(pkScript) < SStxPKHMinOutSize {
		return 0, stakeRuleError(ErrSStxBadCommitAmount, "short read "+
			"of sstx commit pkscript")
	}
	amtEncoded := make([]byte, 8, 8)
	copy(amtEncoded, pkScript[22:30])
	amtEncoded[7] &= ^uint8(1 << 7)  
	return hcashutil.Amount(binary.LittleEndian.Uint64(amtEncoded)), nil
}
func TxSSGenStakeOutputInfo(tx *wire.MsgTx, params *chaincfg.Params) ([]bool,
	[][]byte, []int64, error) {
	numOutputsInSSGen := len(tx.TxOut)
	isP2SH := make([]bool, numOutputsInSSGen-2)
	addresses := make([][]byte, numOutputsInSSGen-2)
	amounts := make([]int64, numOutputsInSSGen-2)
	for idx, out := range tx.TxOut {
		if (idx > 1) && (idx < numOutputsInSSGen) {
			class, addr, _, err :=
				txscript.ExtractPkScriptAddrs(out.Version, out.PkScript, params)
			if err != nil {
				return nil, nil, nil, err
			}
			if class != txscript.StakeGenTy {
				return nil, nil, nil, fmt.Errorf("ssgen output included non "+
					"ssgen tagged output in idx %v", idx)
			}
			subClass, err := txscript.GetStakeOutSubclass(out.PkScript)
			if err != nil {
				return nil, nil, nil, err
			}
			if !(subClass == txscript.PubKeyHashTy ||
				subClass == txscript.ScriptHashTy) {
				return nil, nil, nil, fmt.Errorf("bad script type")
			}
			isP2SH[idx-2] = false
			if subClass == txscript.ScriptHashTy {
				isP2SH[idx-2] = true
			}
			amt := out.Value
			addresses[idx-2] = addr[0].ScriptAddress()
			amounts[idx-2] = amt
		}
	}
	return isP2SH, addresses, amounts, nil
}
func SSGenBlockVotedOn(tx *wire.MsgTx) (chainhash.Hash, uint32, uint32, error) {
	blockHash, err := chainhash.NewHash(tx.TxOut[0].PkScript[2:34])
	if err != nil {
		return chainhash.Hash{}, 0, 0, err
	}
	height := binary.LittleEndian.Uint32(tx.TxOut[0].PkScript[34:38])
	keyHeight := binary.LittleEndian.Uint32(tx.TxOut[0].PkScript[38:42])
	return *blockHash, height, keyHeight, nil
}
func SSGenVoteBits(tx *wire.MsgTx) uint16 {
	return binary.LittleEndian.Uint16(tx.TxOut[1].PkScript[2:4])
}
func SSGenVersion(tx *wire.MsgTx) uint32 {
	if len(tx.TxOut[1].PkScript) < 8 {
		return VoteConsensusVersionAbsent
	}
	return binary.LittleEndian.Uint32(tx.TxOut[1].PkScript[4:8])
}
func TxSSRtxStakeOutputInfo(tx *wire.MsgTx, params *chaincfg.Params) ([]bool,
	[][]byte, []int64, error) {
	numOutputsInSSRtx := len(tx.TxOut)
	isP2SH := make([]bool, numOutputsInSSRtx)
	addresses := make([][]byte, numOutputsInSSRtx)
	amounts := make([]int64, numOutputsInSSRtx)
	for idx, out := range tx.TxOut {
		class, addr, _, err :=
			txscript.ExtractPkScriptAddrs(out.Version, out.PkScript, params)
		if err != nil {
			return nil, nil, nil, err
		}
		if class != txscript.StakeRevocationTy {
			return nil, nil, nil, fmt.Errorf("ssrtx output included non "+
				"ssrtx tagged output in idx %v", idx)
		}
		subClass, err := txscript.GetStakeOutSubclass(out.PkScript)
		if err != nil {
			return nil, nil, nil, err
		}
		if !(subClass == txscript.PubKeyHashTy ||
			subClass == txscript.ScriptHashTy) {
			return nil, nil, nil, fmt.Errorf("bad script type")
		}
		isP2SH[idx] = false
		if subClass == txscript.ScriptHashTy {
			isP2SH[idx] = true
		}
		amt := out.Value
		addresses[idx] = addr[0].ScriptAddress()
		amounts[idx] = amt
	}
	return isP2SH, addresses, amounts, nil
}
func SStxNullOutputAmounts(amounts []int64,
	changeAmounts []int64,
	amountTicket int64) (int64, []int64, error) {
	lengthAmounts := len(amounts)
	if lengthAmounts != len(changeAmounts) {
		errStr := fmt.Sprintf("amounts was not equal in length " +
			"to change amounts!")
		return 0, nil, errors.New(errStr)
	}
	if amountTicket <= 0 {
		errStr := fmt.Sprintf("committed amount was too small!")
		return 0, nil, stakeRuleError(ErrSStxBadCommitAmount, errStr)
	}
	contribAmounts := make([]int64, lengthAmounts)
	sum := int64(0)
	for i := 0; i < lengthAmounts; i++ {
		contribAmounts[i] = amounts[i] - changeAmounts[i]
		if contribAmounts[i] < 0 {
			errStr := fmt.Sprintf("change at idx %v spent more coins than "+
				"allowed (have: %v, spent: %v)", i, amounts[i], changeAmounts[i])
			return 0, nil, stakeRuleError(ErrSStxBadChangeAmts, errStr)
		}
		sum += contribAmounts[i]
	}
	fees := sum - amountTicket
	return fees, contribAmounts, nil
}
func CalculateRewards(amounts []int64, amountTicket int64,
	subsidy int64) []int64 {
	outputsAmounts := make([]int64, len(amounts))
	amountWithStakebase := amountTicket + subsidy
	totalContrib := int64(0)
	for _, amount := range amounts {
		totalContrib += amount
	}
	amountWithStakebaseBig := big.NewInt(amountWithStakebase)
	totalContribBig := big.NewInt(totalContrib)
	for idx, amount := range amounts {
		amountBig := big.NewInt(amount)  
		amountBig.Mul(amountBig, amountWithStakebaseBig)
		amountBig.Lsh(amountBig, 32)
		amountBig.Div(amountBig, totalContribBig)
		amountBig.Rsh(amountBig, 32)
		amountFinal := int64(amountBig.Uint64())
		outputsAmounts[idx] = amountFinal
	}
	return outputsAmounts
}