| 1 | package Crypt::Chimera::User; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA %PARITY); | ||
| 5 | use Data::Dumper; | ||
| 6 | use Crypt::Chimera::Object; | ||
| 7 | use Crypt::Chimera::Event; | ||
| 8 | |||
| 9 | @ISA = qw(Crypt::Chimera::Object); | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | foreach my $i (0..1) { | ||
| 13 | foreach my $j (0..1) { | ||
| 14 | foreach my $k (0..1) { | ||
| 15 | $PARITY{"$i$j$k"} = $i ^ $j ^ $k; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $self = $class->SUPER::new(@_); | ||
| 24 | |||
| 25 | die "No name in user" unless $self->{Name}; | ||
| 26 | die "No world in user" unless $self->{World}; | ||
| 27 | |||
| 28 | $self->{World}->register($self); | ||
| 29 | |||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub init { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | $self->display(1, "new user, verbosity " . $self->{Verbose}, ""); | ||
| 37 | |||
| 38 | my $len = $self->{World}->{Length}; | ||
| 39 | |||
| 40 | my $bits = "0" x $len; | ||
| 41 | foreach (0..$len) { | ||
| 42 | substr($bits, $_, 1) = "1" if (rand() * 16) < 3; | ||
| 43 | } | ||
| 44 | |||
| 45 | $self->display(2, "initial", $bits); | ||
| 46 | |||
| 47 | $self->{Bits} = [ $bits ]; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub fini { | ||
| 51 | } | ||
| 52 | |||
| 53 | sub parity { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | my $bits = $self->{Bits}->[$self->{World}->{Round}]; | ||
| 57 | |||
| 58 | my $len = int(length($bits) / 3); | ||
| 59 | my $out = "x" x $len; | ||
| 60 | |||
| 61 | foreach (0..($len - 1)) { | ||
| 62 | substr($out, $_, 1) = $PARITY{substr($bits, $_ * 3, 3)}; | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->display(3, "parities", $out); | ||
| 66 | |||
| 67 | $self->{Parity} = $out; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub round { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | $self->parity unless exists $self->{Parity}; | ||
| 74 | |||
| 75 | my $event = new Crypt::Chimera::Event( | ||
| 76 | Source => $self->{Name}, | ||
| 77 | Parity => $self->{Parity}, | ||
| 78 | ); | ||
| 79 | $self->{World}->event($event); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub receive { | ||
| 83 | my $self = shift; | ||
| 84 | my $remote = shift; # A string "10100101010" | ||
| 85 | |||
| 86 | $self->parity unless exists $self->{Parity}; | ||
| 87 | |||
| 88 | my $len = length $self->{Parity}; | ||
| 89 | $len = length $remote if length $remote < $len; | ||
| 90 | |||
| 91 | my $rbits = pack("B*", $remote); | ||
| 92 | my $lbits = pack("B*", $self->{Parity}); | ||
| 93 | my $match = unpack("B*", ($rbits ^ ~ $lbits)); | ||
| 94 | $match = substr($match, 0, $len); | ||
| 95 | |||
| 96 | $self->display(5, "parity match", $match); | ||
| 97 | |||
| 98 | my $zeros = $match; | ||
| 99 | $zeros =~ s/[^0]//g; | ||
| 100 | $self->display(7, "zero count", length($zeros)); | ||
| 101 | # $self->display(7, "zero ratio", length($zeros) / length($match)); | ||
| 102 | |||
| 103 | my @match = split //, $match; | ||
| 104 | my $bits = $self->{Bits}->[$self->{World}->{Round}]; | ||
| 105 | my $out = "x" x $len; | ||
| 106 | my $i = 0; | ||
| 107 | |||
| 108 | foreach (0..($len - 1)) { | ||
| 109 | if ($match[$_]) { | ||
| 110 | substr($out, $i++, 1) = substr($bits, $_ * 3, 1); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | $out = substr($out, 0, $i); | ||
| 115 | |||
| 116 | $self->{Bits}->[$self->{World}->{Round} + 1] = $out; | ||
| 117 | |||
| 118 | $self->display(2, "new bits", $out); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub event { | ||
| 122 | my $self = shift; | ||
| 123 | my $event = shift; | ||
| 124 | |||
| 125 | return unless $event->{Source} eq $self->{Remote}; | ||
| 126 | |||
| 127 | # $self->display(4, "process event", $event->{Seq}); | ||
| 128 | |||
| 129 | $self->receive($event->{Parity}); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub clean { | ||
| 133 | my $self = shift; | ||
| 134 | delete $self->{Parity}; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub huffman_recurse { | ||
| 138 | my ($self, $code, $freq) = @_; | ||
| 139 | |||
| 140 | # Make this deterministic, even though it shouldn't matter | ||
| 141 | my @tokens = sort | ||
| 142 | { $freq->{$b} <=> $freq->{$a} || $b cmp $a } | ||
| 143 | keys %$freq; | ||
| 144 | |||
| 145 | if (@tokens == 2) { | ||
| 146 | $code->{$tokens[0]} = '1'; | ||
| 147 | $code->{$tokens[1]} = '0'; | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $s0 = pop @tokens; | ||
| 152 | my $f0 = delete $freq->{$s0}; | ||
| 153 | |||
| 154 | # print STDERR "Least frequent is token $s0 with freq $f0\n"; | ||
| 155 | |||
| 156 | my $s1 = $tokens[-1]; | ||
| 157 | $freq->{$s1} += $f0; | ||
| 158 | |||
| 159 | $self->huffman_recurse($code, $freq); | ||
| 160 | |||
| 161 | $code->{$s0} = $code->{$s1} . '0'; | ||
| 162 | $code->{$s1} .= '1'; | ||
| 163 | |||
| 164 | $freq->{$s1} -= $f0; | ||
| 165 | $freq->{$s0} = $f0; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub freqtable { | ||
| 169 | my ($self, $bits, $frag) = @_; | ||
| 170 | |||
| 171 | $bits = $self->{Bits}->[-1] unless $bits; | ||
| 172 | |||
| 173 | my $len = int(length($bits) / $frag); | ||
| 174 | my %freq = (); | ||
| 175 | |||
| 176 | foreach (0..($len - 1)) { | ||
| 177 | my $ss = substr($bits, $_ * $frag, $frag); | ||
| 178 | $freq{$ss}++; | ||
| 179 | } | ||
| 180 | |||
| 181 | return %freq; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub huffman { | ||
| 185 | my $self = shift; | ||
| 186 | |||
| 187 | my $frag = 12; | ||
| 188 | |||
| 189 | my $bits = $self->{Bits}->[-1]; # Last element | ||
| 190 | |||
| 191 | $self->display(3, "huffman code", $bits); | ||
| 192 | |||
| 193 | my $len = int(length($bits) / $frag); | ||
| 194 | my %freq = $self->freqtable($bits, $frag); | ||
| 195 | my %code = (); | ||
| 196 | |||
| 197 | foreach (0..($len - 1)) { | ||
| 198 | my $ss = substr($bits, $_ * $frag, $frag); | ||
| 199 | $code{$ss} = ''; | ||
| 200 | } | ||
| 201 | |||
| 202 | # print Dumper(\%freq); | ||
| 203 | |||
| 204 | my @tokens = keys %freq; | ||
| 205 | if (@tokens == 0) { | ||
| 206 | } | ||
| 207 | elsif (@tokens == 1) { | ||
| 208 | $code{$tokens[0]} = '1'; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $self->huffman_recurse(\%code, \%freq); | ||
| 212 | } | ||
| 213 | |||
| 214 | # print Dumper(\%code); | ||
| 215 | |||
| 216 | my $out = ''; | ||
| 217 | foreach (0..($len - 1)) { | ||
| 218 | $out .= $code{substr($bits, $_ * $frag, $frag)}; | ||
| 219 | } | ||
| 220 | |||
| 221 | $self->display(1, "huffman output", $out); | ||
| 222 | |||
| 223 | return $out; | ||
| 224 | } | ||
| 225 | |||
| 226 | 1; |