Skip to content

Commit

Permalink
evm: add sha512 precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Aug 9, 2023
1 parent ad5335a commit 0743e83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 3 additions & 2 deletions runtime-sdk/modules/evm/src/precompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ impl<Cfg: Config, B: EVMBackendExt> PrecompileSet for Precompiles<'_, Cfg, B> {
(1, 0, 8) => confidential::call_curve25519_compute_public(handle),
// Oasis-specific, general.
(1, 1, 1) => sha512::call_sha512_256(handle),
(1, 1, 2) => subcall::call_subcall(handle, self.backend),
(1, 1, 2) => sha512::call_sha512(handle),
(1, 1, 3) => subcall::call_subcall(handle, self.backend),
_ => return Cfg::additional_precompiles().and_then(|pc| pc.execute(handle)),
})
}
Expand All @@ -144,7 +145,7 @@ impl<Cfg: Config, B: EVMBackendExt> PrecompileSet for Precompiles<'_, Cfg, B> {
// Oasis-specific, confidential.
(1, 0, 1..=8, true) |
// Oasis-specific, general.
(1, 1, 1..=2, _)
(1, 1, 1..=3, _)
)
{
IsPrecompileResult::Answer {
Expand Down
33 changes: 32 additions & 1 deletion runtime-sdk/modules/evm/src/precompile/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use evm::{
ExitSucceed,
};
use ripemd160::Digest as _;
use sha2::Sha512Trunc256;
use sha2::{Sha512, Sha512Trunc256};

use super::{record_linear_cost, PrecompileResult};

Expand All @@ -23,6 +23,20 @@ pub(super) fn call_sha512_256(handle: &mut impl PrecompileHandle) -> PrecompileR
})
}

pub(super) fn call_sha512(handle: &mut impl PrecompileHandle) -> PrecompileResult {
// Same costs as SHA512_256.
record_linear_cost(handle, handle.input().len() as u64, 115, 13)?;

let mut hasher = Sha512::new();
hasher.update(handle.input());
let digest = hasher.finalize();

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: digest.to_vec(),
})
}

#[cfg(test)]
mod test {
use super::super::testing::*;
Expand All @@ -43,4 +57,21 @@ mod test {
"41f7883fc8df1d31b1b1f7c0379f7b5a990d457347d997fdd76a2f4bb5812342"
);
}

