Skip to content

Commit a5d04b2

Browse files
committed
addressed review comments
1 parent c4c1847 commit a5d04b2

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

sdk/tests/data/records.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const RECORD_CIPHERTEXT_STRING = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q";
2+
const RECORD_CIPHERTEXT_STRING_COPY = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q";
3+
const RECORD_CIPHERTEXT_STRING_NOT_OWNED = "RECORD1QVQSQ5H8YT5682E73ZT7PYNJGPL29MWTSETRVS9VHCKFHJRNX9RX94CFQYXX66TRWFHKXUN9V35HGUERQQPQZQZ6KMY7S5HPKKF02L6R46QM8RQCW9X0K4RQ6GT234AMJ2UG3LMTQT5NY4UG8SXJY3U8D05K4Q3E9F54VX67ZMD3G6JYQQ7KXRWS0R0SWM6P833";
4+
const RECORD_CIPHERTEXT_STRING_NOT_OWNED2 = "RECORD1QVQSP37HJE4CEU8EFZE8XMAHE5TDTXCZ0K534WQPKVN6C9R629X3C4Q8QYRXZMT0W4H8GGCQQGQSPVUJYCN0K7HYFHENXA40HXTFSX68092WMVJ4E3XSEXR2DY0FMCCXT0DS42W5MAASZFJV930QVQRKATQJ900AKU4K777UMH2K54ZHLUGQC2AFJD";
5+
6+
const RECORD_PLAINTEXT_STRING = `{
7+
owner: aleo1j7qxyunfldj2lp8hsvy7mw5k8zaqgjfyr72x2gh3x4ewgae8v5gscf5jh3.private,
8+
microcredits: 1500000000000000u64.private,
9+
_nonce: 3077450429259593211617823051143573281856129402760267155982965992208217472983group.public
10+
}`;
11+
12+
export {RECORD_CIPHERTEXT_STRING, RECORD_CIPHERTEXT_STRING_COPY, RECORD_CIPHERTEXT_STRING_NOT_OWNED, RECORD_CIPHERTEXT_STRING_NOT_OWNED2, RECORD_PLAINTEXT_STRING};

sdk/tests/wasm.test.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import {
1111
recordPlaintextString,
1212
beaconPrivateKeyString
1313
} from "./data/account-data.js";
14+
import {
15+
RECORD_CIPHERTEXT_STRING,
16+
RECORD_CIPHERTEXT_STRING_COPY,
17+
RECORD_CIPHERTEXT_STRING_NOT_OWNED,
18+
RECORD_CIPHERTEXT_STRING_NOT_OWNED2,
19+
RECORD_PLAINTEXT_STRING,
20+
} from "./data/records.js";
1421

1522

1623
describe('WASM Objects', () => {
@@ -440,28 +447,18 @@ describe('WASM Objects', () => {
440447
}
441448
});
442449

443-
444450
describe('EncryptionToolkit', () => {
445-
const recordCiphertextString = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q";
446-
const recordCiphertextStringCopy = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q";
447-
const recordCiphertextStringNotOwned = "RECORD1QVQSQ5H8YT5682E73ZT7PYNJGPL29MWTSETRVS9VHCKFHJRNX9RX94CFQYXX66TRWFHKXUN9V35HGUERQQPQZQZ6KMY7S5HPKKF02L6R46QM8RQCW9X0K4RQ6GT234AMJ2UG3LMTQT5NY4UG8SXJY3U8D05K4Q3E9F54VX67ZMD3G6JYQQ7KXRWS0R0SWM6P833";
448-
const recordCiphertextStringNotOwned2 = "RECORD1QVQSP37HJE4CEU8EFZE8XMAHE5TDTXCZ0K534WQPKVN6C9R629X3C4Q8QYRXZMT0W4H8GGCQQGQSPVUJYCN0K7HYFHENXA40HXTFSX68092WMVJ4E3XSEXR2DY0FMCCXT0DS42W5MAASZFJV930QVQRKATQJ900AKU4K777UMH2K54ZHLUGQC2AFJD";
449-
const recordCiphertext = RecordCiphertext.fromString(recordCiphertextString);
450-
const recordCiphertextNotOwned = RecordCiphertext.fromString(recordCiphertextStringNotOwned);
451-
const recordCiphertextNotOwned2 = RecordCiphertext.fromString(recordCiphertextStringNotOwned2);
451+
const recordCiphertext = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING);
452+
const recordCiphertextNotOwned = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING_NOT_OWNED);
453+
const recordCiphertextNotOwned2 = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING_NOT_OWNED2);
452454
const recordCiphertextArray = [recordCiphertext, recordCiphertextNotOwned, recordCiphertextNotOwned2];
453455
// Create copies of the record ciphertexts
454-
const recordCiphertextCopy1 = RecordCiphertext.fromString(recordCiphertextString);
455-
const recordCiphertextNotOwnedCopy1 = RecordCiphertext.fromString(recordCiphertextStringNotOwned);
456-
const recordCiphertextNotOwned2Copy1 = RecordCiphertext.fromString(recordCiphertextStringNotOwned2);
456+
const recordCiphertextCopy1 = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING_COPY);
457+
const recordCiphertextNotOwnedCopy1 = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING_NOT_OWNED);
458+
const recordCiphertextNotOwned2Copy1 = RecordCiphertext.fromString(RECORD_CIPHERTEXT_STRING_NOT_OWNED2);
457459
const recordCiphertextArrayCopy = [recordCiphertextCopy1, recordCiphertextNotOwnedCopy1, recordCiphertextNotOwned2Copy1];
458-
const recordPlaintextString = `{
459-
owner: aleo1j7qxyunfldj2lp8hsvy7mw5k8zaqgjfyr72x2gh3x4ewgae8v5gscf5jh3.private,
460-
microcredits: 1500000000000000u64.private,
461-
_nonce: 3077450429259593211617823051143573281856129402760267155982965992208217472983group.public
462-
}`;
463-
const recordPlaintext = RecordPlaintext.fromString(recordPlaintextString);
464-
const recordPlaintextCopy = RecordPlaintext.fromString(recordPlaintextString);
460+
const recordPlaintext = RecordPlaintext.fromString(RECORD_PLAINTEXT_STRING);
461+
const recordPlaintextCopy = RecordPlaintext.fromString(RECORD_PLAINTEXT_STRING);
465462
const viewKeyString = "AViewKey1ccEt8A2Ryva5rxnKcAbn7wgTaTsb79tzkKHFpeKsm9NX";
466463
const viewKey = ViewKey.from_string(viewKeyString);
467464
const recordViewKeyString = "4445718830394614891114647247073357094867447866913203502139893824059966201724field";
@@ -484,13 +481,12 @@ owner: aleo1j7qxyunfldj2lp8hsvy7mw5k8zaqgjfyr72x2gh3x4ewgae8v5gscf5jh3.private,
484481
it('can check if a record ciphertext from an array of record ciphertexts is owned by a view key', () => {
485482
const ownedRecords = EncryptionToolkit.checkOwnedRecords(viewKey, recordCiphertextArray);
486483
// Ensure the record ciphertext is owned by the view key
487-
expect(ownedRecords[0].toString()).equal(recordCiphertextStringCopy.toString());
484+
expect(ownedRecords[0].toString()).equal(RECORD_CIPHERTEXT_STRING_COPY.toString());
488485
});
489486
it('can decrypt a record ciphertext from an array of record ciphertexts', () => {
490487
const decryptedRecords = EncryptionToolkit.decryptOwnedRecords(viewKey, recordCiphertextArrayCopy);
491488
// Ensure the decrypted record is the same as the plaintext
492489
expect(decryptedRecords[0].toString()).equal(recordPlaintextCopy.toString());
493490
});
494-
495491
});
496492
});

wasm/src/utilities/encrypt.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ impl EncryptionToolkit {
226226
// Use Rayon to parallelize the decryption process and store successful decryptions.
227227
let decrypted_records: Vec<RecordPlaintext> = records
228228
.par_iter()
229-
.filter_map(|record| match try_decrypt_single_record(view_key, record) {
230-
Ok(plaintext) => Some(plaintext),
231-
Err(_) => None,
229+
.filter_map(|record| {
230+
let record_vk = Self::generate_record_view_key(view_key, record).ok()?;
231+
let decrypted_record = Self::decrypt_record_symmetric_unchecked(&record_vk, record).ok()?;
232+
Some(decrypted_record)
232233
})
233234
.collect();
234235

@@ -237,8 +238,8 @@ impl EncryptionToolkit {
237238

238239
/// Checks if a record ciphertext is owned by the given view key.
239240
///
240-
/// @param {ViewKey} view_key The view key of the owner of the records.
241-
/// @param {Vec<RecordCiphertext>} records The record ciphertexts to check ownership.
241+
/// @param {ViewKey} view_key View key of the owner of the records.
242+
/// @param {Vec<RecordCiphertext>} records The record ciphertexts for which to check ownership.
242243
///
243244
/// @returns {Vec<RecordCiphertext>} The record ciphertexts that are owned by the view key.
244245
#[wasm_bindgen(js_name = "checkOwnedRecords")]
@@ -254,18 +255,6 @@ impl EncryptionToolkit {
254255
}
255256
}
256257

257-
// Private helper method within the impl block to decrypt
258-
fn try_decrypt_single_record(view_key: &ViewKey, record: &RecordCiphertext) -> Result<RecordPlaintext, String> {
259-
// Generate the record view key using the owner's view key
260-
let record_vk = EncryptionToolkit::generate_record_view_key(view_key, record)
261-
.map_err(|_| "Failed to generate record view key".to_string())?;
262-
263-
// Attempt to decrypt the record using the generated record view key
264-
let decryption_attempt = EncryptionToolkit::decrypt_record_symmetric_unchecked(&record_vk, record)
265-
.map_err(|_| "Failed to decrypt record".to_string())?;
266-
267-
Ok(decryption_attempt)
268-
}
269258

270259
#[cfg(test)]
271260
mod tests {

0 commit comments

Comments
 (0)