Thread: set testing code

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    set testing code

    I have a S-Box(lookup table) that I'm trying to test. The test involved creating a set (the details are very small and in the attachment, so its formated nicely) and checking its size. Since I'm working in a GF(2^4), that means additions and subtraction are both equivalent to a XOR.

    I ran my code on a S-Box I'm sure is supposed to pass. I cant figure out why.

    Anyone mind taking a look?
    Thanks?

    Code:
    int testCond1(unsigned char *sbox) {
            int count;
    
            for(unsigned char d_input = 1; d_input < 16; ++d_input) {
                    for(unsigned char d_output = 1; d_output < 16; ++d_output) {
                            count = 0;
    
                            for(unsigned char x = 0; x < 16; ++x) {
                                    if(sbox[x] ^ sbox[x ^ d_input] == d_output)
                                            ++count;
                            }
    
                            if(count > 4)
                                    return 1;
                    }
            }
    
            return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The code matches the text perfectly, on the assumption that you intend 1 to be "false" and 0 to be "true" (or 0 means "passes the test" and 1 means "fails the test").

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    57
    Maybe a example would make this alittle more clear. Im using this S-Box:

    Code:
    unsigned char sbox4[16] = {0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2};
    Let d_input = d_output = 1
    Let x = 0;

    sbox4[0] ^ sbox4[0 ^ 1] == 1
    sbox4[0] ^ sbox4[1] == 1
    0xC ^ 0x5 == 1
    0b1100 ^ 0b101 == 1
    0x1001 == 1 // false! it passes... unless you use my code

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, this:
    Code:
    if(sbox[x] ^ sbox[x ^ d_input] == d_output)
    is not the same as this:
    Code:
    if((sbox[x] ^ sbox[x ^ d_input]) == d_output)
    as your compiler mentioned. Guess which one you want.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    57
    Yes! That did it!

    Thanks. I never seem to catch one to things like that....

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    57
    Alright, hate to say it but I think I have a new and hopefully not related problem. This time I think its related to my FC function.

    Code:
    unsigned char gf_mult(unsigned char a, unsigned char b) {
            unsigned char p = a;
            int wt = (b >> 3) ^ (1 & (b >> 2)) ^ (1 & (b >> 1)) ^ (1 & b);
    
            p ^= a << (3 * (1 & ( b >> 3)));
            p ^= a << (2 * (1 & ( b >> 2)));
            p ^= a <<      (1 & ( b >> 1));
            p ^= a*wt;
    
            return (0x0F & p);
    }
    
    char fc(unsigned char *sbox, unsigned char a, unsigned char b) {
            char retval = 0;
            unsigned char x;
    
            for(x = 0; x < 16; ++x) {
                    if((gf_mult(b, sbox[x]) ^ gf_mult(a, x)) % 2 == 0)
                            ++retval;
                    else
                            --retval;
            }
    
            return retval;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does <pointy brackets> represent in this case -- or equivalently to what do a and b belong?

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    57
    Im fairly sure <A, B> means the inner product, so in this case just A*B.

    A and B are both in the GF(2^4) and change, so I made them variables in my code.

    Hope that helps

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Inner product as a 4-vector over F_2? Technically, that's not possible since F_2 doesn't form any inner product spaces. I would suggest going back to check that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. wchar_t type
    By gustavosserra in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2003, 04:49 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM