1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use primitive_types::{H160, U256};
use zk_evm_abstractions::{
    aux::Timestamp,
    precompiles::{
        ecadd::ecadd_function, ecmul::ecmul_function, ecpairing::ecpairing_function,
        ecrecover::ecrecover_function, keccak256::keccak256_rounds_function,
        modexp::modexp_function, secp256r1_verify::secp256r1_verify_function,
        sha256::sha256_rounds_function,
    },
    queries::{LogQuery, MemoryQuery},
    vm::Memory,
    zkevm_opcode_defs::{
        ECADD_PRECOMPILE_ADDRESS, ECMUL_PRECOMPILE_ADDRESS, ECPAIRING_PRECOMPILE_ADDRESS,
        MODEXP_PRECOMPILE_ADDRESS,
    },
};
use zkevm_opcode_defs::{
    PrecompileCallABI, ECRECOVER_INNER_FUNCTION_PRECOMPILE_ADDRESS,
    KECCAK256_ROUND_FUNCTION_PRECOMPILE_ADDRESS, SECP256R1_VERIFY_PRECOMPILE_ADDRESS,
    SHA256_ROUND_FUNCTION_PRECOMPILE_ADDRESS,
};
use zksync_vm2_interface::CycleStats;

use super::{PrecompileMemoryReader, PrecompileOutput, Precompiles};

fn create_query(input_offset: u32, input_len: u32, aux_data: u64) -> LogQuery {
    let abi = PrecompileCallABI {
        input_memory_offset: input_offset,
        input_memory_length: input_len,
        output_memory_offset: 0,
        output_memory_length: 2, // not read by implementations
        // Pages are fake; we assume that precompiles are implemented correctly and don't read / write anywhere but the specified pages
        memory_page_to_read: 1,
        memory_page_to_write: 2,
        precompile_interpreted_data: aux_data,
    };
    LogQuery {
        timestamp: Timestamp(0),
        key: abi.to_u256(),
        // only two first fields are read by the precompile
        tx_number_in_block: Default::default(),
        aux_byte: Default::default(),
        shard_id: Default::default(),
        address: H160::default(),
        read_value: U256::default(),
        written_value: U256::default(),
        rw_flag: Default::default(),
        rollback: Default::default(),
        is_service: Default::default(),
    }
}

#[derive(Debug)]
struct LegacyIo<'a> {
    input: PrecompileMemoryReader<'a>,
    output: PrecompileOutput,
}

impl<'a> LegacyIo<'a> {
    fn new(input: PrecompileMemoryReader<'a>) -> Self {
        Self {
            input,
            output: PrecompileOutput::default(),
        }
    }
}

impl Memory for LegacyIo<'_> {
    fn execute_partial_query(
        &mut self,
        _monotonic_cycle_counter: u32,
        mut query: MemoryQuery,
    ) -> MemoryQuery {
        let start_word = query.location.index.0;
        if query.rw_flag {
            assert!(start_word < 3, "standard precompiles never write >3 words");
            self.output.buffer[start_word as usize] = query.value;
            self.output.len = self.output.len.max(start_word + 1);
        } else {
            // Access `Heap` directly for a speed-up
            query.value = self.input.heap.read_u256(start_word * 32);
            query.value_is_pointer = false;
        }
        query
    }

    fn specialized_code_query(
        &mut self,
        _monotonic_cycle_counter: u32,
        _query: MemoryQuery,
    ) -> MemoryQuery {
        unimplemented!("should not be called")
    }

    fn read_code_query(&self, _monotonic_cycle_counter: u32, _query: MemoryQuery) -> MemoryQuery {
        unimplemented!("should not be called")
    }
}

/// Precompiles implementation using legacy VM code.
#[derive(Debug)]
pub struct LegacyPrecompiles;

