Private/Share/ConvertTo-SecretSharingShareWord.ps1

function ConvertTo-SecretSharingShareWord {
    <#
    .SYNOPSIS
        Packs a SLIP-0039 share's header fields and value into its data words
        (everything except the trailing 3-word checksum, which New-SecretSharingChecksum
        computes separately over this function's output).
    .DESCRIPTION
        Layout, per SLIP-0039: a 2-word (20-bit) block of
        Identifier(15) + Extendable(1) + IterationExponent(4), a second 2-word
        (20-bit) block of GroupIndex(4) + (GroupThreshold-1)(4) + (GroupCount-1)(4) +
        MemberIndex(4) + (MemberThreshold-1)(4), then the share value left-padded to
        a whole number of 10-bit words. The padding bits are the high-order bits of
        the first value word and are always zero - a natural side effect of
        converting Value's integer representation into more base-1024 digits than
        its bit length strictly needs, rather than anything padded on separately.
    #>

    [CmdletBinding()]
    [OutputType([int[]], [System.Object[]])]
    param(
        [Parameter(Mandatory)]
        [ValidateRange(0, 32767)]
        [int]$Identifier,

        [switch]$Extendable,

        [Parameter(Mandatory)]
        [ValidateRange(0, 15)]
        [int]$IterationExponent,

        [Parameter(Mandatory)]
        [ValidateRange(0, 15)]
        [int]$GroupIndex,

        [Parameter(Mandatory)]
        [ValidateRange(1, 16)]
        [int]$GroupThreshold,

        [Parameter(Mandatory)]
        [ValidateRange(1, 16)]
        [int]$GroupCount,

        [Parameter(Mandatory)]
        [ValidateRange(0, 15)]
        [int]$MemberIndex,

        [Parameter(Mandatory)]
        [ValidateRange(1, 16)]
        [int]$MemberThreshold,

        [Parameter(Mandatory)]
        [byte[]]$Value
    )

    $extendableBit = 0
    if ($Extendable) {
        $extendableBit = 1
    }

    $idExpInt = [System.Numerics.BigInteger]$Identifier
    $idExpInt = ($idExpInt * 32) + ($extendableBit * 16) + $IterationExponent
    $idExpWord = ConvertTo-SecretSharingWordIndex -Value $idExpInt -Length 2 -RadixBit 10

    $paramInt = [System.Numerics.BigInteger]$GroupIndex
    $paramInt = ($paramInt * 16) + ($GroupThreshold - 1)
    $paramInt = ($paramInt * 16) + ($GroupCount - 1)
    $paramInt = ($paramInt * 16) + $MemberIndex
    $paramInt = ($paramInt * 16) + ($MemberThreshold - 1)
    $paramWord = ConvertTo-SecretSharingWordIndex -Value $paramInt -Length 2 -RadixBit 10

    $valueWordCount = [int][System.Math]::Ceiling(($Value.Length * 8) / 10.0)
    $valueInt = ConvertTo-SecretSharingBigIntegerFromByte -Byte $Value
    $valueWord = ConvertTo-SecretSharingWordIndex -Value $valueInt -Length $valueWordCount -RadixBit 10

    return , ([int[]]($idExpWord + $paramWord + $valueWord))
}