From 5fe7461994f06cfebe94a2e50ed7ab8d4f5c88d4 Mon Sep 17 00:00:00 2001 From: rheidner Date: Thu, 6 Oct 2022 17:58:39 -0300 Subject: [PATCH] change statement serialization to use 2 bits With four types of statements is only needed 2 bits to specify its types. Before this commit 4 bits were used. --- example/block_3.kdl | 10 +++++----- example/block_4.kdl | 36 ++++++++++++++++++------------------ src/bits.rs | 10 +++++----- src/test/cli.rs | 6 +++--- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/example/block_3.kdl b/example/block_3.kdl index eba05821..1d86285b 100644 --- a/example/block_3.kdl +++ b/example/block_3.kdl @@ -20,9 +20,9 @@ run { ask x = (Subj); (Done x) } sign { - 008e98b8450d7bc887bf99e300 - 41a496322f43e8117fcea935a0 - 90281422f06d6752506fcbb35d - ab2c8425b6bc2c2bf914260cb8 - 4b71ee52544914c27292749bc3 + 01f14bbfcf540c881dcf0200f2 + ac7f62c6dfd382296f95d05616 + 575957238b34f27687b3ea933e + 1570729d145eb0359c42b9f110 + f911bab0239d5457a18ac2b6d9 } diff --git a/example/block_4.kdl b/example/block_4.kdl index 3d8fa369..fa5af3e8 100644 --- a/example/block_4.kdl +++ b/example/block_4.kdl @@ -6,25 +6,25 @@ // Registers the "Foo" namespace to Alice. // Since this is a top-level name, this must be signed by the global Namer. reg Foo { - #x2b5ad5c4795c026514f8317c7a215e -} sign { - 0055db2c36550b962462a80acb - acb562aa04638674ce654a4fbc - 2ef195591414ee3e87b8e08543 - 10818e8f46ccf15a0f2e338c4f - ee20fa177e4c1cf0365b4acae8 + #x2b5ad5c4795c026514f8317c7a215e +} sign { + 01e98cf3735586ebad0e5f0455 + addda8c242f4b4a7e673695db8 + f11d4e583c51c145bc502f8f86 + 47cb88ea7c922644f0d843c5d3 + b9e3e272cb4e3cb73b70effc0b } // Registers the "Foo.Bar" namespace to Bob. // Since "Foo" is owned by Alice, this must be signed by her. reg Foo.Bar { - #x6813eb9362372eef6200f3b1dbc3f8 + #x6813eb9362372eef6200f3b1dbc3f8 } sign { - 0145ccb8ab88d3f07822a0cff7 - 85d3eb3c8183afffa7d03efa0e - 5956dcc54e0e7007d608aff377 - 05c51d7336c05c37f1e210fbfa - 13621c960eaadedc839b6b86fa + 0033216afb6e55ed55a66b5862 + f10fe3501cd5989867a129e222 + 5760d9702aa8da697df25613ee + c935d9b933e7d9b3522b5f3455 + affe0276d2cd37550de556f47d } // Defines a "Foo.Bar.cats" function that always returns 42. @@ -32,11 +32,11 @@ reg Foo.Bar { fun (Foo.Bar.cats) { (Foo.Bar.cats) = #42 } sign { - 0178f2cc0b6a326640029b64f9 - 7a414d388f24676090f1371857 - 81cc615dc49fff08396c429613 - 9420ff9d45c417bbf0224b5586 - de4149b06405587de4615c780a + 00f5886b04c847874cc3f7319b + 8f32934bd3cd6632bcb8c5d9d6 + d97099ca19bf8f491e95d1d570 + 0e494a31457e55598cd8b9bf83 + 9e452585051b8cc1cae09449c5 } // Runs Bob's cats function! diff --git a/src/bits.rs b/src/bits.rs index 8ef65df1..5517f2ba 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -468,7 +468,7 @@ impl ProtoSerialize for Statement { fn proto_serialize(&self, bits: &mut BitVec, names: &mut Names) { match self { Statement::Fun { name, args, func, init, sign } => { - serialize_fixlen(4, &u256(0), bits, names); + serialize_fixlen(2, &u256(0), bits, names); name.proto_serialize(bits, names); serialize_list(args, bits, names); func.proto_serialize(bits, names); @@ -476,18 +476,18 @@ impl ProtoSerialize for Statement { sign.proto_serialize(bits, names); } Statement::Ctr { name, args, sign } => { - serialize_fixlen(4, &u256(1), bits, names); + serialize_fixlen(2, &u256(1), bits, names); name.proto_serialize(bits, names); serialize_list(args, bits, names); sign.proto_serialize(bits, names); } Statement::Run { expr, sign } => { - serialize_fixlen(4, &u256(2), bits, names); + serialize_fixlen(2, &u256(2), bits, names); expr.proto_serialize(bits, names); sign.proto_serialize(bits, names); } Statement::Reg { name, ownr, sign } => { - serialize_fixlen(4, &u256(3), bits, names); + serialize_fixlen(2, &u256(3), bits, names); name.proto_serialize(bits, names); serialize_fixlen(128, &u256(**ownr), bits, names); sign.proto_serialize(bits, names); @@ -500,7 +500,7 @@ impl ProtoSerialize for Statement { index: &mut u128, names: &mut Names, ) -> Option { - let tag = deserialize_fixlen(4, bits, index, names)?.low_u128(); + let tag = deserialize_fixlen(2, bits, index, names)?.low_u128(); match tag { 0 => { let name = Name::proto_deserialize(bits, index, names)?; diff --git a/src/test/cli.rs b/src/test/cli.rs index 7b9075c7..7c5cff2a 100644 --- a/src/test/cli.rs +++ b/src/test/cli.rs @@ -128,9 +128,9 @@ fn signing_run( } #[rstest] -#[case("example/private_key_1_namer", "4d576ce7dc24f565a7cee2390071191da2b0de13e1fd99c7008225694cf4c21788fe7395ac05091428440fb6e64a0af6d3cdbad53421a46d3d34d49f2864301dd28e774a2a9228434e492ed9c3")] -#[case("example/private_key_2_alice", "4d576ce7dc24f565a7cee23980b6cc8dbc97db445ce9db50e5f7cddba5f5088227bcbb1d517aebe61f324ef5bd38c44d544ffc8ae962de6379f20fa638417184d45a6b25365411657889c7f41e")] -#[case("example/private_key_3_bob", "4d576ce7dc24f565a7cee239808f479434991302dd2cf82b2b4e4acfe67bbd93d81f496e1da6610498261a2bc92270c58c260d857ff98f761e6afff7f9294feccf28f1f5455cf0e80722f82c6b")] +#[case("example/private_key_1_namer", "755db39f7093d5969f3b88e6023f4bf7cca8c046e3cd00013cd7f9198fef2d0653daa42da9a3aa6bab1344b13dbb87355f25f2a0393ae4a1e836b0e50a763c227e21743712e4abaa15450db66c")] +#[case("example/private_key_2_alice", "755db39f7093d5969f3b88e6023771ae6a24b5b8c3be37068d8b0eb74d293c99396bc3f21a0a221aae3dfe8dee8b78680600b304724c4637d3729b982c24249a8dc47b9aa0eeb0eda6cef17490")] +#[case("example/private_key_3_bob", "755db39f7093d5969f3b88e600304bbff232fb35a58fc6c2f1b02a105514bdf9a05a400cfe684342ab9339f3bb628ba11b859e9cfda89730a347fb1194bd88e6d71d40aabc3a3be8799c3d9734")] fn signing(#[case] private_key: &str, #[case] expected_result: &str) { let output = kindelia!() .args([