arnaucube
arnaucube/schnorrPublic Self-hosted repository
schnorr/src/schnorr.rs
src/schnorr.rsRaw
  1use plonky2::hash::poseidon::PoseidonHash;
  2use plonky2::field::goldilocks_field::GoldilocksField;
  3use plonky2::field::types::Field;
  4use plonky2::plonk::config::Hasher;
  5use plonky2::field::types::PrimeField64;
  6use rand;
  7use rand::Rng;
  8
  9const BIG_GROUP_GEN: GoldilocksField = GoldilocksField(14293326489335486720);
 10
 11#[derive(Copy, Clone, Debug)]
 12pub struct SchnorrSigner {
 13    PRIME_GROUP_GEN: GoldilocksField,
 14    PRIME_GROUP_ORDER: u64,
 15}
 16
 17#[derive(Copy, Clone, Debug)]
 18
 19pub struct SchnorrSecretKey {
 20    pub sk: u64,
 21}
 22
 23#[derive(Copy, Clone, Debug)]
 24pub struct SchnorrPublicKey {
 25    pub pk: GoldilocksField,
 26}
 27
 28#[derive(Copy, Clone, Debug)]
 29pub struct SchnorrSignature {
 30    pub s: u64,
 31    pub e: u64,
 32}
 33
 34impl SchnorrSigner{
 35    pub fn new() -> Self {
 36        let quotient_order: u64 = (1 << 48) - (1 << 32);
 37        let PRIME_GROUP_GEN: GoldilocksField = Self::pow(BIG_GROUP_GEN, quotient_order);
 38        let PRIME_GROUP_ORDER: u64 = (1 << 16) + 1;
 39        SchnorrSigner{PRIME_GROUP_GEN, PRIME_GROUP_ORDER}
 40    }
 41
 42    fn pow(x: GoldilocksField, a: u64) -> GoldilocksField {
 43        let mut a_copy = a;
 44        let mut res = GoldilocksField(1);
 45        let mut x_pow_2n = x.clone();
 46        while (a_copy > 0) {
 47            if (a_copy % 2 != 0) {
 48                res *= x_pow_2n;
 49            }
 50            a_copy /= 2;
 51            x_pow_2n *= x_pow_2n;
 52        }
 53        res
 54    }
 55
 56    pub fn keygen(&self, sk: &SchnorrSecretKey) -> SchnorrPublicKey {
 57        let pk: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, sk.sk).inverse();
 58        // self.PRIME_GROUP_GEN is 6612579038192137166
 59        SchnorrPublicKey{pk: pk}
 60    }
 61
 62    fn hash_insecure(&self, r: &GoldilocksField, msg: &Vec<GoldilocksField>) -> u64 {
 63        let poseidon_input: Vec<GoldilocksField> = std::iter::once(r)
 64            .chain(msg.iter())
 65            .copied()
 66            .collect();
 67
 68        let h = PoseidonHash::hash_no_pad(&poseidon_input);
 69        h.elements[0].to_canonical_u64() % self.PRIME_GROUP_ORDER
 70    }
 71
 72    fn rand_group_multiplier(&self, rng: &mut rand::rngs::ThreadRng) -> u64 {
 73        let group_order: u64 = (1 << 16) + 1;
 74        rng.gen_range(0..group_order)
 75    }
 76
 77    pub fn u64_into_goldilocks_vec(&self, msg: Vec<u64>) -> Vec<GoldilocksField> {
 78        msg.into_iter()
 79            .map(|x| GoldilocksField::from_noncanonical_u64(x))
 80            .collect()
 81    }
 82
 83    pub fn sign(&self, msg: &Vec<GoldilocksField>, sk: &SchnorrSecretKey, rng: &mut rand::rngs::ThreadRng) -> SchnorrSignature {
 84        let k: u64 = self.rand_group_multiplier(rng);
 85        let r: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, k);
 86        let e: u64 = self.hash_insecure(&r, msg);
 87        assert!(k < self.PRIME_GROUP_ORDER);
 88        assert!(sk.sk < self.PRIME_GROUP_ORDER);
 89        assert!(e < self.PRIME_GROUP_ORDER);
 90        let mut s128: u128 = ((k as u128) + (sk.sk as u128) * (e as u128));
 91        s128 %= self.PRIME_GROUP_ORDER as u128;
 92        let s: u64 = s128 as u64;
 93        SchnorrSignature{e, s}
 94    }
 95
 96    pub fn verify(&self, sig: &SchnorrSignature, msg: &Vec<GoldilocksField>, pk: &SchnorrPublicKey) -> bool {
 97        let r: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, sig.s)
 98            * Self::pow(pk.pk, sig.e);
 99        let e_v: u64 = self.hash_insecure(&r, msg);
100        e_v == sig.e
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use plonky2::field::goldilocks_field::GoldilocksField;
107
108    use crate::schnorr::{SchnorrPublicKey, SchnorrSecretKey, SchnorrSigner, SchnorrSignature};
109
110    #[test]
111    fn test_pow() {
112        let g = GoldilocksField(3);
113        let res = GoldilocksField(16305451354880172407);
114        assert_eq!(res, SchnorrSigner::pow(g, 1234567));
115    }
116
117    #[test]
118    fn test_sig() {
119        let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
120        let ss = SchnorrSigner::new();
121        let sk: SchnorrSecretKey = SchnorrSecretKey{ sk: 1422 };
122        let pk: SchnorrPublicKey = ss.keygen(&sk);
123
124        let msg0_u64: Vec<u64> = vec![17, 123985, 3, 12];
125        let msg0: Vec<GoldilocksField> = ss.u64_into_goldilocks_vec(msg0_u64);
126        let sig: SchnorrSignature = ss.sign(&msg0, &sk, &mut rng);
127        let res: bool = ss.verify(&sig, &msg0, &pk);
128        println!("Trying to verify:");
129        println!("Secret key: {:?}", sk);
130        println!("Public key: {:?}", pk);
131        println!("Signature: {:?}", sig);
132        assert!(res);
133    }
134}