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

Add ecrecover() builtin for EVM #1543

Merged
merged 1 commit into from
Sep 22, 2023
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
60 changes: 60 additions & 0 deletions src/codegen/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,66 @@ fn expr_builtin(
expr: Box::new(codegen_expr),
}
}
ast::Builtin::ECRecover => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use Instr::Unimplemented here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Instr::Unimplemented preferable over an actual implementation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that as you cannot test this portion of code, we could better just use Instr::Unimplemented as we did so for other Ethereum builtins. This would not diminish our test coverage numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which test coverage numbers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, yolo in code in which we can't test isn't a good practice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I've been outvoted, it's gone

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it could also be commented out, saying that this is how we think it should be implemented, but it's commented out and an Unreachable instead until we can test it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I've added the code I wrote as a comment

// TODO:
// EVM: call precompile 1 (code below is untested)
// let args = args
// .iter()
// .map(|v| expression(v, cfg, contract_no, func, ns, vartab, opt))
// .collect::<Vec<Expression>>();
//
// let payload = abi_encode(loc, args, ns, vartab, cfg, false).0;
//
// let instr = Instr::ExternalCall {
// loc: *loc,
// contract_function_no: None,
// address: Some(Expression::NumberLiteral {
// loc: *loc,
// ty: Type::Address(false),
// value: BigInt::one(),
// }),
// accounts: None,
// seeds: None,
// payload,
// value: Expression::NumberLiteral {
// loc: *loc,
// ty: ns.value_type(),
// value: 0.into(),
// },
// success: None,
// gas: Expression::NumberLiteral {
// loc: *loc,
// ty: ns.value_type(),
// value: 0.into(),
// },
// callty: CallTy::Regular,
// flags: None,
// };
//
// cfg.add(vartab, instr);
//
// let mut res = abi_decode(
// loc,
// &Expression::ReturnData { loc: *loc },
// &[Type::Address(false)],
// ns,
// vartab,
// cfg,
// None,
// );
//
// res.remove(0)

// Polkadot: call ecdsa_recover(): https://docs.rs/pallet-contracts/latest/pallet_contracts/api_doc/trait.Version0.html#tymethod.ecdsa_recover
// Solana: see how neon implements this
cfg.add(vartab, Instr::Unimplemented { reachable: true });

Expression::NumberLiteral {
loc: *loc,
ty: Type::Bool,
value: 0.into(),
}
}
_ => {
let arguments: Vec<Expression> = args
.iter()
Expand Down
1 change: 1 addition & 0 deletions src/sema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ pub enum Builtin {
Accounts,
UserTypeWrap,
UserTypeUnwrap,
ECRecover,
}

#[derive(PartialEq, Eq, Clone, Debug)]
Expand Down
18 changes: 17 additions & 1 deletion src/sema/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Prototype {
}

// A list of all Solidity builtins functions
static BUILTIN_FUNCTIONS: Lazy<[Prototype; 24]> = Lazy::new(|| {
static BUILTIN_FUNCTIONS: Lazy<[Prototype; 25]> = Lazy::new(|| {
[
Prototype {
builtin: Builtin::Assert,
Expand Down Expand Up @@ -306,6 +306,22 @@ static BUILTIN_FUNCTIONS: Lazy<[Prototype; 24]> = Lazy::new(|| {
doc: "unwrap user defined type",
constant: true,
},
Prototype {
builtin: Builtin::ECRecover,
namespace: None,
method: vec![],
name: "ecrecover",
params: vec![
Type::Bytes(32),
Type::Uint(8),
Type::Bytes(32),
Type::Bytes(32),
],
ret: vec![Type::Address(false)],
target: vec![Target::EVM],
doc: "Recover the address associated with the public key from elliptic curve signature",
constant: false,
},
]
});

Expand Down
8 changes: 8 additions & 0 deletions tests/contract_testcases/evm/ecrecover.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract signed {
function recoverSignerFromSignature(uint8 v, bytes32 r, bytes32 s, bytes32 hash) pure external {
address signer = ecrecover(hash, v, r, s);
require(signer != address(0), "ECDSA: invalid signature");
}
}

// ---- Expect: diagnostics ----
2 changes: 1 addition & 1 deletion tests/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn ethereum_solidity_tests() {
})
.sum();

assert_eq!(errors, 1018);
assert_eq!(errors, 1012);
}

fn set_file_contents(source: &str, path: &Path) -> (FileResolver, Vec<String>) {
Expand Down