Operations for rotations in ciphertext using SEAL - seal

Just wanted to know if it is possible for us to perform some operations on rotated ciphertexts such that only part of the ciphertext takes part in the operation and not the whole object.
Let's say we want only the slot_count/2 part of the ct to take part in the operation and the latter slot_count/2 should be left as it is.
Any ideas how to go about it?
I am currently using the BFV scheme

Since we can't really choose which slots will be rotated, the general way to achieve this is more of a workaround:
Given seal::Ciphertext x, int steps and appropriate evaluator/encoder and keys as in the Seal Rotation Example (including Explanations) we can do the following:
make a copy of x seal::Ciphertext copy = x;
rotate the copy evaluator.rotate_vector_inplace(copy, steps, galoisKeys); (assuming you're using the CKKS scheme)
multiply with a mask (plaintext with 0 or 1 in the slots)
std::vector<unsigned long int> mask = {1,1,1,1,1,0,0,0,0 /*...*/};
seal::Plaintext mask_ptxt;
encoder.encode(mask, mask_ptxt);
evaluator.multiply_plain_inplace(copy, mask_ptxt);
multiply the original with the inverse mask
std::vector<unsigned long int> inv_mask = {0,0,0,0,0,1,1,1,1 /*...*/};
seal::Plaintext inv_mask_ptxt;
encoder.encode(inv_mask , inv_mask_ptxt);
evaluator.multiply_plain_inplace(x, mask_ptxt);
add the original and rotated ciphertext together evaluator.add_inplace(x,copy);

Related

Apply an operation on a sliding window in Matlab

I have an big image and I have to :
First, apply a function to every possible patch of the image, like a sliding window. This is actually very similar to convolution, which is supported in Matlab, but instead I need to calculate a "key value" (real) on each image patch (let's consider it a black box function). As suggested by the comments, perharps I can use the "blockproc" function
Then, I need to find n smallest key values and their respective positions, but the catch is that I have several overlapping windows with similarly low key values then sorting will preserve all of them in the list, which is undesirable. Instead I want to detect those overlapping windows and keep only the one with the lowest key value. You can say that I want to find not the n smallest key values but only the n local minima (not sure if this analogy is correct though) . I can't give the code because it is too long and complicated (face recognition using eigenfaces with +5 functions)
Step 1: apply nlfilter to the original image:
keyimg = nlfilter(img, windowsize, keyfun);
Step 2: apply im2col to keyimg and sort key values:
colimg = im2col(keyimg, windowsize, 'sliding');
minimg = sort(colimg, 1); % perhaps take only the first `k` rows

How to efficiently loop through matrix elements

I have a matlab script for 8-bit image analysis and I'm trying to enhance objects in the image by subtracting the background with no objects present. What I want to do at a pixel level is:
if B-I>50 then E=I
else E=255-B-I
Where, B is the background, I the image and E my enhanced image. I know I can do this by looping through each element of the image matrix by the following:
diff=imsubtract(B,I);
nrows=1024;
ncols=1360;
for r=1:nrows
for c=1:ncols
if diff(r,c)>50
E=I(r,c);
else
E=255-diff(r,c);
end
end
end
But is this rather slow when going multiple images. I've also tried the follow:
E=255-diff;
E(diff>50)=I;
But receive the following error:
In an assignment A(I) = B, the
number of elements in B and I must
be the same.
Any tips on optimizing this would be greatly apprenticed!
In an assignment A(I) = B, the number of elements in B and I must be
the same.
The reason for this error is that you are trying to assign all the content of I to a subset of E (those pixels where diff>50). You have to specifically tell MATLAB that you want those pixels set to the matching pixels in I.
E(diff>50)=I(diff>50);
Incidentally you should be careful using imsubtract here. For pixels where I has a higher value than B, that will result in zeros (if your values are uint8). It may be okay (not 100% clear if you're looking for the absolute difference or really just where B is larger than I)
What if you use use find()
ind = find(B-I>50)
E(ind) = I(ind)
% And then the ones that are not `B-I>50`
E(~ind) = 255-B(~ind)-I(~ind)
Try this vectorized approach that uses logical indexing. I could not test it out on images though, so would be great if that's taken care of.
Code
diff1=double(imsubtract(B,I));
E = double(I).*(diff1>50) + (255-diff1).*(diff1<=50);
You might be needed to convert the datatype back to unsigned integer formats as used for images.

BPCS STEGANOGRAPHY

i am doing BPCS STEGANOGRAPHY.. i have separate secret blocks (4096- 8*8 blocks) and conjugation block( 64 - 8*8 blocks) ready for embedding. each can be accessed separately by calling
secret_block(:,:,1),
secret_block(:,:,2),
secret_block(:,:,3)
etc and
conju_block(:,:,1),
conju_block(:,:,2),
conju_block(:,:,3)
etc
During bpcs embedding cover image is bitplane sliced and stored as c1( lsb plane ),c2,c3,c4 c5,c6,c7,c8 (msbplane). I need to select lsb bit plane to msb plane and dividing each bitplane into 8*8 block, selected_block(:,:,i)) and among them complex blocks are replaced with initially secret_block (:,:,i) and later on conju_block(:,:,i) etc until all the 4096+64 blocks reached..
Please anyone tell me how replace on particular block of selected_block(:,:,i) with another block (conju_block(:,:,i) or secret_block(:,:,i)) ? Is it possible to use replace_block() for the same ,then what does 'sys' parameter means ????
I would start by putting all bit planes in one cell,
C{1} = c1;
C{2} = c2;
etc
so you can go through the bit planes in an automated manner.
for i = 1:8
curr_plane = C{i};
end
There are many ways to split a 2D array into blocks. A convenient one is mat2cell. The web documentation gives you a visual example of how it works.
Note that since you want 8x8 blocks, both the rows and columns must be exactly divisible by 8. You have 3 options:
only allow images with number of rows and columns divisible by 8,
automatically rescale images to such a size, or
take the biggest slice of the image matrix with such a size, do your steganography and put that modified image back into your slice of the original image, from where it came from.
We continue with the assumption that you have an image with number of rows and columns divisible by 8. Let's call your initial cover image cover.
row = size(cover, 1)/8;
col = size(cover, 2)/8;
blocks = mat2cell(curr_plane, ones(1, row)*8, ones(1, col)*8);
blocks is now a cell which holds all the 8x8 blocks of your bit plane, curr_plane. For example, if your bit plane had a size of 16x48, you now have 2x6 blocks. You can iterate through them simply with
for bi = 1:size(blocks,1)
for bj = 1:size(blocks,2)
my_8x8_block = blocks{bi,bj};
end
end
You're almost there. To sum up what we've done, we go from bit plane to bit plane and split each one in 8x8 blocks. Now, for every my_8x8_block, you can feed it into a function which calculates whether it's complex enough or not. If it is, you can simply overwrite it with the kth secret or conjugate block.
blocks{bi,bj} = secret_block(:,:,k);
Finally, once you have gone through a whole bit plane, you can put back the blocks to construct the modified bit plane
C{i} = cell2mat(blocks);

Find the connected component in matlab

Given a BW image that contains some connected components.
Then, given a single pixel P in the image. How to find which component that contains the pixel P? It is guaranteed that the pixel P is always on the white area in one of the connected components.
Currently, I use CC = bwconncomp(BW) than I iterate each component using 'for' loop. In the each component, I iterate the index pixel. For each pixels, I check whether the value equal to the (index of) pixel P or not. If I find it, I record the number of connected component.
However, it seems it is not efficient for this simple task. Any suggestion for improvement? Thank you very much in advance.
MATLAB provides multiple functions that implement connected-component in different ways.
In your example, I would suggest bwlabel.
http://www.mathworks.com/help/images/ref/bwlabel.html
[L, num] = bwlabel(imgBW) This will perform a full-image connected-component labeling on a black-and-white image.
After calling this function, the label value that pixel P belongs to can be read off the result matrix L, as in label_to_find = L(row, col) index. Simple as that.
To extract a mask image for that label, use logical(L == label_to_find).
If you use different software packages such as OpenCV you will be able to get better performance (efficiency in terms of cutting unnecessary or redundant computation), but in MATLAB the emphasis is on convenience and prototyping speed.

Bit Rotation operation

Is there a way to make the Bit rotation operation reversible? I mean, if there is an image X (size 256 * 256 *3) then on doing bit rotation, image Y is obtained. Then on subjecting Y to a bit rotation, we get back image X. Also, How to tackle the bit overflow so that there is no information loss.
UPDATE: I've taken the code I posted below and refined it into a complete function with error-checking, help documentation, and the ability to operate on arrays of unsigned integers and double-precision variables just like the related built-in function BITSHIFT. I suggest using the newer version I link to above instead of the older version posted below.
MATLAB does not have a built-in bit rotation function, and the BITSHIFT function will drop bits that overflow. However, you could implement your own bit rotation function based on the existing bit operations. Here's a simple first-pass version I threw together (sans error-checking):
function data = bit_rotate(data,nBits)
dataBits = log2(double(intmax(class(data)))+1); %# Number of bits in data
nBits = rem(nBits,dataBits); %# No need to rotate by dataBits bits or more
if nBits == 0 %# No bit rotation needed, just return
return
end
shiftedData = bitshift(data,nBits); %# Bit shift the data
lostData = bitxor(data,bitshift(shiftedData,-nBits)); %# Find the lost bits
rotatedData = bitshift(lostData,nBits-sign(nBits)*dataBits); %# Rotate them
data = shiftedData+rotatedData; %# Add the rotated bits to the shifted bits
end
And here's some test data:
>> B = uint8(208); %# An unsigned 8-bit integer value
>> disp(dec2bin(B,8)) %# Display the bit pattern of B
11010000
>> disp(dec2bin(bit_rotate(B,2),8)) %# Rotate left by 2 bits
01000011
>> disp(dec2bin(bit_rotate(B,-2),8)) %# Rotate right by 2 bits
00110100
Note that bit_rotate will also operate on any size matrix input for data as long as it's an unsigned integer type.
Sure, bit rotation is reversible, just rotate the other way around by the same amount.
Wikipedia has nice info on how to implement it in C with basic bit shifts so that you don't get overflow:
http://en.wikipedia.org/wiki/Circular_shift
And I suppose that if the operation is called "bit rotation" in Matlab, it surely doesn't overflow anyway.