|
|
@ -17,7 +17,7 @@ use num_traits::{One, Zero}; |
|
|
|
|
|
|
|
use generic_array::GenericArray;
|
|
|
|
|
|
|
|
mod utils;
|
|
|
|
pub mod utils;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
@ -25,7 +25,7 @@ extern crate lazy_static; |
|
|
|
lazy_static! {
|
|
|
|
static ref D: BigInt = BigInt::parse_bytes(b"168696", 10).unwrap();
|
|
|
|
static ref A: BigInt = BigInt::parse_bytes(b"168700", 10).unwrap();
|
|
|
|
static ref Q: BigInt = BigInt::parse_bytes(
|
|
|
|
pub static ref Q: BigInt = BigInt::parse_bytes(
|
|
|
|
b"21888242871839275222246405745257275088548364400416034343698204186575808495617",
|
|
|
|
10,
|
|
|
|
)
|
|
|
@ -156,6 +156,7 @@ pub fn decompress_point(bb: [u8; 32]) -> Result { |
|
|
|
Ok(Point { x: x, y: y })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Signature {
|
|
|
|
r_b8: Point,
|
|
|
|
s: BigInt,
|
|
|
@ -497,25 +498,25 @@ mod tests { |
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_key_sign_verify_mimc_0() {
|
|
|
|
let sk = new_key();
|
|
|
|
let pk = sk.public().unwrap();
|
|
|
|
let msg = 5.to_bigint().unwrap();
|
|
|
|
let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
let v = verify_mimc(pk, sig, msg);
|
|
|
|
assert_eq!(v, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_key_sign_verify_mimc_1() {
|
|
|
|
let sk = new_key();
|
|
|
|
let pk = sk.public().unwrap();
|
|
|
|
let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
|
|
|
|
let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
let v = verify_mimc(pk, sig, msg);
|
|
|
|
assert_eq!(v, true);
|
|
|
|
}
|
|
|
|
// #[test]
|
|
|
|
// fn test_new_key_sign_verify_mimc_0() {
|
|
|
|
// let sk = new_key();
|
|
|
|
// let pk = sk.public().unwrap();
|
|
|
|
// let msg = 5.to_bigint().unwrap();
|
|
|
|
// let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
// let v = verify_mimc(pk, sig, msg);
|
|
|
|
// assert_eq!(v, true);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_new_key_sign_verify_mimc_1() {
|
|
|
|
// let sk = new_key();
|
|
|
|
// let pk = sk.public().unwrap();
|
|
|
|
// let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
|
|
|
|
// let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
// let v = verify_mimc(pk, sig, msg);
|
|
|
|
// assert_eq!(v, true);
|
|
|
|
// }
|
|
|
|
#[test]
|
|
|
|
fn test_new_key_sign_verify_poseidon_0() {
|
|
|
|
let sk = new_key();
|
|
|
@ -596,50 +597,50 @@ mod tests { |
|
|
|
assert_eq!(&p.x, &expected_px);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_point_decompress_loop() {
|
|
|
|
for _ in 0..5 {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
|
|
|
|
let mut hasher = Blake2b::new();
|
|
|
|
let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
|
|
|
|
hasher.input(sk_raw_bytes);
|
|
|
|
let mut h = hasher.result();
|
|
|
|
|
|
|
|
h[0] = h[0] & 0xF8;
|
|
|
|
h[31] = h[31] & 0x7F;
|
|
|
|
h[31] = h[31] | 0x40;
|
|
|
|
|
|
|
|
let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
|
|
|
|
let point = B8.mul_scalar(&sk).unwrap();
|
|
|
|
let cmp_point = point.compress();
|
|
|
|
let dcmp_point = decompress_point(cmp_point).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(&point.x, &dcmp_point.x);
|
|
|
|
assert_eq!(&point.y, &dcmp_point.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_compress_decompress() {
|
|
|
|
let sk = new_key();
|
|
|
|
let pk = sk.public().unwrap();
|
|
|
|
|
|
|
|
for i in 0..5 {
|
|
|
|
let msg_raw = "123456".to_owned() + &i.to_string();
|
|
|
|
let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
|
|
|
|
let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
|
|
|
|
let compressed_sig = sig.compress();
|
|
|
|
let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
|
|
|
|
assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
|
|
|
|
assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
|
|
|
|
assert_eq!(&sig.s, &decompressed_sig.s);
|
|
|
|
|
|
|
|
let v = verify_mimc(pk.clone(), decompressed_sig, msg);
|
|
|
|
assert_eq!(v, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// #[test]
|
|
|
|
// fn test_point_decompress_loop() {
|
|
|
|
// for _ in 0..5 {
|
|
|
|
// let mut rng = rand::thread_rng();
|
|
|
|
// let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
|
|
|
|
// let mut hasher = Blake2b::new();
|
|
|
|
// let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
|
|
|
|
// hasher.input(sk_raw_bytes);
|
|
|
|
// let mut h = hasher.result();
|
|
|
|
//
|
|
|
|
// h[0] = h[0] & 0xF8;
|
|
|
|
// h[31] = h[31] & 0x7F;
|
|
|
|
// h[31] = h[31] | 0x40;
|
|
|
|
//
|
|
|
|
// let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
|
|
|
|
// let point = B8.mul_scalar(&sk).unwrap();
|
|
|
|
// let cmp_point = point.compress();
|
|
|
|
// let dcmp_point = decompress_point(cmp_point).unwrap();
|
|
|
|
//
|
|
|
|
// assert_eq!(&point.x, &dcmp_point.x);
|
|
|
|
// assert_eq!(&point.y, &dcmp_point.y);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_signature_compress_decompress() {
|
|
|
|
// let sk = new_key();
|
|
|
|
// let pk = sk.public().unwrap();
|
|
|
|
//
|
|
|
|
// for i in 0..5 {
|
|
|
|
// let msg_raw = "123456".to_owned() + &i.to_string();
|
|
|
|
// let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
|
|
|
|
// let sig = sk.sign_mimc(msg.clone()).unwrap();
|
|
|
|
//
|
|
|
|
// let compressed_sig = sig.compress();
|
|
|
|
// let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
|
|
|
|
// assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
|
|
|
|
// assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
|
|
|
|
// assert_eq!(&sig.s, &decompressed_sig.s);
|
|
|
|
//
|
|
|
|
// let v = verify_mimc(pk.clone(), decompressed_sig, msg);
|
|
|
|
// assert_eq!(v, true);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_schnorr_signature() {
|
|
|
|