optimisticninja / liberprimus-tool

Python tool/libraray for evolving solutions toward the Liber Primus from Cicada 3301
1 stars 1 forks source link

Add layering of crypto schemes to genetic algorithm #6

Open optimisticninja opened 2 months ago

optimisticninja commented 2 months ago

Reference #5

The specs will need to become linked lists that can layer classical cryptography algorithms when executed by following the next node. The goal is to evolve objects instead of serialized data or strings to keep validations and not have to serialize/deserialize configurations.

Requirements:

d4v1-sudo commented 1 month ago

Initial idea:

class SpecNode:
    def __init__(self, spec_type, spec):
        self.spec_type = spec_type  # 'crypto' ou 'retrieval'
        self.spec = spec
        self.next = None

class SolutionSpec:
    def __init__(self):
        self.head = None
        self.num_layers = 0

    def add_layer(self, spec_type, spec):
        new_node = SpecNode(spec_type, spec)
        if self.head is None:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node
        self.num_layers += 1

    def run(self, silent=False):
        plaintext = None
        current = self.head
        while current:
            if current.spec_type == 'crypto':
                if current.spec.scheme:
                    if plaintext is None:
                        plaintext = current.spec.scheme(current.spec.text)
                    else:
                        plaintext = current.spec.scheme(plaintext)
            elif current.spec_type == 'retrieval':
                # Retrieve text and apply transformations
                # TODO
                pass
            current = current.next
        return plaintext

class CryptoSpec:
    def __init__(self, scheme):
        self.scheme = scheme
        self.text = None  # Input text for encryption

class TextRetrievalSpec:
    def __init__(self, method):
        self.method = method  # Text recovery method
        self.text = None  # Texto recuperado

# example use:
solution_spec = SolutionSpec()
solution_spec.add_layer('crypto', CryptoSpec(caesar_cipher))
solution_spec.add_layer('crypto', CryptoSpec(vigenere_cipher))
solution_spec.add_layer('retrieval', TextRetrievalSpec(get_text_from_web))

plaintext = solution_spec.run()
print(plaintext)
optimisticninja commented 1 month ago

Initial idea:

class SpecNode:
    def __init__(self, spec_type, spec):
        self.spec_type = spec_type  # 'crypto' ou 'retrieval'
        self.spec = spec
        self.next = None

class SolutionSpec:
    def __init__(self):
        self.head = None
        self.num_layers = 0

    def add_layer(self, spec_type, spec):
        new_node = SpecNode(spec_type, spec)
        if self.head is None:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node
        self.num_layers += 1

    def run(self, silent=False):
        plaintext = None
        current = self.head
        while current:
            if current.spec_type == 'crypto':
                if current.spec.scheme:
                    if plaintext is None:
                        plaintext = current.spec.scheme(current.spec.text)
                    else:
                        plaintext = current.spec.scheme(plaintext)
            elif current.spec_type == 'retrieval':
                # Retrieve text and apply transformations
                # TODO
                pass
            current = current.next
        return plaintext

class CryptoSpec:
    def __init__(self, scheme):
        self.scheme = scheme
        self.text = None  # Input text for encryption

class TextRetrievalSpec:
    def __init__(self, method):
        self.method = method  # Text recovery method
        self.text = None  # Texto recuperado

# example use:
solution_spec = SolutionSpec()
solution_spec.add_layer('crypto', CryptoSpec(caesar_cipher))
solution_spec.add_layer('crypto', CryptoSpec(vigenere_cipher))
solution_spec.add_layer('retrieval', TextRetrievalSpec(get_text_from_web))

plaintext = solution_spec.run()
print(plaintext)

This is the right idea, However we only need to layer the crypto spec. A simple solution for this is to reference the same class for next as follows and specify a depth

class CryptoSpec:
  def __init__(self, ...):
    self.next = new CryptoSpec()
    self.prev = None # make it a doubly linked list to traverse backwards
    self.max_layers = 3
    self.layer_num = 0 # first layer
...