Skip to main content

airbender_crypto/secp256k1/
hooks.rs

1//! Hooks for overriding expensive secp256k1 field operations during EC recovery.
2//!
3//! See the [Secp256k1 Hooks](https://matter-labs.github.io/airbender-platform/latest/04-crypto-on-guest-and-host.html#secp256k1-hooks)
4//! section of the book for usage details and examples.
5
6pub trait Secp256k1Hooks {
7    fn fe_sqrt_and_assign(&mut self, fe: &mut super::field::FieldElement) -> bool;
8    fn fe_invert_and_assign(&mut self, fe: &mut super::field::FieldElement);
9    fn scalar_invert_and_assign(&mut self, scalar: &mut super::scalars::Scalar);
10}
11
12pub struct DefaultSecp256k1Hooks;
13
14impl Secp256k1Hooks for DefaultSecp256k1Hooks {
15    #[inline(always)]
16    fn fe_sqrt_and_assign(&mut self, fe: &mut super::field::FieldElement) -> bool {
17        fe.sqrt_in_place()
18    }
19
20    #[inline(always)]
21    fn fe_invert_and_assign(&mut self, fe: &mut super::field::FieldElement) {
22        fe.invert_in_place()
23    }
24
25    #[inline(always)]
26    fn scalar_invert_and_assign(&mut self, scalar: &mut super::scalars::Scalar) {
27        scalar.invert_in_place()
28    }
29}