@@ -13,76 +13,63 @@ import "./ICustomDAProofValidator.sol";
13
13
contract ReferenceDAProofValidator is ICustomDAProofValidator {
14
14
/**
15
15
* @notice Validates a ReferenceDA proof and returns the preimage chunk
16
- * @param proof ReferenceDA proof format: [certKeccak256(32), offset(8), Version(1), CertificateSize(8), Certificate, PreimageSize(8), PreimageData]
16
+ * @param proof Standardized CustomDA proof format is: [certKeccak256(32), offset(8), certSize(8), certificate]
17
+ followed by the ReferenceDA specific: [version(1), preimageSize(8), preimageData]
17
18
* @return preimageChunk The 32-byte chunk at the specified offset
18
19
*/
19
20
function validateReadPreimage (
20
21
bytes calldata proof
21
22
) external pure override returns (bytes memory preimageChunk ) {
22
- // Proof format: [certKeccak256(32), offset(8), Version(1), CertificateSize(8), Certificate, PreimageSize(8), PreimageData]
23
- require (proof.length >= 58 , "Proof too short " ); // 32 + 8 + 1 + 8 + 8 + at least 1 byte
24
-
25
- // Extract certKeccak256 and offset from enhanced proof wrapper
26
- bytes32 certKeccak256;
23
+ // Extract offset from standardized header (already validated by OSP)
27
24
uint256 offset;
25
+ uint256 certSize;
28
26
assembly {
29
- certKeccak256 := calldataload (add (proof.offset, 0 ))
30
- offset := shr (192 , calldataload (add (proof.offset, 32 ))) // Read 8 bytes as uint256
27
+ offset := shr ( 192 , calldataload (add (proof.offset, 32 ))) // Read 8 bytes at position 32
28
+ certSize := shr (192 , calldataload (add (proof.offset, 40 ))) // Read 8 bytes at position 40
31
29
}
32
30
33
- // The actual custom proof starts at offset 40
34
- uint256 customProofStart = 40 ;
31
+ // Certificate has already been validated by OSP, just extract it
32
+ uint256 certStart = 48 ;
33
+ require (proof.length >= certStart + certSize, "Proof too short for certificate " );
34
+ bytes calldata certificate = proof[certStart:certStart + certSize];
35
35
36
- // Verify version
37
- require (proof[customProofStart] == 0x01 , "Unsupported proof version " );
36
+ // Validate certificate format for ReferenceDA
37
+ require (certificate.length == 33 , "Invalid certificate length " );
38
+ require (certificate[0 ] == 0x01 , "Invalid certificate header " );
38
39
39
- // Extract certificate size
40
- uint256 certSize;
41
- assembly {
42
- certSize := shr (192 , calldataload (add (proof.offset, add (customProofStart, 1 )))) // Read 8 bytes as uint256
43
- }
44
- require (certSize == 33 , "Certificate must be 33 bytes " );
40
+ // Extract SHA256 hash from certificate
41
+ bytes32 sha256Hash = bytes32 (certificate[1 :33 ]);
45
42
46
- // Extract and verify certificate
47
- uint256 certStart = customProofStart + 9 ; // Skip version(1) + certSize(8)
48
- bytes memory certificate = proof[certStart:certStart + certSize];
49
- require (certificate[0 ] == 0x01 , "Invalid certificate header " );
50
- require (keccak256 (certificate) == certKeccak256, "Invalid certificate hash " );
43
+ // Custom data starts after certificate
44
+ uint256 customDataStart = certStart + certSize;
45
+ require (proof.length >= customDataStart + 9 , "Proof too short for custom data " );
51
46
52
- // Extract SHA256 from certificate
53
- bytes32 sha256Hash;
54
- assembly {
55
- sha256Hash := mload (add (certificate, 33 )) // Skip length prefix and header byte
56
- }
47
+ // Verify version
48
+ require (proof[customDataStart] == 0x01 , "Unsupported proof version " );
57
49
58
50
// Extract preimage size
59
- uint256 preimageOffset = certStart + certSize;
60
51
uint256 preimageSize;
61
52
assembly {
62
- preimageSize := shr (192 , calldataload (add (proof.offset, preimageOffset ))) // Read 8 bytes as uint256
53
+ preimageSize := shr (192 , calldataload (add (proof.offset, add (customDataStart, 1 ))))
63
54
}
64
55
65
- require (proof.length >= preimageOffset + 8 + preimageSize, "Invalid proof length " );
66
-
67
- // Extract preimage data
68
- bytes memory preimage = proof[preimageOffset + 8 :preimageOffset + 8 + preimageSize];
56
+ require (proof.length >= customDataStart + 9 + preimageSize, "Invalid proof length " );
69
57
70
- // Verify SHA256 hash matches
71
- require (sha256 (abi.encodePacked (preimage)) == sha256Hash, "Invalid preimage hash " );
58
+ // Extract and verify preimage
59
+ bytes calldata preimage = proof[customDataStart + 9 :customDataStart + 9 + preimageSize];
60
+ require (sha256 (preimage) == sha256Hash, "Invalid preimage hash " );
72
61
73
62
// Extract chunk at offset
74
- uint256 chunkStart = offset;
75
63
uint256 chunkEnd = offset + 32 ;
76
64
if (chunkEnd > preimage.length ) {
77
65
chunkEnd = preimage.length ;
78
66
}
79
67
80
- uint256 chunkSize = chunkEnd - chunkStart;
81
68
preimageChunk = new bytes (32 );
82
-
83
- if ( chunkSize > 0 ) {
69
+ if (offset < preimage. length ) {
70
+ uint256 chunkSize = chunkEnd - offset;
84
71
for (uint256 i = 0 ; i < chunkSize; i++ ) {
85
- preimageChunk[i] = preimage[chunkStart + i];
72
+ preimageChunk[i] = preimage[offset + i];
86
73
}
87
74
}
88
75
0 commit comments