k256/
lib.rs

1// Fork of RustCrypto's k256 crate https://docs.rs/k256/latest/k256/
2// that uses zkvm instructions
3
4#![no_std]
5extern crate alloc;
6
7use elliptic_curve::{consts::U32, point::PointCompression, Curve, CurveArithmetic, PrimeCurve};
8
9mod coord;
10mod internal;
11mod point;
12mod scalar;
13
14#[cfg(feature = "ecdsa-core")]
15pub mod ecdsa;
16
17pub use elliptic_curve::{self, bigint::U256};
18// Needs to be public so that the `sw_init` macro can access it
19pub use internal::{
20    Secp256k1Coord, Secp256k1Point, Secp256k1Point as AffinePoint,
21    Secp256k1Point as ProjectivePoint, Secp256k1Scalar as Scalar, Secp256k1Scalar,
22};
23
24// -- Define the ZST for implementing the elliptic curve traits --
25#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
26pub struct Secp256k1;
27
28// --- Implement the Curve trait on Secp256k1 ---
29
30/// Order of the secp256k1 elliptic curve in hexadecimal.
31const ORDER_HEX: &str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141";
32
33/// Order of the secp256k1 elliptic curve.
34const ORDER: U256 = U256::from_be_hex(ORDER_HEX);
35
36impl Curve for Secp256k1 {
37    /// 32-byte serialized field elements.
38    type FieldBytesSize = U32;
39
40    // Perf: Use the U256 type from openvm_ruint here
41    type Uint = U256;
42
43    /// Curve order.
44    const ORDER: U256 = ORDER;
45}
46
47impl PrimeCurve for Secp256k1 {}
48
49impl CurveArithmetic for Secp256k1 {
50    type AffinePoint = AffinePoint;
51    type ProjectivePoint = ProjectivePoint;
52    type Scalar = Scalar;
53}
54
55impl PointCompression for Secp256k1 {
56    /// secp256k1 points are typically compressed.
57    const COMPRESS_POINTS: bool = true;
58}
59
60/// SEC1-encoded secp256k1 (K-256) curve point.
61pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<Secp256k1>;