#[test]
fn test_sha512() {
let input = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02";
let ret = call_contract(
H160([
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0x02,
]),
&hex::decode(input).unwrap(),
3000,
)
.unwrap();
assert_eq!(
hex::encode(ret.unwrap().output),
"2b80ea6632ad6148483537e6afe59b835bd989b4deb1f0e556e6c7cf30f979dbfc2dbd226e1e646fb202b82180faa3bcba6282573a99895956f7005845dd3a6a"
);
}
}
2 changes: 1 addition & 1 deletion tests/e2e/contracts/subcall/evm_subcall.hex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
608060405234801561000f575f80fd5b5061062e8061001d5f395ff3fe608060405260043610610033575f3560e01c80630c5561a61461003757806323bfb16a1461006057806350d82f381461007f575b5f80fd5b61004a610045366004610377565b61009e565b604051610057919061042b565b60405180910390f35b34801561006b575f80fd5b5061004a61007a366004610377565b610162565b34801561008a575f80fd5b5061004a610099366004610377565b610212565b60605f80610102600160981b016001600160a01b0316878787876040516020016100cb9493929190610465565b60408051601f19818403018152908290526100e59161048b565b5f604051808303815f865af19150503d805f811461011e576040519150601f19603f3d011682016040523d82523d5f602084013e610123565b606091505b50915091508161014e5760405162461bcd60e51b8152600401610145906104a6565b60405180910390fd5b610157816102e1565b979650505050505050565b60605f80610102600160981b016001600160a01b03168787878760405160200161018f9493929190610465565b60408051601f19818403018152908290526101a99161048b565b5f60405180830381855af49150503d805f81146101e1576040519150601f19603f3d011682016040523d82523d5f602084013e6101e6565b606091505b5091509150816102085760405162461bcd60e51b8152600401610145906104a6565b9695505050505050565b60605f80610102600160981b016001600160a01b03168787878760405160200161023f9493929190610465565b60408051601f19818403018152908290526102599161048b565b5f604051808303815f865af19150503d805f8114610292576040519150601f19603f3d011682016040523d82523d5f602084013e610297565b606091505b5091509150816102b95760405162461bcd60e51b8152600401610145906104a6565b5f5b60648112156102d657806102ce816104ce565b9150506102bb565b509695505050505050565b60605f80838060200190518101906102f9919061050c565b915091508167ffffffffffffffff165f1461032b57818160405163575a7c4d60e01b81526004016101459291906105ce565b9392505050565b5f8083601f840112610342575f80fd5b50813567ffffffffffffffff811115610359575f80fd5b602083019150836020828501011115610370575f80fd5b9250929050565b5f805f806040858703121561038a575f80fd5b843567ffffffffffffffff808211156103a1575f80fd5b6103ad88838901610332565b909650945060208701359150808211156103c5575f80fd5b506103d287828801610332565b95989497509550505050565b5f5b838110156103f85781810151838201526020016103e0565b50505f910152565b5f81518084526104178160208601602086016103de565b601f01601f19169290920160200192915050565b602081525f61032b6020830184610400565b81835281816020850137505f828201602090810191909152601f909101601f19169091010190565b604081525f61047860408301868861043d565b828103602084015261015781858761043d565b5f825161049c8184602087016103de565b9190910192915050565b6020808252600e908201526d1cdd5898d85b1b0819985a5b195960921b604082015260600190565b5f6001600160ff1b0182016104f157634e487b7160e01b5f52601160045260245ffd5b5060010190565b634e487b7160e01b5f52604160045260245ffd5b5f806040838503121561051d575f80fd5b825167ffffffffffffffff8082168214610535575f80fd5b602085015191935080821115610549575f80fd5b818501915085601f83011261055c575f80fd5b81518181111561056e5761056e6104f8565b604051601f8201601f19908116603f01168101908382118183101715610596576105966104f8565b816040528281528860208487010111156105ae575f80fd5b6105bf8360208301602088016103de565b80955050505050509250929050565b67ffffffffffffffff83168152604060208201525f6105f06040830184610400565b94935050505056fea26469706673582212200d1600b1d5aadfe166b8e12683fa33eb93b7d9b0f2c53c95a01ecb651f082a5b64736f6c63430008150033
608060405234801561000f575f80fd5b5061062e8061001d5f395ff3fe608060405260043610610033575f3560e01c80630c5561a61461003757806323bfb16a1461006057806350d82f381461007f575b5f80fd5b61004a610045366004610377565b61009e565b604051610057919061042b565b60405180910390f35b34801561006b575f80fd5b5061004a61007a366004610377565b610162565b34801561008a575f80fd5b5061004a610099366004610377565b610212565b60605f80610103600160981b016001600160a01b0316878787876040516020016100cb9493929190610465565b60408051601f19818403018152908290526100e59161048b565b5f604051808303815f865af19150503d805f811461011e576040519150601f19603f3d011682016040523d82523d5f602084013e610123565b606091505b50915091508161014e5760405162461bcd60e51b8152600401610145906104a6565b60405180910390fd5b610157816102e1565b979650505050505050565b60605f80610103600160981b016001600160a01b03168787878760405160200161018f9493929190610465565b60408051601f19818403018152908290526101a99161048b565b5f60405180830381855af49150503d805f81146101e1576040519150601f19603f3d011682016040523d82523d5f602084013e6101e6565b606091505b5091509150816102085760405162461bcd60e51b8152600401610145906104a6565b9695505050505050565b60605f80610103600160981b016001600160a01b03168787878760405160200161023f9493929190610465565b60408051601f19818403018152908290526102599161048b565b5f604051808303815f865af19150503d805f8114610292576040519150601f19603f3d011682016040523d82523d5f602084013e610297565b606091505b5091509150816102b95760405162461bcd60e51b8152600401610145906104a6565b5f5b60648112156102d657806102ce816104ce565b9150506102bb565b509695505050505050565b60605f80838060200190518101906102f9919061050c565b915091508167ffffffffffffffff165f1461032b57818160405163575a7c4d60e01b81526004016101459291906105ce565b9392505050565b5f8083601f840112610342575f80fd5b50813567ffffffffffffffff811115610359575f80fd5b602083019150836020828501011115610370575f80fd5b9250929050565b5f805f806040858703121561038a575f80fd5b843567ffffffffffffffff808211156103a1575f80fd5b6103ad88838901610332565b909650945060208701359150808211156103c5575f80fd5b506103d287828801610332565b95989497509550505050565b5f5b838110156103f85781810151838201526020016103e0565b50505f910152565b5f81518084526104178160208601602086016103de565b601f01601f19169290920160200192915050565b602081525f61032b6020830184610400565b81835281816020850137505f828201602090810191909152601f909101601f19169091010190565b604081525f61047860408301868861043d565b828103602084015261015781858761043d565b5f825161049c8184602087016103de565b9190910192915050565b6020808252600e908201526d1cdd5898d85b1b0819985a5b195960921b604082015260600190565b5f6001600160ff1b0182016104f157634e487b7160e01b5f52601160045260245ffd5b5060010190565b634e487b7160e01b5f52604160045260245ffd5b5f806040838503121561051d575f80fd5b825167ffffffffffffffff8082168214610535575f80fd5b602085015191935080821115610549575f80fd5b818501915085601f83011261055c575f80fd5b81518181111561056e5761056e6104f8565b604051601f8201601f19908116603f01168101908382118183101715610596576105966104f8565b816040528281528860208487010111156105ae575f80fd5b6105bf8360208301602088016103de565b80955050505050509250929050565b67ffffffffffffffff83168152604060208201525f6105f06040830184610400565b94935050505056fea26469706673582212202369658d710f3f71613ae8645e6c9ac9e88e7736389b455e83675353c8aed56664736f6c63430008150033
2 changes: 1 addition & 1 deletion tests/e2e/contracts/subcall/evm_subcall.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.0;

contract Test {
address private constant SUBCALL = 0x0100000000000000000000000000000000000102;
address private constant SUBCALL = 0x0100000000000000000000000000000000000103;

error SubcallFailed(uint64 code, bytes module);

Expand Down

0 comments on commit 0743e83

Please sign in to comment.