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

Add canonical address registry contract + fixed circuit #437

Merged
merged 31 commits into from
Sep 7, 2023

Conversation

Sladuca
Copy link
Contributor

@Sladuca Sladuca commented Sep 5, 2023

Motivation

Fix canon addr frontrunning vulnerability due to fact that signed message didn't include eth address. Need EIP712 for general replay protection across chains.

Solution

  • Have circuit take msg directly as PI, then separately compute the msg digest in a replay/frontrunning-resistant manner
  • Add canon addr registry entry EIP712 hashing
  • Add CanonicalAddressRegistry contract, unit tests, and e2e tests
  • Add snap<>fe-sdk methods that allow interface to register addr in mapping

Proof

Unnecessary. Didn't change anything that would break this

PR Checklist

  • added tests
  • updated documentation
  • added changeset if necessary
  • tested in dev/testnet
  • tested site with snap (we haven't automated this yet)
  • re-built & tested circuits if any of them changed
  • updated contracts storage layout (if contracts were updated)

@changeset-bot
Copy link

changeset-bot bot commented Sep 5, 2023

🦋 Changeset detected

Latest commit: ceb405d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@nocturne-xyz/frontend-sdk Minor
@nocturne-xyz/core Minor
@nocturne-xyz/snap Minor
@nocturne-xyz/contracts Minor
@nocturne-xyz/e2e-tests Minor
@nocturne-xyz/config Minor
@nocturne-xyz/deploy Minor
@nocturne-xyz/local-prover Minor
@nocturne-xyz/circuits Minor
@nocturne-xyz/idb-kv-store Patch
@nocturne-xyz/persistent-log Patch
@nocturne-xyz/bundler Patch
@nocturne-xyz/deposit-screener Patch
@nocturne-xyz/insertion-writer Patch
@nocturne-xyz/subtree-updater Patch
@nocturne-xyz/test-actor Patch
@nocturne-xyz/offchain-utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor Author

Sladuca commented Sep 5, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@Sladuca Sladuca marked this pull request as ready for review September 5, 2023 22:58
apps/snap/src/index.ts Outdated Show resolved Hide resolved
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: MIT OR Apache-2.0 OR Apache-2.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: typo

import { CanonAddrSigCheckInputs } from "./proof";
import { CanonAddress, NocturneSignature, SpendPk, ViewingKey } from "./crypto";

export interface SignCanonAddrRegistryEntry {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: SignCanonAddrRegistryEntryMethod

Comment on lines +90 to +98
// Deposit manager proxy admin matches deployment proxy admin
const depositManagerProxyAdmin = await proxyAdmin(
provider,
deployment.depositManagerProxy.proxy
);
assertOrErr(
depositManagerProxyAdmin === deployment.proxyAdmin,
"deposit manager proxy admin incorrectly set"
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

was this not there previously?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think it was just forgotten way back when

Comment on lines 174 to 177
try {
await register(aliceEoa, compressedCanonAddr, proof);
throw new Error("invalid proof, should have reverted");
} catch {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: use expect().to.eventually.be.rejected

Comment on lines 214 to 218
await canonAddrRegistry
.connect(aliceEoa)
.setCanonAddr(compressedCanonAddr, packToSolidityProof(proof));
throw new Error("invalid digest, should have reverted");
} catch {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto expect(...).to.eventually.be.rejected

Comment on lines +111 to +113
"https://frontend-sdk-circuit-artifacts.s3.us-east-2.amazonaws.com/canonAddrSigCheck/canonAddrSigCheck.wasm";
const CANON_ADDR_SIG_CHECK_ZKEY_PATH =
"https://frontend-sdk-circuit-artifacts.s3.us-east-2.amazonaws.com/canonAddrSigCheck/canonAddrSigCheck.zkey";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right I need to upload these - will do right after I finish reviewing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -285,6 +299,47 @@ export class NocturneSdk implements NocturneSdkApi {
);
}

// TODO: use this method in interface
async registerCanonicalAddress(): Promise<ethers.ContractTransaction> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add to api.ts

@@ -1,12 +1,11 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: MIT OR Apache-2.0 OR Apache-2.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: extra Apache-2.0

@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: MIT OR Apache-2.0 OR Apache-2.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above. Also applies to couple of files below.

@@ -9,7 +9,7 @@
// added requiere error messages
//
//
// SPDX-License-Identifier: GPL-3.0
// SPDX-License-Identifier: MIT OR Apache-2.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

May need to keep this GPL..

Copy link
Collaborator

Choose a reason for hiding this comment

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

bytes32 domainSeparator = _domainSeparatorV4();
bytes32 structHash = _hashCanonAddrRegistryEntry(entry);

bytes32 digest = ECDSAUpgradeable.toTypedDataHash(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we using the latest library here? It seems this function has moved to MessageHashUtils in the main branch of openzepplin contracts.

Copy link
Contributor

@luketchang luketchang Sep 6, 2023

Choose a reason for hiding this comment

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

It was the latest published version on npm. We use 4.9.2 but last month came out with 4.9.3. Main is probably not stable

);

// mod digest by 2^252 to fit compressed addr sign bit in 253rd PI bit
return uint256(digest) % MODULUS_252;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is very minor: but we can also XOR with a mask here. Not sure if we want to keep that convention

Copy link
Contributor Author

@Sladuca Sladuca Sep 6, 2023

Choose a reason for hiding this comment

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

If we want to be consistent, we should do uint256(digest) & (1 << 252 - 1)

/// @param entry Canon addr registry entry
function _computeDigest(
CanonAddrRegistryEntry memory entry
) public view returns (uint256) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we document here that this return a uint256 whose top 4 bits is guaranteed to be zero?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep added dev comment

@luketchang luketchang force-pushed the seb/canon-addr-sig-check-r2 branch from 2aada30 to f23d974 Compare September 6, 2023 23:26
@luketchang luketchang force-pushed the seb/canon-addr-sig-check-r2 branch from 703d2c1 to ceb405d Compare September 7, 2023 01:21
@luketchang luketchang changed the title CanonAddrSigCheck takes msg directly as PI Add canonical address registry contract + fixed circuit Sep 7, 2023
@luketchang luketchang enabled auto-merge (squash) September 7, 2023 01:33
@luketchang luketchang merged commit 77c4063 into main Sep 7, 2023
3 checks passed
@luketchang luketchang deleted the seb/canon-addr-sig-check-r2 branch September 7, 2023 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants