Skip to content

Commit afae2d9

Browse files
committed
Refactor ScalarChallenge and how it's used
1 parent e658b96 commit afae2d9

File tree

9 files changed

+49
-92
lines changed

9 files changed

+49
-92
lines changed

ledger/src/proofs/accumulator_check.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::array;
22

3-
use mina_curves::pasta::{Fq, Vesta};
3+
use mina_curves::pasta::Vesta;
44
use mina_hasher::Fp;
55
use mina_p2p_messages::{bigint::BigInt, v2::PicklesProofProofsVerified2ReprStableV2};
66
use poly_commitment::{commitment::CommitmentCurve, srs::SRS};
77

88
use super::public_input::scalar_challenge::ScalarChallenge;
9-
use super::transaction::endos;
109
use super::urs_utils;
1110

1211
pub fn accumulator_check(
@@ -18,8 +17,6 @@ pub fn accumulator_check(
1817
// comms: statement.proof_state.messages_for_next_wrap_proof.challenge_polynomial_commitment
1918
// chals: statement.proof_state.deferred_values.bulletproof_challenges
2019

21-
let (_, endo) = endos::<Fq>();
22-
2320
let deferred_values = &proof.statement.proof_state.deferred_values;
2421
let bulletproof_challenges = &deferred_values.bulletproof_challenges;
2522
let bulletproof_challenges: Vec<Fp> = bulletproof_challenges
@@ -28,11 +25,11 @@ pub fn accumulator_check(
2825
let prechallenge = &chal.prechallenge.inner;
2926
let prechallenge: [u64; 2] = array::from_fn(|k| prechallenge[k].as_u64());
3027

31-
ScalarChallenge::from(prechallenge).to_field(&endo)
28+
ScalarChallenge::limbs_to_field(&prechallenge)
3229
})
3330
.collect();
3431

35-
let of_coord = |x: &(BigInt, BigInt)| Vesta::of_coordinates(x.0.to_field(), x.1.to_field());
32+
let of_coord = |(x, y): &(BigInt, BigInt)| Vesta::of_coordinates(x.to_field(), y.to_field());
3633

3734
// statement.proof_state.messages_for_next_wrap_proof.challenge_polynomial_commitment
3835
let acc_comm = &proof

ledger/src/proofs/prover.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use mina_hasher::Fp;
1111
use once_cell::sync::Lazy;
1212
use poly_commitment::{commitment::CommitmentCurve, evaluation_proof::OpeningProof};
1313

14-
use crate::proofs::transaction::endos;
15-
1614
use super::util::extract_bulletproof;
1715
use mina_curves::pasta::Fq;
1816
use mina_p2p_messages::{bigint::BigInt, v2::PicklesProofProofsVerified2ReprStableV2};
@@ -115,19 +113,14 @@ pub fn make_padded_proof_from_p2p(
115113

116114
let ft_eval1: Fq = proof.ft_eval1.to_field();
117115

118-
let (_, endo) = endos::<Fp>();
119-
120116
let old_bulletproof_challenges = &statement
121117
.proof_state
122118
.messages_for_next_wrap_proof
123119
.old_bulletproof_challenges;
124-
let old_bulletproof_challenges: Vec<[Fq; 15]> = extract_bulletproof(
125-
&[
126-
old_bulletproof_challenges.0[0].0.clone(),
127-
old_bulletproof_challenges.0[1].0.clone(),
128-
],
129-
&endo,
130-
);
120+
let old_bulletproof_challenges: Vec<[Fq; 15]> = extract_bulletproof(&[
121+
old_bulletproof_challenges.0[0].0.clone(),
122+
old_bulletproof_challenges.0[1].0.clone(),
123+
]);
131124

132125
let make_poly = |poly: &(BigInt, BigInt)| {
133126
let point = of_coord(poly);

ledger/src/proofs/public_input/scalar_challenge.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::array::IntoIter;
22

33
use ark_ff::{BigInteger256, Field};
44

5-
use crate::proofs::field::FieldWitness;
5+
use crate::proofs::{field::FieldWitness, transaction::endos};
66

77
#[derive(Clone, Debug)]
88
pub struct ScalarChallenge {
@@ -92,6 +92,16 @@ impl ScalarChallenge {
9292

9393
(a * endo) + b
9494
}
95+
96+
pub fn array_to_fields<F: FieldWitness, const N: usize>(array: &[F; N]) -> [F; N] {
97+
let (_, endo) = endos::<F::Scalar>();
98+
std::array::from_fn(|i| Self::from(array[i]).to_field(&endo))
99+
}
100+
101+
pub fn limbs_to_field<F: FieldWitness>(limbs: &[u64; 2]) -> F {
102+
let (_, endo) = endos::<F::Scalar>();
103+
Self::from(*limbs).to_field(&endo)
104+
}
95105
}
96106

97107
#[cfg(test)]

ledger/src/proofs/step.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,8 +1820,8 @@ pub fn expand_deferred(params: ExpandDeferredParams) -> DeferredValues<Fp> {
18201820

18211821
let plonk0 = &proof_state.deferred_values.plonk;
18221822

1823-
let zeta = ScalarChallenge::from(plonk0.zeta_bytes).to_field(&endo);
1824-
let alpha = ScalarChallenge::from(plonk0.alpha_bytes).to_field(&endo);
1823+
let zeta = ScalarChallenge::limbs_to_field(&plonk0.zeta_bytes);
1824+
let alpha = ScalarChallenge::limbs_to_field(&plonk0.alpha_bytes);
18251825
let step_domain: u8 = proof_state.deferred_values.branch_data.domain_log2.as_u8();
18261826
let domain: Radix2EvaluationDomain<Fp> =
18271827
Radix2EvaluationDomain::new(1 << step_domain as u64).unwrap();
@@ -1877,7 +1877,7 @@ pub fn expand_deferred(params: ExpandDeferredParams) -> DeferredValues<Fp> {
18771877

18781878
let old_bulletproof_challenges: Vec<_> = old_bulletproof_challenges
18791879
.iter()
1880-
.map(|v| std::array::from_fn(|i| ScalarChallenge::from(v[i]).to_field(&endo)))
1880+
.map(ScalarChallenge::array_to_fields)
18811881
.collect();
18821882

18831883
let challenges_digest = {
@@ -1926,7 +1926,7 @@ pub fn expand_deferred(params: ExpandDeferredParams) -> DeferredValues<Fp> {
19261926
.deferred_values
19271927
.bulletproof_challenges
19281928
.iter()
1929-
.map(|v| ScalarChallenge::from(*v).to_field(&endo))
1929+
.map(ScalarChallenge::limbs_to_field)
19301930
.collect();
19311931

19321932
let b_actual = {
@@ -1950,11 +1950,9 @@ pub fn expand_deferred(params: ExpandDeferredParams) -> DeferredValues<Fp> {
19501950
fn wrap_compute_sg(challenges: &[[u64; 2]]) -> GroupAffine<Fp> {
19511951
use super::public_input::scalar_challenge::ScalarChallenge;
19521952

1953-
let (_, endo) = endos::<Fp>();
1954-
19551953
let challenges = challenges
19561954
.iter()
1957-
.map(|c| ScalarChallenge::from(*c).to_field(&endo))
1955+
.map(ScalarChallenge::limbs_to_field)
19581956
.collect::<Vec<_>>();
19591957

19601958
let coeffs = b_poly_coefficients(&challenges);
@@ -2004,9 +2002,8 @@ fn expand_proof(params: ExpandProofParams) -> ExpandedProof {
20042002
.domain_log2
20052003
.as_u8();
20062004

2007-
let (_, endo) = endos::<Fq>();
2008-
let alpha = ScalarChallenge::from(plonk0.alpha_bytes).to_field(&endo);
2009-
let zeta = ScalarChallenge::from(plonk0.zeta_bytes).to_field(&endo);
2005+
let alpha = ScalarChallenge::limbs_to_field(&plonk0.alpha_bytes);
2006+
let zeta = ScalarChallenge::limbs_to_field(&plonk0.zeta_bytes);
20102007
// let w: Fp = Radix2EvaluationDomain::new(1 << dlog_vk.domain.log_size_of_group)
20112008
let w: Fp = Radix2EvaluationDomain::new(1 << domain).unwrap().group_gen;
20122009
let zetaw = zeta * w;
@@ -2037,19 +2034,14 @@ fn expand_proof(params: ExpandProofParams) -> ExpandedProof {
20372034
let statement = &t.statement;
20382035

20392036
let prev_challenges: Vec<[Fq; BACKEND_TOCK_ROUNDS_N]> = {
2040-
let (_, endo) = endos::<Fp>();
2041-
20422037
let old_bulletproof_challenges = &statement
20432038
.proof_state
20442039
.messages_for_next_wrap_proof
20452040
.old_bulletproof_challenges;
2046-
extract_bulletproof(
2047-
&[
2048-
old_bulletproof_challenges.0[0].0.clone(),
2049-
old_bulletproof_challenges.0[1].0.clone(),
2050-
],
2051-
&endo,
2052-
)
2041+
extract_bulletproof(&[
2042+
old_bulletproof_challenges.0[0].0.clone(),
2043+
old_bulletproof_challenges.0[1].0.clone(),
2044+
])
20532045
};
20542046

20552047
let old_bulletproof_challenges: Vec<[Fp; 16]> = statement
@@ -2073,10 +2065,9 @@ fn expand_proof(params: ExpandProofParams) -> ExpandedProof {
20732065
})
20742066
};
20752067

2076-
let (_, endo) = endos::<Fq>();
20772068
let old_bulletproof_challenges: Vec<_> = old_bulletproof_challenges
20782069
.iter()
2079-
.map(|v| std::array::from_fn(|i| ScalarChallenge::from(v[i]).to_field(&endo)))
2070+
.map(ScalarChallenge::array_to_fields)
20802071
.collect();
20812072

20822073
let messages_for_next_step_proof = MessagesForNextStepProof {
@@ -2486,8 +2477,6 @@ pub fn extract_recursion_challenges<const N: usize>(
24862477
) -> Vec<RecursionChallenge<GroupAffine<Fq>>> {
24872478
use poly_commitment::PolyComm;
24882479

2489-
let (_, endo) = endos::<Fq>();
2490-
24912480
let comms: [(Fq, Fq); N] = std::array::from_fn(|i| {
24922481
let p = &proofs[i];
24932482
let (a, b) = &p
@@ -2508,7 +2497,7 @@ pub fn extract_recursion_challenges<const N: usize>(
25082497
.clone()
25092498
})
25102499
.collect::<Vec<_>>();
2511-
let challs = extract_bulletproof(&challs, &endo);
2500+
let challs = extract_bulletproof(&challs);
25122501

25132502
challs
25142503
.into_iter()

ledger/src/proofs/unfinalized.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use mina_hasher::Fp;
44
use mina_p2p_messages::v2;
55

66
use crate::proofs::{
7-
field::FieldWitness, public_input::plonk_checks::derive_plonk, transaction::endos,
8-
verification::make_scalars_env, BACKEND_TICK_ROUNDS_N,
7+
field::FieldWitness, public_input::plonk_checks::derive_plonk, verification::make_scalars_env,
8+
BACKEND_TICK_ROUNDS_N,
99
};
1010

1111
use super::{
@@ -288,9 +288,7 @@ pub fn dummy_ipa_step_challenges() -> [[u64; 2]; BACKEND_TICK_ROUNDS_N] {
288288
pub fn dummy_ipa_step_challenges_computed() -> [Fp; BACKEND_TICK_ROUNDS_N] {
289289
cache_one!([Fp; BACKEND_TICK_ROUNDS_N], {
290290
let challenges = dummy_ipa_step_challenges();
291-
let (_, endo) = endos::<Fq>();
292-
293-
std::array::from_fn(|i| ScalarChallenge::from(challenges[i]).to_field(&endo))
291+
std::array::from_fn(|i| ScalarChallenge::limbs_to_field(&challenges[i]))
294292
})
295293
}
296294

@@ -303,10 +301,8 @@ impl Unfinalized {
303301
let gamma_bytes: [u64; 2] = [8902445049614368905, -5479804816757020655i64 as u64];
304302
let zeta_bytes: [u64; 2] = [621834770194220300, -4327941673388439925i64 as u64];
305303

306-
let (_, endo) = endos::<Fp>();
307-
308-
let zeta: Fq = ScalarChallenge::from(zeta_bytes).to_field(&endo);
309-
let alpha: Fq = ScalarChallenge::from(alpha_bytes).to_field(&endo);
304+
let zeta: Fq = ScalarChallenge::limbs_to_field(&zeta_bytes);
305+
let alpha: Fq = ScalarChallenge::limbs_to_field(&alpha_bytes);
310306
let beta: Fq = u64_to_field(&beta_bytes);
311307
let gamma: Fq = u64_to_field(&gamma_bytes);
312308

ledger/src/proofs/util.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@ pub fn extract_polynomial_commitment<F: FieldWitness>(
2626
.collect()
2727
}
2828

29-
pub fn extract_bulletproof<F: Field + From<i32>, const N: usize>(
29+
pub fn extract_bulletproof<F: FieldWitness, const N: usize>(
3030
v: &[PaddedSeq<
3131
PicklesReducedMessagesForNextProofOverSameFieldWrapChallengesVectorStableV2A,
3232
N,
3333
>],
34-
endo: &F,
3534
) -> Vec<[F; N]> {
3635
v.iter()
3736
.map(|old| {
3837
array::from_fn(|j| {
3938
let prechallenge = &old[j].prechallenge.inner;
4039
let prechallenge: [u64; 2] = array::from_fn(|k| prechallenge[k].as_u64());
41-
ScalarChallenge::from(prechallenge).to_field(endo)
40+
ScalarChallenge::limbs_to_field(&prechallenge)
4241
})
4342
})
4443
.collect()

ledger/src/proofs/verification.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
proofs::{
88
accumulator_check,
99
step::{expand_deferred, StatementProofState},
10-
transaction::endos,
1110
unfinalized::AllEvals,
1211
verifier_index::make_zkapp_verifier_index,
1312
wrap::Domain,
@@ -346,12 +345,9 @@ where
346345
old_bulletproof_challenges,
347346
} = messages_for_next_step_proof;
348347

349-
let (_, endo) = endos::<Fq>();
350-
351348
let challenge_polynomial_commitments: Vec<InnerCurve<Fp>> =
352349
extract_polynomial_commitment(challenge_polynomial_commitments);
353-
let old_bulletproof_challenges: Vec<[Fp; 16]> =
354-
extract_bulletproof(old_bulletproof_challenges, &endo);
350+
let old_bulletproof_challenges: Vec<[Fp; 16]> = extract_bulletproof(old_bulletproof_challenges);
355351
let dlog_plonk_index = commitments;
356352

357353
MessagesForNextStepProof {
@@ -371,15 +367,10 @@ fn get_message_for_next_wrap_proof(
371367
let challenge_polynomial_commitments: Vec<InnerCurve<Fq>> =
372368
extract_polynomial_commitment(&[challenge_polynomial_commitment.clone()]);
373369

374-
let (_, endo) = endos::<Fp>();
375-
376-
let old_bulletproof_challenges: Vec<[Fq; 15]> = extract_bulletproof(
377-
&[
378-
old_bulletproof_challenges[0].0.clone(),
379-
old_bulletproof_challenges[1].0.clone(),
380-
],
381-
&endo,
382-
);
370+
let old_bulletproof_challenges: Vec<[Fq; 15]> = extract_bulletproof(&[
371+
old_bulletproof_challenges[0].0.clone(),
372+
old_bulletproof_challenges[1].0.clone(),
373+
]);
383374

384375
MessagesForNextWrapProof {
385376
challenge_polynomial_commitment: challenge_polynomial_commitments[0].clone(),

ledger/src/proofs/verifier_index.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ use super::{
3232
wrap::{Domain, Domains},
3333
};
3434

35-
const PERMUTS: usize = 7;
36-
const COLUMNS: usize = 15;
37-
3835
pub enum VerifierKind {
3936
Blockchain,
4037
Transaction,

ledger/src/proofs/wrap.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ pub struct WrapParams<'a> {
548548
}
549549

550550
pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness<Fq>) -> WrapProof {
551+
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
552+
551553
let WrapParams {
552554
app_state,
553555
proof,
@@ -566,8 +568,6 @@ pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness
566568
wrap_domain_indices,
567569
} = C::wrap_data();
568570

569-
let (_, endo) = endos::<Fq>();
570-
571571
let messages_for_next_step_proof_hash = crate::proofs::transaction::MessagesForNextStepProof {
572572
app_state,
573573
challenge_polynomial_commitments: step_statement
@@ -580,10 +580,7 @@ pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness
580580
.messages_for_next_step_proof
581581
.old_bulletproof_challenges
582582
.iter()
583-
.map(|v| {
584-
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
585-
std::array::from_fn(|i| ScalarChallenge::from(v[i]).to_field(&endo))
586-
})
583+
.map(ScalarChallenge::array_to_fields)
587584
.collect(),
588585
dlog_plonk_index,
589586
}
@@ -594,15 +591,10 @@ pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness
594591
.iter()
595592
.cloned()
596593
.map(|mut v| {
597-
let (_, endo) = endos::<Fp>();
598-
599594
let old_bulletproof_challenges = v
600595
.old_bulletproof_challenges
601596
.iter()
602-
.map(|v| {
603-
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
604-
std::array::from_fn(|i| ScalarChallenge::from(v[i]).to_field(&endo))
605-
})
597+
.map(ScalarChallenge::array_to_fields)
606598
.collect();
607599
v.old_bulletproof_challenges = old_bulletproof_challenges;
608600
v
@@ -627,10 +619,7 @@ pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness
627619
.messages_for_next_step_proof
628620
.old_bulletproof_challenges
629621
.iter()
630-
.map(|v| {
631-
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
632-
std::array::from_fn(|i| ScalarChallenge::from(v[i]).to_field(&endo))
633-
})
622+
.map(ScalarChallenge::array_to_fields)
634623
.collect();
635624

636625
let actual_proofs_verified = prev_challenges.len();
@@ -666,20 +655,16 @@ pub fn wrap<C: ProofConstants + ForWrapData>(params: WrapParams, w: &mut Witness
666655
};
667656

668657
let messages_for_next_wrap_proof_prepared = {
669-
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
670-
671658
let MessagesForNextWrapProof {
672659
challenge_polynomial_commitment,
673660
old_bulletproof_challenges,
674661
} = &messages_for_next_wrap_proof;
675662

676-
let (_, endo) = endos::<Fp>();
677-
678663
MessagesForNextWrapProof {
679664
challenge_polynomial_commitment: challenge_polynomial_commitment.clone(),
680665
old_bulletproof_challenges: old_bulletproof_challenges
681666
.iter()
682-
.map(|c| c.map(|c| ScalarChallenge::from(c).to_field(&endo)))
667+
.map(ScalarChallenge::array_to_fields)
683668
.collect(),
684669
}
685670
};

0 commit comments

Comments
 (0)