Skip to content

Commit

Permalink
msm contract
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Aug 6, 2024
1 parent 17eed5b commit 47e8eab
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 151 deletions.
13 changes: 13 additions & 0 deletions src/cairo/contracts/base/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "garaga"
version = "0.1.0"

[[package]]
name = "garaga_base"
version = "0.1.0"
dependencies = [
"garaga",
]
3 changes: 3 additions & 0 deletions src/cairo/contracts/base/src/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ edition = "2024_07"
garaga = { path = "../../" }
starknet = "2.7.0"

[cairo]
sierra-replace-ids = false

[[target.starknet-contract]]
137 changes: 53 additions & 84 deletions src/cairo/contracts/base/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,85 +1,54 @@
// use garaga::definitions::{u384, u96, G1Point};

// #[starknet::interface]
// trait IGaraga<TContractState> {
// fn get_p(self: @TContractState, curve_index: usize) -> (felt252, felt252, felt252, felt252);
// fn ec_add_unchecked(self: @TContractState, curve_index: usize) -> felt252;
// fn c1(self: @TContractState, curve_index: usize) -> felt252;
// fn c2(self: @TContractState, curve_index: usize) -> felt252;
// fn c3(self: @TContractState, curve_index: usize) -> felt252;
// fn c4(self: @TContractState, curve_index: usize) -> felt252;
// fn c5(self: @TContractState, curve_index: usize) -> felt252;
// fn c6(self: @TContractState, curve_index: usize) -> felt252;
// // fn c7(self: @TContractState, curve_index: usize) -> felt252;
// }

// #[starknet::contract]
// mod Garaga {
// use core::array::ArrayTrait;
// use garaga::definitions::{get_p, u384, G1Point, u96};
// use garaga::ec_ops::{ec_add_unchecked2};
// use garaga::circuits;
// #[storage]
// struct Storage {}

// #[abi(embed_v0)]
// impl IGaraga of super::IGaraga<ContractState> {
// fn get_p(self: @ContractState, curve_index: usize) -> (felt252, felt252, felt252,
// felt252) {
// let p = get_p(curve_index);
// return (p.limb0.into(), p.limb1.into(), p.limb2.into(), p.limb3.into());
// }
// fn ec_add_unchecked(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];
// let res = ec_add_unchecked2(inputs, curve_index);
// return 0;
// }
// fn c1(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_ACCUMULATE_EVAL_POINT_CHALLENGE_SIGNED_circuit(
// inputs, curve_index
// );
// return 0;
// }
// fn c2(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_DERIVE_POINT_FROM_X_circuit(inputs, curve_index);
// return 0;
// }
// fn c3(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_DOUBLE_EC_POINT_circuit(inputs, curve_index);
// return 0;
// }
// fn c4(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_IS_ON_CURVE_G1_circuit(inputs, curve_index);
// return 0;
// }
// fn c5(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_IS_ON_CURVE_G1_G2_circuit(inputs, curve_index);
// return 0;
// }
// fn c6(self: @ContractState, curve_index: usize) -> felt252 {
// let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// let res = circuits::ec::get_RHS_FINALIZE_ACC_circuit(inputs, curve_index);
// return 0;
// }
// // fn c7(self: @ContractState, curve_index: usize) -> felt252 {
// // let inputs = array![u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }];

// // let res = circuits::ec::get_SLOPE_INTERCEPT_SAME_POINT_circuit(inputs,
// curve_index);
// // return 0;
// // }
// }
// }

use garaga::definitions::{u384, u96, G1Point};

#[starknet::interface]
trait IGaragaBase<TContractState> {
fn msm(
self: @TContractState,
_points: Span<felt252>,
scalars: Span<u256>,
scalars_digits_decompositions: Option<Span<(Span<felt252>, Span<felt252>)>>,
_msm_hint: Span<felt252>,
curve_index: usize
) -> bool;
}

