Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Koukyosyumei committed Dec 29, 2024
1 parent 1e4a1e7 commit 423ac1b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
32 changes: 30 additions & 2 deletions myzkp/src/modules/algebra/curve/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::marker::PhantomData;
use std::ops::{Add, AddAssign, Mul, Neg, Sub};

use num_bigint::BigInt;
use num_traits::{One, Zero};
use num_traits::{One, Signed, Zero};

use crate::modules::algebra::field::Field;
use crate::modules::algebra::field::{Field, FiniteFieldElement, ModulusValue};

pub trait EllipticCurve: Debug + Clone + PartialEq {
fn get_a() -> BigInt;
Expand Down Expand Up @@ -161,11 +161,19 @@ impl<F: Field, E: EllipticCurve> EllipticCurvePoint<F, E> {

pub fn mul_ref<V: Into<BigInt>>(&self, scalar_val: V) -> Self {
let scalar: BigInt = scalar_val.into();
self.mul_ref_bigint(&scalar)
}

pub fn mul_ref_bigint(&self, scalar: &BigInt) -> Self {
if scalar.is_zero() {
// Return the point at infinity for scalar * 0
return EllipticCurvePoint::point_at_infinity();
}

if scalar.is_negative() {
panic!("multiplier should be non-negative");
}

let mut result = EllipticCurvePoint::point_at_infinity();
let mut current = self.clone(); // Start with the current point
let mut scalar_bits = scalar.clone();
Expand Down Expand Up @@ -238,6 +246,26 @@ impl<F: Field, E: EllipticCurve, V: Into<BigInt>> Mul<V> for &EllipticCurvePoint
}
}

impl<F: Field, E: EllipticCurve, M: ModulusValue> Mul<FiniteFieldElement<M>>
for &EllipticCurvePoint<F, E>
{
type Output = EllipticCurvePoint<F, E>;

fn mul(self, field_val: FiniteFieldElement<M>) -> EllipticCurvePoint<F, E> {
self.mul_ref_bigint(&field_val.value)
}
}

impl<'a, F: Field, E: EllipticCurve, M: ModulusValue> Mul<&'a FiniteFieldElement<M>>
for &EllipticCurvePoint<F, E>
{
type Output = EllipticCurvePoint<F, E>;

fn mul(self, field_val: &'a FiniteFieldElement<M>) -> EllipticCurvePoint<F, E> {
self.mul_ref_bigint(&field_val.value)
}
}

impl<F: Field, E: EllipticCurve> fmt::Display for EllipticCurvePoint<F, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_point_at_infinity() {
Expand Down
40 changes: 20 additions & 20 deletions myzkp/src/modules/zksnark/tutorial_snark/protocol_6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ pub fn setup(
let rho_r = FqOrder::random_element(&[]);
let rho_o = &rho_ell * &rho_r;

let g1_ell = g1 * rho_ell.get_value();
let g1_r = g1 * rho_r.get_value();
let g2_r = g2 * rho_r.get_value();
let g1_o = g1 * rho_o.get_value();
let g2_o = g2 * rho_o.get_value();
let g1_ell = g1 * &rho_ell;
let g1_r = g1 * &rho_r;
let g2_r = g2 * &rho_r;
let g1_o = g1 * &rho_o;
let g2_o = g2 * &rho_o;

let mut g1_checksum_vec = Vec::with_capacity(qap.d);

Expand All @@ -79,9 +79,9 @@ pub fn setup(
let r_i_s = qap.r_i_vec[i].eval(&s).sanitize();
let o_i_s = qap.o_i_vec[i].eval(&s).sanitize();
g1_checksum_vec.push(
&g1_ell * beta.mul_ref(&ell_i_s).get_value()
+ &g1_r * beta.mul_ref(&r_i_s).get_value()
+ &g1_o * beta.mul_ref(&o_i_s).get_value(),
&g1_ell * beta.mul_ref(&ell_i_s)
+ &g1_r * beta.mul_ref(&r_i_s)
+ &g1_o * beta.mul_ref(&o_i_s),
);
}

Expand All @@ -104,9 +104,9 @@ pub fn setup(
g1_alpha_o_i_vec: generate_alpha_challenge_vec(&g1_o, &qap.o_i_vec, &s, &alpha_o),
g1_sj_vec: generate_s_powers(&g1, &s, qap.m),
g1_checksum_vec: g1_checksum_vec,
g1_ell_ts: &g1_ell * t_s.get_value(),
g2_r_ts: &g2_r * t_s.get_value(),
g1_o_ts: &g1_o * t_s.get_value(),
g1_ell_ts: &g1_ell * &t_s,
g2_r_ts: &g2_r * &t_s,
g1_o_ts: &g1_o * &t_s,
g1_ell_alpha_ts: &g1_ell * (&t_s * &alpha_ell).get_value(),
g2_r_alpha_ts: &g2_r * (&t_s * &alpha_r).get_value(),
g1_o_alpha_ts: &g1_o * (&t_s * &alpha_o).get_value(),
Expand Down Expand Up @@ -153,23 +153,23 @@ pub fn prove(
let delta_o = FqOrder::random_element(&[]);

PinocchioProof {
g1_ell: &proof_key.g1_ell_ts * delta_ell.get_value()
g1_ell: &proof_key.g1_ell_ts * &delta_ell
+ accumulate_curve_points(&proof_key.g1_ell_i_vec, assignment),
g2_r: &proof_key.g2_r_ts * delta_r.get_value()
g2_r: &proof_key.g2_r_ts * &delta_r
+ accumulate_curve_points(&proof_key.g2_r_i_vec, assignment),
g1_o: &proof_key.g1_o_ts * delta_o.get_value()
g1_o: &proof_key.g1_o_ts * &delta_o
+ accumulate_curve_points(&proof_key.g1_o_i_vec, assignment),
g1_ell_prime: &proof_key.g1_ell_alpha_ts * delta_ell.get_value()
g1_ell_prime: &proof_key.g1_ell_alpha_ts * &delta_ell
+ accumulate_curve_points(&proof_key.g1_alpha_ell_i_vec, assignment),
g2_r_prime: &proof_key.g2_r_alpha_ts * delta_r.get_value()
g2_r_prime: &proof_key.g2_r_alpha_ts * &delta_r
+ accumulate_curve_points(&proof_key.g2_alpha_r_i_vec, assignment),
g1_o_prime: &proof_key.g1_o_alpha_ts * delta_o.get_value()
g1_o_prime: &proof_key.g1_o_alpha_ts * &delta_o
+ accumulate_curve_points(&proof_key.g1_alpha_o_i_vec, assignment),
g1_h: get_shifted_h(qap, assignment, &delta_ell, &delta_r, &delta_o)
.eval_with_powers_on_curve(&proof_key.g1_sj_vec),
g1_z: &proof_key.g1_ell_beta_ts * delta_ell.get_value()
+ &proof_key.g1_r_beta_ts * delta_r.get_value()
+ &proof_key.g1_o_beta_ts * delta_o.get_value()
g1_z: &proof_key.g1_ell_beta_ts * &delta_ell
+ &proof_key.g1_r_beta_ts * &delta_r
+ &proof_key.g1_o_beta_ts * &delta_o
+ accumulate_curve_points(&proof_key.g1_checksum_vec, assignment),
}
}
Expand Down

0 comments on commit 423ac1b

Please sign in to comment.