Skip to content

Commit

Permalink
now that the hard-fork has activated, we can enable BLS ops outside t…
Browse files Browse the repository at this point in the history
…he softfork guard unconditionally (and simplify the code)
  • Loading branch information
arvidn committed Sep 19, 2024
1 parent 3291a32 commit fca5d6a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 75 deletions.
11 changes: 2 additions & 9 deletions fuzz/fuzz_targets/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
use libfuzzer_sys::fuzz_target;

use clvmr::allocator::Allocator;
use clvmr::chia_dialect::{
ChiaDialect, ENABLE_BLS_OPS_OUTSIDE_GUARD, MEMPOOL_MODE, NO_UNKNOWN_OPS,
};
use clvmr::chia_dialect::{ChiaDialect, MEMPOOL_MODE, NO_UNKNOWN_OPS};
use clvmr::cost::Cost;
use clvmr::reduction::Reduction;
use clvmr::run_program::run_program;
Expand All @@ -22,12 +20,7 @@ fuzz_target!(|data: &[u8]| {

let allocator_checkpoint = allocator.checkpoint();

for flags in [
0,
ENABLE_BLS_OPS_OUTSIDE_GUARD,
ENABLE_BLS_OPS_OUTSIDE_GUARD | NO_UNKNOWN_OPS,
MEMPOOL_MODE,
] {
for flags in [0, NO_UNKNOWN_OPS, MEMPOOL_MODE] {
let dialect = ChiaDialect::new(flags);
allocator.restore_checkpoint(&allocator_checkpoint);

Expand Down
48 changes: 19 additions & 29 deletions src/chia_dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ pub const NO_UNKNOWN_OPS: u32 = 0x0002;
// the number of pairs
pub const LIMIT_HEAP: u32 = 0x0004;

// enables the BLS ops extensions *outside* the softfork guard. This is a
// hard-fork and should only be enabled when it activates
pub const ENABLE_BLS_OPS_OUTSIDE_GUARD: u32 = 0x0020;

// The default mode when running grnerators in mempool-mode (i.e. the stricter
// mode)
pub const MEMPOOL_MODE: u32 = NO_UNKNOWN_OPS | LIMIT_HEAP;
Expand Down Expand Up @@ -63,13 +59,10 @@ impl Dialect for ChiaDialect {
o: NodePtr,
argument_list: NodePtr,
max_cost: Cost,
extension: OperatorSet,
_extension: OperatorSet,
) -> Response {
let flags = self.flags
| match extension {
OperatorSet::BLS => ENABLE_BLS_OPS_OUTSIDE_GUARD,
_ => 0,
};
// new softfork extensions go here, to enable new feature flags
let flags = self.flags;
let op_len = allocator.atom_len(o);
if op_len == 4 {
// these are unknown operators with assigned cost
Expand Down Expand Up @@ -141,25 +134,20 @@ impl Dialect for ChiaDialect {
34 => op_all,
// 35 ---
// 36 = softfork
48..=61 if (flags & ENABLE_BLS_OPS_OUTSIDE_GUARD) != 0 => match op {
48 => op_coinid,
49 => op_bls_g1_subtract,
50 => op_bls_g1_multiply,
51 => op_bls_g1_negate,
52 => op_bls_g2_add,
53 => op_bls_g2_subtract,
54 => op_bls_g2_multiply,
55 => op_bls_g2_negate,
56 => op_bls_map_to_g1,
57 => op_bls_map_to_g2,
58 => op_bls_pairing_identity,
59 => op_bls_verify,
60 => op_modpow,
61 => op_mod,
_ => {
unreachable!();
}
},
48 => op_coinid,
49 => op_bls_g1_subtract,
50 => op_bls_g1_multiply,
51 => op_bls_g1_negate,
52 => op_bls_g2_add,
53 => op_bls_g2_subtract,
54 => op_bls_g2_multiply,
55 => op_bls_g2_negate,
56 => op_bls_map_to_g1,
57 => op_bls_map_to_g2,
58 => op_bls_pairing_identity,
59 => op_bls_verify,
60 => op_modpow,
61 => op_mod,
_ => {
return unknown_operator(allocator, o, argument_list, flags, max_cost);
}
Expand All @@ -181,6 +169,8 @@ impl Dialect for ChiaDialect {
// return the Operators it enables (or None) if we don't know what it means
fn softfork_extension(&self, ext: u32) -> OperatorSet {
match ext {
// The BLS extensions (and coinid) operators were brought into the
// main operator set as part of the hard fork
0 => OperatorSet::BLS,
// new extensions go here
_ => OperatorSet::Default,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use allocator::{Allocator, Atom, NodePtr, SExp};
pub use chia_dialect::ChiaDialect;
pub use run_program::run_program;

pub use chia_dialect::{ENABLE_BLS_OPS_OUTSIDE_GUARD, LIMIT_HEAP, MEMPOOL_MODE, NO_UNKNOWN_OPS};
pub use chia_dialect::{LIMIT_HEAP, MEMPOOL_MODE, NO_UNKNOWN_OPS};

#[cfg(feature = "counters")]
pub use run_program::run_program_with_counters;
Expand Down
50 changes: 18 additions & 32 deletions src/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ struct RunProgramTest<'a> {
use crate::test_ops::parse_exp;

#[cfg(test)]
use crate::chia_dialect::{ENABLE_BLS_OPS_OUTSIDE_GUARD, NO_UNKNOWN_OPS};
use crate::chia_dialect::NO_UNKNOWN_OPS;

#[cfg(test)]
const TEST_CASES: &[RunProgramTest] = &[
Expand Down Expand Up @@ -1202,29 +1202,19 @@ const TEST_CASES: &[RunProgramTest] = &[
RunProgramTest {
prg: "(coinid (q . 0x1234500000000000000000000000000000000000000000000000000000000000) (q . 0x6789abcdef000000000000000000000000000000000000000000000000000000) (q . 123456789))",
args: "()",
flags: ENABLE_BLS_OPS_OUTSIDE_GUARD,
flags: 0,
result: Some("0x69bfe81b052bfc6bd7f3fb9167fec61793175b897c16a35827f947d5cc98e4bc"),
cost: 861,
err: "",
},
RunProgramTest {
prg: "(coinid (q . 0x1234500000000000000000000000000000000000000000000000000000000000) (q . 0x6789abcdef000000000000000000000000000000000000000000000000000000) (q . 0x000123456789))",
args: "()",
flags: ENABLE_BLS_OPS_OUTSIDE_GUARD,
flags: 0,
result: None,
cost: 861,
err: "coinid: invalid amount (may not have redundant leading zero)",
},
// make sure the coinid operator is not available unless the flag is
// specified
RunProgramTest {
prg: "(coinid (q . 0x1234500000000000000000000000000000000000000000000000000000000000) (q . 0x6789abcdef000000000000000000000000000000000000000000000000000000) (q . 0x000123456789))",
args: "()",
flags: NO_UNKNOWN_OPS,
result: None,
cost: 861,
err: "unimplemented operator",
},

// secp261k1

Expand Down Expand Up @@ -1331,60 +1321,60 @@ use rstest::rstest;
// this program raises an exception if the computed coin ID matches the
// expected
#[case::coinid("(i (= (coinid (q . 0x1234500000000000000000000000000000000000000000000000000000000000) (q . 0x6789abcdef000000000000000000000000000000000000000000000000000000) (q . 123456789)) (q . 0x69bfe81b052bfc6bd7f3fb9167fec61793175b897c16a35827f947d5cc98e4bd)) (q . 0) (q x))",
(1432, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(1432, 0, 0),
"clvm raise")]
// also test the opposite. This program is the same as above but it raises
// if the coin ID is a mismatch
#[case::coinid("(i (= (coinid (q . 0x1234500000000000000000000000000000000000000000000000000000000000) (q . 0x6789abcdef000000000000000000000000000000000000000000000000000000) (q . 123456789)) (q . 0x69bfe81b052bfc6bd7f3fb9167fec61793175b897c16a35827f947d5cc98e4bc)) (q . 0) (q x))",
(1432, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(1432, 0, 0),
"")]
// modpow
#[case::modpow(
"(i (= (modpow (q . 12345) (q . 6789) (q . 44444444444)) (q . 13456191581)) (q . 0) (q x))",
(18241, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(18241, 0, 0),
""
)]
#[case::modpow(
"(i (= (modpow (q . 12345) (q . 6789) (q . 44444444444)) (q . 13456191582)) (q . 0) (q x))",
(18241, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(18241, 0, 0),
"clvm raise"
)]
// mod
#[case::modulus(
"(i (= (% (q . 80001) (q . 73)) (q . 66)) (q . 0) (q x))",
(1564, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(1564, 0, 0),
""
)]
#[case::modulus(
"(i (= (% (q . 80001) (q . 73)) (q . 67)) (q . 0) (q x))",
(1564, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(1564, 0, 0),
"clvm raise"
)]
// g1_multiply
#[case::g1_mul("(i (= (g1_multiply (q . 0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb) (q . 2)) (q . 0xa572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e)) (q . 0) (q x))",
(706634, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(706634, 0, 0),
"")]
#[case::g1_mul(
"(i (= (g1_multiply (q . 0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb) (q . 2)) (q . 0xa572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4f)) (q . 0) (q x))",
(706634, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(706634, 0, 0),
"clvm raise")]
#[case::g1_neg("(i (= (g1_negate (q . 0xb7f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0xb7f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0) (q x))", (706634, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD), "clvm raise")]
#[case::g1_neg("(i (= (g1_negate (q . 0xb7f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0xb7f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0) (q x))", (706634, 0, 0), "clvm raise")]
#[case::g1_neg("(i (= (g1_negate (q . 0xb2f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0xb7f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb)) (q . 0) (q x))",
(706634, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(706634, 0, 0),
"atom is not a valid G1 point")]
#[case::g2_add("(i (= (g2_add (q . 0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8) (q . 0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8)) (q . 0xaa4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c335771638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053)) (q . 0) (q x))",
(3981700, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(3981700, 0, 0),
"")]
#[case::g2_add("(i (= (g2_add (q . 0x93e12b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8) (q . 0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8)) (q . 0xaa4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c335771638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053)) (q . 0) (q x))",
(3981700, 0, ENABLE_BLS_OPS_OUTSIDE_GUARD),
(3981700, 0, 0),
"atom is not a G2 point")]
fn test_softfork(
#[case] prg: &'static str,
#[case] fields: (u64, u8, u32), // cost, enabled, hard_fork_flag
#[case] err: &'static str,
#[values(0)] flags: u32,
#[values(false, true)] mempool: bool,
#[values(0, 1, 2)] test_ext: u8,
#[values(0, 1)] test_ext: u8,
) {
let (cost, enabled, hard_fork_flag) = fields;
let softfork_prg =
Expand Down Expand Up @@ -1464,13 +1454,9 @@ fn test_softfork(
prg: outside_guard_prg.as_str(),
args: "()",
flags,
result: None,
result: if err.is_empty() { Some("()") } else { None },
cost: cost - 140,
err: if mempool {
"unimplemented operator"
} else {
"clvm raise"
},
err,
};
run_test_case(&t);

Expand Down
8 changes: 4 additions & 4 deletions tools/src/bin/benchmark-clvm-cost.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use clvmr::allocator::{Allocator, NodePtr};
use clvmr::chia_dialect::{ChiaDialect, ENABLE_BLS_OPS_OUTSIDE_GUARD};
use clvmr::chia_dialect::ChiaDialect;
use clvmr::run_program::run_program;
use linreg::linear_regression_of;
use std::fs::{create_dir_all, File};
Expand Down Expand Up @@ -118,7 +118,7 @@ fn substitute(args: Placeholder, s: NodePtr) -> OpArgs {
fn time_invocation(a: &mut Allocator, op: u32, arg: OpArgs, flags: u32) -> f64 {
let call = build_call(a, op, arg, 1, None);
//println!("{:x?}", &Node::new(a, call));
let dialect = ChiaDialect::new(ENABLE_BLS_OPS_OUTSIDE_GUARD);
let dialect = ChiaDialect::new(0);
let start = Instant::now();
let r = run_program(a, &dialect, call, a.nil(), 11000000000);
if (flags & ALLOW_FAILURE) == 0 {
Expand Down Expand Up @@ -169,7 +169,7 @@ fn time_per_byte(a: &mut Allocator, op: &Operator, output: &mut dyn Write) -> f6
// establish how much time each additional argument contributes
fn time_per_arg(a: &mut Allocator, op: &Operator, output: &mut dyn Write) -> f64 {
let mut samples = Vec::<(f64, f64)>::new();
let dialect = ChiaDialect::new(ENABLE_BLS_OPS_OUTSIDE_GUARD);
let dialect = ChiaDialect::new(0);

let subst = a
.new_atom(
Expand Down Expand Up @@ -212,7 +212,7 @@ fn base_call_time(
output: &mut dyn Write,
) -> f64 {
let mut samples = Vec::<(f64, f64)>::new();
let dialect = ChiaDialect::new(ENABLE_BLS_OPS_OUTSIDE_GUARD);
let dialect = ChiaDialect::new(0);

let subst = a
.new_atom(
Expand Down

0 comments on commit fca5d6a

Please sign in to comment.