Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable keccak opcode in simulator #106

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ paste = "1.0.15"
[profile.release]
lto = true
strip = "symbols"

[patch.crates-io]
clvmr = { git = "https://github.com/Chia-Network/clvm_rs", rev = "2f413e72fcf1bcafa4a3117f2c2a0a3a0e7e1c6b" }
14 changes: 4 additions & 10 deletions crates/chia-sdk-test/src/announcements.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chia_protocol::{Bytes, Bytes32, CoinSpend};
use chia_sdk_types::{
announcement_id, AssertCoinAnnouncement, AssertPuzzleAnnouncement, CreateCoinAnnouncement,
CreatePuzzleAnnouncement,
announcement_id, run_puzzle, AssertCoinAnnouncement, AssertPuzzleAnnouncement,
CreateCoinAnnouncement, CreatePuzzleAnnouncement,
};
use clvm_traits::{FromClvm, ToClvm};
use clvmr::{reduction::Reduction, run_program, Allocator, ChiaDialect, NodePtr};
use clvmr::{Allocator, NodePtr};

#[derive(Debug, Default, Clone)]
pub struct Announcements {
Expand Down Expand Up @@ -77,13 +77,7 @@ pub fn announcements_for_spend(coin_spend: &CoinSpend) -> anyhow::Result<Announc
let puzzle = coin_spend.puzzle_reveal.to_clvm(allocator)?;
let solution = coin_spend.solution.to_clvm(allocator)?;

let Reduction(_cost, output) = run_program(
allocator,
&ChiaDialect::new(0),
puzzle,
solution,
11_000_000_000,
)?;
let output = run_puzzle(allocator, puzzle, solution)?;

let conditions = Vec::<NodePtr>::from_clvm(allocator, output)?;

Expand Down
70 changes: 66 additions & 4 deletions crates/chia-sdk-test/src/simulator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use std::collections::HashSet;
use std::{
collections::HashSet,
time::{Duration, Instant},
};

use chia_bls::{DerivableKey, PublicKey, SecretKey};
use chia_bls::{aggregate_verify_gt, hash_to_g2, DerivableKey, PublicKey, SecretKey};
use chia_consensus::{
consensus_constants::ConsensusConstants, gen::validation_error::ErrorCode,
spendbundle_validation::validate_clvm_and_signature,
allocator::make_allocator,
consensus_constants::ConsensusConstants,
gen::{owned_conditions::OwnedSpendBundleConditions, validation_error::ErrorCode},
spendbundle_conditions::run_spendbundle,
spendbundle_validation::ValidationPair,
};
use chia_protocol::{Bytes32, Coin, CoinSpend, CoinState, Program, SpendBundle};
use chia_puzzles::standard::StandardArgs;
use chia_sdk_types::TESTNET11_CONSTANTS;
use clvmr::{
chia_dialect::{ENABLE_KECCAK, ENABLE_KECCAK_OPS_OUTSIDE_GUARD},
sha2::Sha256,
LIMIT_HEAP,
};
use fastrand::Rng;
use indexmap::{IndexMap, IndexSet};

Expand Down Expand Up @@ -288,3 +299,54 @@ impl Simulator {
self.height += 1;
}
}

// currently in mempool_manager.py
// called in threads from pre_validate_spend_bundle()
// pybinding returns (error, cached_results, new_cache_entries, duration)
fn validate_clvm_and_signature(
spend_bundle: &SpendBundle,
max_cost: u64,
constants: &ConsensusConstants,
height: u32,
) -> Result<(OwnedSpendBundleConditions, Vec<ValidationPair>, Duration), ErrorCode> {
let start_time = Instant::now();
let mut a = make_allocator(LIMIT_HEAP);
let (sbc, pkm_pairs) = run_spendbundle(
&mut a,
spend_bundle,
max_cost,
height,
ENABLE_KECCAK | ENABLE_KECCAK_OPS_OUTSIDE_GUARD,
constants,
)
.map_err(|e| e.1)?;
let conditions = OwnedSpendBundleConditions::from(&a, sbc);

// Collect all pairs in a single vector to avoid multiple iterations
let mut pairs = Vec::new();

let mut aug_msg = Vec::<u8>::new();

for (pk, msg) in pkm_pairs {
aug_msg.clear();
aug_msg.extend_from_slice(&pk.to_bytes());
aug_msg.extend(&*msg);
let aug_hash = hash_to_g2(&aug_msg);
let pairing = aug_hash.pair(&pk);

let mut key = Sha256::new();
key.update(&aug_msg);
pairs.push((key.finalize(), pairing));
}
// Verify aggregated signature
let result = aggregate_verify_gt(
&spend_bundle.aggregated_signature,
pairs.iter().map(|tuple| &tuple.1),
);
if !result {
return Err(ErrorCode::BadAggregateSignature);
}

// Collect results
Ok((conditions, pairs, start_time.elapsed()))
}
3 changes: 2 additions & 1 deletion crates/chia-sdk-types/src/run_puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clvmr::{
chia_dialect::{ENABLE_KECCAK, ENABLE_KECCAK_OPS_OUTSIDE_GUARD},
reduction::{EvalErr, Reduction},
Allocator, NodePtr,
};
Expand All @@ -10,7 +11,7 @@ pub fn run_puzzle(
) -> Result<NodePtr, EvalErr> {
let Reduction(_cost, output) = clvmr::run_program(
allocator,
&clvmr::ChiaDialect::new(0),
&clvmr::ChiaDialect::new(ENABLE_KECCAK | ENABLE_KECCAK_OPS_OUTSIDE_GUARD),
puzzle,
solution,
11_000_000_000,
Expand Down
3 changes: 2 additions & 1 deletion napi/src/clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use chia::{
};
use chia_wallet_sdk::{self as sdk, HashedPtr, SpendContext};
use clvmr::{
chia_dialect::{ENABLE_KECCAK, ENABLE_KECCAK_OPS_OUTSIDE_GUARD},
run_program,
serde::{node_from_bytes, node_from_bytes_backrefs},
ChiaDialect, NodePtr, MEMPOOL_MODE,
Expand Down Expand Up @@ -70,7 +71,7 @@ impl ClvmAllocator {
max_cost: BigInt,
mempool_mode: bool,
) -> Result<Output> {
let mut flags = 0;
let mut flags = ENABLE_KECCAK | ENABLE_KECCAK_OPS_OUTSIDE_GUARD;

if mempool_mode {
flags |= MEMPOOL_MODE;
Expand Down