1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use blake2_rfc::blake2b;
use ceres_executor::{
derive::{ReturnValue, Value},
Error, Result,
};
use ceres_sandbox::Sandbox;
use ceres_seal::RuntimeInterfaces;
use sha2::{Digest, Sha256};
use tiny_keccak::{Hasher, Keccak};
pub struct Instance;
impl RuntimeInterfaces for Instance {
fn enabled(&self) -> bool {
true
}
fn seal_println(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 2 {
return Err(Error::WrongArugmentLength);
}
let data = sandbox.read_sandbox_memory(args[0].into(), args[1].into())?;
if let Ok(utf8) = core::str::from_utf8(&data) {
println!("{}", utf8);
}
Ok(ReturnValue::Unit)
}
fn seal_random(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 4 {
return Err(Error::WrongArugmentLength);
}
let subject_ptr = args[0].into();
let subject_len = args[1].into();
let output_ptr: u32 = args[2].into();
let output_len: u32 = args[2].into();
let random = rand::random::<u8>();
let mut subject_buf = sandbox
.read_sandbox_memory(subject_ptr, subject_len)?
.to_vec();
subject_buf.push(random);
let output = blake2b::blake2b(32, &[], &subject_buf);
sandbox.write_sandbox_output(output_ptr, output_len, output.as_bytes())?;
Ok(ReturnValue::Unit)
}
fn seal_hash_sha2_256(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 3 {
return Err(Error::WrongArugmentLength);
}
let input_ptr = args[0].into();
let input_len = args[1].into();
let output_ptr = args[2].into();
let mut dest: [u8; 32] = [0; 32];
let mut hasher = Sha256::new();
let input = sandbox.read_sandbox_memory(input_ptr, input_len)?;
hasher.update(&input);
dest.copy_from_slice(&hasher.finalize());
sandbox.write_sandbox_memory(output_ptr, dest.as_ref())?;
Ok(ReturnValue::Unit)
}
fn seal_hash_keccak_256(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 3 {
return Err(Error::WrongArugmentLength);
}
let input_ptr = args[0].into();
let input_len = args[1].into();
let output_ptr = args[2].into();
let mut dest: [u8; 32] = [0; 32];
let mut keccak = Keccak::v256();
let input = sandbox.read_sandbox_memory(input_ptr, input_len)?;
keccak.update(&input);
keccak.finalize(&mut dest);
sandbox.write_sandbox_memory(output_ptr, dest.as_ref())?;
Ok(ReturnValue::Unit)
}
fn seal_hash_blake2_256(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 3 {
return Err(Error::WrongArugmentLength);
}
let input_ptr = args[0].into();
let input_len = args[1].into();
let output_ptr = args[2].into();
let mut dest = [0; 32];
let input = sandbox.read_sandbox_memory(input_ptr, input_len)?;
dest.copy_from_slice(blake2b::blake2b(32, &[], &input).as_bytes());
sandbox.write_sandbox_memory(output_ptr, dest.as_ref())?;
Ok(ReturnValue::Unit)
}
fn seal_hash_blake2_128(sandbox: &mut Sandbox, args: &[Value]) -> Result<ReturnValue> {
if args.len() != 3 {
return Err(Error::WrongArugmentLength);
}
let input_ptr = args[0].into();
let input_len = args[1].into();
let output_ptr = args[2].into();
let mut dest = [0; 16];
let input = sandbox.read_sandbox_memory(input_ptr, input_len)?;
dest.copy_from_slice(blake2b::blake2b(16, &[], &input).as_bytes());
sandbox.write_sandbox_memory(output_ptr, dest.as_ref())?;
Ok(ReturnValue::Unit)
}
}