Note that there are some explanatory texts on larger screens.

plurals
  1. POString to BigNum and back again (in Ruby) to allow circular shift
    primarykey
    data
    text
    <p>As a personal challenge I'm trying to implement the SIMON block cipher in Ruby. I'm running into some issues finding the best way to work with the data. The full code related to this question is located at: <a href="https://github.com/Rami114/Personal/blob/master/Simon/Simon.rb" rel="nofollow">https://github.com/Rami114/Personal/blob/master/Simon/Simon.rb</a></p> <p>SIMON requires both XOR, shift and circular shift operations, the last of which is forcing me to work with BigNums so I can perform the left circular shift with math rather than a more complex/slower double loop on byte arrays.</p> <p>Is there a better way to convert a string to a BigNum and back again. </p> <p>String -> BigNum (where N is 64 and pt is a string of plaintext)</p> <pre><code> pt = pt.chars.each_slice(N/8).map {|x| x.join.unpack('b*')[0].to_i(2)}.to_a </code></pre> <p>So I break the string into individual characters, slice into N-sized arrays (the word size in SIMON) and unpack each set into a BigNum. That appears to work fine and I can convert it back.</p> <p>Now my SIMON code is currently broken, but that's more the math I think/hope and not the code. The conversion back is (where ct is an array of bignums representing the ciphertext):</p> <pre><code>ct.map { |x| [x.to_s(2).rjust(128,'0')].pack('b*') }.join </code></pre> <p>I seem to have to right-justify pad the string as bignums are of undefined width so I have no leading 0s. Unfortunately the pack requires the defined with to have sensible output.</p> <p>Is this a valid method of conversion? Is there a better way? I'm not sure on either count and hoping someone here can help out.</p> <p>E: For @torimus, the circular shift implementation I'm using (From link above)</p> <pre><code>def self.lcs (bytes, block_size, shift) ((bytes &lt;&lt; shift) | (bytes &gt;&gt; (block_size - shift))) &amp; ((1&lt;&lt; block_size)-1) end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload