Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(random): 🐛 random numbers type are the same as the modulo type #734

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions src/lua/zencode_random.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ When("create the random ''", function(dest)
new_codec(dest, { zentype = 'e' })
end)

function shuffle_array_f(tab)
local function shuffle_array_f(tab)
-- do not enforce CODEC detection since some schemas are also 1st level arrays
local count = isarray(tab)
ZEN.assert( count > 0, "Randomized object is not an array")
Expand All @@ -45,79 +45,84 @@ end
-- random and hashing operations
When("create the random object of '' bits", function(n)
empty'random object'
local bits = tonumber(n)
local bits = tonumber(mayhave(n) or n)
ZEN.assert(bits, 'Invalid number of bits: ' .. n)
ACK.random_object = OCTET.random(math.ceil(bits / 8))
new_codec('random_object', { zentype = 'e' })
end
)
When("create the random object of '' bytes",function(n)
empty'random object'
local bytes = math.ceil(tonumber(n))
local bytes = math.ceil(tonumber(mayhave(n) or n))
ZEN.assert(bytes, 'Invalid number of bytes: ' .. n)
ACK.random_object = OCTET.random(bytes)
new_codec('random_object', { zentype = 'e' })
end
)

When("randomize the '' array", function(arr)
local A = ACK[arr]
ZEN.assert(A, "Object not found: "..arr)
local A = have(arr)
-- ZEN.assert(ZEN.CODEC[arr].zentype == 'a', "Object is not an array: "..arr)
ACK[arr] = shuffle_array_f(A)
end)

local function _create_random_array(array_length, fun_input, fun)
empty 'array'
ACK.array = { }
local length = tonumber(mayhave(array_length) or array_length)
ZEN.assert(length, "Argument is not a number: "..array_length)
for i = length,1,-1 do
table.insert(ACK.array, fun(fun_input))
end
end

When("create the array of '' random objects", function(s)
ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array")
ACK.array = { }
for i = s,1,-1 do
table.insert(ACK.array,OCTET.random(64))
end
new_codec('array', {zentype = 'a'})
_create_random_array(s, 64, OCTET.random)
new_codec('array', {zentype = 'a'})
end)

When("create the array of '' random objects of '' bits", function(s, b)
empty'array'
ACK.array = { }
local q = tonumber(s)
ZEN.assert(q, "Argument is not a number: "..s)
local bits = tonumber(b)
local bytes = math.ceil(bits/8)
for i = q,1,-1 do
table.insert(ACK.array,OCTET.random(bytes))
end
new_codec('array', {zentype = 'a'})
local bits = tonumber(mayhave(b) or b)
ZEN.assert(bits, "Argument is not a number: "..b)
local bytes = math.ceil(bits/8)
_create_random_array(s, bytes, OCTET.random)
new_codec('array', {zentype = 'a'})
end)

When("create the array of '' random objects of '' bytes", function(s, b)
empty'array'
ACK.array = { }
local q = tonumber(s)
ZEN.assert(q, "Argument is not a number: "..s)
local bytes = math.ceil(tonumber(b))
for i = q,1,-1 do
table.insert(ACK.array,OCTET.random(bytes))
end
new_codec('array', {zentype = 'a'})
local n_bytes = tonumber(mayhave(b) or b)
ZEN.assert(n_bytes, "Argument is not a number: "..b)
local bytes = math.ceil(n_bytes)
_create_random_array(s, bytes, OCTET.random)
new_codec('array', {zentype = 'a'})
end)

When("create the array of '' random numbers", function(s)
ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array")
ACK.array = { }
for i = s,1,-1 do
table.insert(ACK.array,F.new(random_int16()))
end
new_codec('array', {zentype = 'a', encoding = 'number' })
_create_random_array(s, null, BIG.random)
new_codec('array', {zentype = 'a', encoding = 'integer' })
end)

When("create the array of '' random numbers modulo ''", function(s,m)
ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array")
ACK.array = { }
for i = s,1,-1 do
table.insert(ACK.array,F.new(math.floor(random_int16() % m)))
end
new_codec('array', {zentype = 'a', encoding = 'number' })
local modulo = mayhave(m)
if not modulo then
local mod = tonumber(m)
ZEN.assert(mod, "Argument is not a number: "..m)
modulo = BIG.new(mod)
end
local fun
local enc
local modulo_type = type(modulo)
if modulo_type == "zenroom.big" then
fun = function(input) return BIG.random() % input end
enc = 'integer'
elseif modulo_type == "zenroom.float" then
fun = function(input) return F.new(math.floor(random_int16() % tonumber(input))) end
enc = 'float'
else
error("Modulo is not a number nor an integer: "..modulo_type)
end
_create_random_array(s, modulo, fun)
new_codec('array', {zentype = 'a', encoding = enc })
end)

local function _extract_random_elements(num, from, random_fun)
Expand Down
2 changes: 1 addition & 1 deletion test/zencode/hash.bats
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Then print 'key derivation'
Then print 'HMAC'
EOF
save_output 'array_random_nums.json'
assert_output '{"HMAC":"JxoWzE+K1YvSOtxkRBkZ53v2ufOAdIAuFNpKcQSpvI8=","key_derivation":"o5vX+ydRxacRDaSZZhyVOecaAdCpssoiUPPL6t0KS68=","sha256":"R0O4u7f3wsTAVUDf4/vVWvTY7AqfFDqxmwo4U+I5wtU=","sha512":"YP2kyOt75BuI/6sCUwBsN0/od81QwWzmg2Pwxh1pXneW3VIygTRO2mU17dO/CZ2XOy3euWb0hbyHlc1Jyn+VQQ=="}'
assert_output '{"HMAC":"dFGFO3vRONcS9DPewsSHoWdsx99vBatnzUVMVJ2W0sg=","key_derivation":"9geWtsqQwnvm/ukbHwMQ6rlKttjWvjAJJUNKdyihe5Y=","sha256":"c75TjcFMRKsBC8ZQHpQ5+Hp0PhZv2cl8LCGf6jEen2Q=","sha512":"0KKLgW/ZJDN4T7QZRfuFCKQMcWRoLUmkKkBIArvri27MXvVfWMb/oWuGyFYeqAAsvgy8y4eR1JKvoRsHhpq60g=="}'


}
Expand Down
2 changes: 1 addition & 1 deletion test/zencode/output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Then print the 'array' as 'number'
and print the 'aggregation' as 'number'
EOF
save_output 'random_array.out'
assert_output '{"aggregation":3271,"array":[89,80,83,31,98,10,79,97,11,39,82,39,38,59,94,95,91,92,61,18,4,67,77,67,36,10,0,22,42,3,49,12,17,66,45,49,77,60,21,66,50,20,67,37,4,95,86,51,53,12,96,22,20,17,82,98,54,39,24,91,81,19,20,57]}'
assert_output '{"aggregation":"3174","array":["37","87","51","22","91","18","70","7","58","38","0","11","92","29","86","46","72","69","10","79","98","83","77","98","27","12","84","6","66","47","75","59","12","38","17","45","79","44","55","93","9","14","19","20","53","86","74","17","39","20","52","50","1","37","53","28","78","53","87","49","32","86","54","75"]}'
}

@test "Report failure" {
Expand Down
38 changes: 29 additions & 9 deletions test/zencode/random.bats
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,43 @@ EOF
cat <<EOF | zexe random_numbers.zen
Given nothing
When I create the array of '64' random numbers
Then print the 'array' as 'number'
Then print the 'array'
EOF
save_output 'array_random_nums.json'
assert_output '{"array":[55389,25280,37183,56931,47998,3110,50979,57297,59111,37439,16882,31139,64738,19859,45394,8295,8791,60792,64161,7418,5804,25067,37377,63567,34236,26310,59500,31422,60742,63503,17049,15212,31717,8466,39545,46949,35577,8260,9521,36166,48750,39520,39967,4937,48704,2695,24586,53251,18153,29612,18996,26522,18620,12517,21882,56298,23854,36539,4924,11491,53981,47919,26420,857]}'

assert_output '{"array":["3904086534545870648103397272374395472111763637484398936882003778415383430137","47814547401978225074551943904213649697478725078403230989228572241412545286587","20250292702156892133228542763254776040948430545701811534484410438642657305351","3638509672679313421396650447123673605069920774626448395168696993467069318322","2257780796768374787384994139875791998977257685729631114695862408498366831791","34685600361283436970232636854390932625142992040784233598000147899704055948718","10836023900635767872849288836365682792291331213435757293942181116469495190770","45011551088056380163568472514045809546537145114392674909708072719772102193807","46834277656873437830989136759743725261823997554247712334741442241521784131458","19036649058844508016250847061982566773624732066731826071450607888506722398538","22951110803797068394872512129896637701638766160588981651261275974969042109800","38464113506542938462921640434104944919670734817666327034073705300328623826311","34688353596503237898697677134242939420276624704608227019800769972238095095092","39729669272673417275689124615539379484034326218570016146219865014945900529329","50386863133821566618949117133531626446249651207178181018096553339617540676186","4780749269461261828871211789348275500996690946681185155546712938465126135346","18625526331360936019891568430062707563085530631611318185938988215690027877772","6120828558719666822714698669619663984354716516772738251985422474038270923369","4754799870786809414550151146387972441361612124041385542550994641638067561310","3171048970402684674413997541996124020856566655552110827874578708591455580379","51839476788245815571019465528585408091391537496445679174358906928439621332898","49284895238044257539799251133024481952259183570395456782029611881640483260383","23793423071278733633975613844392904697589546040471390619478009258514495753377","12422601348263381008231805759761607802196455732153489587240745599475008465898","22025317375021745893533108816830620564850851146338913756760602970780170447727","32938268479228414697959867476954271486636528822952733725679818389998338140012","29122730585157660447505036107005430131848296434991105307897538200349004020484","15679091822420326627917828339776501086424756250619494419606571743992091907006","26378147895327534839771887148112341667711296451314660930990647260520879926566","22827981109584334153926577828538463623899824964835764666076727632693253890647","23319507322545331847173789279574117245409613851450642985873714683555415232275","29346904966098864963634618326423305849751527931569182177493929683953719024359","21411742023623049412601510764676108076988626794280897615155808688138489685812","36577597262380444556516870118129258891630448815821755443719320621514026472138","44076977240693213132216562034824767453272260097121135865761042454142766693717","2263343693355390240564630226500250294548693679824816065318786148427492746145","20465961282908351218639922530194285588387291008350476792494764017072882447479","48802031388515212992418161289805877731235394249071739233340005908405467121044","30682084911053286009707594607270797715530616683689689082906588822724597436455","31270489297794985320083658178580359719066196882478666706065109210021963171693","10298083977621698456471161960404972488476291864246351843241506156315695106309","32250743118427067951355498196839349347720980200028024781374778973202019019014","15988624155440178542438823841594034017554989814129772746240673705056384599419","13493469786672307392372588897524916495700632246543154421334849913488575195520","47486011227022681378584899665338298663667604186593542307024113313108307816953","49376615311346451945914820928989976389181819548451011795425600103856435971686","6116628591184336937379216627750993277880893289105659675240169282338592191674","37694717363739166698808565633546662107826889656655916383749326014140981244017","2000209371551877596398019046309328915395457390948030554810503257358099091639","24426428267761535922463780172791998513206545821246526260030917328450197609420","21992427308692255955026386502908685913037854633409765424331905719565747265952","25182834017338467474619606393760722034275126237079457715918106332004197260250","45012910216816339370463821006545634538712257272918851546428476138491751233401","10023682381689832334957609660006976334383924197901586123477868350581815257237","23185364249367823063650142759083113342465753011192858545809957726517833002553","48557009515744913129811813319139745073331451275608433169897262261901175229828","8055397671964479959808863717350312018602873228201148773369379127077558641078","20527352340820328251225687766552542421403744600135761793525096159436408993753","1744099541349786043869278533733524209480756435241322161942399881616626700287","50813973875547414996260064851094985858929226171042919491763849444470271528749","14565585858096230174066403355487485721955230613815150373683110993650789331632","46757545749844544528061212801492420471771462899112979971344494085263544458086","25764927861691432394653552842636944222904294927399668094600578841908196742954","34713681150265725040619279827727469867214666937890540690204957740395771700475"]}'
}

