-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add keccak * rollback dylibs
- Loading branch information
Showing
12 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Keccak256GasParameters { | ||
pub base: InternalGas, | ||
pub per_byte: InternalGasPerByte, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct GasParameters { | ||
pub keccak256: Keccak256GasParameters, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
// Copyright (c) The Diem Core Contributors | ||
// Copyright (c) The Move Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use move_core_types::gas_algebra::NumBytes; | ||
use move_vm_runtime::native_functions::NativeFunction; | ||
use move_vm_types::{loaded_data::runtime_types::Type, values::Value}; | ||
use smallvec::{smallvec, SmallVec}; | ||
use std::collections::VecDeque; | ||
use tiny_keccak::{Hasher as KeccakHasher, Keccak}; | ||
|
||
use crate::{ | ||
interface::{RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeResult}, | ||
safely_pop_arg, | ||
}; | ||
|
||
/*************************************************************************************************** | ||
* native fun keccak256 | ||
* | ||
* gas cost: base_cost + unit_cost * input_length | ||
* | ||
**************************************************************************************************/ | ||
|
||
fn native_keccak256( | ||
context: &mut SafeNativeContext, | ||
mut _ty_args: Vec<Type>, | ||
mut args: VecDeque<Value>, | ||
) -> SafeNativeResult<SmallVec<[Value; 1]>> { | ||
debug_assert!(_ty_args.is_empty()); | ||
debug_assert!(args.len() == 1); | ||
|
||
let gas_params = &context.native_gas_params.initia_stdlib.keccak.keccak256; | ||
|
||
let bytes = safely_pop_arg!(args, Vec<u8>); | ||
|
||
context.charge(gas_params.base + gas_params.per_byte * NumBytes::new(bytes.len() as u64))?; | ||
|
||
let mut hasher = Keccak::v256(); | ||
hasher.update(&bytes); | ||
let mut output = [0u8; 32]; | ||
hasher.finalize(&mut output); | ||
|
||
Ok(smallvec![Value::vector_u8(output)]) | ||
} | ||
/*************************************************************************************************** | ||
* module | ||
**************************************************************************************************/ | ||
pub fn make_all( | ||
builder: &SafeNativeBuilder, | ||
) -> impl Iterator<Item = (String, NativeFunction)> + '_ { | ||
let natives = [("keccak256", native_keccak256 as RawSafeNative)]; | ||
|
||
builder.make_named_natives(natives) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/// Cryptographic hashes: | ||
/// - Keccak-256: see https://keccak.team/keccak.html | ||
/// | ||
/// In addition, SHA2-256 and SHA3-256 are available in `std::hash`. Note that SHA3-256 is a variant of Keccak: it is | ||
/// NOT the same as Keccak-256. | ||
module initia_std::keccak { | ||
/// Returns the Keccak-256 hash of `bytes`. | ||
native public fun keccak256(byte: vector<u8>): vector<u8>; | ||
// | ||
// Testing | ||
// | ||
|
||
#[test] | ||
fun keccak256_test() { | ||
let inputs = vector[ | ||
b"testing", | ||
b"", | ||
]; | ||
|
||
let outputs = vector[ | ||
x"5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02", | ||
x"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", | ||
]; | ||
|
||
let i = 0; | ||
while (i < std::vector::length(&inputs)) { | ||
let input = *std::vector::borrow(&inputs, i); | ||
let hash_expected = *std::vector::borrow(&outputs, i); | ||
let hash = keccak256(input); | ||
|
||
assert!(hash_expected == hash, 1); | ||
|
||
i = i + 1; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/// Cryptographic hashes: | ||
/// - Keccak-256: see https://keccak.team/keccak.html | ||
/// | ||
/// In addition, SHA2-256 and SHA3-256 are available in `std::hash`. Note that SHA3-256 is a variant of Keccak: it is | ||
/// NOT the same as Keccak-256. | ||
module minitia_std::keccak { | ||
/// Returns the Keccak-256 hash of `bytes`. | ||
native public fun keccak256(byte: vector<u8>): vector<u8>; | ||
// | ||
// Testing | ||
// | ||
|
||
#[test] | ||
fun keccak256_test() { | ||
let inputs = vector[ | ||
b"testing", | ||
b"", | ||
]; | ||
|
||
let outputs = vector[ | ||
x"5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02", | ||
x"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", | ||
]; | ||
|
||
let i = 0; | ||
while (i < std::vector::length(&inputs)) { | ||
let input = *std::vector::borrow(&inputs, i); | ||
let hash_expected = *std::vector::borrow(&outputs, i); | ||
let hash = keccak256(input); | ||
|
||
assert!(hash_expected == hash, 1); | ||
|
||
i = i + 1; | ||
}; | ||
} | ||
} |