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

Encryption.decrypt should not mutate the ciphertext #1980

Closed
maple3142 opened this issue Jan 24, 2025 · 0 comments · Fixed by maple3142/o1js#1 · May be fixed by #1981
Closed

Encryption.decrypt should not mutate the ciphertext #1980

maple3142 opened this issue Jan 24, 2025 · 0 comments · Fixed by maple3142/o1js#1 · May be fixed by #1981

Comments

@maple3142
Copy link

Current Encryption.decrypt implemented here does cipherText.pop(), which mutates the passed in ciphertext unnecessarily:

function decrypt(
{ publicKey, cipherText }: CipherText,
privateKey: PrivateKey
) {
// key exchange
const sharedSecret = publicKey.scale(privateKey.s);
const sponge = new Poseidon.Sponge();
sponge.absorb(sharedSecret.x);
const authenticationTag = cipherText.pop();
// decryption
const message = [];
for (let i = 0; i < cipherText.length; i++) {
// absorb frame tag
if (i === cipherText.length - 1) sponge.absorb(Field(1));
else sponge.absorb(Field(0));
const keyStream = sponge.squeeze();
const messageChunk = cipherText[i].sub(keyStream);
// push the message to our final messages
message.push(messageChunk);
// absorb the cipher text chunk
sponge.absorb(cipherText[i]);
}
// authentication tag
sponge.squeeze().assertEquals(authenticationTag!);
return message;
}

And it will make decryption with the same ciphertext work only the first time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant