Skip to content

Commit adc5793

Browse files
MarcosNicolauMauroToscanoJuArce
authored
fix(aggregation_mode): merkle-tree backend openzeppelin compliant (#1931)
Co-authored-by: MauroFab <[email protected]> Co-authored-by: JuArce <[email protected]>
1 parent a90fd3a commit adc5793

File tree

14 files changed

+96
-33
lines changed

14 files changed

+96
-33
lines changed

aggregation_mode/aggregation_programs/risc0/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,26 @@ impl IsMerkleTreeBackend for Risc0ImageIdAndPubInputs {
4040
leaf.commitment()
4141
}
4242

43+
/// Computes a commutative Keccak256 hash, ensuring H(a, b) == H(b, a).
44+
///
45+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#Hashes
46+
///
47+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/Hashes.sol#L17-L19
48+
///
49+
/// Compliant with OpenZeppelin's `processProofCalldata` function from MerkleProof.sol.
50+
///
51+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#MerkleProof
52+
///
53+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol#L114-L128
4354
fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node {
4455
let mut hasher = Keccak::v256();
45-
hasher.update(child_1);
46-
hasher.update(child_2);
47-
56+
if child_1 < child_2 {
57+
hasher.update(child_1);
58+
hasher.update(child_2);
59+
} else {
60+
hasher.update(child_2);
61+
hasher.update(child_1);
62+
}
4863
let mut hash = [0u8; 32];
4964
hasher.finalize(&mut hash);
5065
hash

aggregation_mode/aggregation_programs/sp1/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,26 @@ impl IsMerkleTreeBackend for SP1VkAndPubInputs {
3737
leaf.hash()
3838
}
3939

40+
/// Computes a commutative Keccak256 hash, ensuring H(a, b) == H(b, a).
41+
///
42+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#Hashes
43+
///
44+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/Hashes.sol#L17-L19
45+
///
46+
/// Compliant with OpenZeppelin's `processProofCalldata` function from MerkleProof.sol.
47+
///
48+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#MerkleProof
49+
///
50+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol#L114-L128
4051
fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node {
4152
let mut hasher = Keccak256::new();
42-
hasher.update(child_1);
43-
hasher.update(child_2);
53+
if child_1 < child_2 {
54+
hasher.update(child_1);
55+
hasher.update(child_2);
56+
} else {
57+
hasher.update(child_2);
58+
hasher.update(child_1);
59+
}
4460
hasher.finalize().into()
4561
}
4662
}

aggregation_mode/programs_ids.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"risc0_image_id": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0",
3-
"sp1_vk_hash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69"
2+
"risc0_image_id": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8",
3+
"sp1_vk_hash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384"
44
}

aggregation_mode/src/aggregators/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,26 @@ impl IsMerkleTreeBackend for AlignedProof {
150150
leaf.commitment()
151151
}
152152

153+
/// Computes a commutative Keccak256 hash, ensuring H(a, b) == H(b, a).
154+
///
155+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#Hashes
156+
///
157+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/Hashes.sol#L17-L19
158+
///
159+
/// Compliant with OpenZeppelin's `processProofCalldata` function from MerkleProof.sol.
160+
///
161+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#MerkleProof
162+
///
163+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol#L114-L128
153164
fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node {
154165
let mut hasher = Keccak256::new();
155-
hasher.update(child_1);
156-
hasher.update(child_2);
166+
if child_1 < child_2 {
167+
hasher.update(child_1);
168+
hasher.update(child_2);
169+
} else {
170+
hasher.update(child_2);
171+
hasher.update(child_1);
172+
}
157173
hasher.finalize().into()
158174
}
159175
}

batcher/aligned-sdk/src/sdk/aggregation.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,26 @@ impl IsMerkleTreeBackend for Hash32 {
7070
leaf.0
7171
}
7272