@test "Array random nums modulo" {
cat <<EOF | zexe random_numbers_modulo.zen
Given nothing
cat <<EOF | save_asset random_numbers_modulo.data
{
"numbers_of_random": 10,
"modulo_int": "1234567890",
"modulo_float": 100
}
EOF

cat <<EOF | zexe random_numbers_modulo.zen random_numbers_modulo.data
Given I have a 'float' named 'numbers_of_random'
and I have a 'integer' named 'modulo_int'
and I have a 'float' named 'modulo_float'
# modulo in statement
When I create the array of '64' random numbers modulo '100'
and I create the aggregation of array 'array'
Then print the 'array' as 'number'
and print the 'aggregation' as 'number'
and I rename 'array' to 'modulo_in_statement'
# modulo as integer
When I create the array of 'numbers_of_random' random numbers modulo 'modulo_int'
and I rename 'array' to 'modulo_as_int'
# modulo as float
When I create the array of 'numbers_of_random' random numbers modulo 'modulo_float'
and I rename 'array' to 'modulo_as_float'

and I create the aggregation of array 'modulo_in_statement'
Then print the 'modulo_in_statement'
and print the 'modulo_as_int'
and print the 'modulo_as_float'
and print the 'aggregation'
EOF
save_output "array_random_nums_modulo.json"
assert_output '{"aggregation":3271,"array":[89,80,83,31,98,10,79,97,11,39,82,39,38,59,94,95,91,92,61,18,4,67,77,67,36,10,0,22,42,3,49,12,17,66,45,49,77,60,21,66,50,20,67,37,4,95,86,51,53,12,96,22,20,17,82,98,54,39,24,91,81,19,20,57]}'
assert_output '{"aggregation":"3174","modulo_as_float":[5,54,34,51,29,15,45,83,85,34],"modulo_as_int":["956626096","1153915691","295091336","359211920","623653102","781512004","89024066","243000631","1160399432","779959027"],"modulo_in_statement":["37","87","51","22","91","18","70","7","58","38","0","11","92","29","86","46","72","69","10","79","98","83","77","98","27","12","84","6","66","47","75","59","12","38","17","45","79","44","55","93","9","14","19","20","53","86","74","17","39","20","52","50","1","37","53","28","78","53","87","49","32","86","54","75"]}'
}

@test "Pick random in array with one element" {
Expand Down
Loading