mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-12 05:59:23 +00:00
Relies on the ff/group API, instead of the custom Curve type. Also removes GENERATOR_TABLE, only used by dalek, as we should provide our own API for that over ff/group instead. This slows down the FROST tests, under debug, by about 0.2-0.3s. Ed25519 and Ristretto together take ~2.15 seconds now.
67 lines
1.5 KiB
Rust
67 lines
1.5 KiB
Rust
use rand_core::{RngCore, CryptoRng};
|
|
|
|
use group::{ff::{Field, PrimeField}, GroupEncoding};
|
|
|
|
use multiexp::BatchVerifier;
|
|
|
|
use crate::{Curve, F_len, G_len};
|
|
|
|
#[allow(non_snake_case)]
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
pub struct SchnorrSignature<C: Curve> {
|
|
pub R: C::G,
|
|
pub s: C::F,
|
|
}
|
|
|
|
impl<C: Curve> SchnorrSignature<C> {
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
let mut res = Vec::with_capacity(G_len::<C>() + F_len::<C>());
|
|
res.extend(self.R.to_bytes().as_ref());
|
|
res.extend(self.s.to_repr().as_ref());
|
|
res
|
|
}
|
|
}
|
|
|
|
pub(crate) fn sign<C: Curve>(
|
|
private_key: C::F,
|
|
nonce: C::F,
|
|
challenge: C::F
|
|
) -> SchnorrSignature<C> {
|
|
SchnorrSignature {
|
|
R: C::GENERATOR * nonce,
|
|
s: nonce + (private_key * challenge)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn verify<C: Curve>(
|
|
public_key: C::G,
|
|
challenge: C::F,
|
|
signature: &SchnorrSignature<C>
|
|
) -> bool {
|
|
(C::GENERATOR * signature.s) == (signature.R + (public_key * challenge))
|
|
}
|
|
|
|
pub(crate) fn batch_verify<C: Curve, R: RngCore + CryptoRng>(
|
|
rng: &mut R,
|
|
triplets: &[(u16, C::G, C::F, SchnorrSignature<C>)]
|
|
) -> Result<(), u16> {
|
|
let mut values = [(C::F::one(), C::GENERATOR); 3];
|
|
let mut batch = BatchVerifier::new(triplets.len());
|
|
for triple in triplets {
|
|
// s = r + ca
|
|
// sG == R + cA
|
|
// R + cA - sG == 0
|
|
|
|
// R
|
|
values[0].1 = triple.3.R;
|
|
// cA
|
|
values[1] = (triple.2, triple.1);
|
|
// -sG
|
|
values[2].0 = -triple.3.s;
|
|
|
|
batch.queue(rng, triple.0, values);
|
|
}
|
|
|
|
batch.verify_vartime_with_vartime_blame()
|
|
}
|