Skip to content

Commit

Permalink
Merge branch 'feat/init-stdlib' into feat/uints
Browse files Browse the repository at this point in the history
  • Loading branch information
katat committed Oct 24, 2024
2 parents 85edebd + a822a1b commit f636680
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 175 deletions.
128 changes: 55 additions & 73 deletions examples/fixture/asm/kimchi/generic_builtin_bits.asm
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
@ noname.0.7.0
@ public inputs: 1

DoubleGeneric<1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<1,0,0,0,-1>
DoubleGeneric<1,1>
Expand All @@ -17,12 +14,6 @@ DoubleGeneric<1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<1,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<0,0,-1,1>
Expand All @@ -34,13 +25,6 @@ DoubleGeneric<1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<2,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<1,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<0,0,-1,1>
Expand All @@ -52,6 +36,13 @@ DoubleGeneric<1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<2,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<4,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
Expand All @@ -77,61 +68,52 @@ DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,-1>
DoubleGeneric<1,0,0,0,-2>
(0,0) -> (55,1) -> (74,1) -> (75,0)
(1,0) -> (2,0) -> (5,0)
(1,2) -> (2,1)
(2,2) -> (3,0)
(4,0) -> (6,0) -> (23,0) -> (41,0)
(5,1) -> (6,1)
(6,2) -> (7,1) -> (11,1)
(7,2) -> (10,0)
(8,0) -> (11,0) -> (13,0) -> (14,0)
(8,1) -> (9,0)
(9,2) -> (10,1)
(11,2) -> (12,0)
(13,2) -> (16,0) -> (17,0) -> (56,0) -> (63,0) -> (64,0)
(14,1) -> (15,0)
(16,2) -> (36,0)
(17,1) -> (18,0)
(19,0) -> (20,0) -> (22,0)
(19,2) -> (20,1)
(20,2) -> (21,0)
(22,1) -> (23,1)
(23,2) -> (24,1) -> (28,1)
(24,2) -> (27,0)
(25,0) -> (28,0) -> (30,0) -> (31,0)
(25,1) -> (26,0)
(26,2) -> (27,1)
(28,2) -> (29,0)
(30,2) -> (33,0) -> (34,0) -> (59,0) -> (66,0) -> (67,0)
(31,1) -> (32,0)
(33,2) -> (36,1)
(34,1) -> (35,0)
(36,2) -> (54,0)
(37,0) -> (38,0) -> (40,0)
(37,2) -> (38,1)
(38,2) -> (39,0)
(40,1) -> (41,1)
(41,2) -> (42,1) -> (46,1)
(42,2) -> (45,0)
(43,0) -> (46,0) -> (48,0) -> (49,0)
(0,0) -> (46,1) -> (65,1) -> (66,0)
(1,0) -> (3,0) -> (14,0) -> (25,0)
(2,1) -> (3,1)
(3,2) -> (4,1) -> (8,1)
(4,2) -> (7,0)
(5,0) -> (8,0) -> (10,0) -> (11,0)
(5,1) -> (6,0)
(6,2) -> (7,1)
(8,2) -> (9,0)
(10,2) -> (35,0) -> (36,0) -> (47,0) -> (54,0) -> (55,0)
(11,1) -> (12,0)
(13,1) -> (14,1)
(14,2) -> (15,1) -> (19,1)
(15,2) -> (18,0)
(16,0) -> (19,0) -> (21,0) -> (22,0)
(16,1) -> (17,0)
(17,2) -> (18,1)
(19,2) -> (20,0)
(21,2) -> (38,0) -> (39,0) -> (50,0) -> (57,0) -> (58,0)
(22,1) -> (23,0)
(24,1) -> (25,1)
(25,2) -> (26,1) -> (30,1)
(26,2) -> (29,0)
(27,0) -> (30,0) -> (32,0) -> (33,0)
(27,1) -> (28,0)
(28,2) -> (29,1)
(30,2) -> (31,0)
(32,2) -> (42,0) -> (43,0) -> (51,0) -> (61,0) -> (62,0)
(33,1) -> (34,0)
(35,2) -> (41,0)
(36,1) -> (37,0)
(38,2) -> (41,1)
(39,1) -> (40,0)
(41,2) -> (45,0)
(42,2) -> (45,1)
(43,1) -> (44,0)
(44,2) -> (45,1)
(46,2) -> (47,0)
(48,2) -> (51,0) -> (52,0) -> (60,0) -> (70,0) -> (71,0)
(49,1) -> (50,0)
(51,2) -> (54,1)
(52,1) -> (53,0)
(54,2) -> (55,0)
(56,1) -> (57,0)
(57,2) -> (58,0)
(60,1) -> (61,0)
(61,2) -> (62,0)
(63,2) -> (69,0)
(64,1) -> (65,0)
(66,2) -> (69,1)
(67,1) -> (68,0)
(69,2) -> (73,0)
(70,2) -> (73,1)
(71,1) -> (72,0)
(73,2) -> (74,0)
(45,2) -> (46,0)
(47,1) -> (48,0)
(48,2) -> (49,0)
(51,1) -> (52,0)
(52,2) -> (53,0)
(54,2) -> (60,0)
(55,1) -> (56,0)
(57,2) -> (60,1)
(58,1) -> (59,0)
(60,2) -> (64,0)
(61,2) -> (64,1)
(62,1) -> (63,0)
(64,2) -> (65,0)
46 changes: 20 additions & 26 deletions examples/fixture/asm/r1cs/generic_builtin_bits.asm
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
@ noname.0.7.0
@ public inputs: 1