impl Precompiles for LegacyPrecompiles {
    #[allow(clippy::cast_possible_truncation)]
    fn call_precompile(
        &self,
        address_low: u16,
        memory: PrecompileMemoryReader<'_>,
        aux_input: u64,
    ) -> PrecompileOutput {
        let query = create_query(memory.offset, memory.len, aux_input);
        let mut io = LegacyIo::new(memory);
        match address_low {
            KECCAK256_ROUND_FUNCTION_PRECOMPILE_ADDRESS => {
                let cycles = keccak256_rounds_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::Keccak256(cycles as u32))
            }
            SHA256_ROUND_FUNCTION_PRECOMPILE_ADDRESS => {
                let cycles = sha256_rounds_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::Sha256(cycles as u32))
            }
            ECRECOVER_INNER_FUNCTION_PRECOMPILE_ADDRESS => {
                let cycles = ecrecover_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::EcRecover(cycles as u32))
            }
            SECP256R1_VERIFY_PRECOMPILE_ADDRESS => {
                let cycles = secp256r1_verify_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::Secp256r1Verify(cycles as u32))
            }
            MODEXP_PRECOMPILE_ADDRESS => {
                let cycles = modexp_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::ModExp(cycles as u32))
            }
            ECADD_PRECOMPILE_ADDRESS => {
                let cycles = ecadd_function::<_, false>(0, query, &mut io).0;
                io.output.with_cycle_stats(CycleStats::EcAdd(cycles as u32))
            }
            ECMUL_PRECOMPILE_ADDRESS => {
                let cycles = ecmul_function::<_, false>(0, query, &mut io).0;
                io.output.with_cycle_stats(CycleStats::EcMul(cycles as u32))
            }
            ECPAIRING_PRECOMPILE_ADDRESS => {
                let cycles = ecpairing_function::<_, false>(0, query, &mut io).0;
                io.output
                    .with_cycle_stats(CycleStats::EcPairing(cycles as u32))
            }
            _ => PrecompileOutput::default(),
        }
    }
}

#[allow(clippy::cast_possible_truncation)] // OK for tests
#[cfg(test)]
mod tests {
    use proptest::{array, collection, num, option, prelude::*};
    use zkevm_opcode_defs::{
        k256::ecdsa::{SigningKey as K256SigningKey, VerifyingKey as K256VerifyingKey},
        p256::ecdsa::SigningKey as P256SigningKey,
        sha3::{self, Digest},
    };
    use zksync_vm2_interface::HeapId;

    use super::*;
    use crate::heap::Heaps;

    const MAX_LEN: usize = 2_048;

    fn arbitrary_aligned_bytes(alignment: usize) -> impl Strategy<Value = Vec<u8>> {
        (0..=(MAX_LEN / alignment)).prop_flat_map(move |len_in_words| {
            collection::vec(num::u8::ANY, len_in_words * alignment)
        })
    }

    fn key_to_address(key: &K256VerifyingKey) -> U256 {
        let encoded_key = key.to_encoded_point(false);
        let encoded_key = &encoded_key.as_bytes()[1..];
        debug_assert_eq!(encoded_key.len(), 64);
        let address_digest = sha3::Keccak256::digest(encoded_key);
        let address_u256 = U256::from_big_endian(&address_digest);
        // Mask out upper bytes of the hash.
        address_u256 & (U256::MAX >> (256 - 160))
    }

    fn test_keccak_precompile(input: &[u8], initial_offset: u32) -> Result<(), TestCaseError> {
        let input_len = input.len() as u32;
        assert_eq!(input_len % 32, 0);

        let mut heaps = Heaps::new(&[]);
        for (i, u256_chunk) in input.chunks(32).enumerate() {
            let offset = i as u32 * 32 + initial_offset;
            heaps.write_u256(HeapId::FIRST, offset, U256::from_big_endian(u256_chunk));
        }

        let memory = PrecompileMemoryReader::new(&heaps[HeapId::FIRST], initial_offset, input_len);
        let output = LegacyPrecompiles.call_precompile(
            KECCAK256_ROUND_FUNCTION_PRECOMPILE_ADDRESS,
            memory,
            0,
        );

        prop_assert_eq!(output.len, 1);
        let expected_hash = sha3::Keccak256::digest(input);
        let expected_hash = U256::from_big_endian(&expected_hash);
        prop_assert_eq!(output.buffer[0], expected_hash);
        prop_assert!(matches!(output.cycle_stats, Some(CycleStats::Keccak256(_))));
        Ok(())
    }

