Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvolear committed Apr 8, 2024
1 parent 24467ce commit 9ea0994
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
64 changes: 38 additions & 26 deletions contracts/registration/Registration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ contract Registration is PoseidonSMT, Initializable {

uint256 public constant E = 65537;

struct PassportInfo {
bytes32 activeIdentity;
uint64 identityReissueCounter;
}

struct IdentityInfo {
bytes32 activePassport;
uint64 issueTimestamp;
}

address public verifier;
bytes32 public icaoMasterTreeMerkleRoot;

mapping(bytes32 => bytes32) public hashedRSAKeyToInternalKey;
mapping(bytes32 => bytes32) public internalKeyToHashedRSAKey;

mapping(bytes32 => uint256) public passportIdentitiesCounter;
mapping(bytes32 => uint256) public identityCreationTimestamps;
mapping(bytes32 => PassportInfo) internal _passportInfos;
mapping(bytes32 => IdentityInfo) internal _identityInfos;

mapping(bytes32 => bool) internal _usedSignatures;

event Registered(bytes32 hashedRSAKey, bytes32 hashedInternalKey);
event Registered(bytes32 passportKey, bytes32 identityKey);

function __Registration_init(
uint256 treeHeight_,
Expand All @@ -42,63 +49,68 @@ contract Registration is PoseidonSMT, Initializable {
}

function register(
uint256 hashedInternalKey_,
bytes memory s_,
bytes memory n_,
VerifierHelper.ProofPoints memory zkPoints_,
uint256 group1Hash_
uint256 identityKey_,
uint256 group1Hash_,
bytes memory passportSignature_,
bytes memory passportPublicKey_,
VerifierHelper.ProofPoints memory zkPoints_
) external {
bytes memory challenge_ = new bytes(8);

uint256 hashedRSAKey_ = PoseidonUnit5L.poseidon(_decomposeRSAKey(n_));
uint256 passportKey_ = PoseidonUnit5L.poseidon(
_decomposePassportPublicKey(passportPublicKey_)
);

for (uint256 i = 0; i < challenge_.length; ++i) {
challenge_[challenge_.length - i - 1] = bytes1(uint8(hashedInternalKey_ >> (8 * i)));
challenge_[challenge_.length - i - 1] = bytes1(uint8(identityKey_ >> (8 * i)));
}

bytes32 sigHash_ = keccak256(s_);
bytes32 sigHash_ = keccak256(passportSignature_);

PassportInfo storage _passportInfo = _passportInfos[bytes32(passportKey_)];
IdentityInfo storage _identityInfo = _identityInfos[bytes32(identityKey_)];

require(!_usedSignatures[sigHash_], "Registration: signature used");
require(
hashedRSAKeyToInternalKey[bytes32(hashedRSAKey_)] == bytes32(0),
_passportInfo.activeIdentity == bytes32(0),
"Registration: passport already registered"
);
require(
internalKeyToHashedRSAKey[bytes32(hashedInternalKey_)] == bytes32(0),
_identityInfo.activePassport == bytes32(0),
"Registration: identity already registered"
);
require(
challenge_.verifyPassport(s_, abi.encodePacked(E), n_),
challenge_.verifyPassport(passportSignature_, abi.encodePacked(E), passportPublicKey_),
"Registration: invalid passport signature"
);

uint256[] memory pubSignals_ = new uint256[](4);

pubSignals_[0] = hashedRSAKey_; // output
pubSignals_[0] = passportKey_; // output
pubSignals_[1] = group1Hash_; // output
pubSignals_[2] = hashedInternalKey_; // output
pubSignals_[2] = identityKey_; // output
pubSignals_[3] = uint256(icaoMasterTreeMerkleRoot); // public input

require(verifier.verifyProof(pubSignals_, zkPoints_), "Registration: invalid zk proof");

_usedSignatures[sigHash_] = true;

hashedRSAKeyToInternalKey[bytes32(hashedRSAKey_)] = bytes32(hashedInternalKey_);
internalKeyToHashedRSAKey[bytes32(hashedInternalKey_)] = bytes32(hashedRSAKey_);
_passportInfo.activeIdentity = bytes32(identityKey_);

identityCreationTimestamps[bytes32(hashedInternalKey_)] = block.timestamp;
_identityInfo.activePassport = bytes32(passportKey_);
_identityInfo.issueTimestamp = uint64(block.timestamp);

uint256 index_ = PoseidonUnit2L.poseidon([hashedRSAKey_, hashedInternalKey_]);
uint256 index_ = PoseidonUnit2L.poseidon([passportKey_, identityKey_]);
uint256 value_ = PoseidonUnit3L.poseidon(
[group1Hash_, passportIdentitiesCounter[bytes32(hashedRSAKey_)], block.timestamp]
[group1Hash_, _passportInfo.identityReissueCounter, uint64(block.timestamp)]
);

_add(bytes32(index_), bytes32(value_));

emit Registered(bytes32(hashedRSAKey_), bytes32(hashedInternalKey_));
emit Registered(bytes32(passportKey_), bytes32(identityKey_));
}

function _decomposeRSAKey(
function _decomposePassportPublicKey(
bytes memory n_
) private pure returns (uint256[5] memory decomposed_) {
assembly {
Expand Down
5 changes: 2 additions & 3 deletions test/registration/Registration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ethers } from "hardhat";
import { expect } from "chai";
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { time } from "@nomicfoundation/hardhat-network-helpers";
import { Reverter, deployPoseidons, getPoseidon, poseidonHash } from "@/test/helpers/";

import { Registration, VerifierMock, RegistrationVerifier } from "@ethers-v6";
Expand Down Expand Up @@ -63,7 +62,7 @@ describe("Registration", () => {
c: [0, 0],
};

await registration.register(poseidon2PubKey, signature, modulus, formattedProof, someHash);
await registration.register(poseidon2PubKey, someHash, signature, modulus, formattedProof);
});

it("should register", async () => {
Expand Down Expand Up @@ -99,7 +98,7 @@ describe("Registration", () => {
],
};

await registration.register(poseidon2PubKey, signature, modulus, formattedProof, group1Hash);
await registration.register(poseidon2PubKey, group1Hash, signature, modulus, formattedProof);
});
});
});

0 comments on commit 9ea0994

Please sign in to comment.