-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
425 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! This module contains functions and constructs related to elliptic curve operations on the | ||
//! secp256k1 curve. | ||
|
||
use starknet::SyscallResult; | ||
|
||
#[derive(Copy, Drop)] | ||
extern type Secp256K1EcPoint; | ||
|
||
/// Computes the addition of secp256k1 EC points `p0 + p1`. | ||
extern fn secp256k1_ec_add_syscall( | ||
p0: Secp256K1EcPoint, p1: Secp256K1EcPoint | ||
) -> SyscallResult<Secp256K1EcPoint> implicits(GasBuiltin, System) nopanic; | ||
|
||
/// Computes the product of a secp256k1 EC point `p` by the given scalar `m`. | ||
extern fn secp256k1_ec_mul_syscall( | ||
p: Secp256K1EcPoint, m: u256 | ||
) -> SyscallResult<Secp256K1EcPoint> implicits(GasBuiltin, System) nopanic; | ||
|
||
/// Computes the point on the secp256k1 curve that matches the given `x` coordinate, if such exists. | ||
/// Out of the two possible y's, chooses according to `y_parity`. | ||
extern fn secp256k1_ec_get_point_from_x_syscall( | ||
x: u256, y_parity: bool | ||
) -> SyscallResult<Option<Secp256K1EcPoint>> implicits(GasBuiltin, System) nopanic; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
crates/cairo-lang-sierra-to-casm/src/invocations/starknet/secp256k1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use cairo_lang_sierra::extensions::starknet::secp256k1::Secp256K1EcConcreteLibfunc; | ||
|
||
use super::{build_syscalls, CompiledInvocation, CompiledInvocationBuilder, InvocationError}; | ||
|
||
/// Builds instructions for Sierra secp256k1 operations. | ||
pub fn build( | ||
libfunc: &Secp256K1EcConcreteLibfunc, | ||
builder: CompiledInvocationBuilder<'_>, | ||
) -> Result<CompiledInvocation, InvocationError> { | ||
match libfunc { | ||
Secp256K1EcConcreteLibfunc::Add(_) => { | ||
build_syscalls(builder, "Secp256K1EcAdd", [1, 1], [1]) | ||
} | ||
Secp256K1EcConcreteLibfunc::Mul(_) => { | ||
build_syscalls(builder, "Secp256K1EcMul", [1, 2], [1]) | ||
} | ||
Secp256K1EcConcreteLibfunc::GetPointFromX(_) => { | ||
build_syscalls(builder, "Secp256K1EcGetPointFromX", [2, 1], [2]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
crates/cairo-lang-sierra/src/extensions/modules/starknet/secp256k1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use super::syscalls::SyscallGenericLibfunc; | ||
use crate::define_libfunc_hierarchy; | ||
use crate::extensions::enm::EnumType; | ||
use crate::extensions::lib_func::SignatureSpecializationContext; | ||
use crate::extensions::modules::{get_bool_type, get_u256_type, get_unit_type}; | ||
use crate::extensions::{NamedType, NoGenericArgsGenericType, SpecializationError}; | ||
use crate::ids::{GenericTypeId, UserTypeId}; | ||
use crate::program::GenericArg; | ||
|
||
define_libfunc_hierarchy! { | ||
pub enum Secp256K1EcLibfunc { | ||
Add(Secp256K1EcAddLibfunc), | ||
Mul(Secp256K1EcMulLibfunc), | ||
GetPointFromX(Secp256K1EcGetPointFromXLibfunc), | ||
}, Secp256K1EcConcreteLibfunc | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Secp256K1EcPointType {} | ||
impl NoGenericArgsGenericType for Secp256K1EcPointType { | ||
const ID: GenericTypeId = GenericTypeId::new_inline("Secp256K1EcPoint"); | ||
const STORABLE: bool = true; | ||
const DUPLICATABLE: bool = true; | ||
const DROPPABLE: bool = true; | ||
const SIZE: i16 = 1; | ||
} | ||
|
||
/// Libfunc for a secp256k1 elliptic curve addition system call. | ||
#[derive(Default)] | ||
pub struct Secp256K1EcAddLibfunc {} | ||
impl SyscallGenericLibfunc for Secp256K1EcAddLibfunc { | ||
const STR_ID: &'static str = "secp256k1_ec_add_syscall"; | ||
|
||
fn input_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
let secp256k1_ec_point_type = context.get_concrete_type(Secp256K1EcPointType::id(), &[])?; | ||
|
||
// Point `p0`, point `p1` | ||
Ok(vec![secp256k1_ec_point_type.clone(), secp256k1_ec_point_type]) | ||
} | ||
|
||
fn success_output_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
Ok(vec![context.get_concrete_type(Secp256K1EcPointType::id(), &[])?]) | ||
} | ||
} | ||
|
||
/// Libfunc for a secp256k1 elliptic curve multiplication system call. | ||
#[derive(Default)] | ||
pub struct Secp256K1EcMulLibfunc {} | ||
impl SyscallGenericLibfunc for Secp256K1EcMulLibfunc { | ||
const STR_ID: &'static str = "secp256k1_ec_mul_syscall"; | ||
|
||
fn input_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
Ok(vec![ | ||
// Point `p`. | ||
context.get_concrete_type(Secp256K1EcPointType::id(), &[])?, | ||
// Scalar `m`. | ||
get_u256_type(context)?, | ||
]) | ||
} | ||
|
||
fn success_output_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
Ok(vec![context.get_concrete_type(Secp256K1EcPointType::id(), &[])?]) | ||
} | ||
} | ||
|
||
/// System call libfunc for getting a point on the secp256k1 elliptic curve, according to the given | ||
/// `x` coordinate and the parity of the relevant y coordinate. | ||
#[derive(Default)] | ||
pub struct Secp256K1EcGetPointFromXLibfunc {} | ||
impl SyscallGenericLibfunc for Secp256K1EcGetPointFromXLibfunc { | ||
const STR_ID: &'static str = "secp256k1_ec_get_point_from_x_syscall"; | ||
|
||
fn input_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
Ok(vec![ | ||
// `x` coordinate. | ||
get_u256_type(context)?, | ||
// `y_parity` - parity of the relevant y coordinate. | ||
get_bool_type(context)?, | ||
]) | ||
} | ||
|
||
fn success_output_tys( | ||
context: &dyn SignatureSpecializationContext, | ||
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> { | ||
let secp256k1_ec_point_type = context.get_concrete_type(Secp256K1EcPointType::id(), &[])?; | ||
|
||
let unit_type = get_unit_type(context)?; | ||
// TODO(yuval): add get_option_type to mod.rs and use it here. | ||
let option_secp256k1_ec_point_type = context.get_concrete_type( | ||
EnumType::id(), | ||
&[ | ||
GenericArg::UserType(UserTypeId::from_string( | ||
"core::option::Option::<core::starknet::secp256k1::Secp256K1EcPoint>", | ||
)), | ||
GenericArg::Type(secp256k1_ec_point_type), | ||
GenericArg::Type(unit_type), | ||
], | ||
)?; | ||
Ok(vec![option_secp256k1_ec_point_type]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.