Skip to content

Commit

Permalink
hardcoded test works
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 26, 2023
1 parent 6af8f16 commit 4180a2b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 30 deletions.
Binary file modified rln/resources/tree_height_20/rln_final.zkey
Binary file not shown.
8 changes: 4 additions & 4 deletions rln/resources/tree_height_20/verification_key.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
],
"vk_delta_2": [
[
"2523581523345602438870592273365560551114006786853353630648371815743722144008",
"18201825774727718770391031007318017629555656373887006469663259302300143440513"
"6832692771504397957590360708759281700999118750809760982970136159661838520508",
"13468886275581916448704001206191042468244128660792047667708868998914217226607"
],
[
"5555492782190641081486842663439173269556909097391063082057561395406903046573",
"6213830102935228858407835261748786097510923072127639422826994862320478810789"
"14994740931449791291635382287837232766460012379707446516704013722977810183046",
"9922411474443524470273139351326464261786154815141265104663214568321167583711"
],
[
"1",
Expand Down
42 changes: 21 additions & 21 deletions rln/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result<Vec<u8>> {
let mut serialized: Vec<u8> = Vec::new();

serialized.append(&mut fr_to_bytes_le(&rln_witness.identity_secret));
serialized.append(&mut fr_to_bytes_le(&rln_witness.user_message_limit));
serialized.append(&mut fr_to_bytes_le(&rln_witness.message_id));
serialized.append(&mut vec_fr_to_bytes_le(&rln_witness.path_elements)?);
serialized.append(&mut vec_u8_to_bytes_le(&rln_witness.identity_path_index)?);
serialized.append(&mut fr_to_bytes_le(&rln_witness.x));
serialized.append(&mut fr_to_bytes_le(&rln_witness.external_nullifier));
serialized.append(&mut fr_to_bytes_le(&rln_witness.user_message_limit));
serialized.append(&mut fr_to_bytes_le(&rln_witness.message_id));

Ok(serialized)
}
Expand All @@ -122,25 +122,25 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)
let (identity_secret, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (path_elements, read) = bytes_le_to_vec_fr(&serialized[all_read..])?;
let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (identity_path_index, read) = bytes_le_to_vec_u8(&serialized[all_read..])?;
let (message_id, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (x, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;
message_id_range_check(&message_id, &user_message_limit)?;

let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]);
let (path_elements, read) = bytes_le_to_vec_fr(&serialized[all_read..])?;
all_read += read;

let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]);
let (identity_path_index, read) = bytes_le_to_vec_u8(&serialized[all_read..])?;
all_read += read;

let (message_id, read) = bytes_le_to_fr(&serialized[all_read..]);
let (x, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

message_id_range_check(&message_id, &user_message_limit)?;
let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

if serialized.len() != all_read {
return Err(Report::msg("serialized length is not equal to all_read"));
Expand All @@ -162,7 +162,7 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)

// This function deserializes input for kilic's rln generate_proof public API
// https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L148
// input_data is [ identity_secret<32> | id_index<8> | epoch<32> | rln_identifier<32> | user_message_limit<32> | user_message_id<32> | signal_len<8> | signal<var> ]
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | signal_len<8> | signal<var> ]
// return value is a rln witness populated according to this information
pub fn proof_inputs_to_rln_witness(
tree: &mut PoseidonTree,
Expand All @@ -178,15 +178,15 @@ pub fn proof_inputs_to_rln_witness(
))?;
all_read += 8;

let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (message_id, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]);
all_read += read;

let signal_len = usize::try_from(u64::from_le_bytes(
serialized[all_read..all_read + 8].try_into()?,
))?;
Expand Down Expand Up @@ -661,18 +661,18 @@ pub fn inputs_for_witness_calculation(
"identitySecret",
vec![to_bigint(&rln_witness.identity_secret)?],
),
(
"userMessageLimit",
vec![to_bigint(&rln_witness.user_message_limit)?],
),
("messageId", vec![to_bigint(&rln_witness.message_id)?]),
("pathElements", path_elements),
("identityPathIndex", identity_path_index),
("x", vec![to_bigint(&rln_witness.x)?]),
(
"externalNullifier",
vec![to_bigint(&rln_witness.external_nullifier)?],
),
(
"userMessageLimit",
vec![to_bigint(&rln_witness.user_message_limit)?],
),
("messageId", vec![to_bigint(&rln_witness.message_id)?]),
])
}

Expand Down Expand Up @@ -797,12 +797,12 @@ pub fn get_json_inputs(rln_witness: &RLNWitnessInput) -> Result<serde_json::Valu

let inputs = serde_json::json!({
"identitySecret": to_bigint(&rln_witness.identity_secret)?.to_str_radix(10),
"userMessageLimit": to_bigint(&rln_witness.user_message_limit)?.to_str_radix(10),
"messageId": to_bigint(&rln_witness.message_id)?.to_str_radix(10),
"pathElements": path_elements,
"identityPathIndex": identity_path_index,
"x": to_bigint(&rln_witness.x)?.to_str_radix(10),
"externalNullifier": to_bigint(&rln_witness.external_nullifier)?.to_str_radix(10),
"userMessageLimit": to_bigint(&rln_witness.user_message_limit)?.to_str_radix(10),
"messageId": to_bigint(&rln_witness.message_id)?.to_str_radix(10),
});