    fn test_sha256_precompile(
        input: &[u8],
        initial_offset_in_words: u32,
    ) -> Result<(), TestCaseError> {
        assert_eq!(input.len() % 64, 0);
        let mut heaps = Heaps::new(&[]);

        for (i, u256_chunk) in input.chunks(32).enumerate() {
            let offset = i as u32 * 32 + initial_offset_in_words * 32;
            heaps.write_u256(HeapId::FIRST, offset, U256::from_big_endian(u256_chunk));
        }

        let max_round_count = input.len() as u32 / 64;
        for round_count in 0..=max_round_count {
            let memory = PrecompileMemoryReader::new(
                &heaps[HeapId::FIRST],
                initial_offset_in_words,
                round_count * 2,
            );
            let output = LegacyPrecompiles.call_precompile(
                SHA256_ROUND_FUNCTION_PRECOMPILE_ADDRESS,
                memory,
                round_count.into(),
            );

            if round_count == 0 {
                prop_assert_eq!(output.len, 0);
            } else {
                prop_assert_eq!(output.len, 1);
                prop_assert_ne!(output.buffer[0], U256::zero());
            }
            prop_assert!(matches!(output.cycle_stats, Some(CycleStats::Sha256(_))));
        }
        Ok(())
    }

    #[derive(Debug, Clone, Copy)]
    enum EcRecoverMutation {
        RecoveryId,
        Digest(usize),
        R(usize),
        S(usize),
    }

    impl EcRecoverMutation {
        fn gen() -> impl Strategy<Value = Self> {
            (0..4).prop_flat_map(|raw| match raw {
                0 => Just(Self::RecoveryId).boxed(),
                1 => (0_usize..32).prop_map(Self::Digest).boxed(),
                2 => (0_usize..32).prop_map(Self::R).boxed(),
                3 => (0_usize..32).prop_map(Self::S).boxed(),
                _ => unreachable!(),
            })
        }
    }