#[starknet::contract]
mod GaragaBase {
use core::array::ArrayTrait;
use garaga::definitions::{get_p, u384, G1Point, u96};
use garaga::utils_calldata::{parse_msm_hint, MSMHint, DerivePointFromXHint, parse_G1Points};
use garaga::ec_ops::{msm_g1, G1PointTrait};

#[storage]
struct Storage {}

#[abi(embed_v0)]
impl IGaragaBase of super::IGaragaBase<ContractState> {
fn msm(
self: @ContractState,
_points: Span<felt252>,
scalars: Span<u256>,
scalars_digits_decompositions: Option<Span<(Span<felt252>, Span<felt252>)>>,
_msm_hint: Span<felt252>,
curve_index: usize
) -> bool {
let n_scalars = scalars.len();
let points = parse_G1Points(_points, n_scalars);
let (msm_hint, derive_point_from_x_hint): (Box<MSMHint>, Box<DerivePointFromXHint>) =
parse_msm_hint(
_msm_hint, n_scalars
);

let result = msm_g1(
points,
scalars,
scalars_digits_decompositions,
msm_hint.unbox(),
derive_point_from_x_hint.unbox(),
curve_index
);
return result.is_on_curve(curve_index);
}
}
}

163 changes: 96 additions & 67 deletions src/cairo/src/utils_calldata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -146,74 +146,76 @@ fn parse_scaling_factor(hint: Span<felt252>) -> MillerLoopResultScalingFactor {
}

