| 1 | # ex:ts=8 sw=4: | ||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||||
| 5 | # | ||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||
| 9 | # | ||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||
| 17 | |||||||||||||
| 18 | package App::FuguSeed::Last; | ||||||||||||
| 19 | 1 | our $VERSION = '0.2.0'; | |||||||||||
| 20 | |||||||||||||
| 21 | 1 | ▶ | use v5.34; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 1 | ▶ | no feature qw(indirect multidimensional bareword_filehandles); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | 1 | ▶ | use App::FuguSeed::List (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 1 | ▶ | use App::FuguSeed::Mnemonic (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | |||||||||||||
| 29 | # App::FuguSeed::Last - the face check, the check word, and the flow | ||||||||||||
| 30 | # of fuguseed-last. | ||||||||||||
| 31 | # | ||||||||||||
| 32 | # The module reads words 1 to 11 and the YELLOW and BLUE faces of | ||||||||||||
| 33 | # word 12 from standard input, and it writes the check word to | ||||||||||||
| 34 | # standard output (LAST-PROGRAM-3, LAST-PROGRAM-4). It is the one | ||||||||||||
| 35 | # module of the program that touches a stream, and the three standard | ||||||||||||
| 36 | # streams are the one contact of the program with the computer | ||||||||||||
| 37 | # (SEC-TRUST-3). | ||||||||||||
| 38 | # | ||||||||||||
| 39 | # A failure line names a word position, a count, or a die, never a | ||||||||||||
| 40 | # word or a face. The check word leaves on standard output only | ||||||||||||
| 41 | # (SEC-CHANNELS-2). | ||||||||||||
| 42 | |||||||||||||
| 43 | # NAME and USAGE: | ||||||||||||
| 44 | # The name of the program in a failure line, and the one usage | ||||||||||||
| 45 | # line of LAST-PROGRAM-2. The program takes no option and no | ||||||||||||
| 46 | # argument (D-12). | ||||||||||||
| 47 | 1 | ▶ | use constant NAME => 'fuguseed-last'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 1 | ▶ | use constant USAGE => "usage: fuguseed-last\n"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | |||||||||||||
| 50 | # SUCCESS, FAILURE and USAGE_ERROR: | ||||||||||||
| 51 | # The three exit codes of the program (LAST-PROGRAM-2, | ||||||||||||
| 52 | # LAST-PROGRAM-4). | ||||||||||||
| 53 | 1 | ▶ | use constant SUCCESS => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 1 | ▶ | use constant FAILURE => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 1 | ▶ | use constant USAGE_ERROR => 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | |||||||||||||
| 57 | # COUNT and FIELDS: | ||||||||||||
| 58 | # The words of the first line, and the faces of the second line | ||||||||||||
| 59 | # (LAST-PROGRAM-3). | ||||||||||||
| 60 | 1 | ▶ | use constant COUNT => 11; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 1 | ▶ | use constant FIELDS => 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | |||||||||||||
| 63 | # YELLOW and BLUE: | ||||||||||||
| 64 | # The faces of the d8 and of the BLUE d16 (D-05). | ||||||||||||
| 65 | 1 | ▶ | use constant YELLOW => 8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 66 | 1 | ▶ | use constant BLUE => 16; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 67 | |||||||||||||
| 68 | # BLOCK and ROW: | ||||||||||||
| 69 | # The words of one YELLOW block and of one BLUE row. The index | ||||||||||||
| 70 | # of a word is (Y-1)*256 + (B-1)*16 + (R-1) (D-05). | ||||||||||||
| 71 | 1 | ▶ | use constant BLOCK => 256; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | 1 | ▶ | use constant ROW => 16; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | |||||||||||||
| 74 | # $class->fault($faces): | ||||||||||||
| 75 | # The failure message for the fields of the array reference | ||||||||||||
| 76 | # $faces, or undef when they are a YELLOW face and a BLUE face | ||||||||||||
| 77 | # (LAST-WORD-2). A face is one of the decimal numbers 1 to the | ||||||||||||
| 78 | # count of its faces, as a string, so "08" and "+8" fail. The | ||||||||||||
| 79 | # message names the field count or the die, never a face. | ||||||||||||
| 80 | 128 | ▶ | sub fault ( $, $faces ) | ||||||||||
|
Subroutine
fault: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 81 | { | ||||||||||||
| 82 | 128 | my $found = scalar @{$faces}; | |||||||||||
| 83 | 128 | ▶ | return "the second line holds $found fields, not " . FIELDS | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | if $found != FIELDS; | ||||||||||||
| 85 | |||||||||||||
| 86 | 128 | my ( $yellow, $blue ) = @{$faces}; | |||||||||||
| 87 | return 'the YELLOW face is not 1 to ' . YELLOW | ||||||||||||
| 88 | 128 | ▶ | unless grep { $_ eq $yellow } 1 .. YELLOW; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | return 'the BLUE face is not 1 to ' . BLUE | ||||||||||||
| 90 | 128 | ▶ | unless grep { $_ eq $blue } 1 .. BLUE; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | |||||||||||||
| 92 | 128 | return; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | # $class->check_word($words, $yellow, $blue): | ||||||||||||
| 96 | # The one word of the BLUE row of word 12 that makes the | ||||||||||||
| 97 | # checksum valid (LAST-WORD-3, LAST-WORD-4). The faces give the | ||||||||||||
| 98 | # first index of the row, and its 7 high bits are the last 7 | ||||||||||||
| 99 | # entropy bits. The RED column gives the 4 checksum bits, so the | ||||||||||||
| 100 | # row holds exactly one valid word (D-04). The search is one | ||||||||||||
| 101 | # computation, and no trial loop exists. The caller proves the | ||||||||||||
| 102 | # words and the faces first. | ||||||||||||
| 103 | 128 | ▶ | sub check_word ( $, $words, $yellow, $blue ) | ||||||||||
|
Subroutine
check_word: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 104 | { | ||||||||||||
| 105 | 128 | my $row = ( $yellow - 1 ) * BLOCK + ( $blue - 1 ) * ROW; | |||||||||||
| 106 | 128 | my @index = App::FuguSeed::Mnemonic->indexes($words); | |||||||||||
| 107 | 128 | my $sum = App::FuguSeed::Mnemonic->checksum( @index, $row ); | |||||||||||
| 108 | |||||||||||||
| 109 | 128 | return App::FuguSeed::List->word( $row + $sum ); | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | # $class->run(@argument): | ||||||||||||
| 113 | # Read the two lines, print the check word, and return the exit | ||||||||||||
| 114 | # code. | ||||||||||||
| 115 | 0 | ▶ | sub run ( $class, @argument ) | ||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 116 | { | ||||||||||||
| 117 | 0 | ▶ | if (@argument) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | print {*STDERR} USAGE; | |||||||||||
| 119 | 0 | return USAGE_ERROR; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 0 | my $first = readline STDIN; | |||||||||||
| 123 | 0 | ▶ | my @words = split q{ }, $first // q{}; | ||||||||||
|
Condition0%0/2
$first // ""
MC/DC0%0/1
$first // ""
$first
Truth table0%0/2
A
A= $first
| |||||||||||||
| 124 | |||||||||||||
| 125 | 0 | my $second = readline STDIN; | |||||||||||
| 126 | 0 | ▶ | my @faces = split q{ }, $second // q{}; | ||||||||||
|
Condition0%0/2
$second // ""
MC/DC0%0/1
$second // ""
$second
Truth table0%0/2
A
A= $second
| |||||||||||||
| 127 | |||||||||||||
| 128 | 0 | my $fault = App::FuguSeed::Mnemonic->fault( \@words, COUNT ); | |||||||||||
| 129 | 0 | ▶ | $fault = $class->fault( \@faces ) if !defined $fault; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 0 | ▶ | if ( defined $fault ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | print {*STDERR} NAME . ": $fault\n"; | |||||||||||
| 132 | 0 | return FAILURE; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | # A class name after print is a bareword filehandle on perl | ||||||||||||
| 136 | # v5.34, so the result reaches a variable first. | ||||||||||||
| 137 | 0 | my $word = $class->check_word( \@words, @faces ); | |||||||||||
| 138 | 0 | print "$word\n"; | |||||||||||
| 139 | |||||||||||||
| 140 | 0 | return SUCCESS; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 1 | 1; | |||||||||||