    fn test_ecrecover_precompile(
        signing_key: &K256SigningKey,
        mutation: Option<EcRecoverMutation>,
        initial_offset_in_words: u32,
    ) -> Result<(), TestCaseError> {
        let mut heaps = Heaps::new(&[]);
        let initial_offset = initial_offset_in_words * 32;

        let message = "test message!";
        let mut message_digest = sha3::Keccak256::digest(message);

        let (signature, recovery_id) = signing_key
            .sign_prehash_recoverable(&message_digest)
            .unwrap();
        if recovery_id.is_x_reduced() {
            return Ok(());
        }
        let mut recovery_id = recovery_id.to_byte();

        println!(
            "testing key {:?} with mutation {mutation:?}",
            signing_key.verifying_key().to_encoded_point(true)
        );
        let mut signature_bytes = signature.to_bytes();

        match mutation {
            Some(EcRecoverMutation::Digest(byte)) => {
                message_digest[byte] ^= 1;
            }
            Some(EcRecoverMutation::RecoveryId) => {
                recovery_id = 1 - recovery_id;
            }
            Some(EcRecoverMutation::R(byte)) => {
                signature_bytes[byte] ^= 1;
            }
            Some(EcRecoverMutation::S(byte)) => {
                signature_bytes[byte + 32] ^= 1;
            }
            None => { /* Do nothing */ }
        }

        heaps.write_u256(
            HeapId::FIRST,
            initial_offset,
            U256::from_big_endian(&message_digest),
        );
        heaps.write_u256(HeapId::FIRST, initial_offset + 32, recovery_id.into());
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 64,
            U256::from_big_endian(&signature_bytes[..32]),
        );
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 96,
            U256::from_big_endian(&signature_bytes[32..]),
        );

        let memory = PrecompileMemoryReader::new(&heaps[HeapId::FIRST], initial_offset_in_words, 4);
        let output = LegacyPrecompiles.call_precompile(
            ECRECOVER_INNER_FUNCTION_PRECOMPILE_ADDRESS,
            memory,
            0,
        );

        prop_assert_eq!(output.len, 2);
        let expected_address = key_to_address(signing_key.verifying_key());
        let [is_success, address, _] = output.buffer;
        if mutation.is_some() {
            prop_assert_ne!(address, expected_address);
        } else {
            prop_assert_eq!(is_success, U256::one());
            prop_assert_eq!(address, expected_address);
        }
        prop_assert!(matches!(output.cycle_stats, Some(CycleStats::EcRecover(1))));
        Ok(())
    }

    #[derive(Debug, Clone, Copy)]
    enum P256Mutation {
        Digest(usize),
        R(usize),
        S(usize),
        Key(usize),
    }

    impl P256Mutation {
        fn gen() -> impl Strategy<Value = Self> {
            (0..4).prop_flat_map(|raw| match raw {
                0 => (0_usize..32).prop_map(Self::Digest).boxed(),
                1 => (0_usize..32).prop_map(Self::R).boxed(),
                2 => (0_usize..32).prop_map(Self::S).boxed(),
                3 => (0_usize..64).prop_map(Self::Key).boxed(),
                _ => unreachable!(),
            })
        }
    }

    fn test_secp256r1_precompile(
        signing_key: &P256SigningKey,
        mutation: Option<P256Mutation>,
        initial_offset_in_words: u32,
    ) -> Result<(), TestCaseError> {
        use zkevm_opcode_defs::p256::ecdsa::{signature::hazmat::PrehashSigner, Signature};

        let mut heaps = Heaps::new(&[]);
        let initial_offset = initial_offset_in_words * 32;

        let message = "test message!";
        let mut message_digest = sha3::Keccak256::digest(message);

        let signature: Signature = signing_key.sign_prehash(&message_digest).unwrap();

        println!(
            "testing key {:?} with mutation {mutation:?}",
            signing_key.verifying_key().to_encoded_point(true)
        );
        let mut signature_bytes = signature.to_bytes();
        let mut key_bytes = signing_key
            .verifying_key()
            .to_encoded_point(false)
            .as_bytes()[1..]
            .to_vec();
        assert_eq!(key_bytes.len(), 64);

        match mutation {
            Some(P256Mutation::Digest(byte)) => {
                message_digest[byte] ^= 1;
            }
            Some(P256Mutation::R(byte)) => {
                signature_bytes[byte] ^= 1;
            }
            Some(P256Mutation::S(byte)) => {
                signature_bytes[byte + 32] ^= 1;
            }
            Some(P256Mutation::Key(byte)) => {
                key_bytes[byte] ^= 1;
            }
            None => { /* Do nothing */ }
        }

        heaps.write_u256(
            HeapId::FIRST,
            initial_offset,
            U256::from_big_endian(&message_digest),
        );
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 32,
            U256::from_big_endian(&signature_bytes[..32]),
        );
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 64,
            U256::from_big_endian(&signature_bytes[32..]),
        );
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 96,
            U256::from_big_endian(&key_bytes[..32]),
        );
        heaps.write_u256(
            HeapId::FIRST,
            initial_offset + 128,
            U256::from_big_endian(&key_bytes[32..]),
        );

        let memory = PrecompileMemoryReader::new(&heaps[HeapId::FIRST], initial_offset_in_words, 5);
        let output =
            LegacyPrecompiles.call_precompile(SECP256R1_VERIFY_PRECOMPILE_ADDRESS, memory, 0);

        prop_assert_eq!(output.len, 2);
        let [is_ok, is_verified, _] = output.buffer;
        if mutation.is_none() {
            prop_assert_eq!(is_ok, U256::one());
            prop_assert_eq!(is_verified, U256::one());
        } else {
            prop_assert!(is_ok.is_zero() || is_verified.is_zero());
        }
        prop_assert!(matches!(
            output.cycle_stats,
            Some(CycleStats::Secp256r1Verify(1))
        ));
        Ok(())
    }

    proptest! {
        #[test]
        fn keccak_precompile_works(
            bytes in arbitrary_aligned_bytes(32),
            initial_offset in 0..u32::MAX / 2,
        ) {
            test_keccak_precompile(&bytes, initial_offset)?;
        }

        #[test]
        fn sha256_precompile_works(
            bytes in arbitrary_aligned_bytes(64),
            initial_offset_in_words in 0..u32::MAX / 64,
        ) {
            test_sha256_precompile(&bytes, initial_offset_in_words)?;
        }

        #[test]
        fn ecrecover_precompile_works(
            signing_key in array::uniform32(num::u8::ANY)
                .prop_filter_map("not a key", |bytes| K256SigningKey::from_bytes(&bytes.into()).ok()),
            mutation in option::of(EcRecoverMutation::gen()),
            initial_offset_in_words in 0..u32::MAX / 64,
        ) {
            test_ecrecover_precompile(&signing_key, mutation, initial_offset_in_words)?;
        }

        #[test]
        fn secp256r1_precompile_works(
            signing_key in array::uniform32(num::u8::ANY)
                .prop_filter_map("not a key", |bytes| P256SigningKey::from_bytes(&bytes.into()).ok()),
            mutation in option::of(P256Mutation::gen()),
            initial_offset_in_words in 0..u32::MAX / 64,
        ) {
            test_secp256r1_precompile(&signing_key, mutation, initial_offset_in_words)?;
        }
    }
}