Skip to content

Commit

Permalink
feat: adding rescue parameters for Bn254 (#343)
Browse files Browse the repository at this point in the history
* constants filled. but failing test

* add tests. fix comments.

* cargo fmt

* fix bug in rescue (#344)

* fix round keys and add test

* CHANGELOG

* fix clippy error

* update link to script

* testing Fq254 for commitment and prf

---------

Co-authored-by: zhenfei <[email protected]>
  • Loading branch information
mrain and zhenfeizhang authored Jul 20, 2023
1 parent 6210b1f commit 072d5d1
Show file tree
Hide file tree
Showing 9 changed files with 1,399 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and follow [semantic versioning](https://semver.org/) for our releases.
- [#320](https://github.com/EspressoSystems/jellyfish/pull/320) Non-native elliptic curve addition in short Weierstrass form
- [#337](https://github.com/EspressoSystems/jellyfish/pull/337) Port VID from another repo
- [#341](https://github.com/EspressoSystems/jellyfish/pull/341) Port VDF from another repo
- [#343](https://github.com/EspressoSystems/jellyfish/pull/343) Rescue parameter for `ark_bn254::Fq`

### Changed

Expand Down
2 changes: 1 addition & 1 deletion plonk/src/circuit/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
Self {
transcript_var: Vec::new(),
state_var: [circuit.zero(); STATE_SIZE],
_phantom: PhantomData::default(),
_phantom: PhantomData,
}
}

Expand Down
2 changes: 2 additions & 0 deletions primitives/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod test {
rescue::{sponge::RescueCRHF, CRHF_RATE},
};
use ark_bls12_377::Fq as Fq377;
use ark_bn254::Fq as Fq254;
use ark_ed_on_bls12_377::Fq as FqEd377;
use ark_ed_on_bls12_381::Fq as FqEd381;
use ark_ed_on_bls12_381_bandersnatch::Fq as FqEd381b;
Expand Down Expand Up @@ -157,5 +158,6 @@ mod test {
test_commit!(FqEd381);
test_commit!(FqEd381b);
test_commit!(Fq377);
test_commit!(Fq254);
}
}
2 changes: 1 addition & 1 deletion primitives/src/pcs/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<F: PrimeField> IOPTranscript<F> {
Self {
transcript: Transcript::new(label),
is_empty: true,
phantom: PhantomData::default(),
phantom: PhantomData,
}
}

Expand Down
2 changes: 2 additions & 0 deletions primitives/src/prf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ mod tests {
rescue::sponge::RescuePRFCore,
};
use ark_bls12_377::Fq as Fq377;
use ark_bn254::Fq as Fq254;
use ark_ed_on_bls12_377::Fq as FqEd377;
use ark_ed_on_bls12_381::Fq as FqEd381;
use ark_ed_on_bn254::Fq as FqEd254;
Expand Down Expand Up @@ -111,5 +112,6 @@ mod tests {
test_prf!(FqEd377);
test_prf!(FqEd381);
test_prf!(Fq377);
test_prf!(Fq254);
}
}
120 changes: 107 additions & 13 deletions primitives/src/rescue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ impl<F: RescueParameter> Permutation<F> {
mod test_prp {
use crate::rescue::{RescueVector, PRP};
use ark_bls12_377::Fq as Fq377;
use ark_bn254::Fq as Fq254;
use ark_ed_on_bls12_377::Fq as Fr377;
use ark_ed_on_bls12_381::Fq as Fr381;
use ark_ed_on_bn254::Fq as Fr254;
Expand Down Expand Up @@ -532,14 +533,58 @@ mod test_prp {
],
];

// this value is cross checked with sage script
// rescue254.Sponge([0,0,0,0], 4)
const OUTPUTFQ254: [[u8; 32]; 4] = [
[
0xC9, 0xAC, 0x42, 0x08, 0x04, 0x18, 0xFC, 0x10, 0xA4, 0x41, 0xF4, 0xF9, 0x06, 0x2B,
0xE2, 0x87, 0xB7, 0x7A, 0xC4, 0x65, 0x8C, 0xE0, 0xE9, 0x78, 0xB0, 0x48, 0x39, 0xB6,
0x96, 0x9B, 0x60, 0x1B,
],
[
0x00, 0xC2, 0xF9, 0x07, 0x54, 0x25, 0xF2, 0xC6, 0x75, 0x21, 0x01, 0x14, 0xF6, 0xD1,
0xAF, 0xE1, 0x0D, 0xD4, 0xFC, 0xEC, 0x15, 0xF3, 0x7D, 0x9A, 0x91, 0x26, 0x51, 0xDE,
0xC8, 0x8A, 0x19, 0x09,
],
[
0x78, 0xDB, 0xB7, 0xEA, 0xD4, 0x35, 0x5E, 0xED, 0xAE, 0x14, 0xD6, 0xB1, 0xE8, 0x0D,
0xB4, 0xA7, 0x29, 0x9B, 0xBA, 0x6F, 0xBD, 0xA4, 0xEB, 0x52, 0x7D, 0xD5, 0x9B, 0x03,
0x17, 0x83, 0x06, 0x1D,
],
[
0x72, 0xC6, 0xB8, 0xF9, 0x9E, 0xF3, 0xDA, 0x20, 0xED, 0x3D, 0xE4, 0x39, 0x87, 0x28,
0xE9, 0x25, 0x0D, 0x8D, 0x57, 0xCE, 0xEE, 0xCA, 0x35, 0xFB, 0x8E, 0x7E, 0xE3, 0x32,
0xDA, 0x03, 0x3F, 0x1B,
],
];

#[test]
fn test_rescue_perm_on_0_vec() {
test_rescue_perm_on_0_vec_fq254();
test_rescue_perm_on_0_vec_254();
test_rescue_perm_on_0_vec_377();
test_rescue_perm_on_0_vec_381();
test_rescue_perm_on_0_vec_761();
}

fn test_rescue_perm_on_0_vec_fq254() {
let rescue = PRP::<Fq254>::default();
let key = RescueVector::zero();
let input = RescueVector::zero();
let expected = RescueVector::from_elems_le_bytes(
&OUTPUTFQ254[0],
&OUTPUTFQ254[1],
&OUTPUTFQ254[2],
&OUTPUTFQ254[3],
);
let real_output = rescue.prp(&key, &input);
let round_keys = rescue.key_schedule(&key);

let real_output_with_round_keys = rescue.prp_with_round_keys(&round_keys, &input);
assert_eq!(real_output, real_output_with_round_keys);
assert_eq!(real_output, expected);
}

fn test_rescue_perm_on_0_vec_254() {
let rescue = PRP::<Fr254>::default();
let key = RescueVector::zero();
Expand Down Expand Up @@ -610,19 +655,26 @@ mod test_prp {
}

// printing vectors as hex bytes little endian
// use ark_ff::{BigInteger, PrimeField};
// use ark_std::{format, println, string::String, vec::Vec};
// #[test]
// fn print(){
// let rescue_hash = RescueBls4::default();
// println!("KeySchedule:");
// let keys = rescue_hash.key_schedule(&RescueBls4Vector::zero());
// for key in keys {
// for elem in key.vec.iter() {
// let str: Vec<String> = elem.into_bigint().to_bytes_le().iter().map(|b|
// format!("0x{:02X},", b)) .collect();
// println!("{:?}", str.join(" "));
// }
// println!("],[");
// }
// fn print() {
// let rescue_hash = PRP::<Fq254>::default();
// println!("KeySchedule:");
// let keys = rescue_hash.key_schedule(&RescueVector::zero());
// println!("[");
// for key in keys {
// for elem in key.vec.iter() {
// let str: Vec<String> = elem
// .into_bigint()
// .to_bytes_le()
// .iter()
// .map(|b| format!("0x{:02X},", b))
// .collect();
// println!("&[{}],", str.join(" "));
// }
// println!("],[");
// }
// }
}

Expand All @@ -633,6 +685,7 @@ mod test_permutation {
Permutation, RescueParameter, RescueVector, PRP,
};
use ark_bls12_377::Fq as Fq377;
use ark_bn254::Fq as Fq254;
use ark_ed_on_bls12_377::Fq as Fr377;
use ark_ed_on_bls12_381::Fq as Fr381;
use ark_ed_on_bn254::Fq as Fr254;
Expand All @@ -641,6 +694,7 @@ mod test_permutation {

#[test]
fn test_round_keys() {
test_round_keys_helper::<Fq254>();
test_round_keys_helper::<Fr254>();
test_round_keys_helper::<Fr377>();
test_round_keys_helper::<Fr381>();
Expand All @@ -657,7 +711,7 @@ mod test_permutation {
// // in a sage friendly format
// for e in keys2.iter() {
// for f in e.vec.iter() {
// ark_std::println!("permutation_round_key.append(0x{})",
// ark_std::println!("permutation_round_key.append({})",
// f.into_bigint()); }
// }
// assert!(false);
Expand Down Expand Up @@ -751,8 +805,34 @@ mod test_permutation {
],
];

// this value is cross checked with sage script
// rescue254.Sponge([0,0,0,0], 4)
const OUTPUTFQ254: [[u8; 32]; 4] = [
[
0xC9, 0xAC, 0x42, 0x08, 0x04, 0x18, 0xFC, 0x10, 0xA4, 0x41, 0xF4, 0xF9, 0x06, 0x2B,
0xE2, 0x87, 0xB7, 0x7A, 0xC4, 0x65, 0x8C, 0xE0, 0xE9, 0x78, 0xB0, 0x48, 0x39, 0xB6,
0x96, 0x9B, 0x60, 0x1B,
],
[
0x00, 0xC2, 0xF9, 0x07, 0x54, 0x25, 0xF2, 0xC6, 0x75, 0x21, 0x01, 0x14, 0xF6, 0xD1,
0xAF, 0xE1, 0x0D, 0xD4, 0xFC, 0xEC, 0x15, 0xF3, 0x7D, 0x9A, 0x91, 0x26, 0x51, 0xDE,
0xC8, 0x8A, 0x19, 0x09,
],
[
0x78, 0xDB, 0xB7, 0xEA, 0xD4, 0x35, 0x5E, 0xED, 0xAE, 0x14, 0xD6, 0xB1, 0xE8, 0x0D,
0xB4, 0xA7, 0x29, 0x9B, 0xBA, 0x6F, 0xBD, 0xA4, 0xEB, 0x52, 0x7D, 0xD5, 0x9B, 0x03,
0x17, 0x83, 0x06, 0x1D,
],
[
0x72, 0xC6, 0xB8, 0xF9, 0x9E, 0xF3, 0xDA, 0x20, 0xED, 0x3D, 0xE4, 0x39, 0x87, 0x28,
0xE9, 0x25, 0x0D, 0x8D, 0x57, 0xCE, 0xEE, 0xCA, 0x35, 0xFB, 0x8E, 0x7E, 0xE3, 0x32,
0xDA, 0x03, 0x3F, 0x1B,
],
];

#[test]
fn test_sponge() {
test_sponge_helper::<Fq254>();
test_sponge_helper::<Fr254>();
test_sponge_helper::<Fr377>();
test_sponge_helper::<Fr381>();
Expand Down Expand Up @@ -785,12 +865,24 @@ mod test_permutation {

#[test]
fn test_rescue_hash_on_0_vec() {
test_rescue_hash_on_0_vec_fq254();
test_rescue_hash_on_0_vec_254();
test_rescue_hash_on_0_vec_377();
test_rescue_hash_on_0_vec_381();
test_rescue_hash_on_0_vec_761()
}

fn test_rescue_hash_on_0_vec_fq254() {
let input = [Fq254::zero(); 3];
let expected = vec![
Fq254::from_le_bytes_mod_order(&OUTPUTFQ254[0]),
Fq254::from_le_bytes_mod_order(&OUTPUTFQ254[1]),
Fq254::from_le_bytes_mod_order(&OUTPUTFQ254[2]),
];
let real_output = RescueCRHF::sponge_no_padding(&input, 3).unwrap();
assert_eq!(real_output, expected);
}

fn test_rescue_hash_on_0_vec_254() {
let input = [Fr254::zero(); 3];
let expected = vec![
Expand Down Expand Up @@ -837,6 +929,7 @@ mod test_permutation {

#[test]
fn test_fsks_no_padding_errors() {
test_fsks_no_padding_errors_helper::<Fq254>();
test_fsks_no_padding_errors_helper::<Fr254>();
test_fsks_no_padding_errors_helper::<Fr377>();
test_fsks_no_padding_errors_helper::<Fr381>();
Expand Down Expand Up @@ -871,6 +964,7 @@ mod test_permutation {

#[test]
fn test_variable_output_sponge_and_fsks() {
test_variable_output_sponge_and_fsks_helper::<Fq254>();
test_variable_output_sponge_and_fsks_helper::<Fr254>();
test_variable_output_sponge_and_fsks_helper::<Fr377>();
test_variable_output_sponge_and_fsks_helper::<Fr381>();
Expand Down
Loading

0 comments on commit 072d5d1

Please sign in to comment.