fn parse_E12DMulQuotient(hint: Span<felt252>) -> Box<E12DMulQuotient> {
BoxTrait::new(E12DMulQuotient {
w0: u384 {
limb0: downcast(*hint.at(0)).unwrap(),
limb1: downcast(*hint.at(1)).unwrap(),
limb2: downcast(*hint.at(2)).unwrap(),
limb3: downcast(*hint.at(3)).unwrap()
},
w1: u384 {
limb0: downcast(*hint.at(4)).unwrap(),
limb1: downcast(*hint.at(5)).unwrap(),
limb2: downcast(*hint.at(6)).unwrap(),
limb3: downcast(*hint.at(7)).unwrap()
},
w2: u384 {
limb0: downcast(*hint.at(8)).unwrap(),
limb1: downcast(*hint.at(9)).unwrap(),
limb2: downcast(*hint.at(10)).unwrap(),
limb3: downcast(*hint.at(11)).unwrap()
},
w3: u384 {
limb0: downcast(*hint.at(12)).unwrap(),
limb1: downcast(*hint.at(13)).unwrap(),
limb2: downcast(*hint.at(14)).unwrap(),
limb3: downcast(*hint.at(15)).unwrap()
},
w4: u384 {
limb0: downcast(*hint.at(16)).unwrap(),
limb1: downcast(*hint.at(17)).unwrap(),
limb2: downcast(*hint.at(18)).unwrap(),
limb3: downcast(*hint.at(19)).unwrap()
},
w5: u384 {
limb0: downcast(*hint.at(20)).unwrap(),
limb1: downcast(*hint.at(21)).unwrap(),
limb2: downcast(*hint.at(22)).unwrap(),
limb3: downcast(*hint.at(23)).unwrap()
},
w6: u384 {
limb0: downcast(*hint.at(24)).unwrap(),
limb1: downcast(*hint.at(25)).unwrap(),
limb2: downcast(*hint.at(26)).unwrap(),
limb3: downcast(*hint.at(27)).unwrap()
},
w7: u384 {
limb0: downcast(*hint.at(28)).unwrap(),
limb1: downcast(*hint.at(29)).unwrap(),
limb2: downcast(*hint.at(30)).unwrap(),
limb3: downcast(*hint.at(31)).unwrap()
},
w8: u384 {
limb0: downcast(*hint.at(32)).unwrap(),
limb1: downcast(*hint.at(33)).unwrap(),
limb2: downcast(*hint.at(34)).unwrap(),
limb3: downcast(*hint.at(35)).unwrap()
},
w9: u384 {
limb0: downcast(*hint.at(36)).unwrap(),
limb1: downcast(*hint.at(37)).unwrap(),
limb2: downcast(*hint.at(38)).unwrap(),
limb3: downcast(*hint.at(39)).unwrap()
},
w10: u384 {
limb0: downcast(*hint.at(40)).unwrap(),
limb1: downcast(*hint.at(41)).unwrap(),
limb2: downcast(*hint.at(42)).unwrap(),
limb3: downcast(*hint.at(43)).unwrap()
BoxTrait::new(
E12DMulQuotient {
w0: u384 {
limb0: downcast(*hint.at(0)).unwrap(),
limb1: downcast(*hint.at(1)).unwrap(),
limb2: downcast(*hint.at(2)).unwrap(),
limb3: downcast(*hint.at(3)).unwrap()
},
w1: u384 {
limb0: downcast(*hint.at(4)).unwrap(),
limb1: downcast(*hint.at(5)).unwrap(),
limb2: downcast(*hint.at(6)).unwrap(),
limb3: downcast(*hint.at(7)).unwrap()
},
w2: u384 {
limb0: downcast(*hint.at(8)).unwrap(),
limb1: downcast(*hint.at(9)).unwrap(),
limb2: downcast(*hint.at(10)).unwrap(),
limb3: downcast(*hint.at(11)).unwrap()
},
w3: u384 {
limb0: downcast(*hint.at(12)).unwrap(),
limb1: downcast(*hint.at(13)).unwrap(),
limb2: downcast(*hint.at(14)).unwrap(),
limb3: downcast(*hint.at(15)).unwrap()
},
w4: u384 {
limb0: downcast(*hint.at(16)).unwrap(),
limb1: downcast(*hint.at(17)).unwrap(),
limb2: downcast(*hint.at(18)).unwrap(),
limb3: downcast(*hint.at(19)).unwrap()
},
w5: u384 {
limb0: downcast(*hint.at(20)).unwrap(),
limb1: downcast(*hint.at(21)).unwrap(),
limb2: downcast(*hint.at(22)).unwrap(),
limb3: downcast(*hint.at(23)).unwrap()
},
w6: u384 {
limb0: downcast(*hint.at(24)).unwrap(),
limb1: downcast(*hint.at(25)).unwrap(),
limb2: downcast(*hint.at(26)).unwrap(),
limb3: downcast(*hint.at(27)).unwrap()
},
w7: u384 {
limb0: downcast(*hint.at(28)).unwrap(),
limb1: downcast(*hint.at(29)).unwrap(),
limb2: downcast(*hint.at(30)).unwrap(),
limb3: downcast(*hint.at(31)).unwrap()
},
w8: u384 {
limb0: downcast(*hint.at(32)).unwrap(),
limb1: downcast(*hint.at(33)).unwrap(),
limb2: downcast(*hint.at(34)).unwrap(),
limb3: downcast(*hint.at(35)).unwrap()
},
w9: u384 {
limb0: downcast(*hint.at(36)).unwrap(),
limb1: downcast(*hint.at(37)).unwrap(),
limb2: downcast(*hint.at(38)).unwrap(),
limb3: downcast(*hint.at(39)).unwrap()
},
w10: u384 {
limb0: downcast(*hint.at(40)).unwrap(),
limb1: downcast(*hint.at(41)).unwrap(),
limb2: downcast(*hint.at(42)).unwrap(),
limb3: downcast(*hint.at(43)).unwrap()
}
}
})
)
}
// Return from hint
// lambda_root_inverse: E12D,
Expand Down Expand Up @@ -451,6 +453,33 @@ fn parse_function_felt(hint: Span<felt252>, msm_size: usize) -> FunctionFelt {
}
}


fn parse_G1Points(points: Span<felt252>, n_elements: usize) -> Span<G1Point> {
let mut array: Array<G1Point> = ArrayTrait::new();
let mut i = 0;
while i != n_elements {
array
.append(
G1Point {
x: u384 {
limb0: downcast(*points.at(i)).unwrap(),
limb1: downcast(*points.at(i + 1)).unwrap(),
limb2: downcast(*points.at(i + 2)).unwrap(),
limb3: downcast(*points.at(i + 3)).unwrap()
},
y: u384 {
limb0: downcast(*points.at(i + 4)).unwrap(),
limb1: downcast(*points.at(i + 5)).unwrap(),
limb2: downcast(*points.at(i + 6)).unwrap(),
limb3: downcast(*points.at(i + 7)).unwrap()
}
}
);
i += 8;
};
array.span()
}

fn parse_msm_hint(
hint: Span<felt252>, msm_size: usize
) -> (Box<MSMHint>, Box<DerivePointFromXHint>) {
Expand Down

0 comments on commit 47e8eab

Please sign in to comment.