Skip to content

Commit

Permalink
Implement keccak operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Oct 31, 2024
1 parent 2973610 commit 36e23fb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
29 changes: 24 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ do-notation = "0.1.3"
serde_json = "1.0"
sha2 = "0.9.5"
tempfile = "3.13.0"
clvmr = { version = "0.8.0", features = ["pre-eval"] }
clvmr = { git = "https://github.com/Chia-Network/clvm_rs", rev = "2f413e72fcf1bcafa4a3117f2c2a0a3a0e7e1c6b", features = ["pre-eval"] }
binascii = "0.1.4"
yaml-rust2 = "0.9"
hashlink = "0.9.1"
Expand Down
39 changes: 29 additions & 10 deletions src/classic/clvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ pub mod syntax_error;
/// each time a new set of operators is included in the primitive set in clvm.
/// We keep track of what was added when so users can specify what version of the
/// tools' output they're expecting when it matters.
pub const OPERATORS_LATEST_VERSION: usize = 1;
pub const OPERATORS_LATEST_VERSION: usize = 2;

struct KwAtomPair {
v: &'static [u8],
n: &'static str,
version: usize,
}

const KW_PAIRS: [KwAtomPair; 48] = [
const KW_PAIRS: [KwAtomPair; 49] = [
KwAtomPair {
v: &[0x01],
n: "q",
Expand Down Expand Up @@ -261,6 +261,11 @@ const KW_PAIRS: [KwAtomPair; 48] = [
n: "secp256r1_verify",
version: 1,
},
KwAtomPair {
v: &[0x40],
n: "keccak256",
version: 2,
},
];

lazy_static! {
Expand Down Expand Up @@ -292,20 +297,34 @@ lazy_static! {
}
result
};
pub static ref KEYWORD_FROM_ATOM_2: HashMap<Vec<u8>, String> = {
let mut result = HashMap::new();
for pair in KW_PAIRS.iter().filter(|p| p.version <= 2) {
result.insert(pair.v.to_vec(), pair.n.to_string());
}
result
};
pub static ref KEYWORD_TO_ATOM_2: HashMap<String, Vec<u8>> = {
let mut result = HashMap::new();
for pair in KW_PAIRS.iter().filter(|p| p.version <= 2) {
result.insert(pair.n.to_string(), pair.v.to_vec());
}
result
};
}

pub fn keyword_from_atom(version: usize) -> &'static HashMap<Vec<u8>, String> {
if version == 0 {
&KEYWORD_FROM_ATOM_0
} else {
&KEYWORD_FROM_ATOM_1
match version {
0 => &KEYWORD_FROM_ATOM_0,
1 => &KEYWORD_FROM_ATOM_1,
_ => &KEYWORD_FROM_ATOM_2,
}
}

pub fn keyword_to_atom(version: usize) -> &'static HashMap<String, Vec<u8>> {
if version == 0 {
&KEYWORD_TO_ATOM_0
} else {
&KEYWORD_TO_ATOM_1
match version {
0 => &KEYWORD_TO_ATOM_0,
1 => &KEYWORD_TO_ATOM_1,
_ => &KEYWORD_TO_ATOM_2,
}
}
4 changes: 2 additions & 2 deletions src/classic/clvm_tools/stages/stage_0.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clvm_rs::allocator::{Allocator, NodePtr};
use clvm_rs::chia_dialect::{ChiaDialect, ENABLE_BLS_OPS_OUTSIDE_GUARD, NO_UNKNOWN_OPS};
use clvm_rs::chia_dialect::{ChiaDialect, ENABLE_KECCAK_OPS_OUTSIDE_GUARD, NO_UNKNOWN_OPS};
use clvm_rs::cost::Cost;
use clvm_rs::reduction::Response;

Expand Down Expand Up @@ -51,7 +51,7 @@ impl TRunProgram for DefaultProgramRunner {
run_program_with_pre_eval(
allocator,
&ChiaDialect::new(
NO_UNKNOWN_OPS | ((new_operators as u32) * ENABLE_BLS_OPS_OUTSIDE_GUARD),
NO_UNKNOWN_OPS | ((new_operators as u32) * ENABLE_KECCAK_OPS_OUTSIDE_GUARD),
),
program,
args,
Expand Down
5 changes: 4 additions & 1 deletion src/tests/classic/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,10 @@ fn test_classic_obeys_operator_choice_at_compile_time_version_0() {
])
.trim()
.to_string();
assert_eq!(compiled, "FAIL: unimplemented operator 48");
assert_eq!(
compiled,
"(q . 0x97c3f14ced4dfc280611fd8d9b158163e8981b3bce4d1bb6dd0bcc679a2e2455)"
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/mod.rs"

[dependencies]
clvm_tools_rs = { path= "..", features = [] }
clvmr = { version = "0.8.0", features = ["pre-eval"] }
clvmr = { git = "https://github.com/Chia-Network/clvm_rs", rev = "2f413e72fcf1bcafa4a3117f2c2a0a3a0e7e1c6b", features = ["pre-eval"] }
wasm-bindgen = "=0.2.92"
wasm-bindgen-test = "=0.3.25"
js-sys = "0.3.60"
Expand Down

0 comments on commit 36e23fb

Please sign in to comment.