def test_maj(x, y, z): # Compilation text = compiler("test_scripts/maj_script.txt") with open("test_scripts/maj.txt", "w") as f: f.write(text) # Load compiled Maj circuit maj_program = Circuit("test_scripts/maj.txt") # Create inputs tape = [int(i) for i in int2bin32(x) + int2bin32(y) + int2bin32(z)] # Run compiled Cicuit result = maj_program.run(tape) # Confirm Result return int("".join([str(i) for i in result]), 2) == Maj(x, y, z)
def test_lsig(x, y, z): text = compiler("test_scripts/lsig_script.txt") with open("test_scripts/lsig.txt", "w") as f: f.write(text) # Load compiled Loop Circuit lsig_program = Circuit("test_scripts/lsig.txt") # Create inputs tape = [int(i) for i in int2bin32(x) + int2bin32(y) + int2bin32(z)] # Run Compiled Circuit result = lsig_program.run(tape) # Confirm Result return int("".join([str(i) for i in result]), 2) == check_lsig_test(x, y, z)
def test_add(x, y): # Compilation text = compiler("test_scripts/add_script.txt") with open("test_scripts/add.txt", "w") as f: f.write(text) # Load compiled Maj circuit add_program = Circuit("test_scripts/add.txt") # Create inputs tape = [int(i) for i in int2bin32(x) + int2bin32(y)] # Run compiled Cicuit result = add_program.run(tape) # Confirm Result a = int("".join([str(i) for i in result]), 2) return a == ((x + y) % (2**32))
def test_ch(y, z): # Compilation text = compiler("test_scripts/ch_script.txt") with open("test_scripts/ch.txt", "w") as f: f.write(text) # Load compiled Loop Circuit ch_program = Circuit("test_scripts/ch.txt") # Create inputs tape = [int(i) for i in int2bin32(y) + int2bin32(z)] # Run Compiled Circuit result = ch_program.run(tape) # Confirm Result x = 2**31 + 3 # constant set in program return int("".join([str(i) for i in result]), 2) == Ch(x, y, z)
def test_loop2(a): # Compilation text = compiler("test_scripts/loop2_script.txt") with open("test_scripts/loop2.txt", "w") as f: f.write(text) # Load compiled Loop Circuit loop2_program = Circuit("test_scripts/loop2.txt") # Create inputs tape = bin(a)[2:] while len(tape) < 256: tape = "0" + tape tape = [int(i) for i in tape] # Run Compiled Circuit result = loop2_program.run(tape) # Confirm Result check = check_loop2_test(tape) return int("".join([str(i) for i in result]), 2) == check
def test_add(x, y): # Compilation text = compiler("add_script.txt") with open("add.txt", "w") as f: f.write(text) # Load compiled Maj circuit add_program = Circuit("add.txt") # Create inputs tape_tmp = [int(i) for i in int2bin32(x) + int2bin32(y)] tape = [tape_tmp[-(i + 1)] for i in range(len(tape_tmp))] print tape # Run compiled Cicuit result = add_program.run(tape) print result # Confirm Result a = int("".join([str(result[-(i + 1)]) for i in range(len(result))]), 2) print a print(x + y) % (2**32) return a == ((x + y) % (2**32))
import hashlib from circuit_interpreter import Circuit from dumb_compiler import compiler # Uncomment to compile again (takes ~ 2minutes) '''text=compiler("2blockSHA256_better_script.txt") with open("2blockSHA256.txt", "w") as f: f.write(text)''' sha256_program = Circuit("2blockSHA256_2.txt") def test_sha256(string): check = int(hashlib.sha256(string).hexdigest(), 16) tape = [int(i) for i in preprocess(string)] result = sha256_program.run(tape) return int("".join([str(i) for i in result]), 2) == check def preprocess(to_hash): bits = "".join([char2bin8(char) for char in to_hash]) n = len(bits) preprocessed = bits + "1" while len(preprocessed) % 512 != 448: preprocessed += "0" b = bin(n)[2:] while len(b) < 64: b = "0" + b preprocessed = preprocessed + b