#!/usr/local/bin/perl -w # # Free Software, (c) David MacKay August 2003 # Distributed under the GPL. # http://www.gnu.org/copyleft/gpl.html # # This program uses the output from hz.p # to make rows of weight tr drawn from GF(q) # usage: # GFQrows.p q=64 M=168 < GFQ/GF64.4.in > GFQ/GF64.4.rows # GFQrows.p q=256 M=126 < GFQ/GF258.4.in # # see code/GFQ/README and GF64.4 $tr = 4 ; # number of elemnts (row weight) $q = 64 ; # which field to do $M=168 ; $verbose = 0 ; @GF16=(0,0,0,0, 0,0,0,0, 0 ,0 ,0 ,0 , 0 ,0 ,0 ,0 , 0,1 ,2 ,3 , 4 ,5 ,6 ,7 , 8 ,9 ,10,11, 12,13,14,15, 0,2 ,4 ,6 , 8 ,10,12,14, 3 ,1 ,7 ,5 , 11,9 ,15,13, 0,3 ,6 ,5 , 12,15,10,9 , 11,8 ,13,14, 7 ,4 ,1 ,2 , 0,4 ,8 ,12, 3 ,7 ,11,15, 6 ,2 ,14,10, 5 ,1 ,13,9 , 0,5 ,10,15, 7 ,2 ,13,8 , 14,11,4 ,1 , 9 ,12,3 ,6 , 0,6 ,12,10, 11,13,7 ,1 , 5 ,3 ,9 ,15, 14, 8, 2, 4, 0,7 ,14,9 , 15,8 ,1 ,6 , 13,10,3 ,4 , 2 , 5,12,11, 0,8 ,3 ,11, 6 ,14,5 ,13, 12,4 ,15,7 , 10, 2, 9, 1, 0,9 ,1 ,8 , 2 ,11,3 ,10, 4 ,13,5 ,12, 6 ,15, 7,14, 0,10,7 ,13, 14,4 ,9 ,3 , 15,5 ,8 ,2 , 1 ,11,6 ,12, 0,11,5 ,14, 10,1 ,15,4 , 7 ,12,2 ,9 , 13,6 ,8 ,3 , 0,12,11,7 , 5 ,9 ,14,2 , 10,6 ,1 ,13, 15,3 ,4 ,8 , 0,13,9 ,4 , 1 ,12,8 ,5 , 2 ,15,11,6 , 3 ,14,10,7 , 0,14,15,1 , 13,3 ,2 ,12, 9 ,7 ,6 ,8 , 4 ,10,11,5 , 0,15,13,2 , 9 ,6 ,4 ,11, 1 ,14,12,3 , 8 ,7 ,5 ,10) ; # How to override with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $B = $q == 4 ? 2 : ( $q == 8 ) ? 3 : ($q == 16) ? 4: ($q == 64) ? 6 : ($q == 256) ? 8 : ($q==2)?1: 0 ; if ( !$B ) { print "Hell baby I jus' don't know what this damn B is! $q\n" ;} if ($B==6) { $Q = 64 ; $mask = "1000011" ; $mask = 2**6+2+1 ; &setupm; # creates look up table for multiply } if ($B==8) { $Q = 256 ; $mask = 2**8+2**4+2**3+2+1 ; &setupm; # creates look up table for multiply } # http://www.cs.utsa.edu/~wagner/laws/FFM.html $Q = $q-1 ; sub tablecheck { for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { printf "+ %2d" , &gfa($a,$b ) ; } print "\n" ; } print "\n" ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { printf "* %2d" , &gfm($a,$b ) ; } print "\n" ; } print "\n" ; } sub vtablecheck { for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { print " $a + $b = " , &gfa($a,$b ) , " : " ; } print "\n" ; } print "\n" ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { print " $a * $b = " , &gfm($a,$b ), " : " ; } print "\n" ; } print "\n" ; } if ( $verbose > 1 ) { &vtablecheck() ; } elsif ( $verbose ) { &tablecheck() ; } # read in the required elementary rows $i = 0 ; while(<>) { $i ++ ; @a = split ; for ( $k = 1 ; $k <= $tr ; $k ++ ) { $row{$i,$k} = $a[$k-1] ; } } $I = $i ; if($verbose) { print "Read in rows:\n" ; for ( $i = 1 ; $i <= $I ; $i ++ ) { for ( $k = 1 ; $k <= $tr ; $k ++ ) { print "$row{$i,$k} "; } print "\n" ; } } for ( ; $M>0 ; $M-- ) { # select a random multipler $u = int(rand( $q-1 )) + 1 ; # select a random row in source list $i = int(rand( $I )) + 1 ; # select a random permutation @pe = permutation( $tr-1 , 1 ) ; if ( 1 ) { # obsessive checking assert( $u>=1 ) ||die "u>=1"; assert( $u<=($q-1) ) ||die "u<=q-1"; assert( $i>=1 ) ||die ">=1"; assert( $i<=$I ) ||die "<=I"; } if ( $verbose ) { # verbose information printf "MULT: %3d ", $u ; printf "ROW: %2d ", $i ; printf "PERM: " ; for ( $k = 1 ; $k <= $tr ; $k ++ ) { print "$pe[$k-1] " ; assert( $pe[$k-1]>=1 ) ||die "pe>=1"; assert( $pe[$k-1]<=$tr ) ||die "pe<=tr"; } } for ( $k = 1 ; $k <= $tr ; $k ++ ) { $kkk = $pe[$k-1] ; $a = &gfm( $u , $row{$i,$kkk} ) ; printf "%3d ", $a ; assert( $a>=1 ) ||die "a>=1"; assert( $a<=($q-1) ) ||die "a<=q-1"; } print "\n" ; } sub permutation { # returns a permn of 0..Q offset by offset eg 0/1 local ( $Q , $offset ) = @_ ; for ( local($q) = 0 ; $q <= $Q ; $q ++ ) { $qs[$q] = $q + $offset ; } for ( local($n) = 0 ; $n <= $Q ; $n ++ ) { # $from = $n ; $q = int ( rand($#qs+1) ) ; $to[$n] = splice ( @qs , $q , 1 ) ; print "to[$n] = $to[$n]\n" if ( $verbose >=3 ) ; } print "Here is a perm $Q:\n" if ( $verbose >=3 ) ; print join ( ':' , @to ) , "\n" if ( $verbose >=3 ) ; return ( @to ) ; } sub assert { local($assertion) =@_ ; if($assertion) { # happy return 1; } else { print "Assertion false\n" ; return 0; } } exit(0); sub gfa { local ( $a , $b ) = @_ ; return $a ^ $b ; } sub fft { # receives a list argument, returns a list # # Note that this should divide by sqrt($q) if it is to be # a self-inverse transform. # I prefer to do a division by $q by hand at a convenient # moment. # local ( @p ) = @_ ; local ( $Q2 ) = $q/2 - 1 ; local ( $b ) = 1 ; local ( $factor ) = 1 ; for ( ; $b <= $B ; $b ++ ) { print "b = $b\n" if ($verbose >= 3 ) ; for ( $rest = 0 ; $rest <= $Q2 ; $rest ++ ) { $restH = int($rest / $factor ) ; # integer $restL = $rest % $factor ; print "$restH:$restL\n" if ($verbose >= 3 ) ; $rest0 = $restH * $factor * 2 + $restL ; $rest1 = $rest0 + $factor ; print "FFT munging $rest0,$rest1: $p[$rest0],$p[$rest1] to " if ( $verbose >= 3) ; local ($sum) = $p[$rest0] + $p[$rest1] ; $p[$rest0] = $p[$rest1] - $p[$rest0] ; $p[$rest1] = $sum ; print "$p[$rest0],$p[$rest1]\n" if ( $verbose >= 3 ) ; } $factor = $factor * 2 ; } return @p ; } sub gfm { local ( $a , $b ) = @_ ; if (( $q == 64 )||( $q == 256 )) { return $gf64{$a,$b} ; } elsif ( $q == 16 ) { return $GF16[ ( $a ) * 16 + ( $b ) ] ; } if ( $a == 0 || $b == 0 ) { return 0 ; } elsif ( $a == 1 ) { return $b ; } elsif ( $b == 1 ) { return $a ; } elsif ( $q == 4 ) { if ( $a == 2 ) { if ( $b == 2 ) { return 3 ; } elsif ( $b == 3 ) { return 1 ; } else { print "ERR" ; } } elsif ( $a == 3 ) { if ( $b == 2 ) { return 1 ; } elsif ( $b == 3 ) { return 2 ; } else { print "ERR" ; } } else { print "ERR" ; } } elsif ( $q == 8 ) { if ( $a == 2 ) { if ( $b == 2 ) { return 4 ; } elsif ( $b == 3 ) { return 6 ; } elsif ( $b == 4 ) { return 3 ; } elsif ( $b == 5 ) { return 1 ; } elsif ( $b == 6 ) { return 7 ; } elsif ( $b == 7 ) { return 5 ; } else { print "ERR" ; } } elsif ( $a == 3 ) { if ( $b == 2 ) { return 6; } elsif ( $b == 3 ) { return 5; } elsif ( $b == 4 ) { return 7; } elsif ( $b == 5 ) { return 4; } elsif ( $b == 6 ) { return 1; } elsif ( $b == 7 ) { return 2; } else { print "ERR" ; } } elsif ( $a == 4 ) { if ( $b == 2 ) { return 3; } elsif ( $b == 3 ) { return 7; } elsif ( $b == 4 ) { return 6; } elsif ( $b == 5 ) { return 2; } elsif ( $b == 6 ) { return 5; } elsif ( $b == 7 ) { return 1; } else { print "ERR" ; } } elsif ( $a == 5 ) { if ( $b == 2 ) { return 1 ; } elsif ( $b == 3 ) { return 4 ; } elsif ( $b == 4 ) { return 2 ; } elsif ( $b == 5 ) { return 7 ; } elsif ( $b == 6 ) { return 3 ; } elsif ( $b == 7 ) { return 6 ; } else { print "ERR" ; } } elsif ( $a == 6 ) { if ( $b == 2 ) { return 7 ; } elsif ( $b == 3 ) { return 1 ; } elsif ( $b == 4 ) { return 5 ; } elsif ( $b == 5 ) { return 3 ; } elsif ( $b == 6 ) { return 2 ; } elsif ( $b == 7 ) { return 4 ; } else { print "ERR" ; } } elsif ( $a == 7 ) { if ( $b == 2 ) { return 5 ; } elsif ( $b == 3 ) { return 2 ; } elsif ( $b == 4 ) { return 1 ; } elsif ( $b == 5 ) { return 6 ; } elsif ( $b == 6 ) { return 4 ; } elsif ( $b == 7 ) { return 3 ; } else { print "ERR" ; } } else { print "ERR" ; } } else { print "ERR" ; } return 0 ; } sub initialize { for ( $a = 0 ; $a <= $Q ; $a ++ ) { $p[$a] = 0.0 ; } $p[0] = 1.0 ; @P = &fft ( @p ) ; } sub fftentropy { @p = fft( @P ) ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { $p[$a] = $p[$a] / $q ; } &entropy(); } sub entropy { $H = 0.0 ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { $H -= &plgp( $p[$a] ) ; } } sub H2 { # binary entropy in natural units # usage ( $f ) local ( $f ) = @_ ; local ( $ans ) = - ( $f * &safelog ( $f ) + (1.0 - $f) * &safelog (1.0 - $f ) ) ; return $ans ; } sub plgp { local ($x) = @_ ; return $x * safelog($x) / log(2.0) ; } sub safelog { local ($f) = @_ ; if ( $f < 0 ) { print STDERR "Can't take log of $f\n" ; } return ( $f > 0 ? log ( $f) : 0.0 ) ; } sub setupm { $i=0; # enter the look up table for ( $j = 0 ; $j < $Q ; $j ++ ) { $gf64{$j,0}=0; $gf64{0,$j}=0; } for ( $j = 1 ; $j < $Q ; $j ++ ) { for ($b=0;$b<$B;$b++) { # create j shifted to the left by 0...B-1 $s[$b] = $j << $b ; } for ( $i = 1 ; $i < $Q ; $i ++ ) { $m=0 ; $ii = $i ; for ($b=0;$b<$B;$b++) { if ( $ii % 2 ) { $m ^= $s[$b] ; } $ii = $ii / 2 ; # shift right and lose the bit. } # now subtract out all the overflowing stuff. for ($b=$B-1;$b>=0;$b--) { if ( ( $m & ( 1 << ($b+$B) ) ) > 0 ) { $m ^= ($mask << $b) ; } # assert... if ( ( ( $m & ( 1 << ($b+$B) ) ) > 0 ) ) { print "something wrong!\n" ; } } $gf64{$i,$j} = $m ; } } if($verbose) { print "\n" ; for ( $j = 1 ; $j < $Q ; $j ++ ) { for ( $i = 1 ; $i <= $j ; $i ++ ) { printf "%2d " , $gf64{$i,$j} ; if ( !($gf64{$i,$j} == $gf64{$j,$i} ) ) { print "error!\n" ; printf "%2d,%2d " ,$gf64{$i,$j},$gf64{$j,$i} ; } } print "\n" ; } } print "\n" ; }