p256/
coord.rs

1use alloc::vec::Vec;
2
3use elliptic_curve::subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
4use openvm_algebra_guest::IntMod;
5
6use crate::internal::P256Coord;
7
8// --- Implement elliptic_curve traits on P256Coord ---
9
10impl Copy for P256Coord {}
11
12impl Default for P256Coord {
13    fn default() -> Self {
14        <Self as IntMod>::ZERO
15    }
16}
17
18impl ConditionallySelectable for P256Coord {
19    fn conditional_select(a: &P256Coord, b: &P256Coord, choice: Choice) -> P256Coord {
20        P256Coord::from_le_bytes_unchecked(
21            &a.as_le_bytes()
22                .iter()
23                .zip(b.as_le_bytes().iter())
24                .map(|(a, b)| u8::conditional_select(a, b, choice))
25                .collect::<Vec<_>>(),
26        )
27    }
28}
29
30impl ConstantTimeEq for P256Coord {
31    fn ct_eq(&self, other: &P256Coord) -> Choice {
32        #[cfg(not(target_os = "zkvm"))]
33        {
34            // Requires canonical form
35            self.as_le_bytes().ct_eq(other.as_le_bytes())
36        }
37        #[cfg(target_os = "zkvm")]
38        {
39            // The zkVM implementation calls iseqmod opcode so it is constant time, _except_ a check
40            // of whether the setup opcode has been called already
41            Choice::from((self == other) as u8)
42        }
43    }
44}