v_3 == (v_2) * (v_2 + -1)
0 == (v_3) * (1)
1 == (v_4) * (1)
v_6 == (v_5) * (-1 * v_2 + v_4)
-1 * v_7 + 1 == (v_6) * (1)
v_8 == (v_7) * (-1 * v_2 + v_4)
0 == (v_8) * (1)
v_10 == (v_9) * (v_9 + -1)
0 == (v_10) * (1)
1 == (v_11) * (1)
v_13 == (v_12) * (-1 * v_9 + v_11)
-1 * v_14 + 1 == (v_13) * (1)
v_15 == (v_14) * (-1 * v_9 + v_11)
0 == (v_15) * (1)
v_17 == (v_16) * (v_16 + -1)
0 == (v_17) * (1)
1 == (v_18) * (1)
v_20 == (v_19) * (-1 * v_16 + v_18)
-1 * v_21 + 1 == (v_20) * (1)
v_22 == (v_21) * (-1 * v_16 + v_18)
0 == (v_22) * (1)
v_1 == (v_7 + 2 * v_14 + 4 * v_21) * (1)
1 == (-1 * v_7 + 1) * (1)
1 == (v_14) * (1)
1 == (-1 * v_21 + 1) * (1)
v_1 == (v_7 + 2 * v_14 + 4 * v_21) * (1)
1 == (v_3) * (1)
v_5 == (v_4) * (-1 * v_2 + v_3)
-1 * v_6 + 1 == (v_5) * (1)
v_7 == (v_6) * (-1 * v_2 + v_3)
0 == (v_7) * (1)
1 == (v_9) * (1)
v_11 == (v_10) * (-1 * v_8 + v_9)
-1 * v_12 + 1 == (v_11) * (1)
v_13 == (v_12) * (-1 * v_8 + v_9)
0 == (v_13) * (1)
1 == (v_15) * (1)
v_17 == (v_16) * (-1 * v_14 + v_15)
-1 * v_18 + 1 == (v_17) * (1)
v_19 == (v_18) * (-1 * v_14 + v_15)
0 == (v_19) * (1)
v_1 == (v_6 + 2 * v_12 + 4 * v_18) * (1)
1 == (-1 * v_6 + 1) * (1)
1 == (v_12) * (1)
1 == (-1 * v_18 + 1) * (1)
v_1 == (v_6 + 2 * v_12 + 4 * v_18) * (1)
2 == (v_1) * (1)
4 changes: 3 additions & 1 deletion src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ impl Expr {
// sanity check
if !matches!(
self.kind,
ExprKind::Variable { .. } | ExprKind::FieldAccess { .. } | ExprKind::ArrayAccess { .. }
ExprKind::Variable { .. }
| ExprKind::FieldAccess { .. }
| ExprKind::ArrayAccess { .. }
) {
panic!("an array access can only follow a variable or another array access");
}
Expand Down
9 changes: 8 additions & 1 deletion src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub mod bits;
pub mod builtins;
pub mod crypto;

/// The directory under [NONAME_DIRECTORY] containing the native stdlib.
pub const STDLIB_DIRECTORY: &str = "src/stdlib/native/";

pub enum AllStdModules {
Builtins,
Crypto,
Expand Down Expand Up @@ -97,7 +100,11 @@ pub fn init_stdlib_dep<B: Backend>(
for lib in libs {
let module = UserRepo::new(&format!("std/{}", lib));
let prefix_stdlib = Path::new(path_prefix);
let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}.no"))).unwrap();
println!(
"Loading stdlib: {}",
prefix_stdlib.join(format!("{lib}/lib.no")).display()
);
let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}/lib.no"))).unwrap();
node_id =
typecheck_next_file(tast, Some(module), sources, lib.to_string(), code, 0).unwrap();
}
Expand Down
37 changes: 14 additions & 23 deletions src/stdlib/native/bits.no → src/stdlib/native/bits/lib.no
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
hint fn nth_bit(value: Field, const nth: Field) -> Field;

fn from_bits(bits: [Bool; LEN]) -> Field {
let mut lc1 = 0;
let mut e2 = 1;
let zero = 0;

for index in 0..LEN {
lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
return lc1;
}

fn to_bits(const LEN: Field, value: Field) -> [Bool; LEN] {
let mut bits = [false; LEN];
let mut lc1 = 0;
let mut e2 = 1;

let one = 1;
let zero = 0;

// todo: ITE should allow literals
let true_val = true;
let false_val = false;

for index in 0..LEN {
let bit_num = unsafe nth_bit(value, index);

// constrain the bit_num to be 0 or 1
assert_eq(bit_num * (bit_num - 1), 0);

// convert the bit_num to boolean
bits[index] = if bit_num == 1 {true_val} else {false_val};

lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
assert_eq(lc1, value);

assert_eq(from_bits(bits), value);
return bits;
}

fn from_bits(bits: [Bool; LEN]) -> Field {
let mut lc1 = 0;
let mut e2 = 1;
let zero = 0;

for index in 0..LEN {
lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
return lc1;
}
File renamed without changes.
Loading

0 comments on commit f636680

Please sign in to comment.