Adapt rln fields etc

Proof works!
This commit is contained in:
Oskar Thoren 2022-03-11 18:07:24 +08:00
parent 224589a1ed
commit 4737dd34c1
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
1 changed files with 75 additions and 80 deletions

View File

@ -19,20 +19,17 @@ use serde::Deserialize;
use serde_json; use serde_json;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] //#[serde(rename_all = "camelCase")]
struct WitnessInput { struct WitnessInput {
root: String, identity_secret: String,
nullifier_hash: String,
recipient: String,
relayer: String,
//fee: String,
fee: i32,
nullifier: String,
path_elements: Vec<String>, path_elements: Vec<String>,
path_indices: Vec<i32>, identity_path_index: Vec<i32>,
x: String,
epoch: String,
rln_identifier: String,
} }
// Poseidon-tornado // RLN
fn groth16_proof_example() -> Result<()> { fn groth16_proof_example() -> Result<()> {
// Tracing to help with debugging // Tracing to help with debugging
@ -45,48 +42,57 @@ fn groth16_proof_example() -> Result<()> {
println!("Trace is: {:?}", trace); println!("Trace is: {:?}", trace);
let cfg = CircomConfig::<Bn254>::new( let cfg = CircomConfig::<Bn254>::new(
"./resources/withdraw.wasm", "./resources/rln.wasm",
"./resources/withdraw.r1cs", "./resources/rln.r1cs",
)?; )?;
// Test // Test
let trace = ConstraintTrace::capture(); let trace = ConstraintTrace::capture();
println!("Trace is: {:?}", trace); println!("Trace is: {:?}", trace);
// From poseidon-tornado JSON witness // From rln JSON witness
// Input generated with https://github.com/oskarth/poseidon-tornado/commit/db64ad09fdb16ad310ba395fc73520f87ad7d344 // Input generated with https://github.com/oskarth/zk-kit/commit/b6a872f7160c7c14e10a0ea40acab99cbb23c9a8
// With nullifier set to 0
let input_json_str = r#" let input_json_str = r#"
{ {
"root": "17777834528943231885798890273562835075271930126129561600279382876922601684948", "identity_secret": "12825549237505733615964533204745049909430608936689388901883576945030025938736",
"nullifierHash": "8506691148847834795277894036216352001616813487121834991716343668271924769133", "path_elements": [
"recipient": "344073830386746567427978432078835137280280269756", "18622655742232062119094611065896226799484910997537830749762961454045300666333",
"relayer": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", "20590447254980891299813706518821659736846425329007960381537122689749540452732",
"fee": 0, "7423237065226347324353380772367382631490014989348495481811164164159255474657",
"nullifier": "0", "11286972368698509976183087595462810875513684078608517520839298933882497716792",
"pathElements": [ "3607627140608796879659380071776844901612302623152076817094415224584923813162",
"21663839004416932945382355908790599225266501822907911457504978515578255421292", "19712377064642672829441595136074946683621277828620209496774504837737984048981",
"8995896153219992062710898675021891003404871425075198597897889079729967997688", "20775607673010627194014556968476266066927294572720319469184847051418138353016",
"15126246733515326086631621937388047923581111613947275249184377560170833782629", "3396914609616007258851405644437304192397291162432396347162513310381425243293",
"6404200169958188928270149728908101781856690902670925316782889389790091378414", "21551820661461729022865262380882070649935529853313286572328683688269863701601",
"17903822129909817717122288064678017104411031693253675943446999432073303897479", "6573136701248752079028194407151022595060682063033565181951145966236778420039",
"11423673436710698439362231088473903829893023095386581732682931796661338615804", "12413880268183407374852357075976609371175688755676981206018884971008854919922",
"10494842461667482273766668782207799332467432901404302674544629280016211342367", "14271763308400718165336499097156975241954733520325982997864342600795471836726",
"17400501067905286947724900644309270241576392716005448085614420258732805558809", "20066985985293572387227381049700832219069292839614107140851619262827735677018",
"7924095784194248701091699324325620647610183513781643345297447650838438175245", "9394776414966240069580838672673694685292165040808226440647796406499139370960",
"3170907381568164996048434627595073437765146540390351066869729445199396390350", "11331146992410411304059858900317123658895005918277453009197229807340014528524"
"21224698076141654110749227566074000819685780865045032659353546489395159395031", ],
"18113275293366123216771546175954550524914431153457717566389477633419482708807", "identity_path_index": [
"1952712013602708178570747052202251655221844679392349715649271315658568301659", 1,
"18071586466641072671725723167170872238457150900980957071031663421538421560166", 1,
"9993139859464142980356243228522899168680191731482953959604385644693217291503", 0,
"14825089209834329031146290681677780462512538924857394026404638992248153156554", 0,
"4227387664466178643628175945231814400524887119677268757709033164980107894508", 0,
"177945332589823419436506514313470826662740485666603469953512016396504401819", 0,
"4236715569920417171293504597566056255435509785944924295068274306682611080863", 0,
"8055374341341620501424923482910636721817757020788836089492629714380498049891" 0,
], 0,
"pathIndices": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} 0,
0,
0,
0,
0,
0
],
"x": "8143228284048792769012135629627737459844825626241842423967352803501040982",
"epoch": "0x0000005b612540fc986b42322f8cb91c2273afad58ed006fdba0c97b4b16b12f",
"rln_identifier": "11412926387081627876309792396682864042420635853496105400039841573530884328439"
}
"#; "#;
let witness_input : WitnessInput = serde_json::from_str(input_json_str).expect("JSON was not well-formatted"); let witness_input : WitnessInput = serde_json::from_str(input_json_str).expect("JSON was not well-formatted");
@ -96,52 +102,41 @@ fn groth16_proof_example() -> Result<()> {
let mut builder = CircomBuilder::new(cfg); let mut builder = CircomBuilder::new(cfg);
builder.push_input( builder.push_input(
"root", "identity_secret",
BigInt::parse_bytes(witness_input.root.as_bytes(), 10).unwrap(), BigInt::parse_bytes(witness_input.identity_secret.as_bytes(), 10).unwrap(),
);
for v in witness_input.path_elements.iter() {
builder.push_input(
"path_elements",
BigInt::parse_bytes(v.as_bytes(), 10).unwrap(),
);
}
for v in witness_input.identity_path_index.iter() {
builder.push_input("identity_path_index", BigInt::from(*v));
}
builder.push_input(
"x",
BigInt::parse_bytes(witness_input.x.as_bytes(), 10).unwrap(),
); );
builder.push_input( builder.push_input(
"nullifierHash", "epoch",
BigInt::parse_bytes(witness_input.nullifier_hash.as_bytes(), 10).unwrap(),
);
builder.push_input(
"recipient",
BigInt::parse_bytes(witness_input.recipient.as_bytes(), 10).unwrap(),
);
builder.push_input(
"relayer",
BigInt::parse_bytes( BigInt::parse_bytes(
witness_input.relayer.strip_prefix("0x").unwrap().as_bytes(), witness_input.epoch.strip_prefix("0x").unwrap().as_bytes(),
16, 16,
) )
.unwrap(), .unwrap(),
); );
// XXX
builder.push_input( builder.push_input(
"fee", "rln_identifier",
witness_input.fee BigInt::parse_bytes(witness_input.rln_identifier.as_bytes(), 10).unwrap(),
//BigInt::parse_bytes(witness_input.fee.as_bytes(), 10).unwrap(),
); );
builder.push_input(
"nullifier",
BigInt::parse_bytes(witness_input.nullifier.as_bytes(), 10).unwrap(),
);
for v in witness_input.path_elements.iter() {
builder.push_input(
"pathElements",
BigInt::parse_bytes(v.as_bytes(), 10).unwrap(),
);
}
for v in witness_input.path_indices.iter() {
builder.push_input("pathIndices", BigInt::from(*v));
}
println!("Builder input:\n {:#?}", builder.inputs); println!("Builder input:\n {:#?}", builder.inputs);
// create an empty instance for setting it up // create an empty instance for setting it up
@ -170,7 +165,7 @@ fn groth16_proof_example() -> Result<()> {
} }
fn main() { fn main() {
println!("tornado-poseidon example proof"); println!("rln example proof");
// Tornado-core // Tornado-core
match groth16_proof_example() { match groth16_proof_example() {