<commit_before>require "bytestream"
# This is to help testing
unless(defined? Crypt::Rijndael::Core)
	require "crypt/rijndael/core"
end

class Crypt
    class Rijndael

=begin rdoc
Crypt::Rijndael allows you to encrypt single blocks of data using the encrypt() and decrypt() methods
below. For convenience, there are also simple CBC encryption wrappers here, although they don't really
belong in this class.
=end
    
        DEFAULT_KEYSIZE_BYTES=16
        DEFAULT_BLOCKSIZE_BYTES=16
        
        @@valid_blocksizes_bytes=[16, 24, 32]
        @@valid_keysizes_bytes=[16, 24, 32]

=begin rdoc
The new() function here takes only one argument: the key to use, as a String (or similar). Valid lengths
are 16, 24 or 32 bytes, and you should ensure that this value is sufficiently random. Most people will
choose 16-byte (128-bit) keys, but a longer key will take longer to crack if security is of unusually
high importance for you.
=end
        def initialize(key)
            raise "Invalid key length" unless(@@valid_keysizes_bytes.find {|size| size==key.length})
            @key=key
            @key_words=@key.length/4
            @block_words=DEFAULT_BLOCKSIZE_BYTES/4
        end
        
        def blocksize=(isize) #:nodoc:
            if(@@valid_blocksizes_bytes.find { |size| size==isize })
                @block_words=isize/4
                @expanded_key=nil
                return self
            else
                raise "Invalid block size selected: #{isize}"
            end
        end
        
        def keysize #:nodoc:
            return @key.length
        end
        
        def blocksize #:nodoc:
            return @block_words*4
        end
        
        
        def Rijndael.pad_pkcs5(string, to_length) #:nodoc:
            diff= to_length - (string.length % to_length)
            string+=[diff].pack("C") * diff
            return string
        end
        
        def Rijndael.unpad_pkcs5(string) #:nodoc:
            return unless string.length > 0
            
            pad_len=string[-1]
            string.slice!(-pad_len .. -1)
            return string
        end
        
        @@shift_for_block_len={
          4=>[0,1,2,3],
          6=>[0,1,2,3],
          8=>[0,1,3,4],
        };
              

        def Rijndael.make_shiftrow_map  #:nodoc:
            @@inv_shiftrow_map=(0 .. 0xff).map {Array.new}
            @@shiftrow_map=(0 .. 0xff).map {Array.new}  
            @@shift_for_block_len.keys.each do
                |block_len|
                row_len=block_len;
                state_b=(0 .. (row_len*4)-1).to_a;
                col_len=4;
                c=@@shift_for_block_len[block_len];
                (0 .. c.length-1).each do
                    |row_n| 
                    # Grab the lossage first
                    next unless c[row_n] > 0;
                    d1=Array.new
                    d2=Array.new
                    (row_len-c[row_n] .. row_len-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        d1+=state_b[offset,1]
                    end 
                    (0 .. row_len-c[row_n]-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        d2+=state_b[offset,1]
                    end  
                    
            	(0 .. row_len-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        state_b[offset]=d1.shift||d2.shift
                    end
                end
                @@inv_shiftrow_map[block_len]=state_b;
                (0 .. state_b.length-1).each do
                    |offset|
                    @@shiftrow_map[block_len][state_b[offset]]=offset;
                end
            end
        end
        
        make_shiftrow_map

        def shift_rows(state_b) #:nodoc:
          row_len=state_b.length/4
          
		  state_o=@@shiftrow_map[row_len].map do
				|offset|
				state_b[offset]
		  end
		  return state_o.pack("C*")
        end
        
        def inv_shift_rows(state_b) #:nodoc:
          col_len=4;
          row_len=state_b.length/4;
          
            state_o=@@inv_shiftrow_map[row_len].map do
                |offset|
                state_b[offset]
            end
            return state_o.pack("C*")
        end
        
        def round0(input, round_key) #:nodoc:
            return round_key^input;
        end
        
        def roundn(input, round_key) #:nodoc:
            row_len=@block_words;
        
            input=Core.sbox_block(input)
            input=shift_rows(input)       
            # Tune this - jim<commit_msg>Might want to "introspect" the shiftrow map<commit_after>require "bytestream"
# This is to help testing
unless(defined? Crypt::Rijndael::Core)
	require "crypt/rijndael/core"
end

class Crypt
    class Rijndael

=begin rdoc
Crypt::Rijndael allows you to encrypt single blocks of data using the encrypt() and decrypt() methods
below. For convenience, there are also simple CBC encryption wrappers here, although they don't really
belong in this class.
=end
    
        DEFAULT_KEYSIZE_BYTES=16
        DEFAULT_BLOCKSIZE_BYTES=16
        
        @@valid_blocksizes_bytes=[16, 24, 32]
        @@valid_keysizes_bytes=[16, 24, 32]

=begin rdoc
The new() function here takes only one argument: the key to use, as a String (or similar). Valid lengths
are 16, 24 or 32 bytes, and you should ensure that this value is sufficiently random. Most people will
choose 16-byte (128-bit) keys, but a longer key will take longer to crack if security is of unusually
high importance for you.
=end
        def initialize(key)
            raise "Invalid key length" unless(@@valid_keysizes_bytes.find {|size| size==key.length})
            @key=key
            @key_words=@key.length/4
            @block_words=DEFAULT_BLOCKSIZE_BYTES/4
        end
        
        def blocksize=(isize) #:nodoc:
            if(@@valid_blocksizes_bytes.find { |size| size==isize })
                @block_words=isize/4
                @expanded_key=nil
                return self
            else
                raise "Invalid block size selected: #{isize}"
            end
        end
        
        def keysize #:nodoc:
            return @key.length
        end
        
        def blocksize #:nodoc:
            return @block_words*4
        end
        
        
        def Rijndael.pad_pkcs5(string, to_length) #:nodoc:
            diff= to_length - (string.length % to_length)
            string+=[diff].pack("C") * diff
            return string
        end
        
        def Rijndael.unpad_pkcs5(string) #:nodoc:
            return unless string.length > 0
            
            pad_len=string[-1]
            string.slice!(-pad_len .. -1)
            return string
        end
        
        @@shift_for_block_len={
          4=>[0,1,2,3],
          6=>[0,1,2,3],
          8=>[0,1,3,4],
        };
              

        def Rijndael.make_shiftrow_map  #:nodoc:
            @@inv_shiftrow_map=(0 .. 0xff).map {Array.new}
            @@shiftrow_map=(0 .. 0xff).map {Array.new}  
            @@shift_for_block_len.keys.each do
                |block_len|
                row_len=block_len;
                state_b=(0 .. (row_len*4)-1).to_a;
                col_len=4;
                c=@@shift_for_block_len[block_len];
                (0 .. c.length-1).each do
                    |row_n| 
                    # Grab the lossage first
                    next unless c[row_n] > 0;
                    d1=Array.new
                    d2=Array.new
                    (row_len-c[row_n] .. row_len-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        d1+=state_b[offset,1]
                    end 
                    (0 .. row_len-c[row_n]-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        d2+=state_b[offset,1]
                    end  
                    
            	(0 .. row_len-1).map {|col| row_n+col_len*col}.each do
                        |offset|
                        state_b[offset]=d1.shift||d2.shift
                    end
                end
                @@inv_shiftrow_map[block_len]=state_b;
                (0 .. state_b.length-1).each do
                    |offset|
                    @@shiftrow_map[block_len][state_b[offset]]=offset;
                end
            end
        end
        
        make_shiftrow_map

				def Rijndael.shiftrow_map
					@@shiftrow_map
				end

        def shift_rows(state_b) #:nodoc:
          row_len=state_b.length/4
          
		  state_o=@@shiftrow_map[row_len].map do
				|offset|
				state_b[offset]
		  end
		  return state_o.pack("C*")
        end
        
        def inv_shift_rows(state_b) #:nodoc:
          col_len=4;
          row_len=state_b.length/4;
          
            state_o=@@inv_shiftrow_map[row_len].map do
                |offset|
                state_b[offset]
            end
            return state_o.pack("C*")
        end
        
        def round0(input, round_key) #:nodoc:
            return round_key^input;
        end
        
        def roundn(input, round_key) #:nodoc:
            row_len=@block_words;
        
            input=Core.sbox_block(input)
            input=shift_rows(input)       
            # Tune this - jim
{'fraction_non_alphanumeric': 0.09511073071573767, 'fraction_numerical': 0.017331764202417888, 'mean_word_length': 1.4167528438469492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '45647', 'n_tokens_mistral': 3000, 'n_tokens_neox': 2792, 'n_words': 621}