Commit b7b7a3cc authored by Jordi's avatar Jordi Committed by Jaromil
Browse files

Modify the examples to allow to choose what to vote

parent 99aa93c7
......@@ -20,7 +20,9 @@ zenroom -a votes.json verify_init.lua
## Vote
```
zenroom -a votes.json vote.lua > votes2.json; mv votes2.json votes.json
cat votes.json | jq --arg opt yes '. +{option: $opt}' | tee votes.json
zenroom -a votes.json vote.lua | tee votes.json
zenroom -a votes.json verify_vote.lua
```
......@@ -33,7 +35,7 @@ and verify that votes are all 0 or 1, and the sum of all the addition is 1
## Tally
```
zenroom -k key.json -a votes.json tally.lua
zenroom -k key.json -a votes.json tally.lua
```
......@@ -47,14 +49,22 @@ zenroom keygen.lua > key.json
zenroom -k key.json init.lua > votes.json
zenroom -a votes.json verify_init.lua
zenroom -a votes.json vote.lua > votes2.json; mv votes2.json votes.json
cat votes.json | jq --arg opt yes '. +{option: $opt}' | tee votes.json
zenroom -a votes.json vote.lua | tee votes.json
zenroom -a votes.json verify_vote.lua
zenroom -a votes.json vote.lua > votes2.json; mv votes2.json votes.json
cat votes.json | jq --arg opt no '. +{option: $opt}' | tee votes.json
zenroom -a votes.json vote.lua | tee votes.json
zenroom -a votes.json verify_vote.lua
zenroom -a votes.json vote.lua > votes2.json; mv votes2.json votes.json
cat votes.json | jq --arg opt yes '. +{option: $opt}' | tee votes.json
zenroom -a votes.json vote.lua | tee votes.json
zenroom -a votes.json verify_vote.lua
zenroom -a votes.json vote.lua > votes2.json; mv votes2.json votes.json
cat votes.json | jq --arg opt no '. +{option: $opt}' | tee votes.json
zenroom -a votes.json vote.lua | tee votes.json
zenroom -a votes.json verify_vote.lua
zenroom -k key.json -a votes.json tally.lua
```
\ No newline at end of file
zenroom -k key.json -a votes.json tally.lua
```
......@@ -70,17 +70,23 @@ function encrypt(m)
}
end
options = {'yes', 'no'}
if DATA_TABLE then
options = DATA_TABLE['options']
else
options = {'yes', 'no'}
end
-- initial state of the options
scores = {0, 0}
scores = LAMBDA.map(options, function(k,v) return 0 end)
-- encrypt them
scores = LAMBDA.map(scores, function(k,v) return encrypt(v) end)
-- create the ZKP that they are zero
proves = LAMBDA.map(scores, function(k,v)
c, rx = provezero(v['a'], v['b'])
return { c = tostring(c), rx = tostring(rx) }
end)
proves = LAMBDA.map(scores, function(k,v)
c, rx = provezero(v['a'], v['b'])
return { c = tostring(c), rx = tostring(rx) }
end)
-- convert the scores in a serializable form
scores = LAMBDA.map(scores, function(k,v) return { a = writeEcp(v['a']), b = writeEcp(v['b']) } end)
......
......@@ -13,9 +13,19 @@ H = HASH.new('sha256')
local DATA_TABLE = JSON.decode(DATA)
function table.indexOf(t, object)
if type(t) ~= "table" then error("table expected, got " .. type(t), 2) end
for i, v in pairs(t) do
if object == v then
return i
end
end
end
function readBig(str)
return BIG.new(hex(str))
end
end
function readEcp(table)
local x = readBig(table['x'])
......@@ -103,7 +113,9 @@ scores = DATA_TABLE['scores']
-- increment to do to the options
increment = LAMBDA.map(options, function(k,v) return 0 end)
increment[1] = 1
increment_index = table.indexOf(DATA_TABLE['options'], DATA_TABLE['option'])
assert(increment_index, "option not found inside options")
increment[increment_index] = 1
-- encrypt them
increment = LAMBDA.map(increment, function(k,v) return encrypt(v) end)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment