Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sha and hash to field #5

Merged
merged 17 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/basic_field_ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,75 @@ from starkware.cairo.common.cairo_builtins import UInt384
from starkware.cairo.common.cairo_builtins import ModBuiltin
from starkware.cairo.common.registers import get_fp_and_pc

const POW_2_32_252 = 0x100000000;
const POW_2_64_252 = 0x10000000000000000;

// Compute u512 mod p, where u512 = high * 2^256 + low
// Each high/low limb is 32 bits big and passed in BE
func u512_mod_p{range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}(
low: (v0: felt, v1: felt, v2: felt, v3: felt, v4: felt, v5: felt, v6: felt, v7: felt),
high: (v0: felt, v1: felt, v2: felt, v3: felt, v4: felt, v5: felt, v6: felt, v7: felt),
p: UInt384,
) -> (result: UInt384) {
let (_, pc) = get_fp_and_pc();

pc_labelx:
let add_offsets_ptr = pc + (add_offsets - pc_labelx);
let mul_offsets_ptr = pc + (mul_offsets - pc_labelx);

// High limbs.
assert [range_check96_ptr] = high.v7 + high.v6 * POW_2_32_252 + high.v5 * POW_2_64_252;
assert [range_check96_ptr + 1] = high.v4 + high.v3 * POW_2_32_252 + high.v2 * POW_2_64_252;
assert [range_check96_ptr + 2] = high.v1 + high.v0 * POW_2_32_252;
assert [range_check96_ptr + 3] = 0;

// Shift Limbs.
assert [range_check96_ptr + 4] = 0;
assert [range_check96_ptr + 5] = 0;
assert [range_check96_ptr + 6] = 0x10000000000000000;
assert [range_check96_ptr + 7] = 0;

// Low limbs.
assert [range_check96_ptr + 8] = low.v7 + low.v6 * POW_2_32_252 + low.v5 * POW_2_64_252;
assert [range_check96_ptr + 9] = low.v4 + low.v3 * POW_2_32_252 + low.v2 * POW_2_64_252;
assert [range_check96_ptr + 10] = low.v1 + low.v0 * POW_2_32_252;
assert [range_check96_ptr + 11] = 0;

assert add_mod_ptr[0] = ModBuiltin(
p=p, values_ptr=cast(range_check96_ptr, UInt384*), offsets_ptr=add_offsets_ptr, n=1
);
assert mul_mod_ptr[0] = ModBuiltin(
p=p, values_ptr=cast(range_check96_ptr, UInt384*), offsets_ptr=mul_offsets_ptr, n=1
);
%{
from starkware.cairo.lang.builtins.modulo.mod_builtin_runner import ModBuiltinRunner
assert builtin_runners["add_mod_builtin"].instance_def.batch_size == 1
assert builtin_runners["mul_mod_builtin"].instance_def.batch_size == 1

ModBuiltinRunner.fill_memory(
memory=memory,
add_mod=(ids.add_mod_ptr.address_, builtin_runners["add_mod_builtin"], 1),
mul_mod=(ids.mul_mod_ptr.address_, builtin_runners["mul_mod_builtin"], 1),
)
%}
let range_check96_ptr = range_check96_ptr + 20;
let add_mod_ptr = add_mod_ptr + ModBuiltin.SIZE;
let mul_mod_ptr = mul_mod_ptr + ModBuiltin.SIZE;
return (result=[cast(range_check96_ptr - 4, UInt384*)]);

mul_offsets:
// Compute High * Shift
dw 0; // [High]
dw 4; // [Shift]
dw 12; // [High * Shift]

// Computes [Low + High * Shift]
add_offsets:
dw 8; // Low
dw 12; // [High * Shift]
dw 16; // [Low + High * Shift]
}

// Compute X + Y mod p.
func add_mod_p{range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*}(
x: UInt384, y: UInt384, p: UInt384
Expand Down
38 changes: 37 additions & 1 deletion src/ec_ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ from definitions import (
TRUE,
FALSE,
)
from basic_field_ops import is_eq_mod_p, is_opposite_mod_p, is_zero_mod_p, assert_eq_mod_p
from basic_field_ops import (
is_eq_mod_p,
is_opposite_mod_p,
is_zero_mod_p,
assert_eq_mod_p,
sub_mod_p,
)

from precompiled_circuits.ec import (
get_IS_ON_CURVE_G1_G2_circuit,
Expand Down Expand Up @@ -42,6 +48,12 @@ from utils import (
hash_efelt_transcript,
)

// Checks if a given point is on the G1 curve for the specified curve_id
// Parameters:
// curve_id: The ID of the elliptic curve
// point: The G1Point to check
// Returns:
// res: 1 if the point is on the curve, 0 otherwise
func is_on_curve_g1{
range_check_ptr, range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*
}(curve_id: felt, point: G1Point) -> (res: felt) {
Expand Down Expand Up @@ -98,6 +110,27 @@ func is_on_curve_g1_g2{
return (res=1);
}

// Subtract two EC points
// This function doesn't check if the inputs are on the curve or if they are the point at infinity
// Parameters:
// curve_id: The ID of the elliptic curve being used
// p: The first G1Point (minuend)
// q: The second G1Point (subtrahend)
// Returns:
// res: The result of p - q as a G1Point
func sub_ec_points{
range_check_ptr, range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*
}(curve_id: felt, p: G1Point, q: G1Point) -> (res: G1Point) {
alloc_locals;
// Negate the second point
let (curve_p) = get_P(curve_id);
let (neg_Q_Y) = sub_mod_p(curve_p, q.y, curve_p); // -Q.y = P - Q.y
let neg_Q = G1Point(q.x, neg_Q_Y);
// Add the first point to the negated second point
let (result) = add_ec_points(curve_id, p, neg_Q);
return (res=result);
}

func all_g1_g2_pairs_are_on_curve{
range_check_ptr, range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*
}(input: felt*, n: felt, curve_id: felt) -> (res: felt) {
Expand Down Expand Up @@ -127,8 +160,10 @@ func add_ec_points{
let (opposite_y) = is_opposite_mod_p(P.y, Q.y, modulus);

if (opposite_y != 0) {
// P + (-P) = O (point at infinity)
return (res=G1Point(UInt384(0, 0, 0, 0), UInt384(0, 0, 0, 0)));
} else {
// P = Q, so we need to double the point
let (circuit) = get_DOUBLE_EC_POINT_circuit(curve_id);
let (A) = get_a(curve_id);
let (input: UInt384*) = alloc();
Expand All @@ -139,6 +174,7 @@ func add_ec_points{
return (res=[cast(res, G1Point*)]);
}
} else {
// P and Q have different x-coordinates, perform regular addition
let (circuit) = get_ADD_EC_POINT_circuit(curve_id);
let (input: UInt384*) = alloc();
assert input[0] = P.x;
Expand Down
Loading
Loading