Ok(inputs)
Expand Down
129 changes: 124 additions & 5 deletions rln/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,123 @@ mod test {
assert_eq!(root_empty, root_after_bad_set);
}

fn fq_from_str(s: String) -> ark_bn254::Fq {
ark_bn254::Fq::from_str(&s).unwrap()
}

fn g1_from_str(g1: &[String]) -> ark_bn254::G1Affine {
let x = fq_from_str(g1[0].clone());
let y = fq_from_str(g1[1].clone());
let z = fq_from_str(g1[2].clone());
ark_bn254::G1Affine::from(ark_bn254::G1Projective::new(x, y, z))
}

fn g2_from_str(g2: &[Vec<String>]) -> ark_bn254::G2Affine {
let c0 = fq_from_str(g2[0][0].clone());
let c1 = fq_from_str(g2[0][1].clone());
let x = ark_bn254::Fq2::new(c0, c1);

let c0 = fq_from_str(g2[1][0].clone());
let c1 = fq_from_str(g2[1][1].clone());
let y = ark_bn254::Fq2::new(c0, c1);

let c0 = fq_from_str(g2[2][0].clone());
let c1 = fq_from_str(g2[2][1].clone());
let z = ark_bn254::Fq2::new(c0, c1);

ark_bn254::G2Affine::from(ark_bn254::G2Projective::new(x, y, z))
}

fn value_to_string_vec(value: &Value) -> Vec<String> {
value
.as_array()
.unwrap()
.into_iter()
.map(|val| val.as_str().unwrap().to_string())
.collect()
}

#[test]
fn test_groth16_proof_hardcoded() {
let tree_height = TEST_TREE_HEIGHT;

let input_buffer =
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
let rln = RLN::new(tree_height, input_buffer).unwrap();

let valid_snarkjs_proof = json!({
"pi_a": [
"4470527391588441860193200161084455226340491373346283552408949960585113822665",
"17066173901974939377117728437830216011764222423156801199965800887938087190121",
"1"
],
"pi_b": [
[
"15972566464269406830925988363875489807783626890329856187767783138745533264635",
"14937519511565349355063307001263881540320664095110809840110097755110649950560"
],
[
"533488241215365262498062426054646750918758165791898421060280269581011723961",
"9035874337973494769294028746597715861635666159729389919309920308765805688602"
],
[
"1",
"0"
]
],
"pi_c": [
"20620241453393708332486848754039748595639801912969370960546027260091108922454",
"20580342189093698831710267260567759683930279312746044733195059538431965163807",
"1"
],
"protocol": "groth16",
"curve": "bn128"
});
let valid_ark_proof = ArkProof {
a: g1_from_str(&value_to_string_vec(&valid_snarkjs_proof["pi_a"])),
b: g2_from_str(
&valid_snarkjs_proof["pi_b"]
.as_array()
.unwrap()
.iter()
.map(|item| value_to_string_vec(item))
.collect::<Vec<Vec<String>>>(),
),
c: g1_from_str(&value_to_string_vec(&valid_snarkjs_proof["pi_c"])),
};

let valid_proof_values = RLNProofValues {
x: str_to_fr(
"16401008481486069296141645075505218976370369489687327284155463920202585288271",
10,
)
.unwrap(),
external_nullifier: str_to_fr(
"8502402278351299594663821509741133196466235670407051417832304486953898514733",
10,
)
.unwrap(),
y: str_to_fr(
"9102791780887227194595604713537772536258726662792598131262022534710887343694",
10,
)
.unwrap(),
root: str_to_fr(
"20645213238265527935869146898028115621427162613172918400241870500502509785943",
10,
)
.unwrap(),
nullifier: str_to_fr(
"21074405743803627666274838159589343934394162804826017440941339048886754734203",
10,
)
.unwrap(),
};

let verified = verify_proof(&rln.verification_key, &valid_ark_proof, &valid_proof_values);
assert!(verified.unwrap());
}

#[test]
// This test is similar to the one in lib, but uses only public API
fn test_groth16_proof() {
Expand Down Expand Up @@ -1706,12 +1823,12 @@ mod test {
let mut serialized: Vec<u8> = Vec::new();
serialized.append(&mut fr_to_bytes_le(&identity_secret_hash));
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut fr_to_bytes_le(&utils_poseidon_hash(&[
epoch,
rln_identifier,
])));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut normalize_usize(signal.len()));
serialized.append(&mut signal.to_vec());

Expand Down Expand Up @@ -1780,8 +1897,10 @@ mod test {
let mut serialized: Vec<u8> = Vec::new();
serialized.append(&mut fr_to_bytes_le(&identity_secret_hash));
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&epoch));
serialized.append(&mut fr_to_bytes_le(&rln_identifier));
serialized.append(&mut fr_to_bytes_le(&utils_poseidon_hash(&[
epoch,
rln_identifier,
])));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut normalize_usize(signal.len()));
Expand Down Expand Up @@ -1885,9 +2004,9 @@ mod test {
let mut serialized: Vec<u8> = Vec::new();
serialized.append(&mut fr_to_bytes_le(&identity_secret_hash));
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&external_nullifier));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut fr_to_bytes_le(&external_nullifier));
serialized.append(&mut normalize_usize(signal.len()));
serialized.append(&mut signal.to_vec());

Expand Down

0 comments on commit 4180a2b

Please sign in to comment.