@zk-kit/poseidon-cipher
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -19,1 +19,11 @@ import { CipherText, EncryptionKey, Nonce, PlainText } from "./types"; | ||
export declare const poseidonDecrypt: (ciphertext: CipherText<bigint>, key: EncryptionKey<bigint>, nonce: Nonce<bigint>, length: number) => PlainText<bigint>; | ||
/** | ||
* Decrypt some ciphertext using poseidon encryption | ||
* @dev Do not throw if the plaintext is invalid | ||
* @param ciphertext the ciphertext to decrypt | ||
* @param key the key to decrypt with | ||
* @param nonce the nonce used to encrypt | ||
* @param length the length of the plaintext | ||
* @returns the plaintext | ||
*/ | ||
export declare const poseidonDecryptWithoutCheck: (ciphertext: CipherText<bigint>, key: EncryptionKey<bigint>, nonce: Nonce<bigint>, length: number) => PlainText<bigint>; |
{ | ||
"name": "@zk-kit/poseidon-cipher", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Poseidon encryption and decryption in TypeScript.", | ||
@@ -50,4 +50,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"@zk-kit/baby-jubjub": "0.1.0" | ||
"@zk-kit/baby-jubjub": "0.1.1", | ||
"@zk-kit/utils": "0.1.0" | ||
} | ||
} |
@@ -128,1 +128,42 @@ // https://github.com/weijiekoh/circomlib/blob/feat/poseidon-encryption/ | ||
} | ||
/** | ||
* Decrypt some ciphertext using poseidon encryption | ||
* @dev Do not throw if the plaintext is invalid | ||
* @param ciphertext the ciphertext to decrypt | ||
* @param key the key to decrypt with | ||
* @param nonce the nonce used to encrypt | ||
* @param length the length of the plaintext | ||
* @returns the plaintext | ||
*/ | ||
export const poseidonDecryptWithoutCheck = ( | ||
ciphertext: CipherText<bigint>, | ||
key: EncryptionKey<bigint>, | ||
nonce: Nonce<bigint>, | ||
length: number | ||
): PlainText<bigint> => { | ||
// Create the initial state | ||
// S = (0, kS[0], kS[1], N + l ∗ 2^128). | ||
let state = [Fr.zero, Fr.e(key[0]), Fr.e(key[1]), Fr.add(Fr.e(nonce), Fr.mul(Fr.e(BigInt(length)), two128))] | ||
const message = [] | ||
const n = Math.floor(ciphertext.length / 3) | ||
for (let i = 0; i < n; i += 1) { | ||
// Iterate Poseidon on the state | ||
state = poseidonPerm(state) | ||
// Release three elements of the message | ||
message.push(Fr.sub(ciphertext[i * 3], state[1])) | ||
message.push(Fr.sub(ciphertext[i * 3 + 1], state[2])) | ||
message.push(Fr.sub(ciphertext[i * 3 + 2], state[3])) | ||
// Modify the state | ||
state[1] = ciphertext[i * 3] | ||
state[2] = ciphertext[i * 3 + 1] | ||
state[3] = ciphertext[i * 3 + 2] | ||
} | ||
return message.slice(0, length) | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
5010775
56703
2
+ Added@zk-kit/utils@0.1.0
+ Added@zk-kit/baby-jubjub@0.1.1(transitive)
- Removed@zk-kit/baby-jubjub@0.1.0(transitive)
Updated@zk-kit/baby-jubjub@0.1.1