Skip to content

Commit

Permalink
Merge pull request #38 from entropyxyz/aux-data
Browse files Browse the repository at this point in the history
improve .wit field names
  • Loading branch information
jakehemmerle authored Nov 7, 2023
2 parents 3b852fe + a265deb commit 008fd64
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
- run: cargo --version
- run:
name: Run Check
command: "cargo check"
command: "cargo install cargo-risczero && cargo risczero install && cargo check"
8 changes: 5 additions & 3 deletions acl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ec-core ={ path = "../core", default-features = false }
ec-evm ={ path = "../evm", default-features = false, optional = true }
ec-core = { path = "../core", default-features = false }
ec-evm = { path = "../evm", default-features = false, optional = true }

serde = { version = "1.0", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["max-encoded-len"]}
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"max-encoded-len",
] }
scale-info = { version = "2.1.0", default-features = false }

[features]
Expand Down
11 changes: 4 additions & 7 deletions constraints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ edition = "2021"
# crate-type = ["cdylib"]

[dependencies]
ec-acl ={ path = "../acl", default-features = false, features = ["evm"]}
ec-evm ={ path = "../evm", default-features = false }
ec-core ={ path = "../core", default-features = false }
ec-acl = { path = "../acl", default-features = false, features = ["evm"] }
ec-evm = { path = "../evm", default-features = false }
ec-core = { path = "../core", default-features = false }

[dev-dependencies]
# ec-runtime = { path = "../runtime", default-features = false }

[features]
default = ["std"]
std = [
"ec-acl/std",
"ec-evm/std"
]
std = ["ec-acl/std", "ec-evm/std"]
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ wit-bindgen = { version = "0.7.0" }
# wasmtime ={ version = "10.0.1", default-features = false, features = ["component-model"] }

[dev-dependencies]
ec-acl ={ path = "../acl", default-features = false, features = ["evm"]}
ec-evm ={ path = "../evm", default-features = false }
ec-acl = { path = "../acl", default-features = false, features = ["evm"] }
ec-evm = { path = "../evm", default-features = false }

[features]
default = ["std"]
Expand Down
10 changes: 0 additions & 10 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ pub mod architecture {
}
}

pub mod runtime {

// /// Contains signature request data that is used by the constraints runtime. Passed into `wasmtime::Store` for state (or maybe `wasmtime::Linker`).
// #[witgen]
// pub struct InitialState {
// /// The preimage of the user's data under constraint evaulation (eg. RLP-encoded ETH transaction request).
// data: Vec<u8>
// }
}

