Skip to content

Commit

Permalink
Add Copy trait to msm hints structs. (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime authored Sep 30, 2024
1 parent 05a082b commit fa27403
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from garaga.starknet.cli.utils import create_directory
from garaga.starknet.groth16_contract_generator.parsing_utils import Groth16VerifyingKey

ECIP_OPS_CLASS_HASH = 0x7918F484291EB154E13D0E43BA6403E62DC1F5FBB3A191D868E2E37359F8713
ECIP_OPS_CLASS_HASH = 0x2672F1F079CCBAFE1BE4A20A76421B509FCFB406CBF6818563ED812EDAEB3A3


def precompute_lines_from_vk(vk: Groth16VerifyingKey) -> StructArray:
Expand Down
2 changes: 1 addition & 1 deletion hydra/garaga/starknet/tests_and_calldata_generators/msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def build_derive_point_from_x_hint(
name="derive_point_from_x_hint",
elmts=[
structs.u384(name="y_last_attempt", elmts=[y]),
structs.u384Array(name="g_rhs_sqrt", elmts=roots),
structs.u384Span(name="g_rhs_sqrt", elmts=roots),
],
)

Expand Down
2 changes: 2 additions & 0 deletions src/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ repository = "https://github.com/keep-starknet-strange/garaga"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[cairo]
sierra-replace-ids = false

[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod Groth16VerifierBLS12_381 {
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
0x2672f1f079ccbafe1be4a20a76421b509fcfb406cbf6818563ed812edaeb3a3;
use starknet::ContractAddress;

#[storage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod Groth16VerifierBN254 {
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
0x2672f1f079ccbafe1be4a20a76421b509fcfb406cbf6818563ed812edaeb3a3;
use starknet::ContractAddress;

#[storage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod Risc0Groth16VerifierBN254 {
use super::{N_FREE_PUBLIC_INPUTS, vk, ic, precomputed_lines, T};

const ECIP_OPS_CLASS_HASH: felt252 =
0x7918f484291eb154e13d0e43ba6403e62dc1f5fbb3a191d868e2e37359f8713;
0x2672f1f079ccbafe1be4a20a76421b509fcfb406cbf6818563ed812edaeb3a3;
use starknet::ContractAddress;

#[storage]
Expand Down
33 changes: 17 additions & 16 deletions src/src/ec_ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ fn get_DERIVE_POINT_FROM_X_circuit(
// If z does not have a square root in Fp, then g*z has a square root in Fp*.
// Note: there is exactly (p-1)//2 square roots in Fp*.
fn derive_ec_point_from_X(
mut x: felt252, y_last_attempt: u384, mut g_rhs_sqrt: Array<u384>, curve_index: usize,
mut x: felt252, y_last_attempt: u384, mut g_rhs_sqrt: Span<u384>, curve_index: usize,
) -> G1Point {
let mut attempt: felt252 = 0;
while let Option::Some(root) = g_rhs_sqrt.pop_front() {
let x_u384: u384 = x.into();
let res: DerivePointFromXOutput = get_DERIVE_POINT_FROM_X_circuit(
x_u384, root, curve_index
x_u384, *root, curve_index
);
assert!(
res.should_be_rhs_or_g_rhs == res.g_rhs, "grhs!=(sqrt(g*rhs))^2 in attempt {attempt}"
Expand All @@ -220,7 +220,7 @@ fn derive_ec_point_from_X(
// from the constant term.
// No information about the degrees of the polynomials is stored here as they are derived
// implicitely from the MSM size.
#[derive(Drop, Debug, PartialEq, Serde)]
#[derive(Drop, Debug, Copy, PartialEq, Serde)]
struct FunctionFelt {
a_num: Span<u384>,
a_den: Span<u384>,
Expand Down Expand Up @@ -264,7 +264,7 @@ impl FunctionFeltImpl of FunctionFeltTrait {
}
}

#[derive(Drop, Debug, PartialEq, Serde)]
#[derive(Drop, Debug, PartialEq, Serde, Copy)]
struct MSMHint {
Q_low: G1Point,
Q_high: G1Point,
Expand All @@ -280,10 +280,10 @@ struct MSMHintSmallScalar {
SumDlogDiv: FunctionFelt,
}

#[derive(Drop, Debug, PartialEq, Serde)]
#[derive(Drop, Debug, PartialEq, Serde, Copy)]
struct DerivePointFromXHint {
y_last_attempt: u384,
g_rhs_sqrt: Array<u384>,
g_rhs_sqrt: Span<u384>,
}

fn scalar_mul_g1_fixed_small_scalar(
Expand Down Expand Up @@ -352,6 +352,7 @@ fn scalar_mul_g1_fixed_small_scalar(
curve_index: curve_index
);

u384_assert_eq(lhs, rhs);
return hint.Q;
}

Expand Down Expand Up @@ -756,7 +757,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 0);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 0);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -788,7 +789,7 @@ mod tests {
limb3: 0x19972c66940a5bb4365da67
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 1);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 1);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -838,7 +839,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 2);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 2);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -870,7 +871,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 3);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 3);
assert!(
result
.x == u384 {
Expand All @@ -895,7 +896,7 @@ mod tests {
limb3: 0x0
};
let grhs_roots: Array<u384> = array![];
let result = derive_ec_point_from_X(x, y, grhs_roots, 4);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 4);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -945,7 +946,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 0);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 0);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -1001,7 +1002,7 @@ mod tests {
limb3: 0x6de8fe79d9b161443b37f30
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 1);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 1);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -1033,7 +1034,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 2);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 2);
assert!(
result
.x == u384 {
Expand Down Expand Up @@ -1065,7 +1066,7 @@ mod tests {
limb3: 0x0
}
];
let result = derive_ec_point_from_X(x, y, grhs_roots, 3);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 3);
assert!(
result
.x == u384 {
Expand All @@ -1090,7 +1091,7 @@ mod tests {
limb3: 0x0
};
let grhs_roots: Array<u384> = array![];
let result = derive_ec_point_from_X(x, y, grhs_roots, 4);
let result = derive_ec_point_from_X(x, y, grhs_roots.span(), 4);
assert!(
result
.x == u384 {
Expand Down
Loading

0 comments on commit fa27403

Please sign in to comment.