Skip to content

Commit

Permalink
remove the DISALLOW_INFINITY_G1 flag, make its behavior enabled uncon…
Browse files Browse the repository at this point in the history
…ditionally. This was enabled in soft-fork 5 which has now activated and this rule can be applied to all previous blocks
  • Loading branch information
arvidn committed Sep 19, 2024
1 parent 598ff61 commit d24ec67
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 45 deletions.
28 changes: 5 additions & 23 deletions crates/chia-consensus/src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -867,15 +867,11 @@ fn decrement(cnt: &mut u32, n: NodePtr) -> Result<(), ValidationErr> {
}
}

fn to_key(a: &Allocator, pk: NodePtr, flags: u32) -> Result<Option<PublicKey>, ValidationErr> {
fn to_key(a: &Allocator, pk: NodePtr, _flags: u32) -> Result<Option<PublicKey>, ValidationErr> {
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))
}
Expand Down Expand Up @@ -3123,7 +3119,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!(
Expand All @@ -3133,21 +3129,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)]
Expand Down
13 changes: 2 additions & 11 deletions crates/chia-consensus/src/gen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 2 additions & 6 deletions crates/chia-consensus/src/spendbundle_validation.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...
Expand Down
1 change: 0 additions & 1 deletion wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...
Expand Down
4 changes: 1 addition & 3 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Coin>()?;
Expand Down

0 comments on commit d24ec67

Please sign in to comment.