這邊先給上 mapping code 範例,第一組為 key-value
的綁定方式
儲存狀態 Storage / Memory ,以這樣表示
def build_mapping():
keys = [1,3,5]
values = [2,4,6]
randomness = hash(keys, values)
mappings = []
for key , value in zip(keys,values):
mappings.append(key + randomness*value)
return(mappings)
想證明某個 key-value 在 key-value 中,可以通過 3 個 plookup 實現
def open_mapping(mappings, keys, values):
randomness = hash(keys,values)
index = 1
# Prover can chose any mapping, key , value
mapping = plookup(mappings)
key = plookup(keys)
value = plookup(values)
# But it has to satisfy this check
require(mappings[index] == key[index] + randomness*value[index])
# with overwhelming probablitiy will not find an invalid mapping.
這邊可以看出完整的 bus_mapping 結構,下一章節會做個簡單的版本
bus_mapping[global_counter] = {
type_flag = ["stack", "memory", "storage"],
rw_flag,
key,
value,
index: opcode,
call_id: call_id,
prog_counter: prog_counter
}
拿上一章看的完整 bus-mapping 實現,這邊我用一個簡單的 class 去實現,之後串接 EVM Proof 和 State Proof 後續會做大更動,所以目前先做簡單的設計
class BusMapping:
def __init__(self):
self.bus_mapping = {
stack: [],
memory: [],
storage: [],
pc: 0,
}
@staticmethod
def build_mapping(keys, values):
randomness = hash((tuple(keys), tuple(values)))
mappings = []
for key, value in zip(keys, values):
mappings.append(key + randomness * value)
return mappings
@staticmethod
def open_mapping(mappings, keys, values):
randomness = hash((tuple(keys), tuple(values)))
index = 1
mapping = ZKEVM.plookup(mappings)
key = ZKEVM.plookup(keys)
value = ZKEVM.plookup(values)
assert mappings[index] == key[index] + randomness * value[index], "Mapping validation failed!"
@staticmethod
def plookup(list_to_search):
return list_to_search[0]