Private/Share/ConvertTo-SecretSharingWordIndex.ps1
|
function ConvertTo-SecretSharingWordIndex { <# .SYNOPSIS Splits a non-negative big integer into a fixed number of base-2^RadixBit digits, most-significant digit first. .DESCRIPTION The same generic operation the SLIP-0039 reference implementation's int_to_indices performs - used both to pack a share's data into 10-bit mnemonic words (RadixBit 10) and to split its 20-bit share-parameters value into five 4-bit fields (RadixBit 4). Implemented with BigInteger division/ modulo rather than bit-shift/mask, since PowerShell's -shr/-band operators are not reliable across BigInteger's full range on every runtime. #> [CmdletBinding()] [OutputType([int[]], [System.Object[]])] param( [Parameter(Mandatory)] [System.Numerics.BigInteger]$Value, [Parameter(Mandatory)] [ValidateRange(1, [int]::MaxValue)] [int]$Length, [Parameter(Mandatory)] [ValidateRange(1, 31)] [int]$RadixBit ) $radix = [System.Numerics.BigInteger]::Pow(2, $RadixBit) $digit = [int[]]::new($Length) $remaining = $Value for ($i = $Length - 1; $i -ge 0; $i--) { $digit[$i] = [int]($remaining % $radix) $remaining = [System.Numerics.BigInteger]::Divide($remaining, $radix) } return , $digit } |