Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update verification key #632

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions fastcrypto-zkp/src/bn254/unit_tests/zk_login_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::bn254::zk_login::{
verify_extended_claim, Claim, JWTDetails, JWTHeader,
};
use crate::bn254::zk_login::{fetch_jwks, OIDCProvider};
use crate::bn254::zk_login_api::Bn254Fr;
use crate::bn254::zk_login_api::ZkLoginEnv;
use crate::bn254::{
zk_login::{ZkLoginInputs, JWK},
Expand Down Expand Up @@ -84,16 +85,13 @@ const BAD_JWK_BYTES: &[u8] = r#"{

#[test]
fn test_verify_zk_login_google() {
use crate::bn254::zk_login_api::Bn254Fr;
use std::str::FromStr;

let kp = Ed25519KeyPair::generate(&mut StdRng::from_seed([0; 32]));
let mut eph_pubkey = vec![0x00];
eph_pubkey.extend(kp.public().as_ref());

assert!(ZkLoginInputs::from_json("{\"something\":{\"pi_a\":[\"17906300526443048714387222471528497388165567048979081127218444558531971001212\",\"16347093943573822555530932280098040740968368762067770538848146419225596827968\",\"1\"],\"pi_b\":[[\"604559992637298524596005947885439665413516028337069712707205304781687795569\",\"3442016989288172723305001983346837664894554996521317914830240702746056975984\"],[\"11525538739919950358574045244601652351196410355282682596092151863632911615318\",\"8054528381876103674715157136115660256860302241449545586065224275685056359825\"],[\"1\",\"0\"]],\"pi_c\":[\"12090542001353421590770702288155881067849038975293665701252531703168853963809\",\"8667909164654995486331191860419304610736366583628608454080754129255123340291\",\"1\"]},\"address_seed\":\"7577247629761003321376053963457717029490787816434302620024795358930497565155\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"yJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLC\",\"index_mod_4\":1},{\"name\":\"aud\",\"value_base64\":\"CJhdWQiOiI1NzU1MTkyMDQyMzctbXNvcDllcDQ1dTJ1bzk4aGFwcW1uZ3Y4ZDg0cWRjOGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLC\",\"index_mod_4\":1}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsImtpZCI6IjkxMWUzOWUyNzkyOGFlOWYxZTlkMWUyMTY0NmRlOTJkMTkzNTFiNDQiLCJ0eXAiOiJKV1QifQ\"}").is_err());

let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"4169504874401756960902573657806649547799902200557854504390915631723967576424\",\"18665360143042979657974479594360541428779995665200717488408301927557636784001\",\"1\"],\"pi_b\":[[\"4190982973503787187215833966444881896490238759852310712484328743034325450002\",\"13961689533496148166195507272593938714345330199311603823531778460871822941733\"],[\"8667724092102706800296481061599598851325940196970007892616205771280439047800\",\"10953429141986591272557238778177272493565418087371365641332421149411387960841\"],[\"1\",\"0\"]],\"pi_c\":[\"8134813789792782328004140831898929066280169330521012844840217819458697957354\",\"2413221756697001634635278094308467092060083404493223540976829788972611042841\",\"1\"]},\"address_seed\":\"19509697479448296403420673595157414978688381950012191268181947137083103804113\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"yJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLC\",\"index_mod_4\":1},{\"name\":\"aud\",\"value_base64\":\"CJhdWQiOiI1NzU1MTkyMDQyMzctbXNvcDllcDQ1dTJ1bzk4aGFwcW1uZ3Y4ZDg0cWRjOGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLC\",\"index_mod_4\":1}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsImtpZCI6IjdjOWM3OGUzYjAwZTFiYjA5MmQyNDZjODg3YjExMjIwYzg3YjdkMjAiLCJ0eXAiOiJKV1QifQ\"}").unwrap().init().unwrap();
let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"16082379985244139257081251352758755486156282972982603863007685291104503933311\",\"924319019028863167372401695750240170246182797458677233202254140761845272417\",\"1\"],\"pi_b\":[[\"13577250540115265266613311991485643078228707057086458534580175835039018572685\",\"12376053001358370647205175062199127322673512803490888228095245375811974804326\"],[\"14035295319062970519340182968766274788478314052702678112524794155602573477951\",\"21275817745084002159703389733799570288229406275961853650678828923527832512195\"],[\"1\",\"0\"]],\"pi_c\":[\"21768939217356454092644810716610021526414672327340826974534017558007065128740\",\"19849276141337612251288394025918481446172401959982365719577887942308529252632\",\"1\"]},\"address_seed\":\"21150353671819850968488494085061363586427266461520959449438048630829862383214\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"yJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLC\",\"index_mod_4\":1},{\"name\":\"aud\",\"value_base64\":\"CJhdWQiOiI1NzU1MTkyMDQyMzctbXNvcDllcDQ1dTJ1bzk4aGFwcW1uZ3Y4ZDg0cWRjOGsuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLC\",\"index_mod_4\":1}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsImtpZCI6IjdjOWM3OGUzYjAwZTFiYjA5MmQyNDZjODg3YjExMjIwYzg3YjdkMjAiLCJ0eXAiOiJKV1QifQ\"}").unwrap().init().unwrap();
assert_eq!(
zklogin_inputs.get_kid(),
"7c9c78e3b00e1bb092d246c887b11220c87b7d20".to_string()
Expand All @@ -116,15 +114,15 @@ fn test_verify_zk_login_google() {
);
assert_eq!(
zklogin_inputs.get_address_seed(),
"19509697479448296403420673595157414978688381950012191268181947137083103804113"
"21150353671819850968488494085061363586427266461520959449438048630829862383214"
);
assert_eq!(
get_enoki_address(
zklogin_inputs.get_address_seed(),
zklogin_inputs.get_address_params()
)
.to_vec(),
Hex::decode("0xe21a10621dfc4aeda576141951527450b41cd2a54ab3586994362393b18e26fc").unwrap()
Hex::decode("0x7bf6145cfe0592c0428ed8ce9612077b9ca1e5bc60308a90990bc952d13ccce8").unwrap()
);

let mut map = HashMap::new();
Expand All @@ -149,7 +147,7 @@ fn test_verify_zk_login_google() {
.calculate_all_inputs_hash(&eph_pubkey, &modulus, 10)
.unwrap(),
vec![Bn254Fr::from_str(
"6470551385883269437362767266627536617163173243418744804736794511574177712392"
"19190136882259072389509967010336890361732579901899057561984458564815999051862"
)
.unwrap()]
);
Expand All @@ -167,7 +165,7 @@ fn test_verify_zk_login_twitch() {
let mut eph_pubkey = vec![0x00];
eph_pubkey.extend(kp.public().as_ref());

let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"19509439357688413199669022796225539258072174653501762782075286761070625831193\",\"20120503733945644082227722545851656398373988272428505660006551813854995822985\",\"1\"],\"pi_b\":[[\"1117994151330468447369450231245311912834573222446261879261674806722923452900\",\"7733936506019730416081557210097197370249464644638417179324894468664184447039\"],[\"2555383383487836544112017329237682823404520910874548397340830670912146360219\",\"16728643212261900382695858553380389189418195572563825008074203570429841922928\"],[\"1\",\"0\"]],\"pi_c\":[\"16601420651419492780459833556057641874870339072643102337908730119218447099772\",\"16764291910173534768202214397426400695948741956099214716087835684238558068409\",\"1\"]},\"address_seed\":\"18404400811258979351843554038529324719581180024248900217069822820095974835369\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"index_mod_4\":2},{\"name\":\"aud\",\"value_base64\":\"yJhdWQiOiJyczFiaDA2NWk5eWE0eWR2aWZpeGw0a3NzMHVocHQiLC\",\"index_mod_4\":1}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}").unwrap().init().unwrap();
let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"2639684184680217707167754014000719722348206659392422133035933088167295844621\",\"15411697389380103098050765723042580180772223011905582881833041447034179685161\",\"1\"],\"pi_b\":[[\"18356546416649273600365508068279984662879338153955858345242905260545040887165\",\"14180424108251071134157931909030745068063443512539428703047837797454965825626\"],[\"13156473667176810581893653079638435272252026941153836815590225135710650196382\",\"21239978751364084281206642892186667820382067271473352046319441969708281386102\"],[\"1\",\"0\"]],\"pi_c\":[\"10224668151896969767148853455746517578322339166888897843411999928700401320418\",\"10920763695594894441298491254988284677195769983974208707015444852382653532723\",\"1\"]},\"address_seed\":\"21483285397923302977910340636259412155696585453250993383687293995976400590480\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"index_mod_4\":2},{\"name\":\"aud\",\"value_base64\":\"yJhdWQiOiJyczFiaDA2NWk5eWE0eWR2aWZpeGw0a3NzMHVocHQiLC\",\"index_mod_4\":1}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}").unwrap().init().unwrap();
assert_eq!(zklogin_inputs.get_kid(), "1".to_string());
assert_eq!(
zklogin_inputs.get_iss(),
Expand All @@ -187,24 +185,43 @@ fn test_verify_zk_login_twitch() {
);
assert_eq!(
zklogin_inputs.get_address_seed(),
"18404400811258979351843554038529324719581180024248900217069822820095974835369"
"21483285397923302977910340636259412155696585453250993383687293995976400590480"
);
assert_eq!(
get_enoki_address(
zklogin_inputs.get_address_seed(),
zklogin_inputs.get_address_params()
)
.to_vec(),
Hex::decode("0x171342d3274658f0641885cd019282336e3f48fd050d610241909dd14bb24d65").unwrap()
Hex::decode("0x18642facd3dcc683f24490f5adb576eb02fc12073c46c9006dbe854cdbfbb899").unwrap()
);

let mut map = HashMap::new();
map.insert(("1".to_string(), OIDCProvider::Twitch.get_config().0.to_string()), JWK {
let content = JWK {
kty: "RSA".to_string(),
e: "AQAB".to_string(),
n: "6lq9MQ-q6hcxr7kOUp-tHlHtdcDsVLwVIw13iXUCvuDOeCi0VSuxCCUY6UmMjy53dX00ih2E4Y4UvlrmmurK0eG26b-HMNNAvCGsVXHU3RcRhVoHDaOwHwU72j7bpHn9XbP3Q3jebX6KIfNbei2MiR0Wyb8RZHE-aZhRYO8_-k9G2GycTpvc-2GBsP8VHLUKKfAs2B6sW3q3ymU6M0L-cFXkZ9fHkn9ejs-sqZPhMJxtBPBxoUIUQFTgv4VXTSv914f_YkNw-EjuwbgwXMvpyr06EyfImxHoxsZkFYB-qBYHtaMxTnFsZBr6fn8Ha2JqT1hoP7Z5r5wxDu3GQhKkHw".to_string(),
alg: "RS256".to_string(),
});
};
let modulus = Base64UrlUnpadded::decode_vec(&content.n).unwrap();

map.insert(
(
"1".to_string(),
OIDCProvider::Twitch.get_config().0.to_string(),
),
content,
);

assert_eq!(
zklogin_inputs
.calculate_all_inputs_hash(&eph_pubkey, &modulus, 10)
.unwrap(),
vec![Bn254Fr::from_str(
"5856188553771750715373571553753599041029773450105736907486194952973723348883"
)
.unwrap()]
);
let res = verify_zk_login(&zklogin_inputs, 10, &eph_pubkey, &map, &ZkLoginEnv::Test);
assert!(res.is_ok());

Expand All @@ -219,7 +236,7 @@ fn test_verify_zk_login_facebook() {
let mut eph_pubkey = vec![0x00];
eph_pubkey.extend(kp.public().as_ref());

let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"11906838442390958947956981941412847123726876942446732183131650721221185200415\",\"13477947960136562774712544330249233431481214258421198481503341839965229181441\",\"1\"],\"pi_b\":[[\"195283665225488233857568361471137028161450380486605042191036163099058537322\",\"21160918906651583322398345961182438829201720266338988247146384325166285027013\"],[\"5056810726385344895153919381342810734795382500127162339510513422213030592198\",\"2052421677419437749436133028803547336046283023917821033707060737419829163114\"],[\"1\",\"0\"]],\"pi_c\":[\"14739199144858043017013900215488708741774378306569808471678552294302294661097\",\"14198628060838424500665994481036804766418315409334469541533831317279068896764\",\"1\"]},\"address_seed\":\"9170870217795363726833321704645580846260479365166849913550847438937458025900\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"yJpc3MiOiJodHRwczpcL1wvd3d3LmZhY2Vib29rLmNvbSIs\",\"index_mod_4\":1},{\"name\":\"aud\",\"value_base64\":\"ImF1ZCI6IjIzMzMwNzE1NjM1MjkxNyIs\",\"index_mod_4\":0}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjU5MzE3MDEzMzExNjVmMDdmNTUwYWM1ZjAxOTQ5NDJkNTRmOWMyNDkifQ\"}").unwrap().init().unwrap();
let zklogin_inputs = ZkLoginInputs::from_json("{\"proof_points\":{\"pi_a\":[\"16500559452186857499124905145218965727454398652759898506130123782737180551024\",\"1403037760258969546586768446760882646660554376880919683180395342618686906382\",\"1\"],\"pi_b\":[[\"12463789295781828009345316567938834871413393951281528901930690034950665391292\",\"16301756414332383815173890006998407782812302695665089990395506495445072039950\"],[\"19728141070117461173622838505925353541939789875408954541048815956055929576938\",\"21239411122885193204521373031249830589601614530017004204270959331789128729582\"],[\"1\",\"0\"]],\"pi_c\":[\"16094781461241847235951763701104954579675913864156691777860223519371049858114\",\"7705218318167899339727292541697723794048510769012014737743407264594062927068\",\"1\"]},\"address_seed\":\"1487011095754058868957639998432654337555495215275691418230823914445177483005\",\"claims\":[{\"name\":\"iss\",\"value_base64\":\"yJpc3MiOiJodHRwczpcL1wvd3d3LmZhY2Vib29rLmNvbSIs\",\"index_mod_4\":1},{\"name\":\"aud\",\"value_base64\":\"ImF1ZCI6IjIzMzMwNzE1NjM1MjkxNyIs\",\"index_mod_4\":0}],\"header_base64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjU5MzE3MDEzMzExNjVmMDdmNTUwYWM1ZjAxOTQ5NDJkNTRmOWMyNDkifQ\"}").unwrap().init().unwrap();
assert_eq!(
zklogin_inputs.get_kid(),
"5931701331165f07f550ac5f0194942d54f9c249".to_string()
Expand All @@ -239,7 +256,7 @@ fn test_verify_zk_login_facebook() {
);
assert_eq!(
zklogin_inputs.get_address_seed(),
"9170870217795363726833321704645580846260479365166849913550847438937458025900"
"1487011095754058868957639998432654337555495215275691418230823914445177483005"
);

assert_eq!(
Expand All @@ -248,16 +265,34 @@ fn test_verify_zk_login_facebook() {
zklogin_inputs.get_address_params()
)
.to_vec(),
Hex::decode("0x5b10433166b4c4a32fcac2b3d073d90d4e0ad6c4bd33f79f982cc46d5b963e5c").unwrap()
Hex::decode("0x5e3733bf03f715a87b641553fce0f8b22bcb6385ce78cc05ddecd55929a5a304").unwrap()
);

let mut map = HashMap::new();
map.insert(("5931701331165f07f550ac5f0194942d54f9c249".to_string(), OIDCProvider::Facebook.get_config().0.to_string()), JWK {
let content = JWK {
kty: "RSA".to_string(),
e: "AQAB".to_string(),
n: "-GuAIboTsRYNprJQOkdmuKXRx8ARnKXOC9Pajg4KxHHPt3OY8rXRmVeDxTj1-m9TfW6V-wJa_8ncBbbFE-aV-eBi_XeuIToBBvLZp1-UPIjitS8WCDrUhHiJnbvkIZf1B1YBIq_Ua81fzxhtjQ0jDftV2m5aavmJG4_94VG3Md7noQjjUKzxJyUNl4v_joMA6pIRCeeamvfIZorjcR4wVf-wR8NiZjjRbcjKBpc7ztc7Gm778h34RSe9-DLH6uicTROSYNa99pUwhn3XVfAv4hTFpLIcgHYadLZjsHfUvivr76uiYbxDZx6UTkK5jmi51b87u1b6iYmijDIMztzrIQ".to_string(),
alg: "RS256".to_string(),
});
};
let modulus = Base64UrlUnpadded::decode_vec(&content.n).unwrap();
assert_eq!(
zklogin_inputs
.calculate_all_inputs_hash(&eph_pubkey, &modulus, 10)
.unwrap(),
vec![Bn254Fr::from_str(
"731385750760775862842838160347366432653065169777359995738835424407706939501"
)
.unwrap()]
);

map.insert(
(
"5931701331165f07f550ac5f0194942d54f9c249".to_string(),
OIDCProvider::Facebook.get_config().0.to_string(),
),
content,
);
let res = verify_zk_login(&zklogin_inputs, 10, &eph_pubkey, &map, &ZkLoginEnv::Test);
assert!(res.is_ok());

Expand Down
8 changes: 4 additions & 4 deletions fastcrypto-zkp/src/bn254/zk_login_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@ fn global_pvk() -> PreparedVerifyingKey<Bn254> {
let mut vk_gamma_abc_g1 = Vec::new();
for e in vec![
vec![
"10650235292452276702815258020174876822554680558613093350826598743737711706082"
"7867381425847202568112484563431973323103411930691887303954018406238548242435"
.to_string(),
"10904000006666353404839309737175457841172416892262756319513121366464849299934"
"9248741518501530047280522988482444540196070811288498251337804330766153222468"
.to_string(),
"1".to_string(),
],
vec![
"13523860369377817188474813326919511067573805860184371020956327842962539802962"
"6921103582886817463237640768843495630434715149818209746147837519636936148422"
.to_string(),
"15924113522601648253933515938165772453615741568509559656790523323812357588202"
"322734211400980047302715221807873863996954295847288894748430574151699272036"
.to_string(),
"1".to_string(),
],
Expand Down