Skip to content

Commit

Permalink
chore: redo repo structure
Browse files Browse the repository at this point in the history
  • Loading branch information
petscheit committed Oct 1, 2024
1 parent 62fa947 commit 630c1ec
Show file tree
Hide file tree
Showing 26 changed files with 1,131 additions and 39 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
lib
node_modules

cairo_programs/deps
cairo_programs/build
cairo_programs/deps
cairo/packages/*
cairo/build
venv

.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cairo/packages/garaga_zero"]
path = cairo/packages/garaga_zero
url = https://github.com/petscheit/garaga-zero.git
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.3
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
setup:
./scripts/setup.sh

buildx:
./scripts/cairo_compile.sh

run:
./scripts/cairo_run.sh

build_and_run:
./scripts/cairo_compile.sh
./scripts/cairo_run.sh


1 change: 1 addition & 0 deletions cairo/packages/garaga_zero
Submodule garaga_zero added at 012bd7
25 changes: 25 additions & 0 deletions cairo/py/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Used for writing a 384 bit integer to the Cairo struct
def write_uint384(ptr, value: int):
mask = (1 << 96) - 1 # Creates a mask of 96 1's in binary
for i in range(4):
chunk = value & mask
setattr(ptr, f'd{i}', chunk)
value >>= 96 # Shift right by 96 bits

# Creates a G1Point from a point dictionary
def write_g1(ptr, point: dict):
write_uint384(ptr.x, int(point["x"], 16))
write_uint384(ptr.y, int(point["y"], 16))

# Creates a G2Point from a point dictionary
def write_g2(ptr, point: dict):
write_uint384(ptr.x0, int(point["x0"], 16))
write_uint384(ptr.x1, int(point["x1"], 16))
write_uint384(ptr.y0, int(point["y0"], 16))
write_uint384(ptr.y1, int(point["y1"], 16))

# Creates a G1G2Pair
def write_g1g2(ptr, g1: dict, g2: dict):
write_g1(ptr.P, g1)
write_g2(ptr.Q, g2)
18 changes: 18 additions & 0 deletions cairo/src/constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from definitions import UInt384, G1Point

func g1_negative() -> G1Point {
return (G1Point(
x=UInt384(
77209383603911340680728987323,
49921657856232494206459177023,
24654436777218005952848247045,
7410505851925769877053596556
),
y=UInt384(
4578755106311036904654095050,
31671107278004379566943975610,
64119167930385062737200089033,
5354471328347505754258634440
)
));
}
42 changes: 42 additions & 0 deletions cairo/src/main.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
%builtins range_check poseidon range_check96 add_mod mul_mod

from starkware.cairo.common.cairo_builtins import PoseidonBuiltin, ModBuiltin
from starkware.cairo.common.registers import get_fp_and_pc
from starkware.cairo.common.alloc import alloc
from cairo.src.constants import g1_negative

from definitions import bn, bls, UInt384, one_E12D, N_LIMBS, BASE, E12D, G1Point, G2Point, G1G2Pair
from pairing import multi_pairing
from modulo_circuit import ExtensionFieldModuloCircuit

func main{
range_check_ptr,
poseidon_ptr: PoseidonBuiltin*,
range_check96_ptr: felt*,
add_mod_ptr: ModBuiltin*,
mul_mod_ptr: ModBuiltin*,
}() {
alloc_locals;


local pk_msg_pair: G1G2Pair;
local sig_point: G2Point;
%{
from cairo.py.utils import write_g2, write_g1g2
write_g2(ids.sig_point, program_input["sig"])
write_g1g2(ids.pk_msg_pair, program_input["pub"], program_input["msg"])
%}
let neg_g1: G1Point = g1_negative();
let g1_sig_pair: G1G2Pair = G1G2Pair(P=neg_g1, Q=sig_point);

let (inputs: G1G2Pair*) = alloc();
assert inputs[0] = g1_sig_pair;
assert inputs[1] = pk_msg_pair;

let (res) = multi_pairing(inputs, 2, 1);
let (one) = one_E12D();
assert res = one;

return ();
}
21 changes: 21 additions & 0 deletions cairo_programs/example_inputs/verify_sig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,25 @@
"y0": "1098650632560096473256354234109145535790592340280843578325165666754124094423343926482992304695649017941057567932801",
"y1": "978459796102980576570505777445808813685038478608728143767319725825501583173045652828825481621594634090727144659837"
}
}



{
"pub": {
"x": "0xCB81D775883FCD8E6F9F32DA1CE6D3E74A958AA6ADBAF4402DC90A8BDB718FE202D894D10B4D62653B51E0FFFF682B9",
"y": "0x230A0C69A911D973C397EF2D2DDF6A8C7702FAABA33D2A22461D4F45EB58B0708C2B6973C05C8E51D6D73637D79DBDD"
},
"sig": {
"x0": "0x11A82E93AAB8472C21DC79161ABF15577B34564005D3C36863D097FA2F88A0E6253910CBB3E61FA0F1E61E822407CCA7",
"x1": "0x15166EFBCFC7A7BED65D4141108F5CBE15411A876DB47EF556586EF87EF91BA5D172A6E4179C93D773BEAB4271CD3E65",
"y0": "0x136C34763107CDDA10E7D3B4C81AF5E2DCACCE1CB67093A94A398EA8B45FF7F5E2850D838B5A1A2C5297E52BD546A249",
"y1": "0x25DE0D4D013B3CECD448CB0961B4478900CD3612647063A1CDC12A628F92C32BB571531F111F7DEFCEC8703FAD1E67A"
},
"msg": {
"x0": "0xF714F39CE91F393E30FB766F6DCA8529EABFB4BAE2BD8AF00222642CC3B842696828C11C785A78F80EB5984386E8A84",
"x1": "0x7D6328FC85A45C2C002CAB4A7D8BCEAEC0A6B777AFE79D052C48FB5D7D07B0C3CAEA28DA84C80C2D00C16429E05B3EB",
"y0": "0x7235906F4390973476BC48E657CC305121237B0540C065F1C55FAFC51EFB0DED57CFA1B79E9CF2780567A496C1F4181",
"y1": "0x65B70406E8D93EED48F96D59500134429A74E9E50C2BD6C68A28F6B3C2ACF0C797A3770148511225D91A831BBFB9B7D"
}
}
5 changes: 5 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bankai Client

This directory contains the required scripts for generating the inputs for the Bankai Cairo programs.

## Setup
Loading

0 comments on commit 630c1ec

Please sign in to comment.