Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MD-Levitan committed Nov 18, 2019
1 parent 7c40a88 commit 06d5cc0
Show file tree
Hide file tree
Showing 44 changed files with 723 additions and 76 deletions.
9 changes: 9 additions & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ __pycache__/
# C extensions
*.so

# Data file
*.bin

# Tex/Latex extensions
*.sty
*.toc
*.aux
*.gz

# Distribution / packaging
.Python
build/
Expand Down
Empty file modified neuro_gost/1_hidden_layer.py
100644 → 100755
Empty file.
74 changes: 52 additions & 22 deletions neuro_gost/1_layer.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,45 @@ def read_from_file(file, num_bytes, format_data=lambda x: int.from_bytes(x, byte
return data


def split_by_byte(block):
def bits(num):
bits_array = []
for _ in range(0, 8):
bits_array.append(num % 2)
num //= 2
return list(reversed(bits_array))


def _int(bit_array):
value = 0
for i in bit_array:
value *= 2
value += i
return int(value)


def split_by_bit(block, bit=32):
rv = []
for i in block:
rv += bits(i)
return rv[:bit]


def split_by_byte(block, bytes=4):
rv = []
for i in block:
rv.append(int(i))
return rv
return rv[:bytes]


def to_float(block, bytes=4):
return [int.from_bytes(block[:4], byteorder='little') / pow(2, 8 * bytes)]


def get_test_values():
__in = read_from_file("GOST_generator/out_x.bin", 8, to_float)
__out = read_from_file("GOST_generator/out_y.bin", 8, to_float)
def get_test_values(size=[32, 32]):
__in = read_from_file("GOST_generator/out_x.bin", 8,
lambda x: split_by_bit(x, size[0]))
__out = read_from_file("GOST_generator/out_y.bin", 8,
lambda x: split_by_bit(x, size[1]))

return np.array(__in), np.array(__out)

Expand All @@ -35,20 +60,26 @@ def init_one_layer_network(input_data, output_data, n_input, n_classes, training
optimizer=tf.train.AdamOptimizer):

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, n_classes])

# Create the model
# W = tf.Variable(tf.random_normal(shape=[n_input, 1]))
# b = tf.Variable(tf.random_normal([1]))
# out = tf.matmul(x, W) + b

out = tf.layers.dense(x, 1, activation=tf.nn.sigmoid, name="output")
out = tf.layers.dense(x, n_classes, activation=tf.nn.sigmoid)

loss = tf.reduce_mean(tf.square(out - y))
train_step = optimizer(learning_rate=0.002).minimize(loss)
correct_prediction = tf.equal(out, y)
hamming_distance = tf.math.count_nonzero(tf.round(out) - y, axis=-1)
accuracy = tf.reduce_mean(hamming_distance)

loss = tf.losses.sigmoid_cross_entropy(y, out)
loss1 = tf.keras.losses.binary_crossentropy(y, out)

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
cost = tf.reduce_mean(tf.square(out - y))

cost1 = tf.reduce_mean(tf.cast(tf.math.count_nonzero(out - y, axis=-1), tf.float32))

train_step = optimizer(learning_rate=0.002).minimize(loss)

