From cb1779c29ef625578f216b8c54a17a31dcfbf570 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Thu, 3 Aug 2023 18:03:49 +0800 Subject: [PATCH 1/4] Remove unreachable constraints and check that account leafs are not deleted for nonce proofs --- src/gadgets/mpt_update.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/gadgets/mpt_update.rs b/src/gadgets/mpt_update.rs index 54a4a0bf..013bd289 100644 --- a/src/gadgets/mpt_update.rs +++ b/src/gadgets/mpt_update.rs @@ -1144,6 +1144,10 @@ fn configure_nonce( bytes: &impl BytesLookup, poseidon: &impl PoseidonLookup, ) { + cb.assert( + "account leafs cannot be deleted", + !config.path_type.current_matches(&[PathType::ExtensionOld]), + ); for variant in SegmentType::iter() { let conditional_constraints = |cb: &mut ConstraintBuilder| match variant { SegmentType::AccountTrie => { @@ -1256,20 +1260,6 @@ fn configure_nonce( ); }, ); - cb.condition( - config.path_type.current_matches(&[PathType::ExtensionOld]), - |cb| { - cb.add_lookup( - "old nonce is 8 bytes", - [config.old_value.current(), Query::from(7)], - bytes.lookup(), - ); - cb.assert_zero( - "code size is 0 for ExtensionOld nonce update", - old_code_size, - ); - }, - ); } _ => {} }; From 5de59bbcca4de3a002ee8a92fbd2827cd62e72e4 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Thu, 3 Aug 2023 18:30:40 +0800 Subject: [PATCH 2/4] Fix constraint typos --- src/gadgets/mpt_update.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gadgets/mpt_update.rs b/src/gadgets/mpt_update.rs index 013bd289..8e61147a 100644 --- a/src/gadgets/mpt_update.rs +++ b/src/gadgets/mpt_update.rs @@ -1227,7 +1227,7 @@ fn configure_nonce( ); cb.add_lookup( "new nonce is 8 bytes", - [config.old_value.current(), Query::from(7)], + [config.new_value.current(), Query::from(7)], bytes.lookup(), ); cb.assert_equal( @@ -1247,7 +1247,7 @@ fn configure_nonce( |cb| { cb.add_lookup( "new nonce is 8 bytes", - [config.old_value.current(), Query::from(7)], + [config.new_value.current(), Query::from(7)], bytes.lookup(), ); cb.assert_zero( From a15881b9acba95697a146fc262b95e5d6af82df5 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Thu, 3 Aug 2023 18:31:23 +0800 Subject: [PATCH 3/4] Deduplicate lookup --- src/gadgets/mpt_update.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/gadgets/mpt_update.rs b/src/gadgets/mpt_update.rs index 8e61147a..c861e649 100644 --- a/src/gadgets/mpt_update.rs +++ b/src/gadgets/mpt_update.rs @@ -1217,6 +1217,11 @@ fn configure_nonce( * Query::Constant(F::from(1 << 32).square().invert().unwrap()); let new_code_size = (config.new_hash.current() - config.new_value.current()) * Query::Constant(F::from(1 << 32).square().invert().unwrap()); + cb.add_lookup( + "new nonce is 8 bytes", + [config.new_value.current(), Query::from(7)], + bytes.lookup(), + ); cb.condition( config.path_type.current_matches(&[PathType::Common]), |cb| { @@ -1225,11 +1230,6 @@ fn configure_nonce( [config.old_value.current(), Query::from(7)], bytes.lookup(), ); - cb.add_lookup( - "new nonce is 8 bytes", - [config.new_value.current(), Query::from(7)], - bytes.lookup(), - ); cb.assert_equal( "old_code_size = new_code_size for nonce update", old_code_size.clone(), @@ -1245,11 +1245,6 @@ fn configure_nonce( cb.condition( config.path_type.current_matches(&[PathType::ExtensionNew]), |cb| { - cb.add_lookup( - "new nonce is 8 bytes", - [config.new_value.current(), Query::from(7)], - bytes.lookup(), - ); cb.assert_zero( "code size is 0 for ExtensionNew nonce update", new_code_size, From 69bbfb39cf23db65f217137f6640c72aaaf0be38 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Thu, 3 Aug 2023 18:34:15 +0800 Subject: [PATCH 4/4] Check that old nonce is 0 for new account and move old_code_size into condition --- src/gadgets/mpt_update.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/gadgets/mpt_update.rs b/src/gadgets/mpt_update.rs index c861e649..9ad820f2 100644 --- a/src/gadgets/mpt_update.rs +++ b/src/gadgets/mpt_update.rs @@ -1213,8 +1213,6 @@ fn configure_nonce( SegmentType::AccountLeaf3 => { cb.assert_zero("direction is 0", config.direction.current()); - let old_code_size = (config.old_hash.current() - config.old_value.current()) - * Query::Constant(F::from(1 << 32).square().invert().unwrap()); let new_code_size = (config.new_hash.current() - config.new_value.current()) * Query::Constant(F::from(1 << 32).square().invert().unwrap()); cb.add_lookup( @@ -1230,6 +1228,9 @@ fn configure_nonce( [config.old_value.current(), Query::from(7)], bytes.lookup(), ); + let old_code_size = (config.old_hash.current() + - config.old_value.current()) + * Query::Constant(F::from(1 << 32).square().invert().unwrap()); cb.assert_equal( "old_code_size = new_code_size for nonce update", old_code_size.clone(), @@ -1237,7 +1238,7 @@ fn configure_nonce( ); cb.add_lookup( "existing code size is 8 bytes", - [old_code_size.clone(), Query::from(7)], + [old_code_size, Query::from(7)], bytes.lookup(), ); }, @@ -1245,6 +1246,10 @@ fn configure_nonce( cb.condition( config.path_type.current_matches(&[PathType::ExtensionNew]), |cb| { + cb.assert_zero( + "old nonce is 0 for ExtensionNew nonce update", + config.old_value.current(), + ); cb.assert_zero( "code size is 0 for ExtensionNew nonce update", new_code_size,