dyne / Zenroom

Embedded no-code VM executing human-like language to manipulate data and process cryptographic operations.
https://dev.zenroom.org
GNU Affero General Public License v3.0
195 stars 62 forks source link

deterministic sort_pairs cannot sort tables with `zenroom.` type keys #838

Closed jaromil closed 5 months ago

jaromil commented 6 months ago

The adoption of vanilla table.sort from lua here: https://github.com/dyne/Zenroom/blob/6262bc2904e60b68a463740f9516feb6b2c45d32/src/lua/zenroom_common.lua#L110

Breaks the deterministic sorting of tables in case they are dictionaries and the keys are a zenroom. type.

We need to implement our own sort to support also zenroom types (it can be over octet bytes).

jaromil commented 6 months ago

Script to reproduce:

claims = { "I am over 18",
           "Born in Pescara",
           "Resident in Pizzoferrato",
           "C level English speaker",
           "Elite Startfighter pilot" }
H = ECP.hashtopoint
mask = ECP.random()
hashed_claims = deepmap(H, claims)
masked_claims = { }
for _,v in sort_pairs(hashed_claims) do
   masked_claims[v] = v +  mask
end
for k,v in sort_pairs(masked_claims) do
   I.print({k=k,v=v})
end

Good candidate to fix this is the pure-lua qsort implementation here https://github.com/DarkRoku12/lua_sort

Else another solution can be to implement the zenroom.ECP comparison, but that may be misleading: does it compares octet-rendered byte content or x/y coordinates of points?