Skip to content

Commit

Permalink
fix ecdsa512 key hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvolear committed Feb 4, 2025
1 parent 668b042 commit f7aba4f
Show file tree
Hide file tree
Showing 8 changed files with 4,261 additions and 3,637 deletions.
7 changes: 5 additions & 2 deletions contracts/certificate/dispatchers/CECDSADispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ contract CECDSADispatcher is AbstractCDispatcher {

function getCertificateKey(
bytes memory certificatePublicKey_
) external pure override returns (uint256 keyHash_) {
return certificatePublicKey_.hash512();
) external view override returns (uint256 keyHash_) {
return
keyByteLength >= 128
? certificatePublicKey_.hash1024()
: certificatePublicKey_.hash512();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ abstract contract AbstractCDispatcher is ICertificateDispatcher, Initializable {
}

/**
* @notice Poseidon5 hash of the `x509KeyByteLength` long RSA X509 key.
* @notice Poseidon hash of the `x509KeyByteLength` long RSA | ECDSA X509 key.
*
* See X509 library for more information
*/
function getCertificateKey(
bytes memory certificatePublicKey_
) external pure virtual override returns (uint256 keyHash_);
) external view virtual override returns (uint256 keyHash_);
}
2 changes: 1 addition & 1 deletion contracts/passport/dispatchers/PRSASHADispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ contract PRSASHADispatcher is IPassportDispatcher, Initializable {
* @notice Get the RSASHA1 passport public key internal representation.
*/
function getPassportKey(bytes memory passportPublicKey_) external pure returns (uint256) {
return passportPublicKey_.hash1024();
return passportPublicKey_.hash1024Strict();
}
}
27 changes: 25 additions & 2 deletions contracts/utils/Bytes2Poseidon.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import {PoseidonUnit2L, PoseidonUnit5L} from "@iden3/contracts/lib/Poseidon.sol";
import {PoseidonUnit2L, PoseidonUnit4L, PoseidonUnit5L} from "@iden3/contracts/lib/Poseidon.sol";

library Bytes2Poseidon {
/**
Expand All @@ -24,11 +24,34 @@ library Bytes2Poseidon {
}

/**
* @notice Apply poseidon5 to [25, 25, 25, 25, 28] bytes long integers
* @notice Apply poseidon4 to [32, 32, 32, 32] bytes long integers mod 2 ** 248
*/
function hash1024(bytes memory byteArray_) internal pure returns (uint256) {
assert(byteArray_.length >= 128);

uint256[4] memory decomposed_;

assembly {
mstore(decomposed_, mload(add(byteArray_, 32))) // skip length and read first 32 bytes
mstore(add(decomposed_, 32), mload(add(byteArray_, 64))) // skip length and read second 32 bytes
mstore(add(decomposed_, 64), mload(add(byteArray_, 96))) // skip length and read third 32 bytes
mstore(add(decomposed_, 96), mload(add(byteArray_, 128))) // skip length and read fourth 32 bytes
}

decomposed_[0] %= 2 ** 248;
decomposed_[1] %= 2 ** 248;
decomposed_[2] %= 2 ** 248;
decomposed_[3] %= 2 ** 248;

return PoseidonUnit4L.poseidon(decomposed_);
}

/**
* @notice Apply poseidon5 to [25, 25, 25, 25, 28] bytes long integers
*/
function hash1024Strict(bytes memory byteArray_) internal pure returns (uint256) {
assert(byteArray_.length >= 128);

uint256[5] memory decomposed_;

assembly {
Expand Down
2 changes: 1 addition & 1 deletion deploy/1_state.migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getConfig } from "./config/config";
export = async (deployer: Deployer) => {
const config = (await getConfig())!;

await deployPoseidons(deployer, [1, 2, 3, 5]);
await deployPoseidons(deployer, [1, 2, 3, 4, 5]);

const registrationSmt = await deploySMTProxy(deployer, "RegistrationSMT");
const certificatesSmt = await deploySMTProxy(deployer, "CertificatesSMT");
Expand Down
Loading

0 comments on commit f7aba4f

Please sign in to comment.