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

Implement keccak256 operator and softfork #489

Merged
merged 6 commits into from
Nov 7, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/build-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
- name: wasm-pack build and pack
run: wasm-pack build --release --target=nodejs wasm && wasm-pack pack wasm

- name: Setup Node 16.x
- name: Setup Node 18.x
uses: actions/setup-node@v4
with:
node-version: "16.x"
node-version: "18.x"

- name: Test wasm
run: node wasm/tests/index.js
Expand Down
20 changes: 20 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ hex-literal = { workspace = true }
# for secp sigs
k256 = { version = "0.13.4", features = ["ecdsa"] }
p256 = { version = "0.13.2", features = ["ecdsa"] }
# for keccak256
sha3 = "0.10.8"

[dev-dependencies]
rstest = { workspace = true }
Expand Down
11 changes: 8 additions & 3 deletions docs/new-operator-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ Follow this checklist when adding operators:
- Include the new operators in the fuzzer `fuzz/fuzz_targets/operators.rs`
- Include the new operators and their signatures in `tools/src/bin/generate-fuzz-corpus.rs`.
Make sure to run this and fuzz for some time before landing the PR.
- extend the benchmark-clvm-cost.rs to include benchmarks for the new operator,
- Extend the benchmark-clvm-cost.rs to include benchmarks for the new operator,
to establish its cost.
- The opcode decoding and dispatching happens in `src/ChiaDialect.rs`
- The opcode decoding and dispatching happens in `src/chia_dialect.rs`
- Add support for the new operators in `src/test_ops.rs` `parse_atom()`, to
compile the name of the operator to its corresponding opcode.
- If the operator(s) are part of an extension to `softfork`, add another value
to the `OperatorSet` enum.
- Add a new flag (in `src/chia_dialect.rs`) that controls whether the
operators are activated or not. This is required in order for the chain to exist
in a state _before_ your soft-fork has activated, and behave consistently with
versions of the node that doesn't know about your new operators.
Make sure the value of the flag does not collide with any of the flags in
[chia_rs](https://github.com/Chia-Network/chia_rs/blob/main/src/gen/flags.rs).
[chia_rs](https://github.com/Chia-Network/chia_rs/blob/main/crates/chia-consensus/src/gen/flags.rs).
This is a quirk where both of these repos share the same flags space.
- Once a soft-fork has activated, if everything on chain before the softfork is
compatible with the new rules (which is likely and ought to be the ambition
with all soft-forks), all logic surrounding activating or deactivating the
soft-fork should be removed.
- Expose the new flag(s) to python in chia_rs.
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ name = "allocator"
path = "fuzz_targets/allocator.rs"
test = false
doc = false

[[bin]]
name = "keccak"
path = "fuzz_targets/keccak.rs"
test = false
doc = false
16 changes: 16 additions & 0 deletions fuzz/fuzz_targets/keccak.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![no_main]
use clvmr::keccak256_ops::op_keccak256;
use clvmr::{reduction::Reduction, Allocator, NodePtr};
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
let mut a = Allocator::new();
let blob = a.new_atom(data).expect("failed to create atom");
let args = a
.new_pair(blob, NodePtr::NIL)
.expect("failed to create pair");
let Reduction(cost, node) = op_keccak256(&mut a, args, 11000000000).expect("keccak256 failed");
assert!(cost >= 210);
assert!(node.is_atom());
assert_eq!(a.atom_len(node), 32);
});
5 changes: 4 additions & 1 deletion fuzz/fuzz_targets/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clvmr::bls_ops::{
};
use clvmr::core_ops::{op_cons, op_eq, op_first, op_if, op_listp, op_raise, op_rest};
use clvmr::cost::Cost;
use clvmr::keccak256_ops::op_keccak256;
use clvmr::more_ops::{
op_add, op_all, op_any, op_ash, op_coinid, op_concat, op_div, op_divmod, op_gr, op_gr_bytes,
op_logand, op_logior, op_lognot, op_logxor, op_lsh, op_mod, op_modpow, op_multiply, op_not,
Expand All @@ -20,7 +21,7 @@ use clvmr::serde::node_from_bytes;

type Opf = fn(&mut Allocator, NodePtr, Cost) -> Response;

const FUNS: [Opf; 45] = [
const FUNS: [Opf; 46] = [
op_if as Opf,
op_cons as Opf,
op_first as Opf,
Expand Down Expand Up @@ -68,6 +69,8 @@ const FUNS: [Opf; 45] = [
// Secp operators
op_secp256k1_verify as Opf,
op_secp256r1_verify as Opf,
// keccak operator
op_keccak256 as Opf,
];

fuzz_target!(|data: &[u8]| {
Expand Down
Loading