Skip to content

Commit 6154949

Browse files
authored
feat: upstream domain vk proving (#424)
1 parent eaf7cd9 commit 6154949

19 files changed

Lines changed: 512 additions & 42 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/authorization/schema/valence-authorization.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,11 +1708,19 @@
17081708
"execute_zk_authorization": {
17091709
"type": "object",
17101710
"required": [
1711+
"domain_message",
1712+
"domain_proof",
17111713
"label",
17121714
"message",
17131715
"proof"
17141716
],
17151717
"properties": {
1718+
"domain_message": {
1719+
"$ref": "#/definitions/Binary"
1720+
},
1721+
"domain_proof": {
1722+
"$ref": "#/definitions/Binary"
1723+
},
17161724
"label": {
17171725
"type": "string"
17181726
},

contracts/authorization/src/contract.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,18 @@ pub fn execute(
176176
label,
177177
message,
178178
proof,
179-
} => execute_zk_authorization(deps, env, info, label, message, proof),
179+
domain_message,
180+
domain_proof,
181+
} => execute_zk_authorization(
182+
deps,
183+
env,
184+
info,
185+
label,
186+
message,
187+
proof,
188+
domain_message,
189+
domain_proof,
190+
),
180191
},
181192
ExecuteMsg::InternalAuthorizationAction(internal_authorization_msg) => {
182193
match internal_authorization_msg {
@@ -910,13 +921,16 @@ fn set_verification_gateway(
910921
.add_attribute("verification_gateway", verification_gateway))
911922
}
912923

924+
#[allow(clippy::too_many_arguments)]
913925
fn execute_zk_authorization(
914926
deps: DepsMut,
915927
env: Env,
916928
info: MessageInfo,
917929
label: String,
918930
message: Binary,
919931
proof: Binary,
932+
domain_message: Binary,
933+
domain_proof: Binary,
920934
) -> Result<Response, ContractError> {
921935
let zk_authorization = ZK_AUTHORIZATIONS
922936
.load(deps.storage, label.clone())
@@ -946,7 +960,7 @@ fn execute_zk_authorization(
946960

947961
// Verify the proof with the verification gateway
948962
let valid: bool = deps.querier.query_wasm_smart(
949-
verification_gateway,
963+
verification_gateway.clone(),
950964
&valence_verification_gateway::msg::QueryMsg::VerifyProof {
951965
vk: zk_authorization.vk,
952966
proof,
@@ -957,6 +971,27 @@ fn execute_zk_authorization(
957971
return Err(ContractError::ZK(ZKErrorReason::InvalidZKProof {}));
958972
}
959973

974+
// Verify the domain proof with the verification gateway
975+
let valid_domain: bool = deps.querier.query_wasm_smart(
976+
verification_gateway,
977+
&valence_verification_gateway::msg::QueryMsg::VerifyDomainProof {
978+
domain_proof,
979+
domain_inputs: domain_message.clone(),
980+
},
981+
)?;
982+
if !valid_domain {
983+
return Err(ContractError::ZK(ZKErrorReason::InvalidZKProof {}));
984+
}
985+
986+
// Compare the first 32 bytes of both messages to ensure the coprocessor root is the same
987+
let message_slice = message.as_slice();
988+
let domain_message_slice = domain_message.as_slice();
989+
let message_coprocessor_root = &message_slice[..32];
990+
let domain_message_coprocessor_root = &domain_message_slice[..32];
991+
if message_coprocessor_root != domain_message_coprocessor_root {
992+
return Err(ContractError::ZK(ZKErrorReason::InvalidCoprocessorRoot {}));
993+
}
994+
960995
// Now that proof is validated we are going to extract the ZkMessage from the message
961996
// This is done by discarding the first 32 bytes of the message which belong to the coprocessor root and I don't need
962997
let message_slice = message.as_slice();

contracts/authorization/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,7 @@ pub enum ZKErrorReason {
155155

156156
#[error("This message is not for this authorization contract!")]
157157
InvalidAuthorizationContract {},
158+
159+
#[error("Invalid Coprocessor root")]
160+
InvalidCoprocessorRoot {},
158161
}

contracts/authorization/src/tests/builders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl NeutronTestAppBuilder {
1212
pub fn new() -> Self {
1313
NeutronTestAppBuilder {
1414
fee_denom: FEE_DENOM.to_string(),
15-
initial_balance: 100_000_000_000,
15+
initial_balance: 100_000_000_000_000,
1616
num_accounts: 5,
1717
}
1818
}

contracts/authorization/src/tests/helpers.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cosmwasm_std::Binary;
12
use margined_neutron_std::types::{
23
cosmos::base::v1beta1::Coin,
34
cosmwasm::wasm::v1::{
@@ -201,6 +202,8 @@ pub fn instantiate_and_set_verification_gateway(
201202
app: &NeutronTestApp,
202203
signer: &SigningAccount,
203204
authorization: String,
205+
owner: String,
206+
domain_vk: Binary,
204207
) {
205208
let wasm = Wasm::new(app);
206209
let code_id = wasm
@@ -216,7 +219,7 @@ pub fn instantiate_and_set_verification_gateway(
216219
let verification_gateway = wasm
217220
.instantiate(
218221
code_id,
219-
&valence_verification_gateway::msg::InstantiateMsg {},
222+
&valence_verification_gateway::msg::InstantiateMsg { domain_vk, owner },
220223
None,
221224
"verification_gateway".into(),
222225
&[],

contracts/authorization/src/tests/user_actions.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,6 @@ fn pause_and_resume_processor_using_zk_authorizations() {
768768
vec![],
769769
);
770770

771-
instantiate_and_set_verification_gateway(
772-
&setup.app,
773-
&setup.owner_accounts[0],
774-
authorization.clone(),
775-
);
776-
777771
// VK of the program that accepts 2 registry values and creates a message to pause the processor for registry 1 and
778772
// creates a message to resume it for registry 2
779773
let program_vk = "LMOPRy+6Xxgx8zUgILvnM0dXQT4shd9cZs9UbVrVazpUvSAARnj1VX+iHQETTMo3wWZBFOK2U1ulExNWmKo8AxPLGxdMzm5rqCF/OF4QO0IfjGhF/GPMJNGeOi8CAAAAAAAAAAcAAAAAAAAAUHJvZ3JhbRMAAAAAAAAAAQAAAA4AAAAAAAAAAAAIAAAAAAAEAAAAAAAAAEJ5dGUQAAAAAAAAAAEAAAALAAAAAAAAAAAAAQAAAAAAAgAAAAAAAAAEAAAAAAAAAEJ5dGUBAAAAAAAAAAcAAAAAAAAAUHJvZ3JhbQAAAAAAAAAA";
@@ -797,6 +791,14 @@ fn pause_and_resume_processor_using_zk_authorizations() {
797791
)
798792
.unwrap();
799793

794+
instantiate_and_set_verification_gateway(
795+
&setup.app,
796+
&setup.owner_accounts[0],
797+
authorization.clone(),
798+
setup.owner_addr.to_string(),
799+
Binary::from(sp1_vk.bytes32().into_bytes()),
800+
);
801+
800802
// Let's create two zk authorizations, one to pause the processor and another to resume it, pause will have registry 1 and resume will have registry 2
801803
let zk_authorization_pause = ZkAuthorizationInfo {
802804
label: "pause".to_string(),
@@ -843,6 +845,8 @@ fn pause_and_resume_processor_using_zk_authorizations() {
843845
label: "pause".to_string(),
844846
message: Binary::from(proof_pause_inputs.clone()),
845847
proof: Binary::from(proof_pause_bytes.clone()),
848+
domain_message: Binary::from(proof_pause_inputs.clone()),
849+
domain_proof: Binary::from(proof_pause_bytes.clone()),
846850
}),
847851
&[],
848852
&setup.user_accounts[0],
@@ -867,8 +871,10 @@ fn pause_and_resume_processor_using_zk_authorizations() {
867871
&authorization,
868872
&ExecuteMsg::PermissionlessAction(PermissionlessMsg::ExecuteZkAuthorization {
869873
label: "resume".to_string(),
870-
message: Binary::from(proof_resume_inputs),
871-
proof: Binary::from(proof_resume_bytes),
874+
message: Binary::from(proof_resume_inputs.clone()),
875+
proof: Binary::from(proof_resume_bytes.clone()),
876+
domain_message: Binary::from(proof_resume_inputs),
877+
domain_proof: Binary::from(proof_resume_bytes),
872878
}),
873879
&[],
874880
&setup.user_accounts[0],
@@ -903,8 +909,10 @@ fn pause_and_resume_processor_using_zk_authorizations() {
903909
&authorization,
904910
&ExecuteMsg::PermissionlessAction(PermissionlessMsg::ExecuteZkAuthorization {
905911
label: "pause".to_string(),
906-
message: Binary::from(proof_pause_inputs),
907-
proof: Binary::from(proof_pause_bytes),
912+
message: Binary::from(proof_pause_inputs.clone()),
913+
proof: Binary::from(proof_pause_bytes.clone()),
914+
domain_message: Binary::from(proof_pause_inputs),
915+
domain_proof: Binary::from(proof_pause_bytes),
908916
}),
909917
&[],
910918
&setup.user_accounts[0],

contracts/middleware/verification-gateway/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ library = []
1616
[dependencies]
1717
cosmwasm-schema = { workspace = true }
1818
cosmwasm-std = { workspace = true }
19+
cw-storage-plus = { workspace = true }
20+
cw-ownable = { workspace = true }
1921
cw2 = { workspace = true }
2022
thiserror = { workspace = true }
2123
sp1-verifier = "5.0.0"

0 commit comments

Comments
 (0)