Skip to content

Commit

Permalink
using correct types from mpc-bulletproof branch
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Sep 13, 2023
1 parent 5cf3582 commit 44d8d5e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 71 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 33 additions & 34 deletions src/verifier/scalar.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ struct Scalar {
impl ScalarImpl of ScalarTrait {
fn inverse(self: @Scalar) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = mult_inverse((*self.inner).into(), stark_curve::ORDER.into())
// .try_into()
// .unwrap();
let inner = felt252_div(1, (*self.inner).try_into().unwrap());
let inner = mult_inverse((*self.inner).into(), stark_curve::ORDER.into())
.try_into()
.unwrap();
// let inner = felt252_div(1, (*self.inner).try_into().unwrap());
Scalar { inner }
}

fn pow(self: @Scalar, exponent: u256) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = pow_mod((*self.inner).into(), exponent, stark_curve::ORDER.into())
// .try_into()
// .unwrap();
// Scalar { inner }

binary_exp(*self, exponent)
let inner = pow_mod((*self.inner).into(), exponent, stark_curve::ORDER.into())
.try_into()
.unwrap();
Scalar { inner }
// binary_exp(*self, exponent)
}
}

Expand All @@ -52,10 +51,10 @@ impl ScalarImpl of ScalarTrait {
impl ScalarAdd of Add<Scalar> {
fn add(lhs: Scalar, rhs: Scalar) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = add_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
// .try_into()
// .unwrap();
let inner = (lhs.inner + rhs.inner);
let inner = add_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
.try_into()
.unwrap();
// let inner = (lhs.inner + rhs.inner);
Scalar { inner }
}
}
Expand All @@ -73,10 +72,10 @@ impl ScalarAddEq of AddEq<Scalar> {
impl ScalarSub of Sub<Scalar> {
fn sub(lhs: Scalar, rhs: Scalar) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = sub_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
// .try_into()
// .unwrap();
let inner = (lhs.inner - rhs.inner);
let inner = sub_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
.try_into()
.unwrap();
// let inner = (lhs.inner - rhs.inner);
Scalar { inner }
}
}
Expand All @@ -94,10 +93,10 @@ impl ScalarSubEq of SubEq<Scalar> {
impl ScalarMul of Mul<Scalar> {
fn mul(lhs: Scalar, rhs: Scalar) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = mult_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
// .try_into()
// .unwrap();
let inner = (lhs.inner * rhs.inner);
let inner = mult_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
.try_into()
.unwrap();
// let inner = (lhs.inner * rhs.inner);
Scalar { inner }
}
}
Expand All @@ -117,10 +116,10 @@ impl ScalarDiv of Div<Scalar> {
// Under the hood, this is implemented as
// lhs * rhs.inverse()
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = div_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
// .try_into()
// .unwrap();
let inner = felt252_div(lhs.inner, rhs.inner.try_into().unwrap());
let inner = div_mod(lhs.inner.into(), rhs.inner.into(), stark_curve::ORDER.into())
.try_into()
.unwrap();
// let inner = felt252_div(lhs.inner, rhs.inner.try_into().unwrap());
Scalar { inner }
}
}
Expand All @@ -138,8 +137,8 @@ impl ScalarDivEq of DivEq<Scalar> {
impl ScalarNeg of Neg<Scalar> {
fn neg(a: Scalar) -> Scalar {
// Safe to unwrap b/c scalar field is smaller than base field
// let inner = add_inverse_mod(a.inner.into(), stark_curve::ORDER.into()).try_into().unwrap();
let inner = -a.inner;
let inner = add_inverse_mod(a.inner.into(), stark_curve::ORDER.into()).try_into().unwrap();
// let inner = -a.inner;
Scalar { inner }
}
}
Expand All @@ -154,19 +153,19 @@ impl ScalarNeg of Neg<Scalar> {

impl U256IntoScalar of Into<u256, Scalar> {
fn into(self: u256) -> Scalar {
// let inner_u256 = self % stark_curve::ORDER.into();
let inner_u256 = self % BASE_FIELD_ORDER;
let inner_u256 = self % stark_curve::ORDER.into();
// let inner_u256 = self % BASE_FIELD_ORDER;
// Safe to unwrap b/c scalar field is smaller than base field
Scalar { inner: inner_u256.try_into().unwrap() }
}
}

impl FeltIntoScalar<T, impl TIntoFelt: Into<T, felt252>> of Into<T, Scalar> {
fn into(self: T) -> Scalar {
// let inner_felt: felt252 = self.into();
// let inner_u256: u256 = inner_felt.into();
// inner_u256.into()
Scalar { inner: self.into() }
let inner_felt: felt252 = self.into();
let inner_u256: u256 = inner_felt.into();
inner_u256.into()
// Scalar { inner: self.into() }
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/verifier/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ fn calc_delta(

let w_R_col = W_R[col_index];
w_R_flat.append(w_R_col.flatten(z));

col_index += 1;
};

// \delta = <y^n * w_R_flat, w_L_flat>
Expand Down
12 changes: 6 additions & 6 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ starknet = { workspace = true }
katana-core = { git = "https://github.com/renegade-fi/dojo.git", branch = "renegade-testing" }
dojo-test-utils = { git = "https://github.com/renegade-fi/dojo.git", branch = "renegade-testing" }
mpc-stark = { workspace = true }
mpc-bulletproof = { git = "https://github.com/renegade-fi/mpc-bulletproof.git", features = ["integration_test"] }
mpc-bulletproof = { git = "https://github.com/renegade-fi/mpc-bulletproof.git", branch="andrew/column-major-weights", features = ["integration_test"] }
merlin = { git = "https://github.com/renegade-fi/merlin.git" }
renegade-crypto = { git = "https://github.com/renegade-fi/renegade.git", branch = "starknet-client-consolidation" }
circuits = { git = "https://github.com/renegade-fi/renegade.git", branch = "starknet-client-consolidation", features = ["test_helpers"] }
circuit-types = { git = "https://github.com/renegade-fi/renegade.git", branch = "starknet-client-consolidation" }
test-helpers = { git = "https://github.com/renegade-fi/renegade.git", branch = "starknet-client-consolidation" }
starknet-client = { git = "https://github.com/renegade-fi/renegade.git", branch = "starknet-client-consolidation" }
renegade-crypto = { git = "https://github.com/renegade-fi/renegade.git", branch = "andrew/column-major-weights" }
circuits = { git = "https://github.com/renegade-fi/renegade.git", branch = "andrew/column-major-weights", features = ["test_helpers"] }
circuit-types = { git = "https://github.com/renegade-fi/renegade.git", branch = "andrew/column-major-weights" }
test-helpers = { git = "https://github.com/renegade-fi/renegade.git", branch = "andrew/column-major-weights" }
starknet-client = { git = "https://github.com/renegade-fi/renegade.git", branch = "andrew/column-major-weights" }
6 changes: 3 additions & 3 deletions tests/src/profiling/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use dojo_test_utils::sequencer::TestSequencer;
use eyre::{eyre, Result};
use merlin::HashChainTranscript;
use mpc_bulletproof::{
r1cs::{Prover, R1CSProof, SparseReducedMatrix, Verifier},
r1cs::{Prover, R1CSProof, SparseWeightMatrix, Verifier},
BulletproofGens, PedersenGens,
};
use mpc_stark::algebra::{scalar::Scalar, stark_curve::StarkPoint};
Expand Down Expand Up @@ -201,8 +201,8 @@ pub async fn invoke_calc_delta(
n: FieldElement,
y_inv_powers_to_n: &Vec<Scalar>,
z: Scalar,
w_l: SparseReducedMatrix,
w_r: SparseReducedMatrix,
w_l: SparseWeightMatrix,
w_r: SparseWeightMatrix,
) -> Result<()> {
let calldata = iter::once(n)
.chain(y_inv_powers_to_n.to_calldata())
Expand Down
18 changes: 9 additions & 9 deletions tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use merlin::HashChainTranscript;
use mpc_bulletproof::{
r1cs::{
CircuitWeights, ConstraintSystem, LinearCombination, Prover, R1CSProof,
RandomizableConstraintSystem, SparseReducedMatrix, SparseWeightRow, Variable,
RandomizableConstraintSystem, SparseWeightMatrix, SparseWeightVec, Variable,
},
r1cs_mpc::R1CSError,
BulletproofGens, PedersenGens,
Expand Down Expand Up @@ -658,15 +658,15 @@ pub enum CircuitParams {
/// Sizing parameters for the circuit
SizeParams(CircuitSizeParams),
/// Sparse-reduced matrix of left input weights for the circuit
Wl(SparseReducedMatrix),
Wl(SparseWeightMatrix),
/// Sparse-reduced matrix of right input weights for the circuit
Wr(SparseReducedMatrix),
Wr(SparseWeightMatrix),
/// Sparse-reduced matrix of output weights for the circuit
Wo(SparseReducedMatrix),
Wo(SparseWeightMatrix),
/// Sparse-reduced matrix of witness weights for the circuit
Wv(SparseReducedMatrix),
Wv(SparseWeightMatrix),
/// Sparse-reduced vector of constants for the circuit
C(SparseWeightRow),
C(SparseWeightVec),
}

pub struct NewWalletArgs {
Expand Down Expand Up @@ -766,7 +766,7 @@ impl<T: CalldataSerializable, const N: usize> CalldataSerializable for [T; N] {
}
}

// `(usize, Scalar)` represents an entry in a `SparseWeightRow`
// `(usize, Scalar)` represents an entry in a `SparseWeightVec`
impl CalldataSerializable for (usize, Scalar) {
fn to_calldata(&self) -> Vec<FieldElement> {
self.0
Expand All @@ -777,13 +777,13 @@ impl CalldataSerializable for (usize, Scalar) {
}
}

impl CalldataSerializable for SparseWeightRow {
impl CalldataSerializable for SparseWeightVec {
fn to_calldata(&self) -> Vec<FieldElement> {
self.0.to_calldata()
}
}

impl CalldataSerializable for SparseReducedMatrix {
impl CalldataSerializable for SparseWeightMatrix {
fn to_calldata(&self) -> Vec<FieldElement> {
self.0.to_calldata()
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/verifier_utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use eyre::{eyre, Result};
use merlin::HashChainTranscript;
use mpc_bulletproof::{
inner_product,
r1cs::{R1CSProof, SparseReducedMatrix, Verifier},
r1cs::{R1CSProof, SparseWeightMatrix, Verifier},
InnerProductProof, TranscriptProtocol,
};
use mpc_stark::algebra::{scalar::Scalar, stark_curve::StarkPoint};
Expand Down Expand Up @@ -106,8 +106,8 @@ pub async fn calc_delta(
account: &ScriptAccount,
y_inv_powers_to_n: &Vec<Scalar>,
z: Scalar,
w_l: SparseReducedMatrix,
w_r: SparseReducedMatrix,
w_l: SparseWeightMatrix,
w_r: SparseWeightMatrix,
) -> Result<Scalar> {
let calldata = iter::once(FieldElement::from(DUMMY_CIRCUIT_N))
.chain(y_inv_powers_to_n.to_calldata())
Expand Down

0 comments on commit 44d8d5e

Please sign in to comment.