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

ZK-559: Add shielder-sdk-crypto package #77

Merged
merged 7 commits into from
Jan 8, 2025
Merged

Conversation

kroist
Copy link
Collaborator

@kroist kroist commented Jan 6, 2025

Introducing a typescript package shielder-sdk-crypto which purpose is to define an interface for platform-native cryptographic functions.

Initial Setup and Configuration:

  • Added shielder-sdk-crypto package to the workspace (ts/pnpm-workspace.yaml). We will eventually transfer all typescript packages in monorepo to the workspace.

Core Functionality Implementation:

  • Defined TypeScript interfaces and types for inputs and outputs of cryptographic operations (ts/shielder-sdk-crypto/src/index.ts).
  • Implemented CryptoClient interface with methods for calling platform-native cryptography functions (ts/shielder-sdk-crypto/src/cryptoClient.ts).
  • Added Scalar class and utility functions for handling scalar values. It is cross-platform vanilla TS/JS code (ts/shielder-sdk-crypto/src/scalar.ts).

Copy link

github-actions bot commented Jan 6, 2025

Transaction NameMainCurrentDifference (%)
NewAccountNative20003472000287-0.00300%
DepositNative18278821828002+0.00656%
WithdrawNative18981261898054-0.00379%

@@ -0,0 +1,40 @@
import eslint from "@eslint/js";
Copy link
Member

Choose a reason for hiding this comment

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

can we share these config files among workspace members?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to workspace, it seems to work.

initialDeposit: Scalar;
}

export interface NewAccountValues {
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: rename Values to Inputs, PrivateInputs, ProverKnowledge, Advice

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed to *Advice

commitment: Scalar;
}

export type ShielderActionSecrets = {
Copy link
Member

Choose a reason for hiding this comment

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

do we want id here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

id and nonce are parameters of the function, keeping it simple for now, no "mother-types".


verifyWithdraw(proof: Proof, pubInputs: WithdrawPubInputs): Promise<boolean>;

proveAndVerifyMerkle(): Promise<Proof>;
Copy link
Member

Choose a reason for hiding this comment

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

do we still care about merkle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed


privateKeyToScalar(hex: `0x${string}`): Promise<Scalar>;

treeHeight(): Promise<number>;
Copy link
Member

Choose a reason for hiding this comment

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

I suggest using 100% clear name, like noteTreeHeight - in some time we might have other trees

Copy link
Member

Choose a reason for hiding this comment

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

similar comment to arity - it's not clear if that's for note tree, poseidon width or e.g. some token operation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to NoteTreeConfig interface

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe just "height", for symmetry with "arity"?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kroist just a gentle reminder to leave a short response if you decide not to apply a suggestion

WithdrawValues
} from "./index";

export interface CryptoClient {
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if that's feasible in ts, but if possible, I would split this interface - currently it mixes a lot of responsibilities:

  • proving
  • veryifying
  • configuration information (arity, height)
  • crypto primitives (hash)
  • conversion
  • secret management

I'm not saying that each of this bullet point shall be under a dedicated interface, but still, some separation might be useful and clarifying

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

did split it into sub-interfaces

Copy link
Contributor

Choose a reason for hiding this comment

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

If it were pure TypeScript, I would agree. But the purpose of CryptoClient is to aggregate the platform-specific functionality. Sometimes we'll inject a WASM crypto client, sometimes a React Native crypto client, and sometimes a mock. It feels simpler to do it once. Also, having just one gateway gives a single entry point for platform-specific initialization, if such initialization were needed. I think it might be better to keep these under 1 interface.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

indeed it can be done with an splitted interface.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, now it is ok. Max just grouped the new interfaces inside CryptoClient and I am satisfied with that.

@@ -0,0 +1,102 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

the same comment as in #76 please flag somehow (e.g. by deprecation marker or in Jira) that there is some code duplication that needs to be removed as soon as possible (to avoid any divergence)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

created issues in jira

Copy link
Contributor

@guspiel guspiel left a comment

Choose a reason for hiding this comment

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

leaving partial comments, will keep reading

ts/shielder-sdk-crypto/package.json Outdated Show resolved Hide resolved
WithdrawValues
} from "./index";

export interface CryptoClient {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, now it is ok. Max just grouped the new interfaces inside CryptoClient and I am satisfied with that.

Copy link
Contributor

@guspiel guspiel left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -0,0 +1,4 @@
packages:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you fix Playwright tests pls?


privateKeyToScalar(hex: `0x${string}`): Promise<Scalar>;

treeHeight(): Promise<number>;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe just "height", for symmetry with "arity"?

@kroist kroist merged commit 32685c8 into main Jan 8, 2025
14 checks passed
@kroist kroist deleted the shielder-sdk-crypto branch January 8, 2025 10:35
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