Skip to content

Commit 61252d9

Browse files
committed
chore: rsa pss - further optimizations
1 parent 88a109a commit 61252d9

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

lib/Nargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "noir_rsa"
33
type = "lib"
44
authors = [""]
5-
compiler_version = ">=0.32.0"
5+
compiler_version = ">=0.33.0"
66

77
[dependencies]
88
bignum = {tag = "v0.3.0", git = "https://github.com/noir-lang/noir-bignum"}

lib/src/rsa.nr

+10-16
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,15 @@ fn mgf1_sha256<let SEED_LEN: u32, let MASK_LEN: u32>(seed: [u8; SEED_LEN]) -> [u
5353
let mut hashed: [u8; HASH_LEN] = [0; HASH_LEN];
5454

5555
for i in 0..iterations {
56-
// Hopefully one day we can use the line below, but for now we'll go with a fixed value
57-
// let mut block: [u8; SEED_LEN + 4] = [0; SEED_LEN + 4];
58-
let mut block: [u8; 256] = [0; 256];
56+
let mut block: [u8; SEED_LEN + 4] = [0; SEED_LEN + 4];
5957

6058
// Copy seed to block
6159
for j in 0..SEED_LEN {
6260
block[j] = seed[j];
6361
}
6462

6563
// Add counter to block
66-
let counter_bytes = (i as Field).to_be_bytes(4);
64+
let counter_bytes: [u8; 4] = (i as Field).to_be_bytes();
6765
for j in 0..4 {
6866
block[SEED_LEN + j] = counter_bytes[j];
6967
}
@@ -169,27 +167,23 @@ impl<BN, BNInstance, let NumBytes: u32> RSA<BN, BNInstance, NumBytes> where BN:
169167
// In this case, we'll have a leading zero byte in em that we need to ignore
170168
// c.f. https://github.com/RustCrypto/RSA/blob/aeedb5adf5297892fcb9e11f7c0f6c0157005c58/src/algorithms/pss.rs#L242
171169
let offset = key_len - em_len;
172-
// As hash is 32 bytes and we also remove the 0xBC at the end, we have up to NumBytes - 33 bytes left for DB
173-
// For example, for 2048 bit RSA, we have 256 - 32 - 1 = 223 bytes left for DB
174-
// and for 1024 bit RSA, we have 128 - 32 - 1 = 95 bytes left for DB
175-
// So we should do something like this:
176-
// let masked_db: [u8; NumBytes - 32 - 1] = get_array_slice(em, offset, db_mask_len + offset);
177-
// But for now we can't so we'll just use NumBytes and have 33 trailing 0s
178-
let masked_db: [u8; NumBytes] = get_array_slice(em, offset, db_mask_len + offset);
170+
// As the hash is 32 bytes and we also remove the 0xBC at the end, we have up to NumBytes - 33 bytes left for DB
171+
// For example, for 2048 bit RSA (i.e. 256 bytes), we have 256 - 33 = 223 bytes left for DB
172+
// and for 1024 bit RSA (i.e. 128 bytes), we have 128 - 33 = 95 bytes left for DB
173+
let masked_db: [u8; NumBytes - 33] = get_array_slice(em, offset, db_mask_len + offset);
179174
let h = get_array_slice(em, db_mask_len + offset, em.len() - 1);
180175

181176
// Make sure the 8 * em_len - em_bits leftmost bits are 0
182177
// c.f. https://github.com/RustCrypto/RSA/blob/aeedb5adf5297892fcb9e11f7c0f6c0157005c58/src/algorithms/pss.rs#L205
183-
let bits_to_mask = 8 * em_len - em_bits;
178+
let bits_to_mask = 8 - (8 * em_len - em_bits);
184179
let mask_value = pow(2, bits_to_mask as u32);
185-
let max_allowed_value = 255 / mask_value;
186-
assert(masked_db[0] as u32 <= max_allowed_value);
180+
assert_eq(masked_db[0] as u32 / mask_value, 0);
187181

188182
// Generate dbMask using MGF1
189-
let db_mask:[u8; NumBytes] = mgf1_sha256(h);
183+
let db_mask:[u8; NumBytes - 33] = mgf1_sha256(h);
190184

191185
// Compute DB = maskedDB xor dbMask
192-
let mut db = [0 as u8; NumBytes];
186+
let mut db = [0 as u8; NumBytes - 33];
193187
for i in 0..db_mask_len {
194188
db[i] = masked_db[i] ^ db_mask[i];
195189
}

0 commit comments

Comments
 (0)