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 keccak operator #153

Draft
wants to merge 5 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
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 = "ad539f504ade075262ac1754a9e0010c2789255f", 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: &[0x3E],
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
11 changes: 6 additions & 5 deletions src/classic/clvm_tools/stages/stage_2/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ impl CompilerOperatorsInternal {
let ops_version = self
.get_operators_version()
.unwrap_or(OPERATORS_LATEST_VERSION);
if ops_version == 0 {
OperatorSet::Default
if ops_version > 1 {
OperatorSet::Keccak
} else {
OperatorSet::BLS
OperatorSet::Default
}
}

Expand Down Expand Up @@ -363,9 +363,10 @@ impl Dialect for CompilerOperatorsInternal {
// The softfork operator comes with an extension argument.
fn softfork_extension(&self, ext: u32) -> OperatorSet {
match ext {
0 => OperatorSet::BLS,
0 | 1 => OperatorSet::Default,
2 => OperatorSet::Keccak,
// new extensions go here
_ => OperatorSet::Default,
_ => OperatorSet::Unknown,
}
}

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
Loading