From 624fe68f63e5198720587960a1c17def3db19824 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 19 Sep 2024 22:56:55 +0200 Subject: [PATCH] remove the DISALLOW_INFINITY_G1 flag, make its behavior enabled unconditionally. This was enabled in soft-fork 5 which has now activated and this rule can be applied to all previous blocks (#715) --- crates/chia-consensus/src/gen/conditions.rs | 62 +++++-------------- crates/chia-consensus/src/gen/flags.rs | 13 +--- .../src/spendbundle_validation.rs | 8 +-- generator-tests/infinity-g1.txt | 6 -- wheel/generate_type_stubs.py | 1 - wheel/python/chia_rs/chia_rs.pyi | 1 - wheel/src/api.rs | 4 +- 7 files changed, 19 insertions(+), 76 deletions(-) diff --git a/crates/chia-consensus/src/gen/conditions.rs b/crates/chia-consensus/src/gen/conditions.rs index e5bf91ca7..77ff4cc04 100644 --- a/crates/chia-consensus/src/gen/conditions.rs +++ b/crates/chia-consensus/src/gen/conditions.rs @@ -17,7 +17,7 @@ use super::opcodes::{ use super::sanitize_int::{sanitize_uint, SanitizedUint}; use super::validation_error::{first, next, rest, ErrorCode, ValidationErr}; use crate::consensus_constants::ConsensusConstants; -use crate::gen::flags::{DISALLOW_INFINITY_G1, NO_UNKNOWN_CONDS, STRICT_ARGS_COUNT}; +use crate::gen::flags::{NO_UNKNOWN_CONDS, STRICT_ARGS_COUNT}; use crate::gen::messages::{Message, SpendId}; use crate::gen::spend_visitor::SpendVisitor; use crate::gen::validation_error::check_nil; @@ -867,17 +867,13 @@ fn decrement(cnt: &mut u32, n: NodePtr) -> Result<(), ValidationErr> { } } -fn to_key(a: &Allocator, pk: NodePtr, flags: u32) -> Result, ValidationErr> { +fn to_key(a: &Allocator, pk: NodePtr) -> Result { let key = PublicKey::from_bytes(a.atom(pk).as_ref().try_into().expect("internal error")) .map_err(|_| ValidationErr(pk, ErrorCode::InvalidPublicKey))?; if key.is_inf() { - if (flags & DISALLOW_INFINITY_G1) != 0 { - Err(ValidationErr(pk, ErrorCode::InvalidPublicKey)) - } else { - Ok(None) - } + Err(ValidationErr(pk, ErrorCode::InvalidPublicKey)) } else { - Ok(Some(key)) + Ok(key) } } @@ -1124,47 +1120,31 @@ pub fn parse_conditions( state.assert_concurrent_puzzle.insert(id); } Condition::AggSigMe(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_me.push((pk, msg)); - } + spend.agg_sig_me.push((to_key(a, pk)?, msg)); } Condition::AggSigParent(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_parent.push((pk, msg)); - } + spend.agg_sig_parent.push((to_key(a, pk)?, msg)); } Condition::AggSigPuzzle(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_puzzle.push((pk, msg)); - } + spend.agg_sig_puzzle.push((to_key(a, pk)?, msg)); } Condition::AggSigAmount(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_amount.push((pk, msg)); - } + spend.agg_sig_amount.push((to_key(a, pk)?, msg)); } Condition::AggSigPuzzleAmount(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_puzzle_amount.push((pk, msg)); - } + spend.agg_sig_puzzle_amount.push((to_key(a, pk)?, msg)); } Condition::AggSigParentAmount(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_parent_amount.push((pk, msg)); - } + spend.agg_sig_parent_amount.push((to_key(a, pk)?, msg)); } Condition::AggSigParentPuzzle(pk, msg) => { - if let Some(pk) = to_key(a, pk, flags)? { - spend.agg_sig_parent_puzzle.push((pk, msg)); - } + spend.agg_sig_parent_puzzle.push((to_key(a, pk)?, msg)); } Condition::AggSigUnsafe(pk, msg) => { // AGG_SIG_UNSAFE messages are not allowed to end with the // suffix added to other AGG_SIG_* conditions check_agg_sig_unsafe_message(a, msg, constants)?; - if let Some(pk) = to_key(a, pk, flags)? { - ret.agg_sig_unsafe.push((pk, msg)); - } + ret.agg_sig_unsafe.push((to_key(a, pk)?, msg)); } Condition::Softfork(cost) => { if *max_cost < cost { @@ -3123,7 +3103,7 @@ fn test_agg_sig_invalid_pubkey( #[case(AGG_SIG_UNSAFE)] fn test_agg_sig_infinity_pubkey( #[case] condition: ConditionOpcode, - #[values(DISALLOW_INFINITY_G1, 0)] mempool: u32, + #[values(MEMPOOL_MODE, 0)] mempool: u32, ) { let ret = cond_test_flag( &format!( @@ -3133,21 +3113,7 @@ fn test_agg_sig_infinity_pubkey( mempool ); - if mempool != 0 { - assert_eq!(ret.unwrap_err().1, ErrorCode::InvalidPublicKey); - } else { - let ret = ret.expect("expected conditions to be valid").1; - assert!(ret.agg_sig_unsafe.is_empty()); - for c in ret.spends { - assert!(c.agg_sig_me.is_empty()); - assert!(c.agg_sig_parent.is_empty()); - assert!(c.agg_sig_puzzle.is_empty()); - assert!(c.agg_sig_amount.is_empty()); - assert!(c.agg_sig_puzzle_amount.is_empty()); - assert!(c.agg_sig_parent_amount.is_empty()); - assert!(c.agg_sig_parent_puzzle.is_empty()); - } - } + assert_eq!(ret.unwrap_err().1, ErrorCode::InvalidPublicKey); } #[cfg(test)] diff --git a/crates/chia-consensus/src/gen/flags.rs b/crates/chia-consensus/src/gen/flags.rs index a6a97d8a1..27c30e10a 100644 --- a/crates/chia-consensus/src/gen/flags.rs +++ b/crates/chia-consensus/src/gen/flags.rs @@ -17,14 +17,5 @@ pub const ALLOW_BACKREFS: u32 = 0x0200_0000; // what features are detected of the spends pub const ANALYZE_SPENDS: u32 = 0x0400_0000; -// When this flag is set, we reject AGG_SIG_* conditions whose public key is the -// infinity G1 point. Such public keys are mathematically valid, but do not -// provide any security guarantees. Chia has historically allowed them. Enabling -// this flag is a soft-fork. -pub const DISALLOW_INFINITY_G1: u32 = 0x1000_0000; - -pub const MEMPOOL_MODE: u32 = CLVM_MEMPOOL_MODE - | NO_UNKNOWN_CONDS - | STRICT_ARGS_COUNT - | ANALYZE_SPENDS - | DISALLOW_INFINITY_G1; +pub const MEMPOOL_MODE: u32 = + CLVM_MEMPOOL_MODE | NO_UNKNOWN_CONDS | STRICT_ARGS_COUNT | ANALYZE_SPENDS; diff --git a/crates/chia-consensus/src/spendbundle_validation.rs b/crates/chia-consensus/src/spendbundle_validation.rs index 80d9b1d81..98fa53d14 100644 --- a/crates/chia-consensus/src/spendbundle_validation.rs +++ b/crates/chia-consensus/src/spendbundle_validation.rs @@ -1,6 +1,6 @@ use crate::allocator::make_allocator; use crate::consensus_constants::ConsensusConstants; -use crate::gen::flags::{ALLOW_BACKREFS, DISALLOW_INFINITY_G1}; +use crate::gen::flags::ALLOW_BACKREFS; use crate::gen::make_aggsig_final_message::make_aggsig_final_message; use crate::gen::opcodes::{ AGG_SIG_AMOUNT, AGG_SIG_ME, AGG_SIG_PARENT, AGG_SIG_PARENT_AMOUNT, AGG_SIG_PARENT_PUZZLE, @@ -98,10 +98,6 @@ fn hash_pk_and_msg(pk: &[u8], msg: &[u8]) -> [u8; 32] { pub fn get_flags_for_height_and_constants(height: u32, constants: &ConsensusConstants) -> u32 { let mut flags: u32 = ENABLE_FIXED_DIV; - if height >= constants.soft_fork5_height { - flags |= DISALLOW_INFINITY_G1; - } - if height >= constants.hard_fork_height { // the hard-fork initiated with 2.0. To activate June 2024 // * costs are ascribed to some unknown condition codes, to allow for @@ -139,7 +135,7 @@ mod tests { #[case(0, ENABLE_FIXED_DIV)] #[case(TEST_CONSTANTS.hard_fork_height, ENABLE_BLS_OPS_OUTSIDE_GUARD | ENABLE_FIXED_DIV | ALLOW_BACKREFS)] #[case(5_716_000, ENABLE_BLS_OPS_OUTSIDE_GUARD | ENABLE_FIXED_DIV | ALLOW_BACKREFS)] - #[case(TEST_CONSTANTS.soft_fork5_height, ENABLE_BLS_OPS_OUTSIDE_GUARD | ENABLE_FIXED_DIV | ALLOW_BACKREFS | DISALLOW_INFINITY_G1)] + #[case(TEST_CONSTANTS.soft_fork5_height, ENABLE_BLS_OPS_OUTSIDE_GUARD | ENABLE_FIXED_DIV | ALLOW_BACKREFS)] fn test_get_flags(#[case] height: u32, #[case] expected_value: u32) { assert_eq!( get_flags_for_height_and_constants(height, &TEST_CONSTANTS), diff --git a/generator-tests/infinity-g1.txt b/generator-tests/infinity-g1.txt index f047b85ed..15c494f40 100644 --- a/generator-tests/infinity-g1.txt +++ b/generator-tests/infinity-g1.txt @@ -1,8 +1,2 @@ ff01ffffffa00101010101010101010101010101010101010101010101010101010101010101ffff01ffff2bffb0c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ff86666f6f6261728080ff7bff80808080 -SPENDS: -- coin id: 10d6c244183579e7704ea71e862098f15d28d63fb63c6a54779d1fe901ca48b1 ph: 99d9c5fd60cc6d5bca3390783d3489cd744139f7f143191ea911de3a22b50907 -cost: 2552512 -removal_amount: 123 -addition_amount: 0 -STRICT: FAILED: 10 diff --git a/wheel/generate_type_stubs.py b/wheel/generate_type_stubs.py index ed35a496f..fc3c0b39d 100644 --- a/wheel/generate_type_stubs.py +++ b/wheel/generate_type_stubs.py @@ -323,7 +323,6 @@ def get_flags_for_height_and_constants( NO_UNKNOWN_CONDS: int = ... STRICT_ARGS_COUNT: int = ... LIMIT_HEAP: int = ... -DISALLOW_INFINITY_G1: int = ... MEMPOOL_MODE: int = ... ENABLE_BLS_OPS: int = ... ENABLE_SECP_OPS: int = ... diff --git a/wheel/python/chia_rs/chia_rs.pyi b/wheel/python/chia_rs/chia_rs.pyi index 7b8b65cbf..94780a9c7 100644 --- a/wheel/python/chia_rs/chia_rs.pyi +++ b/wheel/python/chia_rs/chia_rs.pyi @@ -72,7 +72,6 @@ def get_flags_for_height_and_constants( NO_UNKNOWN_CONDS: int = ... STRICT_ARGS_COUNT: int = ... LIMIT_HEAP: int = ... -DISALLOW_INFINITY_G1: int = ... MEMPOOL_MODE: int = ... ENABLE_BLS_OPS: int = ... ENABLE_SECP_OPS: int = ... diff --git a/wheel/src/api.rs b/wheel/src/api.rs index 2b603d123..af97ed7a0 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -3,8 +3,7 @@ use chia_consensus::allocator::make_allocator; use chia_consensus::consensus_constants::ConsensusConstants; use chia_consensus::gen::conditions::MempoolVisitor; use chia_consensus::gen::flags::{ - ALLOW_BACKREFS, ANALYZE_SPENDS, DISALLOW_INFINITY_G1, MEMPOOL_MODE, NO_UNKNOWN_CONDS, - STRICT_ARGS_COUNT, + ALLOW_BACKREFS, ANALYZE_SPENDS, MEMPOOL_MODE, NO_UNKNOWN_CONDS, STRICT_ARGS_COUNT, }; use chia_consensus::gen::owned_conditions::{OwnedSpendBundleConditions, OwnedSpendConditions}; use chia_consensus::gen::run_block_generator::setup_generator_args; @@ -505,7 +504,6 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add("MEMPOOL_MODE", MEMPOOL_MODE)?; m.add("ALLOW_BACKREFS", ALLOW_BACKREFS)?; m.add("ANALYZE_SPENDS", ANALYZE_SPENDS)?; - m.add("DISALLOW_INFINITY_G1", DISALLOW_INFINITY_G1)?; // Chia classes m.add_class::()?;