init = tf.global_variables_initializer()
# Train the model, and feed in test data and record summaries every 10 steps
Expand All @@ -69,15 +100,13 @@ def init_one_layer_network(input_data, output_data, n_input, n_classes, training
feed = {x: train_dataset, y: train_values}
sess.run(train_step, feed_dict=feed)

test_values1 = sess.run(out, feed_dict={
x: test_dataset,
})
test_values1 = tf.round(out).eval(feed_dict={x: test_dataset})

plt.plot(test_dataset, test_values, "bo",
test_dataset, test_values1, "ro")
plt.show()
print(list(map(lambda x: hex(_int(x)), test_values1)))
print(list(map(lambda x: hex(_int(x)), test_values)))
print(list(map(lambda x: hex(_int(x)), test_dataset)))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
accuracy = tf.reduce_mean(tf.cast(hamming_distance, "float"))
acc = accuracy.eval(feed_dict={x: test_dataset, y: test_values})
los = loss.eval(feed_dict={x: test_dataset, y: test_values})

Expand All @@ -90,10 +119,11 @@ def init_one_layer_network(input_data, output_data, n_input, n_classes, training


if __name__ == "__main__":
input_data, output_data = get_test_values()
input_data, output_data = get_test_values([64, 64])

# Network Parameters
n_input = 1
n_input = 64
n_classes = 64

x, y = init_one_layer_network(input_data, output_data, n_input, 1,
training_epochs=10000, display_step=1000)
x, y = init_one_layer_network(input_data, output_data, n_input, n_classes,
training_epochs=10000, display_step=1000)
Empty file modified neuro_gost/GOST_generator/Makefile
100644 → 100755
Empty file.
Binary file modified neuro_gost/GOST_generator/generator
Binary file not shown.
50 changes: 42 additions & 8 deletions neuro_gost/GOST_generator/generator.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ static uint8_t key[32] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
};

const char *usage = "Usage: ./generator <SIZE>";
const char *usage = "Usage: ./generator <SIZE> <GEN_TYPE>";

void iterate_generator(struct crypto_tfm *ctx, uint64_t size,
FILE *out_file_x, FILE *out_file_y)
{
uint64_t in, out;
for (in = 0; in < size; ++in)
{
//out = in * 2;
magma_it(ctx, (uint8_t *) &out, (uint8_t *) &in, 0);

fwrite((uint8_t *) &in, sizeof(uint8_t), 8, out_file_x);
fwrite((uint8_t *) &out, sizeof(uint8_t), 8, out_file_y);
}
Expand All @@ -35,14 +35,34 @@ void random_generator(struct crypto_tfm *ctx, uint64_t size,

for (uint64_t i = 0; i < size; ++i)
{
in = rand();
//out = in * 2;
in = 0;
((uint32_t *) &in)[0] = rand();
((uint32_t *) &in)[1] = rand();
magma_it(ctx, (uint8_t *) &out, (uint8_t *) &in, 0);
fwrite((uint8_t *) &in, sizeof(uint8_t), 8, out_file_x);
fwrite((uint8_t *) &out, sizeof(uint8_t), 8, out_file_y);
}
}

void random_generator_n(struct crypto_tfm *ctx, uint64_t size,
uint8_t n, FILE *out_file_x, FILE *out_file_y)
{
uint64_t in, out;
srand(time(NULL));

for (uint64_t i = 0; i < size; ++i)
{
in = 0;
((uint32_t *) &in)[0] = rand();
((uint32_t *) &in)[1] = rand();
magma_it_n(ctx, (uint8_t *) &out, (uint8_t *) &in, n);
fwrite((uint8_t *) &in, sizeof(uint8_t), 8, out_file_x);
fwrite((uint8_t *) &out, sizeof(uint8_t), 8, out_file_y);
}
}



/*
* Generator
* Usage: ./generator <SIZE> <GEN_TYPE>
Expand Down Expand Up @@ -76,13 +96,27 @@ int main(int argc, const char **argv) {

ctx = create_tfm_ctx();
magma_setkey(ctx, key, sizeof(key));
if (rv == 2) {
random_generator(ctx, size, out_file_x, out_file_y);
} else {
iterate_generator(ctx, size, out_file_x, out_file_y);
switch(rv)
{
case 2:
{
random_generator(ctx, size, out_file_x, out_file_y);
break;
}
case 1:
{
iterate_generator(ctx, size, out_file_x, out_file_y);
break;
}
case 3:
{
random_generator_n(ctx, size, 2, out_file_x, out_file_y);
break;
}
}

fclose(out_file_x);
fclose(out_file_y);
delete_tfm_ctx(ctx);
}

34 changes: 30 additions & 4 deletions neuro_gost/GOST_generator/magma.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ inline static uint32_t f(uint32_t x)
int magma_setkey(struct crypto_tfm *tfm, const uint8_t *key,
unsigned int key_len)
{
if (key_len != 32)
return 1;

magma_subkeys *subkeys = crypto_tfm_ctx(tfm);
subkeys->k[0] = GETU32_BE(key);
subkeys->k[1] = GETU32_BE(key + 4);
Expand Down Expand Up @@ -313,8 +316,6 @@ void magma_it(struct crypto_tfm *tfm, uint8_t *out,
uint32_t n1 = GETU32_BE(in + 4);
uint32_t buf = 0;

//printf("in - %"PRId64" , n2 - %lld , n1 - %lld \n", *((uint64_t*) in), n2, n1);

if (iter >= 23) {
buf = n2 ^ f(n1 + subkeys->k[7 - iter % 8]);
}
Expand All @@ -326,9 +327,34 @@ void magma_it(struct crypto_tfm *tfm, uint8_t *out,
buf = 0;

((uint32_t*)out)[0] = n1;
((uint32_t*)out)[1] = n2;
((uint32_t*)out)[1] = n2;
}

void magma_it_n(struct crypto_tfm *tfm, uint8_t *out,
const uint8_t *in, uint8_t num)
{
magma_subkeys *subkeys = crypto_tfm_ctx(tfm);
uint32_t n2 = GETU32_BE(in);
uint32_t n1 = GETU32_BE(in + 4);
uint32_t buf = 0;

for(uint8_t it = 0; it < num; ++it)
{

buf = n2 ^ f(n1 + subkeys->k[(it >=23) ? (7 - it % 8) : (it % 8)]);
n2 = n1;
n1 = buf;
}

//printf("out - %"PRId64" , n2 - %lld , n1 - %lld \n", *((uint64_t*) out), n2, n1);
if (num % 2)
{
n1 ^= n2;
n2 ^= n1;
n1 ^= n2;
}

((uint32_t*)out)[0] = n1;
((uint32_t*)out)[1] = n2;

}

Expand Down
62 changes: 62 additions & 0 deletions neuro_gost/GOST_generator/magma.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,77 @@ typedef struct magma_subkeys magma_subkeys;
typedef struct crypto_tfm crypto_tfm;


/**
* @brief Get from crypto instance magma keys
*
* @param tfm crypto instance
* @return magma_subkeys
*/
magma_subkeys *crypto_tfm_ctx(struct crypto_tfm *tfm);

/**
* @brief Create a tfm ctx object
*
* @return struct crypto instance
*/
struct crypto_tfm *create_tfm_ctx();

/**
* @brief Destroy crypto instance
*
* @param tfm crypto instance
*/
void delete_tfm_ctx(struct crypto_tfm *tfm);

/**
* @brief Set key to crypto instance
*
* @param tfm crypto instance
* @param key ptr to key
* @param key_len key length (support only 32)
* @return int retun 1 if any error
*/
extern int magma_setkey(struct crypto_tfm *tfm, const uint8_t *key,
unsigned int key_len);

/**
* @brief Encrypt block of data
*
* @param tfm crypto instance
* @param out result of encryption
* @param in plaintext block
*/
extern void magma_encrypt(struct crypto_tfm *tfm, uint8_t *out, const uint8_t *in);

/**
* @brief Encyption of block by only i iteration
*
* @param tfm crypto instance
* @param out result of encryption
* @param in plaintext block
* @param iter iteration of GOST alg
*/
extern void magma_it(struct crypto_tfm *tfm, uint8_t *out,
const uint8_t *in, uint8_t iter);

/**
* @brief Encyption of block by first n iterations
*
* @param tfm crypto instance
* @param out result of encryption
* @param in plaintext block
* @param n number of iteration
*/
extern void magma_it_n(struct crypto_tfm *tfm, uint8_t *out,
const uint8_t *in, uint8_t n);

/**
* @brief Decrypt block of data
*
* @param tfm crypto instance
* @param out result of decryption
* @param in ciphertext block
*/
extern void magma_decrypt(struct crypto_tfm *tfm, uint8_t *out,
const uint8_t *in);

Expand Down
Binary file modified neuro_gost/GOST_generator/out_x.bin
100644 → 100755
Binary file not shown.
Binary file modified neuro_gost/GOST_generator/out_y.bin
100644 → 100755
Binary file not shown.
26 changes: 26 additions & 0 deletions neuro_gost/log1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Start of neuron number_layers - 2
Accuracy at step 0: 61.000000 - loss: 122.113472
Accuracy at step 1000: 32.000000 - loss: 0.298664
Accuracy at step 2000: 32.000000 - loss: 0.258922
Accuracy at step 3000: 31.000000 - loss: 0.253251
Accuracy at step 4000: 31.000000 - loss: 0.251476
Accuracy at step 5000: 31.000000 - loss: 0.250731
Accuracy at step 6000: 31.000000 - loss: 0.250369
Accuracy at step 7000: 31.000000 - loss: 0.250176
Accuracy at step 8000: 31.000000 - loss: 0.250055
Accuracy at step 9000: 31.000000 - loss: 0.249972
Accuracy at step 10000: 31.000000 - loss: 0.249888
Accuracy at step 11000: 31.000000 - loss: 0.249786
Accuracy at step 12000: 31.000000 - loss: 0.249629
Accuracy at step 13000: 31.000000 - loss: 0.249353
Accuracy at step 14000: 30.000000 - loss: 0.248876
Accuracy at step 15000: 30.000000 - loss: 0.247959
Accuracy at step 16000: 29.000000 - loss: 0.246557
Accuracy at step 17000: 29.000000 - loss: 0.244267
Accuracy at step 18000: 28.000000 - loss: 0.239052
Accuracy at step 19000: 27.000000 - loss: 0.234482
Accuracy at step 20000: 26.000000 - loss: 0.230066
Accuracy at step 21000: 25.000000 - loss: 0.223980
Accuracy at step 22000: 25.000000 - loss: 0.218310
Accuracy at step 23000: 24.000000 - loss: 0.214387
Accuracy at step 24000: 24.000000 - loss: 0.212212
Loading

0 comments on commit 06d5cc0

Please sign in to comment.