|
|
@ -8,19 +8,24 @@ use rand::Rng; |
|
|
|
|
|
|
|
const BIG_GROUP_GEN: GoldilocksField = GoldilocksField(14293326489335486720);
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
struct SchnorrSigner {
|
|
|
|
PRIME_GROUP_GEN: GoldilocksField,
|
|
|
|
PRIME_GROUP_ORDER: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
|
|
|
struct SchnorrSecretKey {
|
|
|
|
sk: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
struct SchnorrPublicKey {
|
|
|
|
pk: GoldilocksField,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
struct SchnorrSignature {
|
|
|
|
s: u64,
|
|
|
|
e: u64,
|
|
|
@ -30,7 +35,7 @@ impl SchnorrSigner{ |
|
|
|
fn new() -> Self {
|
|
|
|
let quotient_order: u64 = (1 << 48) - (1 << 32);
|
|
|
|
let PRIME_GROUP_GEN: GoldilocksField = Self::pow(BIG_GROUP_GEN, quotient_order);
|
|
|
|
let PRIME_GROUP_ORDER: u64 = (1 << 32) + 1;
|
|
|
|
let PRIME_GROUP_ORDER: u64 = (1 << 16) + 1;
|
|
|
|
SchnorrSigner{PRIME_GROUP_GEN, PRIME_GROUP_ORDER}
|
|
|
|
}
|
|
|
|
|
|
|
@ -48,8 +53,10 @@ impl SchnorrSigner{ |
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn keygen(&self, sk: SchnorrSecretKey) -> SchnorrPublicKey {
|
|
|
|
let pk: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, sk.sk);
|
|
|
|
fn keygen(&self, sk: &SchnorrSecretKey) -> SchnorrPublicKey {
|
|
|
|
let pk: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, sk.sk).inverse();
|
|
|
|
println!("{:?}", self.PRIME_GROUP_GEN);
|
|
|
|
// self.PRIME_GROUP_GEN is 6612579038192137166
|
|
|
|
SchnorrPublicKey{pk: pk}
|
|
|
|
}
|
|
|
|
|
|
|
@ -69,20 +76,29 @@ impl SchnorrSigner{ |
|
|
|
rng.gen_range(0..group_order)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sign(&self, msg: &Vec<GoldilocksField>, sk: SchnorrSecretKey, rng: &mut rand::rngs::ThreadRng) -> SchnorrSignature {
|
|
|
|
fn u64_into_goldilocks_vec(&self, msg: Vec<u64>) -> Vec<GoldilocksField> {
|
|
|
|
msg.into_iter()
|
|
|
|
.map(|x| GoldilocksField::from_noncanonical_u64(x))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sign(&self, msg: &Vec<GoldilocksField>, sk: &SchnorrSecretKey, rng: &mut rand::rngs::ThreadRng) -> SchnorrSignature {
|
|
|
|
let k: u64 = self.rand_group_multiplier(rng);
|
|
|
|
let r: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, k);
|
|
|
|
let e: u64 = self.hash_insecure(&r, msg);
|
|
|
|
assert!(k < self.PRIME_GROUP_ORDER);
|
|
|
|
assert!(sk < self.PRIME_GROUP_ORDER);
|
|
|
|
assert!(sk.sk < self.PRIME_GROUP_ORDER);
|
|
|
|
assert!(e < self.PRIME_GROUP_ORDER);
|
|
|
|
//println!("Super secret k: {:?}", k);
|
|
|
|
//println!("Super secret r: {:?}", r);
|
|
|
|
//println!("PRIME_GROUP_ORDER: {:?}", self.PRIME_GROUP_ORDER);
|
|
|
|
let mut s128: u128 = ((k as u128) + (sk.sk as u128) * (e as u128));
|
|
|
|
s128 %= (self.PRIME_GROUP_ORDER as u128);
|
|
|
|
s128 %= self.PRIME_GROUP_ORDER as u128;
|
|
|
|
let s: u64 = s128 as u64;
|
|
|
|
SchnorrSignature{e, s}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify(&self, sig: SchnorrSignature, msg: &Vec<GoldilocksField>, pk: SchnorrPublicKey) -> bool {
|
|
|
|
fn verify(&self, sig: &SchnorrSignature, msg: &Vec<GoldilocksField>, pk: &SchnorrPublicKey) -> bool {
|
|
|
|
let r: GoldilocksField = Self::pow(self.PRIME_GROUP_GEN, sig.s)
|
|
|
|
* Self::pow(pk.pk, sig.e);
|
|
|
|
let e_v: u64 = self.hash_insecure(&r, msg);
|
|
|
@ -106,7 +122,7 @@ fn main() { |
|
|
|
mod tests {
|
|
|
|
use plonky2::field::goldilocks_field::GoldilocksField;
|
|
|
|
|
|
|
|
use crate::SchnorrSigner;
|
|
|
|
use crate::{SchnorrPublicKey, SchnorrSecretKey, SchnorrSigner, SchnorrSignature};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pow() {
|
|
|
@ -118,5 +134,19 @@ mod tests { |
|
|
|
#[test]
|
|
|
|
fn test_sig() {
|
|
|
|
println!("NOT IMPLEMENTED");
|
|
|
|
let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
|
|
|
|
let ss = SchnorrSigner::new();
|
|
|
|
let sk: SchnorrSecretKey = SchnorrSecretKey{ sk: 1422 };
|
|
|
|
let pk: SchnorrPublicKey = ss.keygen(&sk);
|
|
|
|
|
|
|
|
let msg0_u64: Vec<u64> = vec![17, 123985, 3, 12];
|
|
|
|
let msg0: Vec<GoldilocksField> = ss.u64_into_goldilocks_vec(msg0_u64);
|
|
|
|
let sig: SchnorrSignature = ss.sign(&msg0, &sk, &mut rng);
|
|
|
|
let res: bool = ss.verify(&sig, &msg0, &pk);
|
|
|
|
println!("Trying to verify:");
|
|
|
|
println!("Secret key: {:?}", sk);
|
|
|
|
println!("Public key: {:?}", pk);
|
|
|
|
println!("Signature: {:?}", sig);
|
|
|
|
assert!(res);
|
|
|
|
}
|
|
|
|
}
|