2022-01-05
ciruit mapA valid mapDigital circuits is hard , right?#! /usr/bin/python3from FLAG import flagfrom Crypto.Util.number import *import randomdef genPrime():while True:a = random.getrandbits(512)b = rand
·
ciruit map
A valid map
Digital circuits is hard , right?
text.py
import hashlib
from icecream import *
from private_data import keys,flag
from Crypto.Util.number import *
def xor(A, B):
return bytes(a ^ b for a, b in zip(A, B))
the_chaos=b''
for i in keys:
tmp = sum(keys[i])
the_chaos += bytes(long_to_bytes(tmp))
mask = hashlib.md5(the_chaos).digest()
print(xor(mask,flag).hex())
# 1661fe85c7b01b3db1d432ad3c5ac83a
这其中涉及一个概念:乱码电路
乱码电路是一种启用两方安全计算的加密协议,在该协议中,两个不信任方可以在不存在可信第三方的情况下通过其私有输入共同评估函数。在乱码电路协议中,必须将该功能描述为布尔电路。
根据circuit_map.json可以画出电路
gen_key.py
import json
from block_cipher import decrypt
from random import randrange
from yao_circuit import GarbledGate as Ggate
flag = b'****************'
circuit_filename = "circuit_map.json"
with open(circuit_filename) as json_file:
circuit = json.load(json_file)
def init_keys(circuit):
keys = {}
gates = circuit["gates"]
wires = set()
for gate in gates:
wires.add(gate["id"])
wires.update(set(gate["in"]))
for wireidx in wires:
# the index of keys[wireidx] 1 and 0 means TRUE and FALSE in garbled circuit
keys[wireidx] = (randrange(0, 2**24),randrange(0, 2**24))
return keys
def validate_the_circuit(geta_table, key0, key1):
for g in geta_table:
gl, v = g
label = decrypt(gl, key0, key1)
validation = decrypt(v, key0, key1)
if validation == 0:
return label
def init_garbled_tables(circuit,keys):
gates = circuit["gates"]
garbled_tables = {}
for gate in gates:
garbled_table = Ggate(gate, keys)
garbled_tables[gate["id"]] = garbled_table
return garbled_tables
keys = init_keys(circuit)
G_Table = init_garbled_tables(circuit,keys)
# ic(keys)
# ic(G_Table)
# hint
# circuit wiring has only one sit uation mack ASSERT be true
geta_table = G_Table[9]
key0 = keys[7][1]
key1 = keys[4][0]
msg = validate_the_circuit(geta_table, key0, key1)
assert msg == keys[circuit["out"][0]][1]
#
with open("public_data.py","r+") as f:
print("G_Table = {}".format(G_Table),file=f)
with open("private_data.py","r+") as f:
print("keys = {}".format(keys),file=f)
print("flag = {}".format(str(flag)),file=f)
我们还知道
G_Table = { 5: [(13303835, 2123830),
(2801785, 11303723),
(13499998, 248615),
(13892520, 7462011)],
6: [(3244202, 918053),
(3277177, 6281266),
(1016382, 7097624),
(10016472, 13600867)],
7: [(5944875, 3442862),
(7358369, 8423543),
(6495696, 9927178),
(13271900, 11855272)],
9: [(5333988, 87113),
(9375869, 11687470),
(5011062, 14981756),
(2509493, 12330305)]}
过程参考
解题代码
keys = {1: (8343801, 13675268),
2: (10251687, 12870274),
3: (6827786, 12490757),
4: (2096572, 3391233),
5: (0, 0),
6: (0, 0),
7: (0, 0),
9: (0, 0)}
# 5,6,7-> use AND gate
for gate in circuit["gates"]:
gate_ID = gate["id"]
# Right/Left_value_index
rv = gate["in"][0]
lv = gate["in"][1]
if(gate["type"]=="AND"):
"""
AND gate
1 1 1
1 0 0
0 1 0
0 0 0
"""
msg1 = validate_the_circuit(G_Table[gate_ID],keys[rv][1],keys[lv][1])
msg0 = validate_the_circuit(G_Table[gate_ID],keys[rv][0],keys[lv][0])
else:
"""
XOR gate
1 1 0
1 0 1
0 1 1
0 0 0
"""
msg1 = validate_the_circuit(G_Table[gate_ID],keys[rv][1],keys[lv][0])
msg0 = validate_the_circuit(G_Table[gate_ID],keys[rv][0],keys[lv][0])
keys[gate_ID] = (msg0,msg1)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)