halo2curves_axiom/secp256k1/
curve.rsuse crate::ff::WithSmallOrderMulGroup;
use crate::ff::{Field, PrimeField};
use crate::group::{prime::PrimeCurveAffine, Curve, Group as _, GroupEncoding};
use crate::hash_to_curve::svdw_hash_to_curve;
use crate::secp256k1::Fp;
use crate::secp256k1::Fq;
use crate::{Coordinates, CurveAffine, CurveAffineExt, CurveExt};
use core::cmp;
use core::fmt::Debug;
use core::iter::Sum;
use core::ops::{Add, Mul, Neg, Sub};
use rand::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};
impl group::cofactor::CofactorGroup for Secp256k1 {
type Subgroup = Secp256k1;
fn clear_cofactor(&self) -> Self {
*self
}
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
CtOption::new(self, 1.into())
}
fn is_torsion_free(&self) -> Choice {
1.into()
}
}
const SECP_GENERATOR_X: Fp = Fp::from_raw([
0x59F2815B16F81798,
0x029BFCDB2DCE28D9,
0x55A06295CE870B07,
0x79BE667EF9DCBBAC,
]);
const SECP_GENERATOR_Y: Fp = Fp::from_raw([
0x9C47D08FFB10D4B8,
0xFD17B448A6855419,
0x5DA4FBFC0E1108A8,
0x483ADA7726A3C465,
]);
const SECP_A: Fp = Fp::from_raw([0, 0, 0, 0]);
const SECP_B: Fp = Fp::from_raw([7, 0, 0, 0]);
use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
new_curve_impl,
};
new_curve_impl!(
(pub),
Secp256k1,
Secp256k1Affine,
true,
Fp,
Fq,
(SECP_GENERATOR_X,SECP_GENERATOR_Y),
SECP_A,
SECP_B,
"secp256k1",
|curve_id, domain_prefix| svdw_hash_to_curve(curve_id, domain_prefix, Secp256k1::SVDW_Z),
);
impl Secp256k1 {
const SVDW_Z: Fp = Fp::ONE;
}
#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256k1>();
}
#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<Secp256k1>();
}
#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<Secp256k1>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<Secp256k1>();
}
#[test]
fn test_endo_consistency() {
let g = Secp256k1::generator();
assert_eq!(g * Fq::ZETA, g.endo());
}
#[test]
fn ecdsa_example() {
use crate::group::Curve;
use crate::CurveAffine;
use ff::FromUniformBytes;
use rand_core::OsRng;
fn mod_n(x: Fp) -> Fq {
let mut x_repr = [0u8; 32];
x_repr.copy_from_slice(x.to_repr().as_ref());
let mut x_bytes = [0u8; 64];
x_bytes[..32].copy_from_slice(&x_repr[..]);
Fq::from_uniform_bytes(&x_bytes)
}
let g = Secp256k1::generator();
for _ in 0..1000 {
let sk = Fq::random(OsRng);
let pk = (g * sk).to_affine();
let msg_hash = Fq::random(OsRng);
let (r, s) = {
let k = Fq::random(OsRng);
let k_inv = k.invert().unwrap();
let r_point = (g * k).to_affine().coordinates().unwrap();
let x = r_point.x();
let r = mod_n(*x);
let s = k_inv * (msg_hash + (r * sk));
(r, s)
};
{
let s_inv = s.invert().unwrap();
let u_1 = msg_hash * s_inv;
let u_2 = r * s_inv;
let v_1 = g * u_1;
let v_2 = pk * u_2;
let r_point = (v_1 + v_2).to_affine().coordinates().unwrap();
let x_candidate = r_point.x();
let r_candidate = mod_n(*x_candidate);
assert_eq!(r, r_candidate);
}
}
}