diff --git a/ecvrf/src/helper.rs b/ecvrf/src/helper.rs index 7449f29..2a7dc1a 100644 --- a/ecvrf/src/helper.rs +++ b/ecvrf/src/helper.rs @@ -295,10 +295,14 @@ pub fn recover_raw_keypair(secret_key: &[u8; SECRET_KEY_SIZE]) -> RawKeyPair { #[cfg(test)] mod tests { use crate::ECVRF; - use libsecp256k1::{curve::Scalar, SecretKey}; + use libsecp256k1::{ + curve::{Affine, Field, Jacobian, Scalar}, + SecretKey, + }; use super::{ - is_on_curve, new_candidate_point, random_bytes, randomize, scalar_is_gt, scalar_is_gte, + is_on_curve, new_candidate_point, projective_ec_add, random_bytes, randomize, scalar_is_gt, + scalar_is_gte, }; #[test] @@ -373,4 +377,187 @@ mod tests { } } } + + #[test] + // The results below have been tested on chainlink. + fn test_projective_ec_add() { + //Check the correctness of the projective_ec_add function + + //The x and y coordinates of the points P + let P_x = [ + Field::new_raw( + 1014095, 27245485, 18079781, 62818885, 6005449, 27754920, 47166199, 49620396, + 20482281, 64453196, + ), + Field::new_raw( + 2881937, 30269476, 30651653, 27828758, 49430413, 38968291, 62159435, 14563297, + 3762092, 64301321, + ), + Field::new_raw( + 888935, 44876924, 33376525, 46322353, 23014199, 43534704, 2075997, 58613759, + 13276206, 38459183, + ), + Field::new_raw( + 1173538, 5251979, 45514189, 47172267, 41083573, 64264174, 57207202, 37126255, + 20634493, 18626283, + ), + Field::new_raw( + 34978, 8986890, 32328974, 6429458, 62735188, 6404883, 65609615, 56258350, 14825711, + 27493980, + ), + ]; + let P_y = [ + Field::new_raw( + 3323709, 63880799, 34911351, 22193901, 58305475, 42647907, 64791389, 9908645, + 34832096, 24942443, + ), + Field::new_raw( + 3681752, 46030946, 56815721, 9970617, 44706522, 58420823, 4363185, 7983735, 763546, + 58977104, + ), + Field::new_raw( + 1626124, 10498509, 50083977, 34434173, 29951089, 28916303, 774447, 31747696, + 38256805, 32493680, + ), + Field::new_raw( + 1482774, 28662690, 19892534, 7635144, 21201279, 55891098, 48982497, 25061615, + 55177987, 56318785, + ), + Field::new_raw( + 1962413, 32872974, 54025012, 31576763, 52170737, 5067518, 34877004, 58106407, + 31099250, 49435711, + ), + ]; + + //The x and y coordinates of the points Q + let Q_x = [ + Field::new_raw( + 3949615, 66779817, 1606560, 47410390, 831760, 1795286, 36595269, 30921352, + 48240622, 31121077, + ), + Field::new_raw( + 466159, 4706190, 56774435, 50585987, 56106904, 4236017, 49124608, 24621076, + 37495853, 20301811, + ), + Field::new_raw( + 3865535, 15171696, 3241387, 42670770, 59787283, 45319471, 30668869, 17429567, + 66937484, 58643956, + ), + Field::new_raw( + 3965362, 8238738, 33125405, 28588179, 11796129, 21871629, 24563963, 10945136, + 59314097, 36909114, + ), + Field::new_raw( + 2937111, 50241608, 8444056, 52466045, 22141634, 60295819, 54344661, 56167125, + 55470749, 49532653, + ), + ]; + let Q_y = [ + Field::new_raw( + 2142565, 27820731, 60992473, 42219931, 62769828, 14814230, 7831328, 45332626, + 47127755, 36936398, + ), + Field::new_raw( + 1749105, 43673108, 52090356, 36082059, 30968079, 37767043, 52130192, 58004526, + 32090187, 10356912, + ), + Field::new_raw( + 3016472, 23348998, 40196548, 39679416, 64455134, 13771147, 19775228, 5711618, + 26240227, 36404485, + ), + Field::new_raw( + 656600, 11568354, 22113171, 57657914, 30818497, 5831077, 34556057, 44295645, + 59443515, 55588429, + ), + Field::new_raw( + 1167556, 27341782, 57918609, 58030667, 18863569, 48751500, 61601032, 34671145, + 26108266, 2677945, + ), + ]; + + // The x,y,z coordinates of the points R + let R_x = [ + Field::new_raw( + 1838677, 7158945, 56087361, 27064659, 42315756, 49776020, 38075574, 33867422, + 49809746, 14281852, + ), + Field::new_raw( + 2462413, 22740053, 33150857, 8412637, 21642197, 4835937, 32718487, 50201252, + 12415716, 18472476, + ), + Field::new_raw( + 2960637, 32560841, 57171124, 64725753, 47879677, 1694297, 3505259, 13397197, + 15134383, 37571661, + ), + Field::new_raw( + 904856, 57226798, 41399843, 40377572, 40886400, 57915880, 62676632, 50889666, + 60821450, 29856311, + ), + Field::new_raw( + 784655, 44258734, 27600880, 66250229, 57875392, 24729073, 38070598, 2602594, + 6003575, 24191994, + ), + ]; + let R_y = [ + Field::new_raw( + 76599, 57880985, 16656958, 5837499, 9722279, 28603191, 42014985, 37913965, + 41355961, 30599916, + ), + Field::new_raw( + 793801, 55065223, 41505364, 35191912, 14566757, 65865732, 39564011, 28065904, + 55682892, 39222388, + ), + Field::new_raw( + 165206, 3315826, 48468169, 17069393, 56978389, 39387409, 52681255, 58481794, + 40551398, 61009165, + ), + Field::new_raw( + 1825482, 27778166, 30683534, 28169350, 22418579, 62384284, 1656908, 32702240, + 29990198, 64177265, + ), + Field::new_raw( + 3201736, 19484849, 61910818, 23829149, 16109573, 3621671, 62931831, 12545403, + 50031917, 15180079, + ), + ]; + let R_z = [ + Field::new_raw( + 2963800, 35258506, 56555144, 48221335, 37275811, 47111649, 30564697, 62163006, + 5607768, 33627847, + ), + Field::new_raw( + 3621819, 56142224, 6497026, 19513487, 31005699, 64382370, 61973006, 31282047, + 39692822, 38703415, + ), + Field::new_raw( + 936954, 42542377, 52907648, 55981871, 33969320, 44110150, 10742065, 33193877, + 35994484, 58271547, + ), + Field::new_raw( + 3474970, 13596286, 53644147, 59861713, 20092981, 15712353, 35065860, 41924848, + 58811872, 51889383, + ), + Field::new_raw( + 3745138, 41183687, 19090533, 45391971, 36511456, 51087497, 33703536, 23019368, + 26518385, 19502344, + ), + ]; + + let mut P = Affine::default(); + let mut Q = Affine::default(); + let mut R = Jacobian::default(); + + for i in 0..P_x.len() { + P.x = P_x[i]; + P.y = P_y[i]; + + Q.x = Q_x[i]; + Q.y = Q_y[i]; + + R = projective_ec_add(&P, &Q); + assert!(R.x == R_x[i]); + assert!(R.y == R_y[i]); + assert!(R.z == R_z[i]); + } + } }