-
Notifications
You must be signed in to change notification settings - Fork 170
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
Fix schnorr test #158
Merged
jonathanpwang
merged 3 commits into
axiom-crypto:develop-ce
from
justcode740:fix_schnorr_signature
Sep 18, 2023
Merged
Fix schnorr test #158
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#![allow(non_snake_case)] | ||
use crate::halo2_proofs::{ | ||
arithmetic::CurveAffine, | ||
halo2curves::bn256::Fr, | ||
halo2curves::secp256k1::{Fp, Fq, Secp256k1Affine}, | ||
}; | ||
use halo2_base::{halo2_proofs::arithmetic::Field, utils::testing::base_test}; | ||
use crate::secp256k1::{FpChip, FqChip}; | ||
use crate::{ | ||
ecc::{schnorr_signature::schnorr_verify_no_pubkey_check, EccChip}, | ||
fields::FieldChip, | ||
}; | ||
use halo2_base::utils::BigPrimeField; | ||
use halo2_base::utils::fe_to_biguint; | ||
use num_bigint::BigUint; | ||
use halo2_base::gates::RangeChip; | ||
use halo2_base::Context; | ||
use num_integer::Integer; | ||
use rand::rngs::StdRng; | ||
use rand_core::SeedableRng; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use std::io::Write; | ||
use std::{fs, io::BufRead}; | ||
|
||
use super::CircuitParams; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct SchnorrInput { | ||
pub r: Fp, | ||
pub s: Fq, | ||
pub msg_hash: Fq, | ||
pub pk: Secp256k1Affine, | ||
} | ||
|
||
pub fn schnorr_signature_test<F: BigPrimeField>( | ||
ctx: &mut Context<F>, | ||
range: &RangeChip<F>, | ||
params: CircuitParams, | ||
input: SchnorrInput | ||
) -> F { | ||
let fp_chip = FpChip::<F>::new(&range, params.limb_bits, params.num_limbs); | ||
let fq_chip = FqChip::<F>::new(&range, params.limb_bits, params.num_limbs); | ||
|
||
let [m, s] = [input.msg_hash, input.s].map(|x| fq_chip.load_private(ctx, x)); | ||
let r = fp_chip.load_private(ctx, input.r); | ||
|
||
let ecc_chip = EccChip::<F, FpChip<F>>::new(&fp_chip); | ||
let pk = ecc_chip.assign_point(ctx, input.pk); | ||
// test schnorr signature | ||
let res = schnorr_verify_no_pubkey_check::<F, Fp, Fq, Secp256k1Affine>( | ||
&ecc_chip, ctx, pk, r, s, m, 4, 4, | ||
); | ||
*res.value() | ||
} | ||
|
||
pub fn random_schnorr_signature_input(rng: &mut StdRng) -> SchnorrInput { | ||
let sk = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone()); | ||
let pk = Secp256k1Affine::from(Secp256k1Affine::generator() * sk); | ||
let msg_hash = | ||
<Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone()); | ||
|
||
let mut k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone()); | ||
|
||
let mut r_point = | ||
Secp256k1Affine::from(Secp256k1Affine::generator() * k).coordinates().unwrap(); | ||
let mut x: &Fp = r_point.x(); | ||
let mut y: &Fp = r_point.y(); | ||
// make sure R.y is even | ||
while fe_to_biguint(y).mod_floor(&BigUint::from(2u64)) != BigUint::from(0u64) { | ||
k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone()); | ||
r_point = Secp256k1Affine::from(Secp256k1Affine::generator() * k).coordinates().unwrap(); | ||
x = r_point.x(); | ||
y = r_point.y(); | ||
} | ||
|
||
let r = *x; | ||
let s = k + sk * msg_hash; | ||
|
||
SchnorrInput {r, s, msg_hash, pk} | ||
} | ||
|
||
pub fn run_test(input: SchnorrInput) { | ||
let path = "configs/secp256k1/schnorr_circuit.config"; | ||
let params: CircuitParams = serde_json::from_reader( | ||
File::open(path).unwrap_or_else(|e| panic!("{path} does not exist: {e:?}")), | ||
) | ||
.unwrap(); | ||
|
||
let res = base_test() | ||
.k(params.degree) | ||
.lookup_bits(params.lookup_bits) | ||
.run(|ctx, range| schnorr_signature_test(ctx, range, params, input)); | ||
assert_eq!(res, Fr::ONE); | ||
} | ||
|
||
#[test] | ||
fn test_secp256k1_schnorr() { | ||
let mut rng = StdRng::seed_from_u64(0); | ||
let input = random_schnorr_signature_input(&mut rng); | ||
run_test(input); | ||
} | ||
|
||
#[test] | ||
fn bench_secp256k1_schnorr() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut rng = StdRng::from_seed([0u8; 32]); | ||
let config_path = "configs/secp256k1/bench_schnorr.config"; | ||
let bench_params_file = | ||
File::open(config_path).unwrap_or_else(|e| panic!("{config_path} does not exist: {e:?}")); | ||
fs::create_dir_all("results/secp256k1").unwrap(); | ||
fs::create_dir_all("data").unwrap(); | ||
let results_path = "results/secp256k1/schnorr_bench.csv"; | ||
let mut fs_results = File::create(results_path).unwrap(); | ||
writeln!(fs_results, "degree,num_advice,num_lookup,num_fixed,lookup_bits,limb_bits,num_limbs,proof_time,proof_size,verify_time")?; | ||
|
||
let bench_params_reader = BufReader::new(bench_params_file); | ||
for line in bench_params_reader.lines() { | ||
let bench_params: CircuitParams = serde_json::from_str(line.unwrap().as_str()).unwrap(); | ||
let k = bench_params.degree; | ||
println!("---------------------- degree = {k} ------------------------------",); | ||
|
||
let stats = | ||
base_test().k(k).lookup_bits(bench_params.lookup_bits).unusable_rows(20).bench_builder( | ||
random_schnorr_signature_input(&mut rng), | ||
random_schnorr_signature_input(&mut rng), | ||
|pool, range, input| { | ||
schnorr_signature_test(pool.main(), range, bench_params, input); | ||
}, | ||
); | ||
|
||
writeln!( | ||
fs_results, | ||
"{},{},{},{},{},{},{},{:?},{},{:?}", | ||
bench_params.degree, | ||
bench_params.num_advice, | ||
bench_params.num_lookup_advice, | ||
bench_params.num_fixed, | ||
bench_params.lookup_bits, | ||
bench_params.limb_bits, | ||
bench_params.num_limbs, | ||
stats.proof_time.time.elapsed(), | ||
stats.proof_size, | ||
stats.verify_time.time.elapsed() | ||
)?; | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of the random test is to use different seeded Rngs. So you should use the given
rng
but mutate it.Since
rng: &mut StdRng
is already a mutable reference, I think you should replace allrng.clone()
with justrng
.