73+
/// Computes a commutative Keccak256 hash, ensuring H(a, b) == H(b, a).
74+
///
75+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#Hashes
76+
///
77+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/Hashes.sol#L17-L19
78+
///
79+
/// Compliant with OpenZeppelin's `processProofCalldata` function from MerkleProof.sol.
80+
///
81+
/// See: https://docs.openzeppelin.com/contracts/5.x/api/utils#MerkleProof
82+
///
83+
/// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol#L114-L128
7384
fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node {
7485
let mut hasher = Keccak256::new();
75-
hasher.update(child_1);
76-
hasher.update(child_2);
86+
if child_1 < child_2 {
87+
hasher.update(child_1);
88+
hasher.update(child_2);
89+
} else {
90+
hasher.update(child_2);
91+
hasher.update(child_1);
92+
}
7793
hasher.finalize().into()
7894
}
7995
}

contracts/script/deploy/config/devnet/proof-aggregator-service.devnet.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"alignedAggregatorAddressPrivateKey": "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6"
77
},
88
"programs_id": {
9-
"sp1AggregationProgramVKHash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69",
10-
"risc0AggregationProgramImageId": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0"
9+
"sp1AggregationProgramVKHash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384",
10+
"risc0AggregationProgramImageId": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8"
1111
},
1212
"permissions": {
1313
"owner": "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955"

contracts/script/deploy/config/devnet/proof-aggregator-service.devnet.mock.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"alignedAggregatorAddressPrivateKey": "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6"
77
},
88
"programs_id": {
9-
"sp1AggregationProgramVKHash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69",
10-
"risc0AggregationProgramImageId": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0"
9+
"sp1AggregationProgramVKHash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384",
10+
"risc0AggregationProgramImageId": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8"
1111
},
1212
"permissions": {
1313
"owner": "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955"

contracts/script/deploy/config/holesky/proof-aggregator-service.holesky.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"alignedAggregatorAddress": "0x9403dF48130621f87974a5A1d1d11d3aF1222A82"
66
},
77
"programs_id": {
8-
"sp1AggregationProgramVKHash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69",
9-
"risc0AggregationProgramImageId": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0"
8+
"sp1AggregationProgramVKHash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384",
9+
"risc0AggregationProgramImageId": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8"
1010
},
1111
"permissions": {
1212
"owner": "0x97aEC5F28181abe5d2aD40dBe7FbaEe014529b7D"

contracts/script/deploy/config/holesky/proof-aggregator-service.holesky.config.stage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"alignedAggregatorAddress": "0x3595aa7d30f89f65933e7421dec77e4478d9fb01"
66
},
77
"programs_id": {
8-
"sp1AggregationProgramVKHash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69",
9-
"risc0AggregationProgramImageId": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0"
8+
"sp1AggregationProgramVKHash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384",
9+
"risc0AggregationProgramImageId": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8"
1010
},
1111
"permissions": {
1212
"owner": "0xE3C695b73dbe27106aF4530b152de3e57456B385"

contracts/script/deploy/config/mainnet/proof-aggregator-service.mainnet.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"alignedAggregatorAddress": "<aligned_aggregator_address>"
66
},
77
"programs_id": {
8-
"sp1AggregationProgramVKHash": "0x0035279b39b0a20aac302dd3c569f3a6324a00acc1b8e864af9e3a5fbf453b69",
9-
"risc0AggregationProgramImageId": "0xb0ce0665805c4219d134845b74be6f1f97ab74dbbc1b341b73af4068d33bc1b0"
8+
"sp1AggregationProgramVKHash": "0x008ad92d4c564c912ad93d8660bf67a8d2e9088aa942da4e605a5e8c4383c384",
9+
"risc0AggregationProgramImageId": "0x8bb4145379d717fd7e3921751a2060246445ef290cdad782884180861cf0a3e8"
1010
},
1111
"permissions": {
1212
"owner": "<owner_address>"

0 commit comments

Comments
 (0)