Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: implement ICS23 - existence proof verification part #200

Merged
merged 34 commits into from
Feb 14, 2025

Conversation

Farhad-Shabani
Copy link
Member

@Farhad-Shabani Farhad-Shabani commented Jan 15, 2025

Closes: #266
Closes: #267
First part of #208

Description

This PR introduces a new ics23 library under the cairo-libs, which includes the implementation of ICS-23 existence proof verification, along with minimal happy-path tests. We here also fix some issues in our protobuf library to make it work with this PR. The non-existence proof verification requires further changes to our protobuf APIs and will be handled in a separate follow-up PR. Also, more rigorous tests will be added once the implementation is fully completed.

@Farhad-Shabani Farhad-Shabani changed the title feat(contract): implement ICS23 vector commitment in Cairo (WIP) feat: implement ICS23 - existence proof verification part Feb 10, 2025
@Farhad-Shabani Farhad-Shabani marked this pull request as ready for review February 10, 2025 15:43
}
}

fn decode_raw(ref self: Proof, ref context: DecodeContext) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary implementation until we appropriately handle #265.

@@ -1,219 +0,0 @@
use protobuf::types::message::{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into the packages/ics23 library, and the enums have been revised to include only the operations (variants) that are already supported on Starknet or those we have an implementation for.

@@ -0,0 +1,91 @@
use protobuf::errors::ProtobufErrors;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gathered the Varint conversions that were added earlier, along with the new ones, under the varint module. Similarly, the related tests have been placed under protobuf/src/tests/varint.cairo for better organization.

@@ -68,3 +66,26 @@ pub impl ArrayAsProtoMessage<T, +ProtoMessage<T>, +Drop<T>, +Default<T>> of Prot
}
}

pub impl BytesAsProtoMessage of ProtoMessage<Array<u8>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More context on this: #267

@Farhad-Shabani Farhad-Shabani force-pushed the farhad/ics23-cairo-impl branch 2 times, most recently from c9f591a to 9a34698 Compare February 14, 2025 03:00
@Farhad-Shabani Farhad-Shabani force-pushed the farhad/ics23-cairo-impl branch from 9a34698 to 60d08e7 Compare February 14, 2025 03:05
}
}

impl ExistenceProofAsProtoMessage of ProtoMessage<ExistenceProof> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think we don't need to encode the Merkle proofs as protobuf. They are not part of the chain commitment, and thus can be freely re-encoded as Cairo when transmitted by the relayer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. But this requires further exploration into relayer code -- which is out of scope of this PR. Maybe we open an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm here basically implementing ICS-23 as a library. That's the next step. To decide how to integrate ICS-23 into both the Cairo and relayer codebases. We have the flexibility to use either encoded Protobuf data on-chain or the Cairo-encoded version.

By the way, I preferred to include the Protobuf implementation here as well, specifically since we were having bunch of artifacts ready to use. A main reason is that it allows me to reuse the existing cosmos/ics23 test data, keeping them as reference test cases. Otherwise, we’d have to generate new test data, which could raise questions about correctness.

#[derive(Default, Debug, Copy, Drop, PartialEq, Serde)]
pub enum HashOp {
#[default]
NoOp,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, the NoOp hash op is insecure, and should never be used in production. We are probably better off only support the Cosmos-specific SHA256 hash and nothing else to reduce the attack surface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. but let's tackle the discussion in a different issue? Because it's present in main as part of the old protobuf impl done by me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In production, only SHA256 is supported—anything else causes verification to fail. That said, I’d still advocate for keeping the NoOp variant. One really useful function of this variant that I found is in testing. It lets you skip hashing and check whether the verifier's behavior stays correct. (Defaults, No ops, zeros all should be rejected by verifier functions)
Additionally, I think making NoOp as the default improves the system security, since if no proof spec or hash op is specified, it defaults to NoOp, causing proof verification to fail. Basically this forces users / relayers to be explicit about the hash ops in their proof specification.

Copy link
Member

@rnbguy rnbguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Farhad-Shabani Farhad-Shabani merged commit ba79d92 into main Feb 14, 2025
9 checks passed
@Farhad-Shabani Farhad-Shabani deleted the farhad/ics23-cairo-impl branch February 14, 2025 14:14
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Protobuf encoding/decoding of Array<T> Missing Varint protobuf encoding/decoding for ICS-23
3 participants