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

Re-implement encrypt.circom and poseidon decrypt #45

Merged
merged 11 commits into from
Aug 26, 2024
Merged

Re-implement encrypt.circom and poseidon decrypt #45

merged 11 commits into from
Aug 26, 2024

Conversation

jimthematrix
Copy link
Contributor

@jimthematrix jimthematrix commented Aug 22, 2024

So that both the encryption (in circom and js) and decryption (in js) follow the paper closely: https://drive.google.com/file/d/1EVrP3DzoGbmzkRmYnyEDcIQcXVU7GlOd/view

Also added two more circuits, poseidon.circom and poseidon-ex.circom, purely for testing purposes, to ensure that the underlying poseidon templates continue to work as expected.

This PR also eliminates GPL-v3 dependencies from the SDK library (zkp/js/index). Some GPL-v3 exists but only used in constructing tests.

The poseidon-lite dependency is using a fork at git://github.com/kaleido-io/poseidon-lite.git#pre-build. This is a temporary solution until the PR chancehudson/poseidon-lite#3 is merged into a new release.

@jimthematrix jimthematrix marked this pull request as draft August 22, 2024 03:39
Copy link

codecov bot commented Aug 22, 2024

Codecov Report

Attention: Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.

Project coverage is 75.22%. Comparing base (3b7a354) to head (930499e).
Report is 12 commits behind head on main.

Files Patch % Lines
go-sdk/internal/utxo/utils.go 86.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #45      +/-   ##
==========================================
+ Coverage   73.23%   75.22%   +1.99%     
==========================================
  Files          12       13       +1     
  Lines         538      553      +15     
==========================================
+ Hits          394      416      +22     
+ Misses        103       93      -10     
- Partials       41       44       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jimthematrix jimthematrix marked this pull request as ready for review August 23, 2024 01:36
Copy link
Contributor

@Chengxuan Chengxuan left a comment

Choose a reason for hiding this comment

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

Thanks for the change and the detailed comments! One suggestion on changing --> to ==> to ensure the constraints are added correctly, they were missed in previous reviews.

// ensure that the salt fits inside the field of SNARKs
maxRounds := 10
var e error
for rounds := 0; rounds < maxRounds; rounds++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why this loop is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mainly to account for temporary failures due to entropy related errors

}

func NewEncryptionNonce() *big.Int {
max, _ := new(big.Int).SetString("340282366920938463463374607431768211456", 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be clearer to either add a comment explaining that the number is 2^128 like the node.js version or construct the calculation in code. Also, max can be a constant

Comment on lines 137 to 140
encrypt1.cipherText[0] --> cipherTextReceiver[0];
encrypt1.cipherText[1] --> cipherTextReceiver[1];
encrypt1.cipherText[2] --> cipherTextReceiver[2];
encrypt1.cipherText[3] --> cipherTextReceiver[3];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
encrypt1.cipherText[0] --> cipherTextReceiver[0];
encrypt1.cipherText[1] --> cipherTextReceiver[1];
encrypt1.cipherText[2] --> cipherTextReceiver[2];
encrypt1.cipherText[3] --> cipherTextReceiver[3];
encrypt1.cipherText[0] ==> cipherTextReceiver[0];
encrypt1.cipherText[1] ==> cipherTextReceiver[1];
encrypt1.cipherText[2] ==> cipherTextReceiver[2];
encrypt1.cipherText[3] ==> cipherTextReceiver[3];

idx2++;
j3++;
}
encrypt2.cipherText --> cipherTextAuthority;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
encrypt2.cipherText --> cipherTextAuthority;
encrypt2.cipherText ==> cipherTextAuthority;

Comment on lines 40 to 43
var l = length;
while (l % 3 != 0) {
l += 1;
}
Copy link
Contributor

@Chengxuan Chengxuan Aug 24, 2024

Choose a reason for hiding this comment

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

Suggested change
var l = length;
while (l % 3 != 0) {
l += 1;
}
var l = length;
if (l % 3 != 0) {
l += (3 - (l % 3));
}

I tend to avoid while loop in other programming languages, thus the suggestion. But I don't know whether this will improve/reduce performance in the circom world.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand the coding style. and thanks for suggesting the optimization

state = poseidon(state, 4);

// Release three elements of the decrypted message
message.push(addMod(ciphertext[i * 3], -state[1]));
Copy link
Contributor

@Chengxuan Chengxuan Aug 24, 2024

Choose a reason for hiding this comment

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

I was confused by the paper which didn't use subtraction for decryption. But I guess it's just the level of details that didn't need to be expressed explicitly

Signed-off-by: Jim Zhang <[email protected]>
@jimthematrix
Copy link
Contributor Author

thanks @Chengxuan for the detailed reviews. All good comments especially catching the non-verifying assignments to the cipher text outputs 👍

@Chengxuan Chengxuan merged commit e015cdc into main Aug 26, 2024
7 checks passed
@jimthematrix jimthematrix deleted the encrypt branch August 26, 2024 12:49
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.

2 participants