Class: ToyOre::Scheme::OreScheme
- Inherits:
-
Object
- Object
- ToyOre::Scheme::OreScheme
- Defined in:
- lib/toy_ore/scheme.rb
Instance Method Summary collapse
- #encrypt(plaintext) ⇒ Object
-
#initialize(domain_size = 0..255) ⇒ OreScheme
constructor
A new instance of OreScheme.
Constructor Details
#initialize(domain_size = 0..255) ⇒ OreScheme
Returns a new instance of OreScheme.
115 116 117 118 119 120 121 122 |
# File 'lib/toy_ore/scheme.rb', line 115 def initialize(domain_size = 0..255) @domain_size = domain_size # PRF key - hash key @prf = (domain_size).to_a.shuffle() # PRP key - shuffle key @prp = (domain_size).to_a.shuffle() end |
Instance Method Details
#encrypt(plaintext) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/toy_ore/scheme.rb', line 124 def encrypt(plaintext) iv = rand(@domain_size) # This represents all the values in the domain. # This is a small domain example. # This is 1 block, 1 byte, 8 bits, total value is 256. domain = (@domain_size).to_a # Array to hold the encrypted value of the cmp result for each value in the domain. encryptions = [] domain.each do |d| # The offset is what ever index d (domain value) is in the shuffled PRP array. # If we used the plaintext value as the offset, this would give away the value of the plaintext. # Using the PRP, swaps the plaintext value for a random number in the PRP to store the encrypted # comparison result in the right ciphertext encryptions array. offset = @prp[d] # Compare the domain value to the plaintext value # We get -1, 0 or 1 here. cmp_result = ToyOre::Scheme.compare_plaintexts(d, plaintext) # At index offset in the encryptions array # set the xor'd value of (iv and value) xor'd with the cmp result # to the index at the offset value. # We store the encrypted value of the comparison result # in the encryptions array # at the index = to the offset. # # To encrypt we pass the random iv, the key is the value at the offset index in the # random shuffled key array. # # encryptions[offset] = ToyOre::Scheme.encrypt(iv, @prf[offset], cmp_result) end # The left ciphertext: # 1. Stores the value of the offset. # # The offset is the index of the encrypted comparison result, # in the encryptions array. # # 2. Stores the key that was used to encrypt the comparison result # at the offset in the right ciphertext. # # The right ciphertext: # # 1. Stores the IV used in the encryption # 2. Stores an array of all encrypted comparison results. # # # OreCiphertext.new(@prp[plaintext], @prf[@prp[plaintext]], iv, encryptions) end |