Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(program): constrain sync commitee on updates #25

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contracts/src/SP1Helios.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ contract SP1Helios {
uint256 newHead;
bytes32 prevHeader;
uint256 prevHead;
// Hash of the sync committee at the new head.
bytes32 syncCommitteeHash;
// Hash of the current sync committee that signed the previous update.
bytes32 startSyncCommitteeHash;
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment for this hash as well.

}

struct InitParams {
Expand All @@ -72,6 +75,7 @@ contract SP1Helios {
error SyncCommitteeAlreadySet(uint256 period);
error HeaderRootAlreadySet(uint256 slot);
error StateRootAlreadySet(uint256 slot);
error SyncCommitteeStartMismatch(bytes32 given, bytes32 expected);

constructor(InitParams memory params) {
GENESIS_VALIDATORS_ROOT = params.genesisValidatorsRoot;
Expand Down Expand Up @@ -99,17 +103,31 @@ contract SP1Helios {
revert SlotBehindHead(po.newHead);
}

uint256 currentPeriod = getSyncCommitteePeriod(head);

// Note: We should always have a sync committee for the current head.
// The "start" sync committee hash is the hash of the sync committee that should sign the next update.
bytes32 currentSyncCommitteeHash = syncCommittees[currentPeriod];
if (currentSyncCommitteeHash != po.startSyncCommitteeHash) {
revert SyncCommitteeStartMismatch(po.startSyncCommitteeHash, currentSyncCommitteeHash);
}

// Verify the proof with the associated public values. This will revert if proof invalid.
ISP1Verifier(verifier).verifyProof(heliosProgramVkey, publicValues, proof);

// Check that the new header hasnt been set already.
head = po.newHead;
if (headers[po.newHead] != bytes32(0)) {
revert HeaderRootAlreadySet(po.newHead);
}

// Check that the new state root hasnt been set already.
headers[po.newHead] = po.newHeader;
if (executionStateRoots[po.newHead] != bytes32(0)) {
revert StateRootAlreadySet(po.newHead);
}

// Finally set the new state root.
executionStateRoots[po.newHead] = po.executionStateRoot;
emit HeadUpdate(po.newHead, po.newHeader);

Expand Down
Binary file modified elf/sp1-helios-elf
Binary file not shown.
1 change: 1 addition & 0 deletions primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ sol! {
bytes32 prevHeader;
uint256 prevHead;
bytes32 syncCommitteeHash;
bytes32 startSyncCommitteeHash;
}
}
2 changes: 2 additions & 0 deletions program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn main() {
forks,
} = serde_cbor::from_slice(&encoded_inputs).unwrap();

let start_sync_committee_hash = store.current_sync_committee.tree_hash_root();
let prev_header: B256 = store.finalized_header.beacon().tree_hash_root();
let prev_head = store.finalized_header.beacon().slot;

Expand Down Expand Up @@ -84,6 +85,7 @@ pub fn main() {
prevHeader: prev_header,
prevHead: U256::from(prev_head),
syncCommitteeHash: sync_committee_hash,
startSyncCommitteeHash: start_sync_committee_hash,
};
sp1_zkvm::io::commit_slice(&proof_outputs.abi_encode());
}
Loading