Compare commits

...
10 Commits
5 changed files with 50 additions and 39 deletions
+2
View File
@@ -6,3 +6,5 @@ I made it because I want to try to break it.
This will work iff I succeed in building a PPT-discriminator for sha256 from randomness
As my first approach this discriminator will be based on an LSTM-network.
Update: This worked out way better than expected; given long enought sequences (128 Bytes are more than enough) we can discriminate successfully in 100% of cases.
Update 2: I did an upsie in the training-code and the discriminator is actually shit.
Update 3: Turns out: sha256 produces fairly high quality randomness and this project seems to have failed...
+2 -1
View File
@@ -8,8 +8,9 @@ import numpy as np
import random
import shark
from model import Model
bs = int(256/8)
bs = shark.bs
class Model(nn.Module):
def __init__(self):
+28
View File
@@ -0,0 +1,28 @@
import torch
from torch import nn
from torch import nn, optim
from torch.utils.data import DataLoader
import shark
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.lstm = nn.LSTM(
input_size=8,
hidden_size=16,
num_layers=5,
dropout=0.01,
)
self.fc = nn.Linear(16, 1)
self.out = nn.Sigmoid()
def forward(self, x, prev_state):
output, state = self.lstm(x, prev_state)
logits = self.fc(output)
val = self.out(logits)
return val, state
def init_state(self, sequence_length):
return (torch.zeros(5, 1, 16),
torch.zeros(5, 1, 16))
-6
View File
@@ -3,12 +3,6 @@ import math
import os
import random
# Shark is a sha256+xor based encryption.
# I made it because I want to try to break it.
# (Precisely: Show it does not provide semantic security, because it is not IND-CPA-secure)
# This will work iff I succeed in building a PPT-discriminator for sha256 from randomness
# As my first approach this discriminator will be based on an LSTM-network.
bs = int(256/8)
def xor(ta,tb):
+18 -32
View File
@@ -4,37 +4,16 @@ from torch import nn, optim
from torch.utils.data import DataLoader
import numpy as np
import random
import math
import shark
from model import Model
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.lstm = nn.LSTM(
input_size=8,
hidden_size=16,
num_layers=3,
dropout=0.1,
)
self.fc = nn.Linear(16, 1)
self.out = nn.Sigmoid()
def forward(self, x, prev_state):
output, state = self.lstm(x, prev_state)
logits = self.fc(output)
val = self.out(logits)
#print(str(logits.item())+" > "+str(val.item()))
return val, state
def init_state(self, sequence_length):
return (torch.zeros(3, 1, 16),
torch.zeros(3, 1, 16))
def train(model, seq_len=16*64):
def train(model, seq_len=16*256): # 0.5KiB
tid = str(int(random.random()*99999)).zfill(5)
print("[i] I am "+str(tid))
ltLoss = 50
lltLoss = 51
ltLoss = 0.75
lltLoss = 0.80
model.train()
criterion = nn.BCELoss()
@@ -44,13 +23,15 @@ def train(model, seq_len=16*64):
state_c = [None,None]
blob = [None,None]
correct = [None,None]
err = [None,None]
ltErr = 0.5
for epoch in range(1024):
state_h[0], state_c[0] = model.init_state(seq_len)
state_h[1], state_c[1] = model.init_state(seq_len)
blob[0], _ = shark.getSample(min(seq_len, 16*(epoch+1)), 0)
blob[1], _ = shark.getSample(min(seq_len, 16*(epoch+1)), 1)
blob[0], _ = shark.getSample(seq_len, 0)
blob[1], _ = shark.getSample(seq_len, 1)
optimizer.zero_grad()
for i in range(len(blob[0])):
for t in range(2):
@@ -65,11 +46,16 @@ def train(model, seq_len=16*64):
optimizer.step()
correct[t] = round(y_pred.item()) == t
err[t] = abs(t - y_pred.item())
ltLoss = ltLoss*0.9 + 0.1*loss.item()
lltLoss = lltLoss*0.9 + 0.1*ltLoss
print({ 'epoch': epoch, 'loss': loss.item(), 'ltLoss': ltLoss, 'ok0': correct[0], 'ok1': correct[1], 'succ': correct[0] and correct[1] })
if epoch % 8 == 0:
torch.save(model.state_dict(), 'model_savepoints/'+tid+'_'+str(epoch)+'.n')
ltErr = ltErr*0.99 + (err[0] + err[1])*0.005
lltLoss = lltLoss*0.9 + 0.1*ltLoss
print({ 'epoch': epoch, 'loss': loss.item(), 'lltLoss': lltLoss, 'ok0': correct[0], 'ok1': correct[1], 'succ': correct[0] and correct[1], 'acc': str(int(100-(err[0]+err[1])*50))+"%" })
torch.save(model.state_dict(), 'model_savepoints/'+tid+'_'+str(epoch)+'.n')
if 0.45 < ltErr < 0.55:
print("[~] My emperor! I've failed! A BARREL ROLL!")
else:
print("[~] Booyaaa!!!!")
model = Model()