Skip to content

Commit 6f52d05

Browse files
committed
add secp256r1 testing programs and newhint#17 and #18
1 parent af18fc9 commit 6f52d05

File tree

5 files changed

+85
-79
lines changed

5 files changed

+85
-79
lines changed

cairo_programs/doubling_slope_and_div_mod_n.cairo

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
%builtins range_check
2+
3+
// Source: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/ec.cairo#L32
4+
5+
func compute_doubling_slope{range_check_ptr}(point: EcPoint) -> (slope: BigInt3) {
6+
// Note that y cannot be zero: assume that it is, then point = -point, so 2 * point = 0, which
7+
// contradicts the fact that the size of the curve is odd.
8+
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P %}
9+
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_ALPHA as ALPHA %}
10+
// Hint #19
11+
%{
12+
from starkware.cairo.common.cairo_secp.secp_utils import pack
13+
from starkware.python.math_utils import ec_double_slope
14+
15+
# Compute the slope.
16+
x = pack(ids.point.x, PRIME)
17+
y = pack(ids.point.y, PRIME)
18+
value = slope = ec_double_slope(point=(x, y), alpha=ALPHA, p=SECP_P)
19+
%}
20+
let (slope: BigInt3) = nondet_bigint3();
21+
22+
let (x_sqr: UnreducedBigInt3) = unreduced_sqr(point.x);
23+
let (slope_y: UnreducedBigInt3) = unreduced_mul(slope, point.y);
24+
verify_zero(
25+
UnreducedBigInt3(
26+
d0=3 * x_sqr.d0 + A0 - 2 * slope_y.d0,
27+
d1=3 * x_sqr.d1 + A1 - 2 * slope_y.d1,
28+
d2=3 * x_sqr.d2 + A2 - 2 * slope_y.d2,
29+
),
30+
);
31+
32+
return (slope=slope);
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
%builtins range_check
2+
3+
// Sources: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/signature.cairo#L48
4+
// Sources: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/ec.cairo#L32
5+
6+
from starkware.cairo.common.alloc import alloc
7+
from starkware.cairo.common.bitwise import bitwise_and
8+
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
9+
from starkware.cairo.common.cairo_secp.bigint import (
10+
BASE,
11+
BigInt3,
12+
UnreducedBigInt3,
13+
bigint_mul,
14+
nondet_bigint3,
15+
)
16+
from starkware.cairo.common.cairo_secp.ec import EcPoint
17+
from starkware.cairo.common.math import assert_nn, assert_nn_le, assert_not_zero, unsigned_div_rem
18+
from starkware.cairo.common.math_cmp import RC_BOUND
19+
from starkware.cairo.common.uint256 import Uint256
20+
21+
func div_mod_n{range_check_ptr}(a: BigInt3, b: BigInt3) -> (res: BigInt3) {
22+
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_N as N %}
23+
%{
24+
from starkware.cairo.common.cairo_secp.secp_utils import pack
25+
from starkware.python.math_utils import div_mod, safe_div
26+
27+
a = pack(ids.a, PRIME)
28+
b = pack(ids.b, PRIME)
29+
value = res = div_mod(a, b, N)
30+
%}
31+
let (res) = nondet_bigint3();
32+
33+
return (res=res);
34+
}
35+
36+
func main{range_check_ptr}(){
37+
let x = BigInt3(235, 522, 111);
38+
let y = BigInt3(1323, 15124, 796759);
39+
40+
let a = div_mod_n(x, y);
41+
assert a = a;
42+
return ();
43+
44+
}

src/hint_processor/builtin_hint_processor/secp/secp_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ lazy_static! {
4848
"115792089210356248762697446949407573530086143415290314195533631308867097853951"
4949
).unwrap();
5050
//SECP256R1_N = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551
51-
pub(crate) static ref SECP256R1_N: BigUint = BigUint::from_str(
51+
pub(crate) static ref SECP256R1_N: BigInt = BigInt::from_str(
5252
"115792089210356248762697446949407573529996955224135760342422259061068512044369"
5353
).unwrap();
5454
//SECP256R1_ALPHA = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC

src/tests/cairo_run_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,3 +1343,10 @@ fn cairo_run_is_zero() {
13431343
let program_data = include_bytes!("../../cairo_programs/is_zero.json");
13441344
run_program_simple(program_data.as_slice());
13451345
}
1346+
1347+
#[test]
1348+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1349+
fn cairo_run_secp256r1_div_mod_n() {
1350+
let program_data = include_bytes!("../../cairo_programs/secp256r1_div_mod_n.json");
1351+
run_program_simple(program_data.as_slice());
1352+
}

0 commit comments

Comments
 (0)