/// Includes items that should be imported into most scopes
pub mod prelude {
// reexport getrandom custom handler (move to macro)
Expand Down
14 changes: 7 additions & 7 deletions evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ impl Parse<Evm> for <Evm as Architecture>::TransactionRequest {
hex_rlp_raw_tx: String,
) -> Result<<Evm as Architecture>::TransactionRequest, CoreError> {
let bytes = hex::decode(hex_rlp_raw_tx.replace("0x", "")).map_err(|e| {
CoreError::InvalidTransactionRequest(format!("Unable to parse to RLP: {}", e))
CoreError::InvalidSignatureRequest(format!("Unable to parse to RLP: {}", e))
})?;
let rlp = Rlp::new(&bytes);
match Self::decode_unsigned_rlp(&rlp) {
Ok(tx) => match tx.to {
// Clients shouldn't even be able to serialize tx reqs with ENS names, but it it
// does somehow, err
Some(NameOrAddress::Name(_)) => Err(CoreError::InvalidTransactionRequest(
Some(NameOrAddress::Name(_)) => Err(CoreError::InvalidSignatureRequest(
"ENS recipients not supported. Resolve to an address first.".to_string(),
)),
_ => Ok(tx),
},
Err(e) => Err(CoreError::InvalidTransactionRequest(format!(
Err(e) => Err(CoreError::InvalidSignatureRequest(format!(
"Unable to decode string: {}",
e
))),
Expand All @@ -69,23 +69,23 @@ impl TryParse<Evm> for <Evm as Architecture>::TransactionRequest {
/// TODO expect the hex-encoded RLP of the transaction request, so user doesn't have to hex::decode
fn try_parse(bytes: &[u8]) -> Result<Self, CoreError> {
let request_as_string = String::from_utf8(bytes.to_owned()).map_err(|e| {
CoreError::InvalidTransactionRequest(format!("Unable to parse to String: {}", e))
CoreError::InvalidSignatureRequest(format!("Unable to parse to String: {}", e))
})?;
let into_bytes = hex::decode(request_as_string.replace("0x", "")).map_err(|e| {
CoreError::InvalidTransactionRequest(format!("Unable to parse to RLP: {}", e))
CoreError::InvalidSignatureRequest(format!("Unable to parse to RLP: {}", e))
})?;
let rlp = Rlp::new(&into_bytes);

match Self::decode_unsigned_rlp(&rlp) {
Ok(tx) => match tx.to {
// Clients shouldn't even be able to serialize tx reqs with ENS names, but it it
// does somehow, err
Some(NameOrAddress::Name(_)) => Err(CoreError::InvalidTransactionRequest(
Some(NameOrAddress::Name(_)) => Err(CoreError::InvalidSignatureRequest(
"ENS recipients not supported. Resolve to an address first.".to_string(),
)),
_ => Ok(tx),
},
Err(e) => Err(CoreError::InvalidTransactionRequest(format!(
Err(e) => Err(CoreError::InvalidSignatureRequest(format!(
"Unable to decode string: {}",
e
))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example-barebones-with-extra"
name = "example-barebones-with-auxilary"
version = "0.1.0"
edition = "2021"

Expand All @@ -10,11 +10,11 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
ec-core ={ workspace = true }
ec-core = { workspace = true }

# These are used by `cargo component`
[package.metadata.component]
package = "entropy:example-barebones-with-extra"
package = "entropy:example-barebones-with-auxilary"

[package.metadata.component.target]
path = "../../wit"
Expand Down
81 changes: 81 additions & 0 deletions examples/barebones-with-auxilary/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! This example demonstrates a contrieved program that can include auxilary data. Note, only the data in `message` will be signed by Entropy; `auxilary_data` is used to provide additional data (eg an additional signature or a zkp related to the preimage) that the user requires during program evaluation.

#![no_std]

extern crate alloc;

use alloc::string::ToString;

use ec_core::{bindgen::Error, bindgen::*, export_program, prelude::*};

// TODO confirm this isn't an issue for audit
register_custom_getrandom!(always_fail);

pub struct BarebonesWithAuxilary;

impl Program for BarebonesWithAuxilary {
/// This is the only function required by the program runtime. `signature_request` includes the message to be
/// signed, eg. RLP-serialized Ethereum transaction request, raw x86_64 executable, etc.
fn evaluate(signature_request: SignatureRequest) -> Result<(), Error> {
let SignatureRequest {
message,
auxilary_data,
} = signature_request;

// our constraint just checks that the length of the signature request is greater than 10
if message.len() < 10 {
return Err(Error::Evaluation(
"Length of message is too short.".to_string(),
));
}

// Just check and make sure the `auxilary_data` field is not empty.
auxilary_data.ok_or(Error::Evaluation(
"This program requires that `auxilary_data` be `Some`.".to_string(),
))?;

Ok(())
}
}

export_program!(BarebonesWithAuxilary);

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

#[test]
fn test_message_length_is_valid() {
let signature_request = SignatureRequest {
message: "some_data_longer_than_10_bytes".to_string().into_bytes(),
auxilary_data: Some(vec![0x00]),
};

assert!(BarebonesWithAuxilary::evaluate(signature_request).is_ok());
}

/// Note, the program is written s.t. if `message` is less than 10 bytes, the program will error.
#[test]
fn test_message_length_is_invalid() {
let signature_request = SignatureRequest {
// should error since preimage is less than 10 bytes
message: "under10".to_string().into_bytes(),
auxilary_data: Some(vec![0x00]),
};

assert!(BarebonesWithAuxilary::evaluate(signature_request).is_err());
}

/// Note, the program is written s.t. if `auxilary_data` is `None`, the program will error.
#[test]
fn test_error_when_auxilary_field_is_none() {
let signature_request = SignatureRequest {
message: "some_data_longer_than_10_bytes".to_string().into_bytes(),
// should error since auxilary_data field is None
auxilary_data: None,
};

assert!(BarebonesWithAuxilary::evaluate(signature_request).is_err());
}
}
77 changes: 0 additions & 77 deletions examples/barebones-with-extra/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/barebones/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
ec-core ={ workspace = true }
ec-core = { workspace = true }

# These are used by `cargo component`
[package.metadata.component]
Expand Down
24 changes: 12 additions & 12 deletions examples/barebones/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ register_custom_getrandom!(always_fail);
pub struct BarebonesProgram;

impl Program for BarebonesProgram {
/// This is the only function required by the program runtime. `signature_request` is the preimage of the curve element to be
/// This is the only function required by the program runtime. `message` is the preimage of the curve element to be
/// signed, eg. RLP-serialized Ethereum transaction request, raw x86_64 executable, etc.
fn evaluate(signature_request: InitialState) -> Result<(), Error> {
let data: vec::Vec<u8> = signature_request.preimage;
fn evaluate(signature_request: SignatureRequest) -> Result<(), Error> {
let message: vec::Vec<u8> = signature_request.message;

// our constraint just checks that the length of the signature request is greater than 10
if data.len() < 10 {
// our constraint just checks that the length of the message is greater than 10
if message.len() < 10 {
return Err(Error::Evaluation(
"Length of data is too short.".to_string(),
"Length of message is too short.".to_string(),
));
}

Expand All @@ -39,9 +39,9 @@ mod tests {

#[test]
fn test_should_sign() {
let signature_request = InitialState {
preimage: "some_data_longer_than_10_bytes".to_string().into_bytes(),
extra: None,
let signature_request = SignatureRequest {
message: "some_data_longer_than_10_bytes".to_string().into_bytes(),
auxilary_data: None,
};

assert!(BarebonesProgram::evaluate(signature_request).is_ok());
Expand All @@ -50,9 +50,9 @@ mod tests {
#[test]
fn test_should_error() {
// data being checked is under 10 bytes in length
let signature_request = InitialState {
preimage: "under10".to_string().into_bytes(),
extra: None,
let signature_request = SignatureRequest {
message: "under10".to_string().into_bytes(),
auxilary_data: None,
};

assert!(BarebonesProgram::evaluate(signature_request).is_err());
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
ec-constraints ={ workspace = true }
ec-constraints = { workspace = true }
# TODO move hex parsing into the ec-evm crate
hex = { version = "0.4.3", default-features = false }

Expand Down
Loading

0 comments on commit 008fd64

Please sign in to comment.