referrerpolicy=no-referrer-when-downgrade

pallet_revive/precompiles/builtin/
ripemd160.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::{
19	precompiles::{BuiltinAddressMatcher, Error, Ext, PrimitivePrecompile},
20	vm::RuntimeCosts,
21	Config,
22};
23use alloc::vec::Vec;
24use core::{marker::PhantomData, num::NonZero};
25use ripemd::Digest;
26
27pub struct Ripemd160<T>(PhantomData<T>);
28
29impl<T: Config> PrimitivePrecompile for Ripemd160<T> {
30	type T = T;
31	const MATCHER: BuiltinAddressMatcher = BuiltinAddressMatcher::Fixed(NonZero::new(3).unwrap());
32	const HAS_CONTRACT_INFO: bool = false;
33
34	fn call(
35		_address: &[u8; 20],
36		input: Vec<u8>,
37		env: &mut impl Ext<T = Self::T>,
38	) -> Result<Vec<u8>, Error> {
39		env.gas_meter_mut().charge(RuntimeCosts::Ripemd160(input.len() as _))?;
40		let mut ret = [0u8; 32];
41		ret[12..32].copy_from_slice(&ripemd::Ripemd160::digest(input));
42		Ok(ret.to_vec())
43	}
44}
45
46#[cfg(test)]
47mod tests {
48	use super::*;
49	use crate::{precompiles::tests::run_test_vectors, tests::Test};
50
51	#[test]
52	fn test_ripemd160() {
53		run_test_vectors::<Ripemd160<Test>>(include_str!("./testdata/3-ripemd160.json"));
54	}
55}