File size: 5,939 Bytes
19f1582 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #!/usr/bin/env python3
"""
Standard Mathematical Fractal Generator & Cryptographic Chain Verifier
This script provides two functional, objective programming tools:
1. A standard Julia Set generator that computes complex plane iterations and
saves the resulting fractal visualization as a text-based ASCII art representation.
2. A basic cryptographic ledger simulation demonstrating how blocks of data are
sequentially linked using standard SHA-256 hashing to verify integrity.
"""
import hashlib
import json
import time
# ==============================================================================
# 1. STANDARD CRYPTOGRAPHIC LEDGER SIMULATION
# ==============================================================================
class SimpleBlock:
def __init__(self, index, previous_hash, timestamp, data):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash = self.calculate_hash()
def calculate_hash(self):
"""Computes a standard SHA-256 hash of the block contents."""
block_string = json.dumps({
"index": self.index,
"previous_hash": self.previous_hash,
"timestamp": self.timestamp,
"data": self.data
}, sort_keys=True)
return hashlib.sha256(block_string.encode('utf-8')).hexdigest()
class SimpleLedger:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
"""Initializes the chain with a standard static starting block."""
return SimpleBlock(0, "0", 1718300000, "Genesis Data")
def get_latest_block(self):
return self.chain[-1]
def add_data(self, data):
"""Appends a new block containing arbitrary verification data."""
latest = self.get_latest_block()
new_block = SimpleBlock(
index=latest.index + 1,
previous_hash=latest.hash,
timestamp=int(time.time()),
data=data
)
self.chain.append(new_block)
return new_block
def verify_integrity(self):
"""
Walks the chain block-by-block to confirm that hashes match
and the cryptographic sequence is unbroken.
"""
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
# Recalculate hash to detect data tampering
if current.hash != current.calculate_hash():
return False, f"Block {i} data has been altered."
# Verify the chain linkage
if current.previous_hash != previous.hash:
return False, f"Block {i} link to Block {i-1} is broken."
return True, "Ledger integrity verified. Sequence is intact."
# ==============================================================================
# 2. JULIA SET FRACTAL GENERATOR (ASCII REPRESENTATION)
# ==============================================================================
def generate_ascii_julia(width=80, height=40, max_iter=30, c_real=-0.7, c_imag=0.27015):
"""
Generates a standard Julia set fractal using the quadratic formula f(z) = z^2 + c.
Outputs a text-based (ASCII) representation of the fractal boundaries.
"""
# Define complex plane boundaries
x_min, x_max = -1.5, 1.5
y_min, y_max = -1.0, 1.0
# ASCII scale representation
chars = " .:-=+*#%@"
output = []
for y_img in range(height):
row = ""
# Map grid index to the imaginary component of z
zy = y_min + (y_img / (height - 1)) * (y_max - y_min)
for x_img in range(width):
# Map grid index to the real component of z
zx = x_min + (x_img / (width - 1)) * (x_max - x_min)
z = complex(zx, zy)
c = complex(c_real, c_imag)
n = 0
# Standard escape velocity check (divergence threshold = 2.0)
while abs(z) <= 2.0 and n < max_iter:
z = z**2 + c
n += 1
# Map iteration count to ASCII shading character
char_index = int((n / max_iter) * (len(chars) - 1))
row += chars[char_index]
output.append(row)
return "\n".join(output)
# ==============================================================================
# 3. EXECUTION CONTROL
# ==============================================================================
if __name__ == "__main__":
print("=" * 80)
print(" OBJECTIVE MATHEMATICS & LOGIC PLATFORM")
print("=" * 80)
# Execute and display the Julia Set Fractal
print("\n[1] Generating Julia Set Fractal (c = -0.7 + 0.27i)...")
print("-" * 80)
fractal_text = generate_ascii_julia()
print(fractal_text)
print("-" * 80)
print("Fractal computed successfully using standard complex iteration: z(n+1) = z(n)^2 + c")
# Execute and verify the cryptographic chain simulation
print("\n[2] Initializing Cryptographic Verification Ledger...")
ledger = SimpleLedger()
# Simulate adding records
ledger.add_data("System initialization verified.")
ledger.add_data("Parameter constraints checked.")
ledger.add_data("Heartbeat event recorded.")
# Audit sequence integrity
is_valid, report = ledger.verify_integrity()
print(f"Total blocks in ledger: {len(ledger.chain)}")
print(f"Audit Status: {'PASS' if is_valid else 'FAIL'}")
print(f"Audit Report: {report}")
# Display sample block data
print("\nSample block entry data (Block #2):")
sample_block = ledger.chain[2]
print(json.dumps({
"Index": sample_block.index,
"Timestamp": sample_block.timestamp,
"Previous Hash": sample_block.previous_hash[:16] + "...",
"Block Hash": sample_block.hash[:16] + "...",
"Payload": sample_block.data
}, indent=4))
print("=" * 80) |